[BUGFIX] Fix warning in AbstractConfigurationManagerTest
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / MailUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\MailUtility;
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 /**
21 * Testcase for the \TYPO3\CMS\Core\Utility\MailUtility class.
22 */
23 class MailUtilityTest extends UnitTestCase
24 {
25 /**
26 * @var bool Reset singletons created by subject
27 */
28 protected $resetSingletonInstances = true;
29
30 /**
31 * @test
32 */
33 public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven()
34 {
35 $this->assertEmpty(MailUtility::breakLinesForEmail(''));
36 }
37
38 /**
39 * @test
40 */
41 public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded()
42 {
43 $newlineChar = LF;
44 $lineWidth = 76;
45 $str = 'This text is not longer than 76 chars and therefore will not be broken.';
46 $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
47 $this->assertEquals(1, count(explode($newlineChar, $returnString)));
48 }
49
50 /**
51 * @test
52 */
53 public function breakLinesForEmailBreaksTextIfCharWithIsExceeded()
54 {
55 $newlineChar = LF;
56 $lineWidth = 50;
57 $str = 'This text is longer than 50 chars and therefore will be broken.';
58 $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
59 $this->assertEquals(2, count(explode($newlineChar, $returnString)));
60 }
61
62 /**
63 * @test
64 */
65 public function breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit()
66 {
67 $newlineChar = LF;
68 $lineWidth = 10;
69 // first space after 20 chars (more than $lineWidth)
70 $str = 'abcdefghijklmnopqrst uvwxyz 123456';
71 $returnString = MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
72 $this->assertEquals($returnString, 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456');
73 }
74
75 /**
76 * @test
77 */
78 public function breakLinesForEmailBreaksTextIfLineIsLongerThanTheLineWidth()
79 {
80 $str = 'Mein Link auf eine News (Link: http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)';
81 $returnString = MailUtility::breakLinesForEmail($str);
82 $this->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)');
83 }
84
85 /**
86 * Data provider for parseAddressesTest
87 *
88 * @return array Data sets
89 */
90 public function parseAddressesProvider()
91 {
92 return [
93 'name &ltemail&gt;' => ['name <email@example.org>', ['email@example.org' => 'name']],
94 '&lt;email&gt;' => ['<email@example.org>', ['email@example.org']],
95 '@localhost' => ['@localhost', []],
96 '000@example.com' => ['000@example.com', ['000@example.com']],
97 'email' => ['email@example.org', ['email@example.org']],
98 'email1,email2' => ['email1@example.org,email2@example.com', ['email1@example.org', 'email2@example.com']],
99 'name &ltemail&gt;,email2' => ['name <email1@example.org>,email2@example.com', ['email1@example.org' => 'name', 'email2@example.com']],
100 '"last, first" &lt;name@example.org&gt;' => ['"last, first" <email@example.org>', ['email@example.org' => '"last, first"']],
101 'email,name &ltemail&gt;,"last, first" &lt;name@example.org&gt;' => [
102 'email1@example.org, name <email2@example.org>, "last, first" <email3@example.org>',
103 [
104 'email1@example.org',
105 'email2@example.org' => 'name',
106 'email3@example.org' => '"last, first"'
107 ]
108 ]
109 ];
110 }
111
112 /**
113 * @test
114 * @dataProvider parseAddressesProvider
115 */
116 public function parseAddressesTest($source, $addressList)
117 {
118 $returnArray = MailUtility::parseAddresses($source);
119 $this->assertEquals($addressList, $returnArray);
120 }
121
122 /**
123 * @return array
124 */
125 public function replyToProvider(): array
126 {
127 return [
128 'only address' => [
129 ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => ''],
130 ['noreply@example.org'],
131 ],
132 'name and address' => [
133 ['defaultMailReplyToAddress' => 'noreply@example.org', 'defaultMailReplyToName' => 'John Doe'],
134 ['noreply@example.org' => 'John Doe'],
135 ],
136 'no address' => [
137 ['defaultMailReplyToAddress' => '', 'defaultMailReplyToName' => ''],
138 [],
139 ],
140 'invalid address' => [
141 ['defaultMailReplyToAddress' => 'foo', 'defaultMailReplyToName' => ''],
142 [],
143 ],
144 ];
145 }
146
147 /**
148 * @test
149 * @dataProvider replyToProvider
150 * @param array $configuration
151 * @param array $expectedReplyTo
152 */
153 public function getSystemReplyToTest(array $configuration, array $expectedReplyTo)
154 {
155 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $configuration;
156 $returnArray = MailUtility::getSystemReplyTo();
157 $this->assertSame($expectedReplyTo, $returnArray);
158 }
159 }