[BUGFIX] MailUtility::parseAddresses() returns @localhost on empty string
[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 * Copyright notice
6 *
7 * (c) 2010-2013 Oliver Klee (typo3-coding@oliverklee.de)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for the \TYPO3\CMS\Core\Utility\MailUtility class.
29 *
30 * @author Oliver Klee <typo3-coding@oliverklee.de>
31 */
32 class MailUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var array A backup of registered singleton instances
36 */
37 protected $singletonInstances = array();
38
39 /**
40 * backed-up TYPO3_CONF_VARS SC_OPTIONS
41 *
42 * @var array
43 */
44 private $scOptionsBackup = array();
45
46 /**
47 * backed-up T3_VAR callUserFunction
48 *
49 * @var array
50 */
51 private $callUserFunctionBackup = array();
52
53 public function setUp() {
54 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
55 $this->scOptionsBackup = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'];
56 $this->callUserFunctionBackup = $GLOBALS['T3_VAR']['callUserFunction'];
57 }
58
59 public function tearDown() {
60 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
61 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] = $this->scOptionsBackup;
62 $GLOBALS['T3_VAR']['callUserFunction'] = $this->callUserFunctionBackup;
63 }
64
65 //////////////////////////
66 // Tests concerning mail
67 //////////////////////////
68 /**
69 * @test
70 */
71 public function mailCallsHook() {
72 $this->doMailCallsHook();
73 }
74
75 /**
76 * @test
77 */
78 public function mailCallsHookWithDefaultMailFrom() {
79 $this->doMailCallsHook('no-reply@localhost', 'TYPO3 Mailer');
80 }
81
82 /**
83 * Method called from tests mailCallsHook() and mailCallsHookWithDefaultMailFrom().
84 */
85 protected function doMailCallsHook($fromAddress = '', $fromName = '') {
86 // Backup configuration
87 $mailConfigurationBackup = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
88 $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] = $fromAddress;
89 $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] = $fromName;
90 $to = 'john@example.com';
91 $subject = 'Good news everybody!';
92 $messageBody = 'The hooks works!';
93 $additionalHeaders = 'Reply-to: jane@example.com';
94 $additionalParameters = '-f postmaster@example.com';
95 $fakeThis = FALSE;
96 $additionalHeadersExpected = $additionalHeaders;
97 if ($fromAddress !== '' && $fromName !== '') {
98 $additionalHeadersExpected .= LF . sprintf('From: "%s" <%s>', $fromName, $fromAddress);
99 }
100 $mockMailer = $this->getMock('TYPO3\\CMS\\Core\\Mail\\MailerAdapterInterface', array('mail'));
101 $mockClassName = get_class($mockMailer);
102 \TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($mockClassName, $mockMailer);
103 $mockMailer->expects($this->once())->method('mail')->with($to, $subject, $messageBody, $additionalHeadersExpected, $additionalParameters, $fakeThis);
104 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] = array($mockClassName);
105 \TYPO3\CMS\Core\Utility\MailUtility::mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters);
106 // Restore configuration
107 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $mailConfigurationBackup;
108 }
109
110 /**
111 * @test
112 */
113 public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven() {
114 $this->assertEmpty(\TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail(''));
115 }
116
117 /**
118 * @test
119 */
120 public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded() {
121 $newlineChar = LF;
122 $lineWidth = 76;
123 $str = 'This text is not longer than 76 chars and therefore will not be broken.';
124 $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
125 $this->assertEquals(1, count(explode($newlineChar, $returnString)));
126 }
127
128 /**
129 * @test
130 */
131 public function breakLinesForEmailBreaksTextIfCharWithIsExceeded() {
132 $newlineChar = LF;
133 $lineWidth = 50;
134 $str = 'This text is longer than 50 chars and therefore will be broken.';
135 $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
136 $this->assertEquals(2, count(explode($newlineChar, $returnString)));
137 }
138
139 /**
140 * @test
141 */
142 public function breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit() {
143 $newlineChar = LF;
144 $lineWidth = 10;
145 // first space after 20 chars (more than $lineWidth)
146 $str = 'abcdefghijklmnopqrst uvwxyz 123456';
147 $returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
148 $this->assertEquals($returnString, 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456');
149 }
150
151 /**
152 * Data provider for parseAddressesTest
153 *
154 * @return array Data sets
155 */
156 public function parseAddressesProvider() {
157 return array(
158 'name &ltemail&gt;' => array('name <email@example.org>', array('email@example.org' => 'name')),
159 '&lt;email&gt;' => array('<email@example.org>', array('email@example.org')),
160 '@localhost' => array('@localhost', array()),
161 '000@example.com' => array('000@example.com', array('000@example.com')),
162 'email' => array('email@example.org', array('email@example.org')),
163 'email1,email2' => array('email1@example.org,email2@example.com', array('email1@example.org', 'email2@example.com')),
164 'name &ltemail&gt;,email2' => array('name <email1@example.org>,email2@example.com', array('email1@example.org' => 'name', 'email2@example.com')),
165 '"last, first" &lt;name@example.org&gt;' => array('"last, first" <email@example.org>', array('email@example.org' => '"last, first"')),
166 'email,name &ltemail&gt;,"last, first" &lt;name@example.org&gt;' => array(
167 'email1@example.org, name <email2@example.org>, "last, first" <email3@example.org>',
168 array(
169 'email1@example.org',
170 'email2@example.org' => 'name',
171 'email3@example.org' => '"last, first"'
172 )
173 )
174 );
175 }
176
177 /**
178 * @test
179 * @dataProvider parseAddressesProvider
180 */
181 public function parseAddressesTest($source, $addressList) {
182 $returnArray = \TYPO3\CMS\Core\Utility\MailUtility::parseAddresses($source);
183 $this->assertEquals($addressList, $returnArray);
184 }
185
186 }
187
188 ?>