[!!!][TASK] Extract testing framework for TYPO3
[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 /**
18 * Testcase for the \TYPO3\CMS\Core\Utility\MailUtility class.
19 */
20 class MailUtilityTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
21 {
22 /**
23 * @var array A backup of registered singleton instances
24 */
25 protected $singletonInstances = [];
26
27 protected function setUp()
28 {
29 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
30 }
31
32 protected function tearDown()
33 {
34 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
35 parent::tearDown();
36 }
37
38 /**
39 * @test
40 */
41 public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven()
42 {
43 $this->assertEmpty(\TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail(''));
44 }
45
46 /**
47 * @test
48 */
49 public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded()
50 {
51 $newlineChar = LF;
52 $lineWidth = 76;
53 $str = 'This text is not longer than 76 chars and therefore will not be broken.';
54 $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
55 $this->assertEquals(1, count(explode($newlineChar, $returnString)));
56 }
57
58 /**
59 * @test
60 */
61 public function breakLinesForEmailBreaksTextIfCharWithIsExceeded()
62 {
63 $newlineChar = LF;
64 $lineWidth = 50;
65 $str = 'This text is longer than 50 chars and therefore will be broken.';
66 $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
67 $this->assertEquals(2, count(explode($newlineChar, $returnString)));
68 }
69
70 /**
71 * @test
72 */
73 public function breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit()
74 {
75 $newlineChar = LF;
76 $lineWidth = 10;
77 // first space after 20 chars (more than $lineWidth)
78 $str = 'abcdefghijklmnopqrst uvwxyz 123456';
79 $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
80 $this->assertEquals($returnString, 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456');
81 }
82
83 /**
84 * @test
85 */
86 public function breakLinesForEmailBreaksTextIfLineIsLongerThanTheLineWidth()
87 {
88 $str = 'Mein Link auf eine News (Link: http://zzzzzzzzzzzzz.xxxxxxxxx.de/index.php?id=10&tx_ttnews%5Btt_news%5D=1&cHash=66f5af320da29b7ae1cda49047ca7358)';
89 $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str);
90 $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)');
91 }
92
93 /**
94 * Data provider for parseAddressesTest
95 *
96 * @return array Data sets
97 */
98 public function parseAddressesProvider()
99 {
100 return [
101 'name &ltemail&gt;' => ['name <email@example.org>', ['email@example.org' => 'name']],
102 '&lt;email&gt;' => ['<email@example.org>', ['email@example.org']],
103 '@localhost' => ['@localhost', []],
104 '000@example.com' => ['000@example.com', ['000@example.com']],
105 'email' => ['email@example.org', ['email@example.org']],
106 'email1,email2' => ['email1@example.org,email2@example.com', ['email1@example.org', 'email2@example.com']],
107 'name &ltemail&gt;,email2' => ['name <email1@example.org>,email2@example.com', ['email1@example.org' => 'name', 'email2@example.com']],
108 '"last, first" &lt;name@example.org&gt;' => ['"last, first" <email@example.org>', ['email@example.org' => '"last, first"']],
109 'email,name &ltemail&gt;,"last, first" &lt;name@example.org&gt;' => [
110 'email1@example.org, name <email2@example.org>, "last, first" <email3@example.org>',
111 [
112 'email1@example.org',
113 'email2@example.org' => 'name',
114 'email3@example.org' => '"last, first"'
115 ]
116 ]
117 ];
118 }
119
120 /**
121 * @test
122 * @dataProvider parseAddressesProvider
123 */
124 public function parseAddressesTest($source, $addressList)
125 {
126 $returnArray = \TYPO3\CMS\Core\Utility\MailUtility::parseAddresses($source);
127 $this->assertEquals($addressList, $returnArray);
128 }
129 }