[FEATURE] Introduce PSR-14-based EventDispatcher as alternative for hooks
[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 Prophecy\Argument;
19 use Symfony\Component\Mailer\Transport\NullTransport;
20 use Symfony\Component\Mailer\Transport\SendmailTransport;
21 use Symfony\Component\Mailer\Transport\TransportInterface;
22 use TYPO3\CMS\Core\Controller\ErrorPageController;
23 use TYPO3\CMS\Core\EventDispatcher\EventDispatcher;
24 use TYPO3\CMS\Core\Exception;
25 use TYPO3\CMS\Core\Mail\DelayedTransportInterface;
26 use TYPO3\CMS\Core\Mail\Mailer;
27 use TYPO3\CMS\Core\Mail\TransportFactory;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
30
31 /**
32 * Test case
33 */
34 class MailerTest extends UnitTestCase
35 {
36 /**
37 * @var bool Reset singletons created by subject
38 */
39 protected $resetSingletonInstances = true;
40
41 /**
42 * @var Mailer
43 */
44 protected $subject;
45
46 /**
47 * Set up
48 */
49 protected function setUp(): void
50 {
51 parent::setUp();
52 $eventDispatcher = $this->prophesize(EventDispatcher::class);
53 $eventDispatcher->dispatch(Argument::cetera())->willReturnArgument(0);
54 GeneralUtility::setSingletonInstance(EventDispatcher::class, $eventDispatcher->reveal());
55
56 $this->subject = $this->getMockBuilder(Mailer::class)
57 ->setMethods(null)
58 ->disableOriginalConstructor()
59 ->getMock();
60 }
61
62 /**
63 * @test
64 */
65 public function injectedSettingsAreNotReplacedByGlobalSettings()
66 {
67 $settings = ['transport' => 'mbox', 'transport_mbox_file' => '/path/to/file'];
68 $GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail -bs'];
69
70 $transportFactory = $this->prophesize(TransportFactory::class);
71 $transportFactory->get(Argument::any())->willReturn($this->prophesize(SendmailTransport::class));
72 GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
73 $this->subject->injectMailSettings($settings);
74 $this->subject->__construct();
75
76 $transportFactory->get($settings)->shouldHaveBeenCalled();
77 }
78
79 /**
80 * @test
81 */
82 public function globalSettingsAreUsedIfNoSettingsAreInjected()
83 {
84 $settings = ($GLOBALS['TYPO3_CONF_VARS']['MAIL'] = ['transport' => 'sendmail', 'transport_sendmail_command' => 'sendmail -bs']);
85 $this->subject->__construct();
86 $transportFactory = $this->prophesize(TransportFactory::class);
87 $transportFactory->get(Argument::any())->willReturn($this->prophesize(SendmailTransport::class));
88 GeneralUtility::setSingletonInstance(TransportFactory::class, $transportFactory->reveal());
89 $this->subject->injectMailSettings($settings);
90 $this->subject->__construct();
91
92 $transportFactory->get($settings)->shouldHaveBeenCalled();
93 }
94
95 /**
96 * Data provider for wrongConfigurationThrowsException
97 *
98 * @return array Data sets
99 */
100 public static function wrongConfigurationProvider()
101 {
102 return [
103 'smtp but no host' => [['transport' => 'smtp']],
104 'mbox but no file' => [['transport' => 'mbox']],
105 'no instance of TransportInterface' => [['transport' => ErrorPageController::class]]
106 ];
107 }
108
109 /**
110 * @test
111 * @param $settings
112 * @dataProvider wrongConfigurationProvider
113 */
114 public function wrongConfigurationThrowsException($settings)
115 {
116 $this->expectException(Exception::class);
117 $this->expectExceptionCode(1291068569);
118
119 $this->subject->injectMailSettings($settings);
120 $this->subject->__construct();
121 }
122
123 /**
124 * @test
125 */
126 public function providingCorrectClassnameDoesNotThrowException()
127 {
128 $this->subject->injectMailSettings(['transport' => NullTransport::class]);
129 $this->subject->__construct();
130 }
131
132 /**
133 * @test
134 */
135 public function noPortSettingSetsPortTo25()
136 {
137 $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost']);
138 $this->subject->__construct();
139 $port = $this->subject->getTransport()->getStream()->getPort();
140 $this->assertEquals(25, $port);
141 }
142
143 /**
144 * @test
145 */
146 public function emptyPortSettingSetsPortTo25()
147 {
148 $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:']);
149 $this->subject->__construct();
150 $port = $this->subject->getTransport()->getStream()->getPort();
151 $this->assertEquals(25, $port);
152 }
153
154 /**
155 * @test
156 */
157 public function givenPortSettingIsRespected()
158 {
159 $this->subject->injectMailSettings(['transport' => 'smtp', 'transport_smtp_server' => 'localhost:12345']);
160 $this->subject->__construct();
161 $port = $this->subject->getTransport()->getStream()->getPort();
162 $this->assertEquals(12345, $port);
163 }
164
165 /**
166 * @test
167 * @dataProvider getRealTransportReturnsNoSpoolTransportProvider
168 */
169 public function getRealTransportReturnsNoSpoolTransport($settings)
170 {
171 $this->subject->injectMailSettings($settings);
172 $transport = $this->subject->getRealTransport();
173
174 $this->assertInstanceOf(TransportInterface::class, $transport);
175 $this->assertNotInstanceOf(DelayedTransportInterface::class, $transport);
176 }
177
178 /**
179 * Data provider for getRealTransportReturnsNoSpoolTransport
180 *
181 * @return array Data sets
182 */
183 public static function getRealTransportReturnsNoSpoolTransportProvider()
184 {
185 return [
186 'without spool' => [[
187 'transport' => 'sendmail',
188 'spool' => '',
189 ]],
190 'with spool' => [[
191 'transport' => 'sendmail',
192 'spool' => 'memory',
193 ]],
194 ];
195 }
196 }