This repository was archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathAbstractBootstrap.php
More file actions
385 lines (350 loc) · 11.9 KB
/
AbstractBootstrap.php
File metadata and controls
385 lines (350 loc) · 11.9 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
namespace Nimut\TestingFramework\Bootstrap;
/*
* This file is part of the NIMUT testing-framework project.
*
* It was taken from the TYPO3 CMS project (www.typo3.org).
*
* 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
* LICENSE file that was distributed with this source code.
*/
use Nimut\TestingFramework\File\NtfStreamWrapper;
use TYPO3\CMS\Core\Cache\Backend\NullBackend;
use TYPO3\CMS\Core\Cache\Frontend\PhpFrontend;
use TYPO3\CMS\Core\Configuration\ConfigurationManager;
use TYPO3\CMS\Core\Core\Bootstrap as CoreBootstrap;
use TYPO3\CMS\Core\Core\ClassLoadingInformation;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Package\PackageManager;
use TYPO3\CMS\Core\Package\UnitTestPackageManager;
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Core\Utility\GeneralUtility;
abstract class AbstractBootstrap
{
/**
* @var CoreBootstrap
*/
protected $bootstrap;
/**
* AbstractBootstrap constructor.
*
* @param CoreBootstrap $bootstrap
*/
public function __construct(CoreBootstrap $bootstrap = null)
{
putenv('TYPO3_CONTEXT=Testing');
$this->bootstrap = (null !== $bootstrap) ? $bootstrap : CoreBootstrap::getInstance();
}
/**
* Includes the Core Bootstrap class and calls its first few functions
*
* @return void
*/
protected function includeAndStartCoreBootstrap()
{
$classLoaderFilepath = $this->getClassLoaderFilepath();
$classLoader = require $classLoaderFilepath;
SystemEnvironmentBuilder::run(0, SystemEnvironmentBuilder::REQUESTTYPE_BE | SystemEnvironmentBuilder::REQUESTTYPE_CLI);
CoreBootstrap::initializeClassLoader($classLoader);
CoreBootstrap::baseSetup();
}
/**
* Initializes core cache handling
*
* @return void
*/
protected function initializeCachingHandling()
{
$this->bootstrap->disableCoreCache()
->initializeCachingFramework();
}
/**
* Bootstraps the system for functional tests
*
* @return void
*/
public function bootstrapFunctionalTestSystem()
{
$this->enableDisplayErrors();
$this->createNecessaryDirectoriesInDocumentRoot();
$this->defineOriginalRootPath();
$this->defineOriginalWebPath();
}
/**
* Bootstraps the system for unit tests
*
* @return void
*/
public function bootstrapUnitTestSystem()
{
$this->enableDisplayErrors();
$this->createNecessaryDirectoriesInDocumentRoot();
$this->defineSitePath();
$this->setTypo3Context();
$this->includeAndStartCoreBootstrap();
$this->initializeConfiguration();
$this->initializePackageManager();
$this->dumpAutoloadFiles();
$this->registerNtfStreamWrapper();
}
/**
* Makes sure error messages during the tests get displayed no matter what is set in php.ini.
*
* @return void
*/
protected function enableDisplayErrors()
{
@ini_set('display_errors', 1);
}
/**
* Creates the following directories in the TYPO3 document root:
* - typo3conf/ext
* - typo3temp/assets
* - typo3temp/var/tests
* - typo3temp/var/transient
* - uploads
*
* @return void
*/
protected function createNecessaryDirectoriesInDocumentRoot()
{
$webRoot = $this->getWebRoot();
$this->createDirectory($webRoot . 'typo3conf/ext');
$this->createDirectory($webRoot . 'typo3temp/assets');
$this->createDirectory($webRoot . 'typo3temp/var/tests');
$this->createDirectory($webRoot . 'typo3temp/var/transient');
$this->createDirectory($webRoot . 'uploads');
}
/**
* Defines the constant ORIGINAL_ROOT for the path to the original TYPO3 document root
*
* @return void
*/
protected function defineOriginalRootPath()
{
if (!defined('ORIGINAL_ROOT')) {
/** @var string */
define('ORIGINAL_ROOT', $this->getWebRoot());
}
if (!file_exists(ORIGINAL_ROOT . 'typo3/index.php')) {
$this->exitWithMessage(
'Unable to determine path to entry script.'
. ' Please check your path or set an environment variable \'TYPO3_PATH_ROOT\' to your root path.'
);
}
}
/**
* Defines the constant ORIGINAL_WEB for the path to the web(public) TYPO3 root dir
* In case of using helhum/typo3-secure-web your private and public sources can have different root paths
*
* @return void
*/
protected function defineOriginalWebPath()
{
if (!defined('ORIGINAL_WEB')) {
/** @var string */
define('ORIGINAL_WEB', $this->getPublicRootPath());
}
if (!file_exists(ORIGINAL_WEB . 'index.php')) {
$this->exitWithMessage(
'Unable to determine path to entry script.'
. ' Please check your path or set an environment variable \'TYPO3_PATH_WEB\' to your public root path.'
);
}
}
/**
* Returns the absolute path to the TYPO3 document root
*
* @return string the TYPO3 document root using Unix path separators
*/
protected function getWebRoot()
{
if (getenv('TYPO3_PATH_ROOT')) {
$webRoot = getenv('TYPO3_PATH_ROOT');
} elseif (getenv('TYPO3_PATH_WEB')) {
$webRoot = getenv('TYPO3_PATH_WEB');
} else {
$webRoot = getcwd();
}
return rtrim(str_replace('\\', '/', $webRoot), '/') . '/';
}
/**
* Returns the absolute path to the TYPO3 public root dir
*
* @return string the TYPO3 public root using Unix path separators
*/
protected function getPublicRootPath()
{
if (getenv('TYPO3_PATH_WEB')) {
$publicRoot = getenv('TYPO3_PATH_WEB');
} elseif (getenv('TYPO3_PATH_ROOT')) {
$publicRoot = getenv('TYPO3_PATH_ROOT');
} else {
$publicRoot = getcwd();
}
return rtrim(str_replace('\\', '/', $publicRoot), '/') . '/';
}
/**
* Creates the directory $directory (recursively if required).
*
* If $directory already exists, this method is a no-op.
*
* @param string $directory absolute path of the directory to be created
* @throws \RuntimeException
* @return void
*/
protected function createDirectory($directory)
{
clearstatcache();
if (is_dir($directory)) {
return;
}
if (!@mkdir($directory, 0777, true)) {
// Wait a couple of microseconds to prevent multiple derectory access due to parallel testing
usleep(mt_rand(1000, 2000));
if (!@mkdir($directory, 0777, true) && !is_dir($directory)) {
throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1423043755);
}
}
}
/**
* Echo out a text message and exit with error code
*
* @param string $message
*/
protected function exitWithMessage($message)
{
echo $message . PHP_EOL;
exit(1);
}
/**
* Defines the PATH_site, PATH_thisScript constant and sets $_SERVER['SCRIPT_NAME'].
*
* @return void
*/
protected function defineSitePath()
{
/** @var string */
define('PATH_site', $this->getWebRoot());
/** @var string */
define('PATH_thisScript', PATH_site . 'typo3/index.php');
$_SERVER['SCRIPT_NAME'] = PATH_thisScript;
if (!file_exists(PATH_thisScript)) {
$this->exitWithMessage('Unable to determine path to entry script. Please check your path or set an environment variable \'TYPO3_PATH_ROOT\' to your root path.');
}
}
/**
* Defines some constants and sets the environment variable TYPO3_CONTEXT
*
* @return void
*/
protected function setTypo3Context()
{
/** @var string */
define('TYPO3_MODE', 'BE');
/** @var string */
define('TYPO3_cliMode', true);
// Disable TYPO3_DLOG
define('TYPO3_DLOG', false);
}
/**
* Provides the default configuration in $GLOBALS['TYPO3_CONF_VARS']
*
* @return void
*/
protected function initializeConfiguration()
{
$configurationManager = new ConfigurationManager();
$GLOBALS['TYPO3_CONF_VARS'] = $configurationManager->getDefaultConfiguration();
// Avoid failing tests that rely on HTTP_HOST retrieval
$GLOBALS['TYPO3_CONF_VARS']['SYS']['trustedHostsPattern'] = '.*';
}
/**
* Initializes a package manager for tests that activates all packages by default
*
* @return void
*/
protected function initializePackageManager()
{
$cache = new PhpFrontend('cache_core', new NullBackend('production', []));
$packageManager = CoreBootstrap::createPackageManager(UnitTestPackageManager::class, $cache);
GeneralUtility::setSingletonInstance(PackageManager::class, $packageManager);
ExtensionManagementUtility::setPackageManager($packageManager);
}
/**
* Dump autoload info if in non composer mode
*
* @return void
*/
protected function dumpAutoloadFiles()
{
if (!Environment::isComposerMode()) {
ClassLoadingInformation::dumpClassLoadingInformation();
ClassLoadingInformation::registerClassLoadingInformation();
}
}
/**
* Registers the NtfStreamWrapper for ntf:// protocol
*
* @return void
*/
protected function registerNtfStreamWrapper()
{
NtfStreamWrapper::register();
}
/**
* Defines a list of basic constants that are used by GeneralUtility and other
* helpers during tests setup. Those are sanitized in SystemEnvironmentBuilder
* to be not defined again.
*
* @return void
* @see SystemEnvironmentBuilder::defineBaseConstants()
*/
protected function defineBaseConstants()
{
// A null, a tabulator, a linefeed, a carriage return, a substitution, a CR-LF combination
defined('NUL') ?: define('NUL', chr(0));
defined('TAB') ?: define('TAB', chr(9));
defined('LF') ?: define('LF', chr(10));
defined('CR') ?: define('CR', chr(13));
defined('SUB') ?: define('SUB', chr(26));
defined('CRLF') ?: define('CRLF', CR . LF);
if (!defined('TYPO3_OS')) {
// Operating system identifier
// Either "WIN" or empty string
$typoOs = '';
if (!stristr(PHP_OS, 'darwin') && !stristr(PHP_OS, 'cygwin') && stristr(PHP_OS, 'win')) {
$typoOs = 'WIN';
}
define('TYPO3_OS', $typoOs);
}
}
/**
* Checks and returns the file path of the autoload.php
*
* @return string
*/
protected function getClassLoaderFilepath()
{
$classLoaderFilepath = __DIR__ . '/../../../../../autoload.php';
if (!file_exists($classLoaderFilepath)) {
if (file_exists(__DIR__ . '/../../../.Build/vendor/autoload.php')) {
$classLoaderFilepath = __DIR__ . '/../../../.Build/vendor/autoload.php';
} elseif (file_exists($this->getWebRoot() . '../vendor/autoload.php')) {
$classLoaderFilepath = $this->getWebRoot() . '../vendor/autoload.php';
} else {
$this->exitWithMessage(
'ClassLoader can\'t be loaded.'
. ' Tried to find "' . $classLoaderFilepath . '".'
. ' Please check your path or set an environment variable \'TYPO3_PATH_ROOT\' to your root path.'
);
}
}
return $classLoaderFilepath;
}
}