[BUGFIX] Faulty check for missing SMTP port
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Mail / MailerTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Mail;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2011-2013 Ernesto Baschny (ernst@cron-it.de)
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for the TYPO3\CMS\Core\Mail\Mailer class.
29 *
30 * @author Helmut Hummel <helmut.hummel@typo3.org>
31 */
32 class MailerTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
33
34 /**
35 * @var \TYPO3\CMS\Core\Mail\Mailer
36 */
37 protected $fixture;
38
39 /**
40 * @var array
41 */
42 protected $globalMailSettings;
43
44 public function setUp() {
45 $this->globalMailSettings = $GLOBALS['TYPO3_CONF_VARS']['MAIL'];
46 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Mail\\Mailer', array('noMethodMocked'), array(), '', FALSE);
47 }
48
49 public function tearDown() {
50 unset($this->fixture);
51 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = $this->globalMailSettings;
52 }
53
54 //////////////////////////
55 // Tests concerning TYPO3\CMS\Core\Mail\Mailer
56 //////////////////////////
57 /**
58 * @test
59 */
60 public function injectedSettingsAreNotReplacedByGlobalSettings() {
61 $settings = array('transport' => 'mbox', 'transport_mbox_file' => '/path/to/file');
62 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail');
63 $this->fixture->injectMailSettings($settings);
64 $this->fixture->__construct();
65 $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
66 }
67
68 /**
69 * @test
70 */
71 public function globalSettingsAreUsedIfNoSettingsAreInjected() {
72 $settings = ($GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail'));
73 $this->fixture->__construct();
74 $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
75 }
76
77 /**
78 * Data provider for wrongConfigigurationThrowsException
79 *
80 * @return array Data sets
81 */
82 static public function wrongConfigigurationProvider() {
83 return array(
84 'smtp but no host' => array(array('transport' => 'smtp')),
85 'sendmail but no command' => array(array('transport' => 'sendmail')),
86 'mbox but no file' => array(array('transport' => 'mbox')),
87 'no instance of Swift_Transport' => array(array('transport' => 'TYPO3\\CMS\\Core\\Messaging\\ErrorpageMessage'))
88 );
89 }
90
91 /**
92 * @test
93 * @param $settings
94 * @dataProvider wrongConfigigurationProvider
95 * @expectedException \TYPO3\CMS\Core\Exception
96 */
97 public function wrongConfigigurationThrowsException($settings) {
98 $this->fixture->injectMailSettings($settings);
99 $this->fixture->__construct();
100 }
101
102 /**
103 * @test
104 */
105 public function providingCorrectClassnameDoesNotThrowException() {
106 if (!class_exists('t3lib_mail_SwiftMailerFakeTransport')) {
107 // Create fake custom transport class
108 eval('class t3lib_mail_SwiftMailerFakeTransport extends \\TYPO3\\CMS\\Core\\Mail\\MboxTransport {
109 public function __construct($settings) {}
110 }');
111 }
112 $this->fixture->injectMailSettings(array('transport' => 't3lib_mail_SwiftMailerFakeTransport'));
113 $this->fixture->__construct();
114 }
115
116 /**
117 * @test
118 */
119 public function noPortSettingSetsPortTo25() {
120 $this->fixture->injectMailSettings(array('transport' => 'smtp', 'transport_smtp_server' => 'localhost'));
121 $this->fixture->__construct();
122 $port = $this->fixture->getTransport()->getPort();
123 $this->assertEquals(25, $port);
124 }
125
126 /**
127 * @test
128 */
129 public function emptyPortSettingSetsPortTo25() {
130 $this->fixture->injectMailSettings(array('transport' => 'smtp', 'transport_smtp_server' => 'localhost:'));
131 $this->fixture->__construct();
132 $port = $this->fixture->getTransport()->getPort();
133 $this->assertEquals(25, $port);
134 }
135
136 /**
137 * @test
138 */
139 public function givenPortSettingIsRespected() {
140 $this->fixture->injectMailSettings(array('transport' => 'smtp', 'transport_smtp_server' => 'localhost:12345'));
141 $this->fixture->__construct();
142 $port = $this->fixture->getTransport()->getPort();
143 $this->assertEquals(12345, $port);
144 }
145 }
146
147 ?>