class.t3lib_utility_mailTest.php 4.99 KB
Newer Older
1
2
<?php
/***************************************************************
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 * Copyright notice
 *
 * (c) 2010-2011 Oliver Klee (typo3-coding@oliverklee.de)
 * 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!
 ***************************************************************/
24
25
26
27
28
29
30
/**
 * Testcase for the t3lib_utility_Mail class.
 *
 * @package TYPO3
 * @subpackage t3lib
 * @author Oliver Klee <typo3-coding@oliverklee.de>
 */
31
class t3lib_utility_MailTest extends tx_phpunit_testcase {
32

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
	/**
	 * 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() {
53
		\TYPO3\CMS\Core\Utility\GeneralUtility::purgeInstances();
54
55
56
57
58
59
60
61
62
63
64
		$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] = $this->scOptionsBackup;
		$GLOBALS['T3_VAR']['callUserFunction'] = $this->callUserFunctionBackup;
	}

	//////////////////////////
	// Tests concerning mail
	//////////////////////////
	/**
	 * @test
	 */
	public function mailCallsHook() {
65
66
67
68
69
70
71
72
73
74
75
76
77
78
		$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 = '') {
79
		// Backup configuration
80
81
82
		$mailConfigurationBackup = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
		$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromAddress'] = $fromAddress;
		$GLOBALS['TYPO3_CONF_VARS']['MAIL']['defaultMailFromName'] = $fromName;
83
84
85
86
87
		$to = 'john@example.com';
		$subject = 'Good news everybody!';
		$messageBody = 'The hooks works!';
		$additionalHeaders = 'Reply-to: jane@example.com';
		$additionalParameters = '-f postmaster@example.com';
88
		$fakeThis = FALSE;
89
90
91
92
		$additionalHeadersExpected = $additionalHeaders;
		if ($fromAddress !== '' && $fromName !== '') {
			$additionalHeadersExpected .= LF . sprintf('From: "%s" <%s>', $fromName, $fromAddress);
		}
93
		$mockMailer = $this->getMock('TYPO3\\CMS\\Core\\Mail\\MailerAdapterInterface', array('mail'));
94
		$mockClassName = get_class($mockMailer);
95
96
		\TYPO3\CMS\Core\Utility\GeneralUtility::addInstance($mockClassName, $mockMailer);
		$mockMailer->expects($this->once())->method('mail')->with($to, $subject, $messageBody, $additionalHeadersExpected, $additionalParameters, $fakeThis);
97
		$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/utility/class.t3lib_utility_mail.php']['substituteMailDelivery'] = array($mockClassName);
98
99
		\TYPO3\CMS\Core\Utility\MailUtility::mail($to, $subject, $messageBody, $additionalHeaders, $additionalParameters);
		// Restore configuration
100
		$GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $mailConfigurationBackup;
101
	}
102

103
104
105
106
	/**
	 * @test
	 */
	public function breakLinesForEmailReturnsEmptyStringIfEmptryStringIsGiven() {
107
		$this->assertEmpty(\TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail(''));
108
109
110
111
112
113
114
115
116
	}

	/**
	 * @test
	 */
	public function breakLinesForEmailReturnsOneLineIfCharWithIsNotExceeded() {
		$newlineChar = LF;
		$lineWidth = 76;
		$str = 'This text is not longer than 76 chars and therefore will not be broken.';
117
118
		$returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
		$this->assertEquals(1, count(explode($newlineChar, $returnString)));
119
120
121
122
123
124
125
126
127
	}

	/**
	 * @test
	 */
	public function breakLinesForEmailBreaksTextIfCharWithIsExceeded() {
		$newlineChar = LF;
		$lineWidth = 50;
		$str = 'This text is longer than 50 chars and therefore will be broken.';
128
129
		$returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
		$this->assertEquals(2, count(explode($newlineChar, $returnString)));
130
131
132
133
134
135
136
137
	}

	/**
	 * @test
	 */
	public function breakLinesForEmailBreaksTextWithNoSpaceFoundBeforeLimit() {
		$newlineChar = LF;
		$lineWidth = 10;
138
		// first space after 20 chars (more than $lineWidth)
139
		$str = 'abcdefghijklmnopqrst uvwxyz 123456';
140
141
		$returnString = \TYPO3\CMS\Core\Utility\MailUtility::breakLinesForEmail($str, $newlineChar, $lineWidth);
		$this->assertEquals($returnString, ((('abcdefghijklmnopqrst' . LF) . 'uvwxyz') . LF) . '123456');
142
	}
143

144
}
145

146
?>