c199341c5834acb9787c52d97d56854c87867b9c
[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 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] = $this->scOptionsBackup;
55 $GLOBALS['T3_VAR']['callUserFunction'] = $this->callUserFunctionBackup;
56 }
57
58
59 //////////////////////////
60 // Tests concerning mail
61 //////////////////////////
62
63 /**
64 * @test
65 */
66 public function mailCallsHook() {
67 $this->doMailCallsHook();
68 }
69
70 /**
71 * @test
72 */
73 public function mailCallsHookWithDefaultMailFrom() {
74 $this->doMailCallsHook('no-reply@localhost', 'TYPO3 Mailer');
75 }
76
77 /**
78 * Method called from tests mailCallsHook() and mailCallsHookWithDefaultMailFrom().
79 */
80 protected function doMailCallsHook($fromAddress = '', $fromName = '') {
81 // Backup configuration
82 $mailConfigurationBackup = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
83
84 $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] = $fromAddress;
85 $GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] = $fromName;
86
87 $to = 'john@example.com';
88 $subject = 'Good news everybody!';
89 $messageBody = 'The hooks works!';
90 $additionalHeaders = 'Reply-to: jane@example.com';
91 $additionalParameters = '-f postmaster@example.com';
92 $fakeThis = FALSE;
93
94 $additionalHeadersExpected = $additionalHeaders;
95 if ($fromAddress !== '' && $fromName !== '') {
96 $additionalHeadersExpected .= LF . sprintf('From: "%s" <%s>', $fromName, $fromAddress);
97 }
98
99 $mockMailer = $this->getMock('t3lib_mail_MailerAdapter', array('mail'));
100 $mockClassName = get_class($mockMailer);
101 t3lib_div::addInstance($mockClassName, $mockMailer);
102 $mockMailer->expects($this->once())->method('mail')
103 ->with(
104 $to,
105 $subject,
106 $messageBody,
107 $additionalHeadersExpected,
108 $additionalParameters,
109 $fakeThis
110 );
111
112 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']
113 ['t3lib/utility/class.t3lib_utility_mail.php']
114 ['substituteMailDelivery'] = array($mockClassName);
115
116 t3lib_utility_Mail::mail(
117 $to, $subject, $messageBody, $additionalHeaders,
118 $additionalParameters
119 );
120
121 // Restore configuration
122 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $mailConfigurationBackup;
123 }
124
125 /**
126 * @test
127 */
128 public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven() {
129 $this->assertEmpty(
130 t3lib_utility_Mail::breakLinesForEmail('')
131 );
132 }
133
134 /**
135 * @test
136 */
137 public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded() {
138 $newlineChar = LF;
139 $lineWidth = 76;
140 $str = 'This text is not longer than 76 chars and therefore will not be broken.';
141 $returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
142 $this->assertEquals(
143 1,
144 count(explode($newlineChar, $returnString))
145 );
146 }
147
148 /**
149 * @test
150 */
151 public function breakLinesForEmailBreaksTextIfCharWithIsExceeded() {
152 $newlineChar = LF;
153 $lineWidth = 50;
154 $str = 'This text is longer than 50 chars and therefore will be broken.';
155 $returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
156 $this->assertEquals(
157 2,
158 count(explode($newlineChar, $returnString))
159 );
160 }
161
162 /**
163 * @test
164 */
165 public function breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit() {
166 $newlineChar = LF;
167 $lineWidth = 10;
168 // first space after 20 chars (more than $lineWidth)
169 $str = 'abcdefghijklmnopqrst uvwxyz 123456';
170 $returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
171 $this->assertEquals(
172 $returnString,
173 'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456'
174 );
175 }
176 }
177 ?>