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