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