[FEATURE] Integrate Swift Mailer's spool transport
[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 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Tests\Unit\Mail\Fixtures\FakeTransportFixture;
17
18 /**
19 * Testcase for the TYPO3\CMS\Core\Mail\Mailer class.
20 */
21 class MailerTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
22 {
23 /**
24 * @var \TYPO3\CMS\Core\Mail\Mailer
25 */
26 protected $subject;
27
28 protected function setUp()
29 {
30 $this->subject = $this->getMockBuilder(\TYPO3\CMS\Core\Mail\Mailer::class)
31 ->setMethods(['emitPostInitializeMailerSignal'])
32 ->disableOriginalConstructor()
33 ->getMock();
34 }
35
36 //////////////////////////
37 // Tests concerning TYPO3\CMS\Core\Mail\Mailer
38 //////////////////////////
39 /**
40 * @test
41 */
42 public function injectedSettingsAreNotReplacedByGlobalSettings()
43 {
44 $settings = ['transport' => 'mbox', 'transport_mbox_file' => '/path/to/file'];
45 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail'];
46 $this->subject->injectMailSettings($settings);
47 $this->subject->__construct();
48 $this->assertAttributeSame($settings, 'mailSettings', $this->subject);
49 }
50
51 /**
52 * @test
53 */
54 public function globalSettingsAreUsedIfNoSettingsAreInjected()
55 {
56 $settings = ($GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail']);
57 $this->subject->__construct();
58 $this->assertAttributeSame($settings, 'mailSettings', $this->subject);
59 }
60
61 /**
62 * Data provider for wrongConfigurationThrowsException
63 *
64 * @return array Data sets
65 */
66 public static function wrongConfigurationProvider()
67 {
68 return [
69 'smtp but no host' => [['transport' => 'smtp']],
70 'sendmail but no command' => [['transport' => 'sendmail']],
71 'mbox but no file' => [['transport' => 'mbox']],
72 'no instance of Swift_Transport' => [['transport' => \TYPO3\CMS\Core\Controller\ErrorPageController::class]]
73 ];
74 }
75
76 /**
77 * @test
78 * @param $settings
79 * @dataProvider wrongConfigurationProvider
80 */
81 public function wrongConfigurationThrowsException($settings)
82 {
83 $this->expectException(\TYPO3\CMS\Core\Exception::class);
84 $this->expectExceptionCode(1291068569);
85
86 $this->subject->injectMailSettings($settings);
87 $this->subject->__construct();
88 }
89
90 /**
91 * @test
92 */
93 public function providingCorrectClassnameDoesNotThrowException()
94 {
95 $this->subject->injectMailSettings(['transport' => FakeTransportFixture::class]);
96 $this->subject->__construct();
97 }
98
99 /**
100 * @test
101 */
102 public function noPortSettingSetsPortTo25()
103 {
104 $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost']);
105 $this->subject->__construct();
106 $port = $this->subject->getTransport()->getPort();
107 $this->assertEquals(25, $port);
108 }
109
110 /**
111 * @test
112 */
113 public function emptyPortSettingSetsPortTo25()
114 {
115 $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:']);
116 $this->subject->__construct();
117 $port = $this->subject->getTransport()->getPort();
118 $this->assertEquals(25, $port);
119 }
120
121 /**
122 * @test
123 */
124 public function givenPortSettingIsRespected()
125 {
126 $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:12345']);
127 $this->subject->__construct();
128 $port = $this->subject->getTransport()->getPort();
129 $this->assertEquals(12345, $port);
130 }
131
132 /**
133 * @test
134 * @dataProvider getRealTransportReturnsNoSpoolTransportProvider
135 */
136 public function getRealTransportReturnsNoSpoolTransport($settings)
137 {
138 $this->subject->injectMailSettings($settings);
139 // $this->subject->__construct();
140 $transport = $this->subject->getRealTransport();
141
142 $this->assertInstanceOf(\Swift_Transport::class, $transport);
143 $this->assertNotInstanceOf(\Swift_SpoolTransport::class, $transport);
144 }
145
146 /**
147 * Data provider for getRealTransportReturnsNoSpoolTransport
148 *
149 * @return array Data sets
150 */
151 public static function getRealTransportReturnsNoSpoolTransportProvider()
152 {
153 return [
154 'without spool' => [[
155 'transport' => 'mail',
156 'spool' => '',
157 ]],
158 'with spool' => [[
159 'transport' => 'mail',
160 'spool' => 'memory',
161 ]],
162 ];
163 }
164 }