[FEATURE] Use dynamic path for typo3temp/var/
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Mail / TransportFactoryTest.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\Core\Environment;
19 use TYPO3\CMS\Core\Mail\MemorySpool;
20 use TYPO3\CMS\Core\Mail\TransportFactory;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23
24 /**
25 * Testcase for the TYPO3\CMS\Core\Mail\TransportFactory class.
26 */
27 class TransportFactoryTest extends UnitTestCase
28 {
29 /**
30 * Subject is not notice free, disable E_NOTICES
31 */
32 protected static $suppressNotices = true;
33
34 /**
35 * * @var TransportFactory
36 */
37 protected $subject;
38
39 protected function setUp()
40 {
41 $this->subject = GeneralUtility::makeInstance(TransportFactory::class);
42 }
43
44 /**
45 * @test
46 */
47 public function getReturnsSwiftSpoolTransportUsingSwiftFileSpool()
48 {
49 $mailSettings = [
50 'transport' => 'mail',
51 'transport_smtp_server' => 'localhost:25',
52 'transport_smtp_encrypt' => '',
53 'transport_smtp_username' => '',
54 'transport_smtp_password' => '',
55 'transport_sendmail_command' => '',
56 'transport_mbox_file' => '',
57 'defaultMailFromAddress' => '',
58 'defaultMailFromName' => '',
59 'transport_spool_type' => 'file',
60 'transport_spool_filepath' => '.',
61 ];
62
63 // Register fixture class
64 $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][\Swift_FileSpool::class]['className'] = Fixtures\FakeFileSpoolFixture::class;
65
66 /** @var \Swift_SpoolTransport $transport */
67 $transport = $this->subject->get($mailSettings);
68 $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
69
70 /** @var Fixtures\FakeFileSpoolFixture $spool */
71 $spool = $transport->getSpool();
72 $this->assertInstanceOf(\Swift_FileSpool::class, $spool);
73
74 $path = $spool->getPath();
75 $this->assertContains($mailSettings['transport_spool_filepath'], $path);
76 }
77
78 /**
79 * @test
80 */
81 public function getReturnsSwiftSpoolTransportUsingSwiftMemorySpool()
82 {
83 $mailSettings = [
84 'transport' => 'mail',
85 'transport_smtp_server' => 'localhost:25',
86 'transport_smtp_encrypt' => '',
87 'transport_smtp_username' => '',
88 'transport_smtp_password' => '',
89 'transport_sendmail_command' => '',
90 'transport_mbox_file' => '',
91 'defaultMailFromAddress' => '',
92 'defaultMailFromName' => '',
93 'transport_spool_type' => 'memory',
94 'transport_spool_filepath' => Environment::getVarPath() . '/messages/',
95 ];
96
97 // Register fixture class
98 $GLOBALS['TYPO3_CONF_VARS']['SYS']['Objects'][MemorySpool::class]['className'] = Fixtures\FakeMemorySpoolFixture::class;
99
100 /** @var \Swift_SpoolTransport $transport */
101 $transport = $this->subject->get($mailSettings);
102 $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
103
104 /** @var \Swift_MemorySpool $spool */
105 $spool = $transport->getSpool();
106 $this->assertInstanceOf(\Swift_MemorySpool::class, $spool);
107 }
108
109 /**
110 * @test
111 */
112 public function getReturnsSwiftSpoolTransportUsingCustomSpool()
113 {
114 $mailSettings = [
115 'transport' => 'mail',
116 'transport_smtp_server' => 'localhost:25',
117 'transport_smtp_encrypt' => '',
118 'transport_smtp_username' => '',
119 'transport_smtp_password' => '',
120 'transport_sendmail_command' => '',
121 'transport_mbox_file' => '',
122 'defaultMailFromAddress' => '',
123 'defaultMailFromName' => '',
124 'transport_spool_type' => 'TYPO3\\CMS\\Core\\Tests\\Unit\\Mail\\Fixtures\\FakeValidSpoolFixture',
125 'transport_spool_filepath' => Environment::getVarPath() . '/messages/',
126 ];
127
128 /** @var \Swift_SpoolTransport $transport */
129 $transport = $this->subject->get($mailSettings);
130 $this->assertInstanceOf(\Swift_SpoolTransport::class, $transport);
131
132 /** @var Fixtures\FakeValidSpoolFixture $spool */
133 $spool = $transport->getSpool();
134 $this->assertInstanceOf(Fixtures\FakeValidSpoolFixture::class, $spool);
135
136 $this->assertSame($mailSettings, $spool->getSettings());
137 }
138
139 /**
140 * @test
141 */
142 public function getThrowsRuntimeExceptionForInvalidCustomSpool()
143 {
144 $this->expectException(\RuntimeException::class);
145 $this->expectExceptionCode(1466799482);
146
147 $mailSettings = [
148 'transport' => 'mail',
149 'transport_smtp_server' => 'localhost:25',
150 'transport_smtp_encrypt' => '',
151 'transport_smtp_username' => '',
152 'transport_smtp_password' => '',
153 'transport_sendmail_command' => '',
154 'transport_mbox_file' => '',
155 'defaultMailFromAddress' => '',
156 'defaultMailFromName' => '',
157 'transport_spool_type' => 'TYPO3\\CMS\\Core\\Tests\\Unit\\Mail\\Fixtures\\FakeInvalidSpoolFixture',
158 'transport_spool_filepath' => Environment::getVarPath() . '/messages/',
159 ];
160
161 $this->subject->get($mailSettings);
162 }
163
164 /**
165 * @test
166 */
167 public function getReturnsSwiftMailTransport()
168 {
169 $mailSettings = [
170 'transport' => 'mail',
171 'transport_smtp_server' => 'localhost:25',
172 'transport_smtp_encrypt' => '',
173 'transport_smtp_username' => '',
174 'transport_smtp_password' => '',
175 'transport_sendmail_command' => '',
176 'transport_mbox_file' => '',
177 'defaultMailFromAddress' => '',
178 'defaultMailFromName' => '',
179 'transport_spool_type' => '',
180 'transport_spool_filepath' => Environment::getVarPath() . '/messages/',
181 ];
182
183 $transport = $this->subject->get($mailSettings);
184 $this->assertInstanceOf(\Swift_MailTransport::class, $transport);
185 }
186 }