‪TYPO3CMS  ‪main
MailUtilityTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
25 final class ‪MailUtilityTest extends UnitTestCase
26 {
27  protected bool ‪$resetSingletonInstances = true;
28 
29  #[Test]
31  {
32  self::assertEmpty(‪MailUtility::breakLinesForEmail(''));
33  }
34 
35  #[Test]
37  {
38  $newlineChar = LF;
39  $lineWidth = 76;
40  $str = 'This text is not longer than 76 chars and therefore will not be broken.';
41  $returnString = ‪MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
42  self::assertCount(1, explode($newlineChar, $returnString));
43  }
44 
45  #[Test]
47  {
48  $newlineChar = LF;
49  $lineWidth = 50;
50  $str = 'This text is longer than 50 chars and therefore will be broken.';
51  $returnString = ‪MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
52  self::assertCount(2, explode($newlineChar, $returnString));
53  }
54 
55  #[Test]
57  {
58  $newlineChar = LF;
59  $lineWidth = 10;
60  // first space after 20 chars (more than $lineWidth)
61  $str = 'abcdefghijklmnopqrst uvwxyz 123456';
62  $returnString = ‪MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
63  self::assertEquals($returnString, 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456');
64  }
65 
66  #[Test]
68  {
69  $str = 'Mein Link auf eine News (Link: http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)';
70  $returnString = ‪MailUtility::breakLinesForEmail($str);
71  self::assertEquals($returnString, 'Mein Link auf eine News (Link:' . LF . 'http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)');
72  }
73 
77  public static function ‪parseAddressesProvider(): array
78  {
79  return [
80  'name &ltemail&gt;' => ['name <email@example.org>', ['email@example.org' => 'name']],
81  '&lt;email&gt;' => ['<email@example.org>', ['email@example.org']],
82  '@localhost' => ['@localhost', []],
83  '000@example.com' => ['000@example.com', ['000@example.com']],
84  'email' => ['email@example.org', ['email@example.org']],
85  'email1,email2' => ['email1@example.org,email2@example.com', ['email1@example.org', 'email2@example.com']],
86  'name &ltemail&gt;,email2' => ['name <email1@example.org>,email2@example.com', ['email1@example.org' => 'name', 'email2@example.com']],
87  '"last, first" &lt;name@example.org&gt;' => ['"last, first" <email@example.org>', ['email@example.org' => '"last, first"']],
88  'email,name &ltemail&gt;,"last, first" &lt;name@example.org&gt;' => [
89  'email1@example.org, name <email2@example.org>, "last, first" <email3@example.org>',
90  [
91  'email1@example.org',
92  'email2@example.org' => 'name',
93  'email3@example.org' => '"last, first"',
94  ],
95  ],
96  ];
97  }
98 
99  #[DataProvider('parseAddressesProvider')]
100  #[Test]
101  public function ‪parseAddressesTest(string $source, array $addressList): void
102  {
103  $returnArray = ‪MailUtility::parseAddresses($source);
104  self::assertEquals($addressList, $returnArray);
105  }
106 
107  public static function ‪replyToProvider(): array
108  {
109  return [
110  'only address' => [
111  ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => ''],
112  ['noreply@example.org'],
113  ],
114  'name and address' => [
115  ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => 'John Doe'],
116  ['noreply@example.org' => 'John Doe'],
117  ],
118  'no address' => [
119  ['defaultMailReplyToAddress' => '', 'defaultMailReplyToName' => ''],
120  [],
121  ],
122  'invalid address' => [
123  ['defaultMailReplyToAddress' => 'foo', 'defaultMailReplyToName' => ''],
124  [],
125  ],
126  ];
127  }
128 
129  #[DataProvider('replyToProvider')]
130  #[Test]
131  public function ‪getSystemReplyToTest(array $configuration, array $expectedReplyTo): void
132  {
133  ‪$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $configuration;
134  $returnArray = MailUtility::getSystemReplyTo();
135  self::assertSame($expectedReplyTo, $returnArray);
136  }
137 }
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\breakLinesForEmailBreaksTextIfCharWithIsExceeded
‪breakLinesForEmailBreaksTextIfCharWithIsExceeded()
Definition: MailUtilityTest.php:46
‪TYPO3\CMS\Core\Utility\MailUtility\breakLinesForEmail
‪static string breakLinesForEmail(string $str, string $newlineChar=LF, int $lineWidth=76)
Definition: MailUtility.php:133
‪TYPO3\CMS\Core\Tests\Unit\Utility
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: MailUtilityTest.php:27
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded
‪breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded()
Definition: MailUtilityTest.php:36
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest
Definition: MailUtilityTest.php:26
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\getSystemReplyToTest
‪getSystemReplyToTest(array $configuration, array $expectedReplyTo)
Definition: MailUtilityTest.php:131
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\parseAddressesTest
‪parseAddressesTest(string $source, array $addressList)
Definition: MailUtilityTest.php:101
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit
‪breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit()
Definition: MailUtilityTest.php:56
‪TYPO3\CMS\Core\Utility\MailUtility
Definition: MailUtility.php:26
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\breakLinesForEmailReturnsEmptyStringIfEmptyStringIsGiven
‪breakLinesForEmailReturnsEmptyStringIfEmptyStringIsGiven()
Definition: MailUtilityTest.php:30
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Utility\MailUtility\parseAddresses
‪static array parseAddresses(string $rawAddresses)
Definition: MailUtility.php:183
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\parseAddressesProvider
‪static parseAddressesProvider()
Definition: MailUtilityTest.php:77
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\replyToProvider
‪static replyToProvider()
Definition: MailUtilityTest.php:107
‪TYPO3\CMS\Core\Tests\Unit\Utility\MailUtilityTest\breakLinesForEmailBreaksTextIfLineIsLongerThanTheLineWidth
‪breakLinesForEmailBreaksTextIfLineIsLongerThanTheLineWidth()
Definition: MailUtilityTest.php:67