class.t3lib_utility_mailTest.php 4.9 KB
Newer Older
1
2
3
4
<?php
/***************************************************************
* Copyright notice
*
5
* (c) 2010-2011 Oliver Klee (typo3-coding@oliverklee.de)
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * Testcase for the t3lib_utility_Mail class.
 *
 * @package TYPO3
 * @subpackage t3lib
 *
 * @author Oliver Klee <typo3-coding@oliverklee.de>
 */
33
class t3lib_utility_MailTest extends tx_phpunit_testcase {
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
	/**
	 * backed-up TYPO3_CONF_VARS SC_OPTIONS
	 *
	 * @var array
	 */
	private $scOptionsBackup = array();

	/**
	 * backed-up T3_VAR callUserFunction
	 *
	 * @var array
	 */
	private $callUserFunctionBackup = array();

	public function setUp() {
		$this->scOptionsBackup = $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'];
		$this->callUserFunctionBackup = $GLOBALS['T3_VAR']['callUserFunction'];
	}

	public function tearDown() {
54
		t3lib_div::purgeInstances();
55
56
57
58
59
60
61
62
63
64
65
66
67
		$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] = $this->scOptionsBackup;
		$GLOBALS['T3_VAR']['callUserFunction'] = $this->callUserFunctionBackup;
	}


	//////////////////////////
	// Tests concerning mail
	//////////////////////////

	/**
	 * @test
	 */
	public function mailCallsHook() {
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
		$this->doMailCallsHook();
	}

	/**
	 * @test
	 */
	public function mailCallsHookWithDefaultMailFrom() {
		$this->doMailCallsHook('no-reply@localhost', 'TYPO3 Mailer');
	}

	/**
	 * Method called from tests mailCallsHook() and mailCallsHookWithDefaultMailFrom().
	 */
	protected function doMailCallsHook($fromAddress = '', $fromName = '') {
			// Backup configuration
		$mailConfigurationBackup = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];

		$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] = $fromAddress;
		$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] = $fromName;

88
89
90
91
92
		$to = 'john@example.com';
		$subject = 'Good news everybody!';
		$messageBody = 'The hooks works!';
		$additionalHeaders = 'Reply-to: jane@example.com';
		$additionalParameters = '-f postmaster@example.com';
93
		$fakeThis = FALSE;
94

95
96
97
98
99
		$additionalHeadersExpected = $additionalHeaders;
		if ($fromAddress !== '' && $fromName !== '') {
			$additionalHeadersExpected .= LF . sprintf('From: "%s" <%s>', $fromName, $fromAddress);
		}

100
101
102
		$mockMailer = $this->getMock('t3lib_mail_MailerAdapter', array('mail'));
		$mockClassName = get_class($mockMailer);
		t3lib_div::addInstance($mockClassName, $mockMailer);
103
104
		$mockMailer->expects($this->once())->method('mail')
			->with(
105
106
107
108
109
110
				$to,
				$subject,
				$messageBody,
				$additionalHeadersExpected,
				$additionalParameters,
				$fakeThis
111
112
			);

113
		$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] = array($mockClassName);
114
115
116
117
118

		t3lib_utility_Mail::mail(
			$to, $subject, $messageBody, $additionalHeaders,
			$additionalParameters
		);
119
120
121

			// Restore configuration
		$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $mailConfigurationBackup;
122
	}
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
	/**
	 * @test
	 */
	public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven() {
		$this->assertEmpty(
			t3lib_utility_Mail::breakLinesForEmail('')
		);
	}

	/**
	 * @test
	 */
	public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded() {
		$newlineChar = LF;
		$lineWidth = 76;
		$str = 'This text is not longer than 76 chars and therefore will not be broken.';
		$returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
		$this->assertEquals(
			1,
			count(explode($newlineChar, $returnString))
		);
	}

	/**
	 * @test
	 */
	public function breakLinesForEmailBreaksTextIfCharWithIsExceeded() {
		$newlineChar = LF;
		$lineWidth = 50;
		$str = 'This text is longer than 50 chars and therefore will be broken.';
		$returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
		$this->assertEquals(
			2,
			count(explode($newlineChar, $returnString))
		);
	}

	/**
	 * @test
	 */
	public function breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit() {
		$newlineChar = LF;
		$lineWidth = 10;
167
		// first space after 20 chars (more than $lineWidth)
168
169
170
171
172
173
174
		$str = 'abcdefghijklmnopqrst uvwxyz 123456';
		$returnString = t3lib_utility_Mail::breakLinesForEmail($str, $newlineChar, $lineWidth);
		$this->assertEquals(
			$returnString,
				'abcdefghijklmnopqrst' . LF . 'uvwxyz' . LF . '123456'
		);
	}
175
}
176
?>