‪TYPO3CMS  10.4
AbstractRequestHandlingTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
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 
19 
20 use Symfony\Component\Mailer\SentMessage;
26 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerFactory;
27 use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerWriter;
28 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
29 
30 abstract class ‪AbstractRequestHandlingTest extends FunctionalTestCase
31 {
33 
34  protected const ‪ROOT_PAGE_BASE_URI = 'http://localhost';
35 
36  protected const ‪LANGUAGE_PRESETS = [
37  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_GB.UTF8', 'iso' => 'en', 'hrefLang' => 'en-GB', 'direction' => ''],
38  ];
39 
40  protected const ‪MAIL_SPOOL_FOLDER = 'typo3temp/var/transient/spool/';
41 
42  protected ‪$coreExtensionsToLoad = ['form', 'fluid_styled_content'];
43 
44  protected ‪$testExtensionsToLoad = [
45  'typo3/sysext/form/Tests/Functional/RequestHandling/Fixtures/Extensions/form_caching_tests',
46  ];
47 
49  'MAIL' => [
50  'defaultMailFromAddress' => 'hello@typo3.org',
51  'defaultMailFromName' => 'TYPO3',
52  'transport' => 'mbox',
53  'transport_spool_type' => 'file',
54  'transport_spool_filepath' => ‪self::MAIL_SPOOL_FOLDER,
55  ],
56  'SYS' => [
57  'caching' => [
58  'cacheConfigurations' => [
59  'hash' => [
60  'backend' => Typo3DatabaseBackend::class,
61  'frontend' => VariableFrontend::class,
62  ],
63  'pages' => [
64  'backend' => Typo3DatabaseBackend::class,
65  'frontend' => VariableFrontend::class,
66  ],
67  'pagesection' => [
68  'backend' => Typo3DatabaseBackend::class,
69  'frontend' => VariableFrontend::class,
70  ],
71  'rootline' => [
72  'backend' => Typo3DatabaseBackend::class,
73  'frontend' => VariableFrontend::class,
74  ],
75  ],
76  ],
77  'encryptionKey' => '4408d27a916d51e624b69af3554f516dbab61037a9f7b9fd6f81b4d3bedeccb6',
78  ],
79 
80  ];
81 
85  protected ‪$databaseScenarioFile = '';
86 
87  protected function ‪setUp(): void
88  {
89  parent::setUp();
90 
92  'site1',
93  $this->‪buildSiteConfiguration(1000, static::ROOT_PAGE_BASE_URI . '/'),
94  [
95  $this->‪buildDefaultLanguageConfiguration('EN', '/'),
96  ]
97  );
98 
99  $this->withDatabaseSnapshot(function () {
100  $this->‪setUpDatabase();
101  });
102  }
103 
104  protected function ‪tearDown(): void
105  {
106  $this->purgeMailSpool();
107  parent::tearDown();
108  }
109 
110  private function ‪setUpDatabase(): void
111  {
112  $backendUser = $this->setUpBackendUserFromFixture(1);
114 
115  $factory = DataHandlerFactory::fromYamlFile($this->databaseScenarioFile);
116  $writer = DataHandlerWriter::withBackendUser($backendUser);
117  $writer->invokeFactory($factory);
118  static::failIfArrayIsNotEmpty(
119  $writer->getErrors()
120  );
121  }
122 
123  protected function ‪getMailSpoolMessages(): array
124  {
125  $messages = [];
126  foreach (array_filter(glob($this->instancePath . '/' . self::MAIL_SPOOL_FOLDER . '*'), 'is_file') as $path) {
127  $serializedMessage = file_get_contents($path);
128  $sentMessage = unserialize($serializedMessage);
129  if (!$sentMessage instanceof SentMessage) {
130  continue;
131  }
132  $fluidEmail = $sentMessage->getOriginalMessage();
133  if (!$fluidEmail instanceof ‪FluidEmail) {
134  continue;
135  }
136 
137  $parsedHeaders = $this->parseRawHeaders($sentMessage->toString());
138  $item = [
139  'plaintext' => $fluidEmail->getTextBody(),
140  'html' => $fluidEmail->getHtmlBody(),
141  'subject' => $fluidEmail->getSubject(),
142  'date' => $fluidEmail->getDate() ?? $parsedHeaders['Date'] ?? null,
143  'to' => $fluidEmail->getTo(),
144  ];
145  if (is_string($item['date'])) {
146  // @todo `@timezone` is not handled here - probably tests don't need date at all
147  $item['date'] = new \DateTimeImmutable($item['date']);
148  }
149  $messages[] = $item;
150  }
151 
152  return $messages;
153  }
154 
159  protected function parseRawHeaders(string $rawMessage): array
160  {
161  $rawParts = explode("\r\n\r\n", $rawMessage, 2);
162  $rawLines = explode("\r\n", $rawParts[0]);
163  $rawHeaders = array_map(
164  function (string $rawLine) {
165  return array_map(
166  'trim',
167  explode(':', $rawLine, 2)
168  );
169  },
170  $rawLines
171  );
172  return array_combine(
173  array_column($rawHeaders, 0),
174  array_column($rawHeaders, 1)
175  );
176  }
177 
178  protected function purgeMailSpool(): void
179  {
180  foreach (glob($this->instancePath . '/' . self::MAIL_SPOOL_FOLDER . '*') as $path) {
181  unlink($path);
182  }
183  }
184 }
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\setUp
‪setUp()
Definition: AbstractRequestHandlingTest.php:85
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling
Definition: AbstractRequestHandlingTest.php:18
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\getMailSpoolMessages
‪getMailSpoolMessages()
Definition: AbstractRequestHandlingTest.php:121
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\$databaseScenarioFile
‪string $databaseScenarioFile
Definition: AbstractRequestHandlingTest.php:83
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:36
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:58
‪TYPO3\CMS\Core\Cache\Backend\Typo3DatabaseBackend
Definition: Typo3DatabaseBackend.php:31
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\ROOT_PAGE_BASE_URI
‪const ROOT_PAGE_BASE_URI
Definition: AbstractRequestHandlingTest.php:33
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\setUpDatabase
‪setUpDatabase()
Definition: AbstractRequestHandlingTest.php:108
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildDefaultLanguageConfiguration
‪array buildDefaultLanguageConfiguration(string $identifier, string $base)
Definition: SiteBasedTestTrait.php:124
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:617
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest
Definition: AbstractRequestHandlingTest.php:31
‪TYPO3\CMS\Core\Mail\FluidEmail
Definition: FluidEmail.php:35
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: AbstractRequestHandlingTest.php:35
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\$coreExtensionsToLoad
‪$coreExtensionsToLoad
Definition: AbstractRequestHandlingTest.php:41
‪TYPO3\CMS\Core\Cache\Frontend\VariableFrontend
Definition: VariableFrontend.php:25
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\$configurationToUseInTestInstance
‪$configurationToUseInTestInstance
Definition: AbstractRequestHandlingTest.php:47
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\tearDown
‪tearDown()
Definition: AbstractRequestHandlingTest.php:102
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\$testExtensionsToLoad
‪$testExtensionsToLoad
Definition: AbstractRequestHandlingTest.php:43
‪TYPO3\CMS\Form\Tests\Functional\RequestHandling\AbstractRequestHandlingTest\MAIL_SPOOL_FOLDER
‪const MAIL_SPOOL_FOLDER
Definition: AbstractRequestHandlingTest.php:39
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:66
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪array buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:109