eebbcee3268a5c1abece059aea838d9c76ed82d1
[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 public function setUp() {
40 $this->fixture = $this->getMock('TYPO3\\CMS\\Core\\Mail\\Mailer', array('noMethodMocked'), array(), '', FALSE);
41 }
42
43 //////////////////////////
44 // Tests concerning TYPO3\CMS\Core\Mail\Mailer
45 //////////////////////////
46 /**
47 * @test
48 */
49 public function injectedSettingsAreNotReplacedByGlobalSettings() {
50 $settings = array('transport' => 'mbox', 'transport_mbox_file' => '/path/to/file');
51 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail');
52 $this->fixture->injectMailSettings($settings);
53 $this->fixture->__construct();
54 $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
55 }
56
57 /**
58 * @test
59 */
60 public function globalSettingsAreUsedIfNoSettingsAreInjected() {
61 $settings = ($GLOBALS['TYPO3_CONF_VARS']['MAIL'] = array('transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail'));
62 $this->fixture->__construct();
63 $this->assertAttributeSame($settings, 'mailSettings', $this->fixture);
64 }
65
66 /**
67 * Data provider for wrongConfigigurationThrowsException
68 *
69 * @return array Data sets
70 */
71 static public function wrongConfigigurationProvider() {
72 return array(
73 'smtp but no host' => array(array('transport' => 'smtp')),
74 'sendmail but no command' => array(array('transport' => 'sendmail')),
75 'mbox but no file' => array(array('transport' => 'mbox')),
76 'no instance of Swift_Transport' => array(array('transport' => 'TYPO3\\CMS\\Core\\Messaging\\ErrorpageMessage'))
77 );
78 }
79
80 /**
81 * @test
82 * @param $settings
83 * @dataProvider wrongConfigigurationProvider
84 * @expectedException \TYPO3\CMS\Core\Exception
85 */
86 public function wrongConfigigurationThrowsException($settings) {
87 $this->fixture->injectMailSettings($settings);
88 $this->fixture->__construct();
89 }
90
91 /**
92 * @test
93 */
94 public function providingCorrectClassnameDoesNotThrowException() {
95 if (!class_exists('t3lib_mail_SwiftMailerFakeTransport')) {
96 // Create fake custom transport class
97 eval('class t3lib_mail_SwiftMailerFakeTransport extends \\TYPO3\\CMS\\Core\\Mail\\MboxTransport {
98 public function __construct($settings) {}
99 }');
100 }
101 $this->fixture->injectMailSettings(array('transport' => 't3lib_mail_SwiftMailerFakeTransport'));
102 $this->fixture->__construct();
103 }
104
105 /**
106 * @test
107 */
108 public function noPortSettingSetsPortTo25() {
109 $this->fixture->injectMailSettings(array('transport' => 'smtp', 'transport_smtp_server' => 'localhost'));
110 $this->fixture->__construct();
111 $port = $this->fixture->getTransport()->getPort();
112 $this->assertEquals(25, $port);
113 }
114
115 /**
116 * @test
117 */
118 public function emptyPortSettingSetsPortTo25() {
119 $this->fixture->injectMailSettings(array('transport' => 'smtp', 'transport_smtp_server' => 'localhost:'));
120 $this->fixture->__construct();
121 $port = $this->fixture->getTransport()->getPort();
122 $this->assertEquals(25, $port);
123 }
124
125 /**
126 * @test
127 */
128 public function givenPortSettingIsRespected() {
129 $this->fixture->injectMailSettings(array('transport' => 'smtp', 'transport_smtp_server' => 'localhost:12345'));
130 $this->fixture->__construct();
131 $port = $this->fixture->getTransport()->getPort();
132 $this->assertEquals(12345, $port);
133 }
134 }