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