[TASK] Unit test for t3lib_utility_Mail should check From user
[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
93 $additionalHeadersExpected = $additionalHeaders;
94 if ($fromAddress !== '' && $fromName !== '') {
95 $additionalHeadersExpected .= LF . sprintf('From: "%s" <%s>', $fromName, $fromAddress);
96 }
97
98 $mockMailer = $this->getMock('mockMailer', array('mail'));
99 $mockMailer->expects($this->once())->method('mail')
100 ->with(
101 array(
102 'to' => $to,
103 'subject' => $subject,
104 'messageBody' => $messageBody,
105 'additionalHeaders' => $additionalHeadersExpected,
106 'additionalParameters' => $additionalParameters,
107 ),
108 FALSE
109 );
110 $GLOBALS['T3_VAR']['callUserFunction']['mockMailer->mail']
111 = array('obj' => $mockMailer, 'method' => 'mail');
112
113 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']
114 ['t3lib/utility/class.t3lib_utility_mail.php']
115 ['substituteMailDelivery'] = array('mockMailer->mail');
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 ?>