This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
FunctionalTestCase.php
166 lines (134 loc) · 5.23 KB
/
FunctionalTestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
declare(strict_types=1);
/*
* This file is part of the hcaptcha extension for TYPO3
* - (c) 2021 waldhacker UG (haftungsbeschränkt)
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*
* The TYPO3 project - inspiring people to share!
*/
namespace Waldhacker\Hcaptcha\Tests\Functional;
use Symfony\Component\Mailer\SentMessage;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerFactory;
use TYPO3\TestingFramework\Core\Functional\Framework\DataHandling\Scenario\DataHandlerWriter;
use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequestContext;
use Waldhacker\Hcaptcha\Tests\Functional\SiteHandling\SiteBasedTestTrait;
use ZBateson\MailMimeParser\Message;
abstract class FunctionalTestCase extends \TYPO3\TestingFramework\Core\Functional\FunctionalTestCase
{
use SiteBasedTestTrait;
public const MAIL_SPOOL_FOLDER = 'typo3temp/var/transient/spool/';
protected const ENCRYPTION_KEY = '4408d27a916d51e624b69af3554f516dbab61037a9f7b9fd6f81b4d3bedeccb6';
protected const TYPO3_CONF_VARS = [
'SYS' => [
'encryptionKey' => self::ENCRYPTION_KEY,
],
];
protected const LANGUAGE_PRESETS = [
'DE' => ['id' => 0, 'title' => 'Deutsch', 'locale' => 'de_DE.UTF8', 'iso' => 'de', 'hrefLang' => 'de-DE', 'direction' => ''],
];
protected const ROOT_PAGE_BASE_URI = 'http://localhost';
/**
* @see https://docs.hcaptcha.com/#integration-testing-test-keys
*/
protected const VALID_HCAPTCHA_RESPONSE = '10000000-aaaa-bbbb-cccc-000000000001';
protected InternalRequestContext $internalRequestContext;
protected $pathsToLinkInTestInstance = [
'typo3conf/ext/hcaptcha/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
];
protected $coreExtensionsToLoad = [
'core',
'backend',
'frontend',
'extbase',
'install',
'recordlist',
'fluid',
'fluid_styled_content',
'form',
];
protected $testExtensionsToLoad = ['typo3conf/ext/hcaptcha'];
protected $rootPageUid = 1;
protected $databaseScenarioFile = __DIR__ . '/Fixtures/Frontend/StandardPagesScenario.yaml';
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();
static::initializeDatabaseSnapshot();
}
public static function tearDownAfterClass(): void
{
static::destroyDatabaseSnapshot();
parent::tearDownAfterClass();
}
protected function setUp(): void
{
parent::setUp();
$GLOBALS['TYPO3_CONF_VARS']['SYS']['encryptionKey'] = self::ENCRYPTION_KEY;
$this->writeSiteConfiguration(
'acme-com',
$this->buildSiteConfiguration($this->rootPageUid, self::ROOT_PAGE_BASE_URI . '/'),
[
$this->buildDefaultLanguageConfiguration('DE', '/'),
],
[
$this->buildErrorHandlingConfiguration('Fluid', [404]),
]
);
$this->internalRequestContext = (new InternalRequestContext())
->withGlobalSettings(['TYPO3_CONF_VARS' => static::TYPO3_CONF_VARS]);
$this->withDatabaseSnapshot(function () {
$this->setUpDatabase();
});
}
protected function tearDown(): void
{
unset($GLOBALS['TYPO3_CONF_VARS']);
unset($this->internalRequestContext);
$this->purgeMailSpool();
parent::tearDown();
}
protected function setUpDatabase(): void
{
$backendUser = $this->setUpBackendUserFromFixture(1);
Bootstrap::initializeLanguageObject();
$factory = DataHandlerFactory::fromYamlFile($this->databaseScenarioFile);
$writer = DataHandlerWriter::withBackendUser($backendUser);
$writer->invokeFactory($factory);
static::failIfArrayIsNotEmpty(
$writer->getErrors()
);
}
protected function getMailSpoolMessages(): array
{
$messages = [];
foreach (array_filter(glob($this->instancePath . '/' . self::MAIL_SPOOL_FOLDER . '*'), 'is_file') as $path) {
$serializedMessage = file_get_contents($path);
$message = unserialize($serializedMessage);
if (!($message instanceof SentMessage)) {
continue;
}
$message = Message::from($message->toString(), false);
$messages[] = [
'plaintext' => $message->getTextContent(),
//'html' => $message->getHtmlContent(),
'subject' => $message->getHeaderValue('Subject'),
'date' => new \DateTime($message->getHeaderValue('Date')),
'to' => $message->getHeaderValue('To'),
];
}
return $messages;
}
protected function purgeMailSpool(): void
{
foreach (glob($this->instancePath . '/' . self::MAIL_SPOOL_FOLDER . '*') as $path) {
unlink($path);
}
}
}