2 namespace TYPO3\CMS\Core\Tests
;
5 * This file is part of the TYPO3 CMS project.
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
14 * The TYPO3 project - inspiring people to share!
18 * Utility class to set up and bootstrap TYPO3 CMS for functional tests
20 class FunctionalTestCaseBootstrapUtility
{
23 * @var string Identifier calculated from test case class
25 protected $identifier;
28 * @var string Absolute path to test instance document root
30 protected $instancePath;
33 * @var string Name of test database
35 protected $databaseName;
38 * @var string Name of original database
40 protected $originalDatabaseName;
43 * @var array These extensions are always loaded
45 protected $defaultActivatedCoreExtensions = array(
56 * @var array These folder are always created
58 protected $defaultFoldersToCreate = array(
68 * Set up creates a test instance and database.
70 * @param string $testCaseClassName Name of test case class
71 * @param array $coreExtensionsToLoad Array of core extensions to load
72 * @param array $testExtensionsToLoad Array of test extensions to load
73 * @param array $pathsToLinkInTestInstance Array of source => destination path pairs to be linked
74 * @param array $configurationToUse Array of TYPO3_CONF_VARS that need to be overridden
75 * @param array $additionalFoldersToCreate Array of folder paths to be created
76 * @return string Path to TYPO3 CMS test installation for this test case
78 public function setUp(
80 array $coreExtensionsToLoad,
81 array $testExtensionsToLoad,
82 array $pathsToLinkInTestInstance,
83 array $configurationToUse,
84 array $additionalFoldersToCreate
86 $this->setUpIdentifier($testCaseClassName);
87 $this->setUpInstancePath();
88 if ($this->recentTestInstanceExists()) {
89 $this->setUpBasicTypo3Bootstrap();
90 $this->initializeTestDatabase();
91 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->loadExtensionTables(TRUE);
93 $this->removeOldInstanceIfExists();
94 $this->setUpInstanceDirectories($additionalFoldersToCreate);
95 $this->setUpInstanceCoreLinks();
96 $this->linkTestExtensionsToInstance($testExtensionsToLoad);
97 $this->linkPathsInTestInstance($pathsToLinkInTestInstance);
98 $this->setUpLocalConfiguration($configurationToUse);
99 $this->setUpPackageStates($coreExtensionsToLoad, $testExtensionsToLoad);
100 $this->setUpBasicTypo3Bootstrap();
101 $this->setUpTestDatabase();
102 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->loadExtensionTables(TRUE);
103 $this->createDatabaseStructure();
106 return $this->instancePath
;
110 * Checks whether the current test instance exists and is younger than
115 protected function recentTestInstanceExists() {
116 if (@file_get_contents
($this->instancePath
. '/last_run.txt') <= (time() - 300)) {
119 // Test instance exists and is pretty young -> re-use
125 * Calculate a "unique" identifier for the test database and the
126 * instance patch based on the given test case class name.
128 * As a result, the database name will be identical between different
129 * test runs, but different between each test case.
131 protected function setUpIdentifier($testCaseClassName) {
132 // 7 characters of sha1 should be enough for a unique identification
133 $this->identifier
= substr(sha1($testCaseClassName), 0, 7);
137 * Calculates path to TYPO3 CMS test installation for this test case.
141 protected function setUpInstancePath() {
142 $this->instancePath
= ORIGINAL_ROOT
. 'typo3temp/functional-' . $this->identifier
;
146 * Remove test instance folder structure in setUp() if it exists.
147 * This may happen if a functional test before threw a fatal.
151 protected function removeOldInstanceIfExists() {
152 if (is_dir($this->instancePath
)) {
153 $this->removeInstance();
158 * Create folder structure of test instance.
160 * @param array $additionalFoldersToCreate Array of additional folders to be created
164 protected function setUpInstanceDirectories(array $additionalFoldersToCreate = array()) {
165 $foldersToCreate = array_merge($this->defaultFoldersToCreate
, $additionalFoldersToCreate);
166 foreach ($foldersToCreate as $folder) {
167 $success = mkdir($this->instancePath
. $folder);
170 'Creating directory failed: ' . $this->instancePath
. $folder,
176 // Store the time we created this directory
177 file_put_contents($this->instancePath
. '/last_run.txt', time());
181 * Link TYPO3 CMS core from "parent" instance.
186 protected function setUpInstanceCoreLinks() {
188 ORIGINAL_ROOT
. 'typo3' => $this->instancePath
. '/typo3',
189 ORIGINAL_ROOT
. 'index.php' => $this->instancePath
. '/index.php'
191 foreach ($linksToSet as $from => $to) {
192 $success = symlink($from, $to);
195 'Creating link failed: from ' . $from . ' to: ' . $to,
203 * Link test extensions to the typo3conf/ext folder of the instance.
205 * @param array $extensionPaths Contains paths to extensions relative to document root
209 protected function linkTestExtensionsToInstance(array $extensionPaths) {
210 foreach ($extensionPaths as $extensionPath) {
211 $absoluteExtensionPath = ORIGINAL_ROOT
. $extensionPath;
212 if (!is_dir($absoluteExtensionPath)) {
214 'Test extension path ' . $absoluteExtensionPath . ' not found',
218 $destinationPath = $this->instancePath
. '/typo3conf/ext/' . basename($absoluteExtensionPath);
219 $success = symlink($absoluteExtensionPath, $destinationPath);
222 'Can not link extension folder: ' . $absoluteExtensionPath . ' to ' . $destinationPath,
230 * Link paths inside the test instance, e.g. from a fixture fileadmin subfolder to the
231 * test instance fileadmin folder
233 * @param array $pathsToLinkInTestInstance Contains paths as array of source => destination in key => value pairs of folders relative to test instance root
234 * @throws \TYPO3\CMS\Core\Tests\Exception if a source path could not be found
235 * @throws \TYPO3\CMS\Core\Tests\Exception on failing creating the symlink
237 * @see \TYPO3\CMS\Core\Tests\FunctionalTestCase::$pathsToLinkInTestInstance
239 protected function linkPathsInTestInstance(array $pathsToLinkInTestInstance) {
240 foreach ($pathsToLinkInTestInstance as $sourcePathToLinkInTestInstance => $destinationPathToLinkInTestInstance) {
241 $sourcePath = $this->instancePath
. '/' . ltrim($sourcePathToLinkInTestInstance, '/');
242 if (!file_exists($sourcePath)) {
244 'Path ' . $sourcePath . ' not found',
248 $destinationPath = $this->instancePath
. '/' . ltrim($destinationPathToLinkInTestInstance, '/');
249 $success = symlink($sourcePath, $destinationPath);
252 'Can not link the path ' . $sourcePath . ' to ' . $destinationPath,
260 * Create LocalConfiguration.php file in the test instance
262 * @param array $configurationToMerge
266 protected function setUpLocalConfiguration(array $configurationToMerge) {
267 $databaseName = getenv('typo3DatabaseName');
268 $databaseHost = getenv('typo3DatabaseHost');
269 $databaseUsername = getenv('typo3DatabaseUsername');
270 $databasePassword = getenv('typo3DatabasePassword');
271 $databasePort = getenv('typo3DatabasePort');
272 $databaseSocket = getenv('typo3DatabaseSocket');
273 if ($databaseName ||
$databaseHost ||
$databaseUsername ||
$databasePassword ||
$databasePort ||
$databaseSocket) {
274 // Try to get database credentials from environment variables first
275 $originalConfigurationArray = array(
279 $originalConfigurationArray['DB']['database'] = $databaseName;
282 $originalConfigurationArray['DB']['host'] = $databaseHost;
284 if ($databaseUsername) {
285 $originalConfigurationArray['DB']['username'] = $databaseUsername;
287 if ($databasePassword) {
288 $originalConfigurationArray['DB']['password'] = $databasePassword;
291 $originalConfigurationArray['DB']['port'] = $databasePort;
293 if ($databaseSocket) {
294 $originalConfigurationArray['DB']['socket'] = $databaseSocket;
296 } elseif (file_exists(ORIGINAL_ROOT
. 'typo3conf/LocalConfiguration.php')) {
297 // See if a LocalConfiguration file exists in "parent" instance to get db credentials from
298 $originalConfigurationArray = require ORIGINAL_ROOT
. 'typo3conf/LocalConfiguration.php';
301 'Database credentials for functional tests are neither set through environment'
302 . ' variables, and can not be found in an existing LocalConfiguration file',
307 // Base of final LocalConfiguration is core factory configuration
308 $finalConfigurationArray = require ORIGINAL_ROOT
. 'typo3/sysext/core/Configuration/FactoryConfiguration.php';
310 $this->mergeRecursiveWithOverrule($finalConfigurationArray, require ORIGINAL_ROOT
. 'typo3/sysext/core/Build/Configuration/FunctionalTestsConfiguration.php');
311 $this->mergeRecursiveWithOverrule($finalConfigurationArray, $configurationToMerge);
312 $finalConfigurationArray['DB'] = $originalConfigurationArray['DB'];
313 // Calculate and set new database name
314 $this->originalDatabaseName
= $originalConfigurationArray['DB']['database'];
315 $this->databaseName
= $this->originalDatabaseName
. '_ft' . $this->identifier
;
317 // Maximum database name length for mysql is 64 characters
318 if (strlen($this->databaseName
) > 64) {
319 $maximumOriginalDatabaseName = 64 - strlen('_ft' . $this->identifier
);
321 'The name of the database that is used for the functional test (' . $this->databaseName
. ')' .
322 ' exceeds the maximum length of 64 character allowed by MySQL. You have to shorten your' .
323 ' original database name to ' . $maximumOriginalDatabaseName . ' characters',
328 $finalConfigurationArray['DB']['database'] = $this->databaseName
;
330 $result = $this->writeFile(
331 $this->instancePath
. '/typo3conf/LocalConfiguration.php',
335 $finalConfigurationArray
341 throw new Exception('Can not write local configuration', 1376657277);
346 * Compile typo3conf/PackageStates.php containing default packages like core,
347 * a functional test specific list of additional core extensions, and a list of
350 * @param array $coreExtensionsToLoad Additional core extensions to load
351 * @param array $testExtensionPaths Paths to extensions relative to document root
353 * @TODO Figure out what the intention of the upper arguments is
355 protected function setUpPackageStates(array $coreExtensionsToLoad, array $testExtensionPaths) {
356 $packageStates = array(
357 'packages' => array(),
361 // Register default list of extensions and set active
362 foreach ($this->defaultActivatedCoreExtensions
as $extensionName) {
363 $packageStates['packages'][$extensionName] = array(
365 'packagePath' => 'typo3/sysext/' . $extensionName . '/',
366 'classesPath' => 'Classes/',
370 // Register additional core extensions and set active
371 foreach ($coreExtensionsToLoad as $extensionName) {
372 if (isset($packageSates['packages'][$extensionName])) {
374 $extensionName . ' is already registered as default core extension to load, no need to load it explicitly',
378 $packageStates['packages'][$extensionName] = array(
380 'packagePath' => 'typo3/sysext/' . $extensionName . '/',
381 'classesPath' => 'Classes/',
385 // Activate test extensions that have been symlinked before
386 foreach ($testExtensionPaths as $extensionPath) {
387 $extensionName = basename($extensionPath);
388 if (isset($packageSates['packages'][$extensionName])) {
390 $extensionName . ' is already registered as extension to load, no need to load it explicitly',
394 $packageStates['packages'][$extensionName] = array(
396 'packagePath' => 'typo3conf/ext/' . $extensionName . '/',
397 'classesPath' => 'Classes/',
401 $result = $this->writeFile(
402 $this->instancePath
. '/typo3conf/PackageStates.php',
412 throw new Exception('Can not write PackageStates', 1381612729);
417 * Bootstrap basic TYPO3
421 protected function setUpBasicTypo3Bootstrap() {
422 $_SERVER['PWD'] = $this->instancePath
;
423 $_SERVER['argv'][0] = 'index.php';
425 define('TYPO3_MODE', 'BE');
426 define('TYPO3_cliMode', TRUE);
428 require_once $this->instancePath
. '/typo3/sysext/core/Classes/Core/CliBootstrap.php';
429 \TYPO3\CMS\Core\Core\CliBootstrap
::checkEnvironmentOrDie();
431 require_once $this->instancePath
. '/typo3/sysext/core/Classes/Core/Bootstrap.php';
432 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()
434 ->loadConfigurationAndInitialize(TRUE)
435 ->loadTypo3LoadedExtAndExtLocalconf(TRUE)
436 ->applyAdditionalConfigurationSettings();
440 * Populate $GLOBALS['TYPO3_DB'] and create test database
442 * @throws \TYPO3\CMS\Core\Tests\Exception
445 protected function setUpTestDatabase() {
446 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->initializeTypo3DbGlobal();
447 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
448 $database = $GLOBALS['TYPO3_DB'];
449 if(!$database->sql_pconnect()) {
451 'TYPO3 Fatal Error: The current username, password or host was not accepted when the'
452 . ' connection to the database was attempted to be established!',
457 // Drop database in case a previous test had a fatal and did not clean up properly
458 $database->admin_query('DROP DATABASE IF EXISTS `' . $this->databaseName
. '`');
459 $createDatabaseResult = $database->admin_query('CREATE DATABASE `' . $this->databaseName
. '`');
460 if (!$createDatabaseResult) {
461 $user = $GLOBALS['TYPO3_CONF_VARS']['DB']['username'];
462 $host = $GLOBALS['TYPO3_CONF_VARS']['DB']['host'];
464 'Unable to create database with name ' . $this->databaseName
. '. This is probably a permission problem.'
465 . ' For this instance this could be fixed executing'
466 . ' "GRANT ALL ON `' . $this->originalDatabaseName
. '_ft%`.* TO `' . $user . '`@`' . $host . '`;"',
470 $database->setDatabaseName($this->databaseName
);
471 $database->sql_select_db();
475 * Populate $GLOBALS['TYPO3_DB'] reusing an existing database with
476 * all tables truncated.
478 * @throws \TYPO3\CMS\Core\Tests\Exception
481 protected function initializeTestDatabase() {
482 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->initializeTypo3DbGlobal();
483 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
484 $database = $GLOBALS['TYPO3_DB'];
485 if (!$database->sql_pconnect()) {
487 'TYPO3 Fatal Error: The current username, password or host was not accepted when the'
488 . ' connection to the database was attempted to be established!',
492 $this->databaseName
= $GLOBALS['TYPO3_CONF_VARS']['DB']['database'];
493 $database->setDatabaseName($this->databaseName
);
494 $database->sql_select_db();
495 foreach ($database->admin_get_tables() as $table) {
496 $database->admin_query('TRUNCATE ' . $table['Name'] . ';');
501 * Create tables and import static rows
505 protected function createDatabaseStructure() {
506 /** @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService */
507 $schemaMigrationService = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService
::class);
508 /** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
509 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(\TYPO3\CMS\Extbase\
Object\ObjectManager
::class);
510 /** @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService $expectedSchemaService */
511 $expectedSchemaService = $objectManager->get(\TYPO3\CMS\Install\Service\SqlExpectedSchemaService
::class);
513 // Raw concatenated ext_tables.sql and friends string
514 $expectedSchemaString = $expectedSchemaService->getTablesDefinitionString(TRUE);
515 $statements = $schemaMigrationService->getStatementArray($expectedSchemaString, TRUE);
516 list($_, $insertCount) = $schemaMigrationService->getCreateTables($statements, TRUE);
518 $fieldDefinitionsFile = $schemaMigrationService->getFieldDefinitions_fileContent($expectedSchemaString);
519 $fieldDefinitionsDatabase = $schemaMigrationService->getFieldDefinitions_database();
520 $difference = $schemaMigrationService->getDatabaseExtra($fieldDefinitionsFile, $fieldDefinitionsDatabase);
521 $updateStatements = $schemaMigrationService->getUpdateSuggestions($difference);
523 $schemaMigrationService->performUpdateQueries($updateStatements['add'], $updateStatements['add']);
524 $schemaMigrationService->performUpdateQueries($updateStatements['change'], $updateStatements['change']);
525 $schemaMigrationService->performUpdateQueries($updateStatements['create_table'], $updateStatements['create_table']);
527 foreach ($insertCount as $table => $count) {
528 $insertStatements = $schemaMigrationService->getTableInsertStatements($statements, $table);
529 foreach ($insertStatements as $insertQuery) {
530 $insertQuery = rtrim($insertQuery, ';');
531 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
532 $database = $GLOBALS['TYPO3_DB'];
533 $database->admin_query($insertQuery);
539 * Drop test database.
541 * @throws \TYPO3\CMS\Core\Tests\Exception
544 protected function tearDownTestDatabase() {
545 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
546 $database = $GLOBALS['TYPO3_DB'];
547 $result = $database->admin_query('DROP DATABASE `' . $this->databaseName
. '`');
550 'Dropping test database ' . $this->databaseName
. ' failed',
557 * Removes instance directories and files
559 * @throws \TYPO3\CMS\Core\Tests\Exception
562 protected function removeInstance() {
563 $success = $this->rmdir($this->instancePath
, TRUE);
566 'Can not remove folder: ' . $this->instancePath
,
573 * COPIED FROM GeneralUtility
575 * Wrapper function for rmdir, allowing recursive deletion of folders and files
577 * @param string $path Absolute path to folder, see PHP rmdir() function. Removes trailing slash internally.
578 * @param bool $removeNonEmpty Allow deletion of non-empty directories
579 * @return bool TRUE if @rmdir went well!
581 protected function rmdir($path, $removeNonEmpty = FALSE) {
583 // Remove trailing slash
584 $path = preg_replace('|/$|', '', $path);
585 if (file_exists($path)) {
587 if (!is_link($path) && is_dir($path)) {
588 if ($removeNonEmpty == TRUE && ($handle = opendir($path))) {
589 while ($OK && FALSE !== ($file = readdir($handle))) {
590 if ($file == '.' ||
$file == '..') {
593 $OK = $this->rmdir($path . '/' . $file, $removeNonEmpty);
601 // If $path is a symlink to a folder we need rmdir() on Windows systems
602 if (!stristr(PHP_OS
, 'darwin') && stristr(PHP_OS
, 'win') && is_link($path) && is_dir($path . '/')) {
609 } elseif (is_link($path)) {
617 * COPIED FROM GeneralUtility
619 * Writes $content to the file $file
621 * @param string $file Filepath to write to
622 * @param string $content Content to write
623 * @return bool TRUE if the file was successfully opened and written to.
625 protected function writeFile($file, $content) {
626 if ($fd = fopen($file, 'wb')) {
627 $res = fwrite($fd, $content);
629 if ($res === FALSE) {
638 * COPIED FROM ArrayUtility
640 * Exports an array as string.
641 * Similar to var_export(), but representation follows the TYPO3 core CGL.
643 * See unit tests for detailed examples
645 * @param array $array Array to export
646 * @param int $level Internal level used for recursion, do *not* set from outside!
647 * @return string String representation of array
648 * @throws \RuntimeException
650 protected function arrayExport(array $array = array(), $level = 0) {
651 $lines = 'array(' . chr(10);
653 $writeKeyIndex = FALSE;
654 $expectedKeyIndex = 0;
655 foreach ($array as $key => $value) {
656 if ($key === $expectedKeyIndex) {
659 // Found a non integer or non consecutive key, so we can break here
660 $writeKeyIndex = TRUE;
664 foreach ($array as $key => $value) {
666 $lines .= str_repeat(chr(9), $level);
667 if ($writeKeyIndex) {
668 // Numeric / string keys
669 $lines .= is_int($key) ?
$key . ' => ' : '\'' . $key . '\' => ';
671 if (is_array($value)) {
672 if (count($value) > 0) {
673 $lines .= $this->arrayExport($value, $level);
675 $lines .= 'array(),' . chr(10);
677 } elseif (is_int($value) ||
is_float($value)) {
678 $lines .= $value . ',' . chr(10);
679 } elseif (is_null($value)) {
680 $lines .= 'NULL' . ',' . chr(10);
681 } elseif (is_bool($value)) {
682 $lines .= $value ?
'TRUE' : 'FALSE';
683 $lines .= ',' . chr(10);
684 } elseif (is_string($value)) {
686 $stringContent = str_replace('\\', '\\\\', $value);
688 $stringContent = str_replace('\'', '\\\'', $stringContent);
689 $lines .= '\'' . $stringContent . '\'' . ',' . chr(10);
691 throw new \
RuntimeException('Objects are not supported', 1342294986);
694 $lines .= str_repeat(chr(9), ($level - 1)) . ')' . ($level - 1 == 0 ?
'' : ',' . chr(10));
699 * COPIED FROM ArrayUtility
701 * Merges two arrays recursively and "binary safe" (integer keys are
702 * overridden as well), overruling similar values in the original array
703 * with the values of the overrule array.
704 * In case of identical keys, ie. keeping the values of the overrule array.
706 * This method takes the original array by reference for speed optimization with large arrays
708 * The differences to the existing PHP function array_merge_recursive() are:
709 * * Keys of the original array can be unset via the overrule array. ($enableUnsetFeature)
710 * * Much more control over what is actually merged. ($addKeys, $includeEmptyValues)
711 * * Elements or the original array get overwritten if the same key is present in the overrule array.
713 * @param array $original Original array. It will be *modified* by this method and contains the result afterwards!
714 * @param array $overrule Overrule array, overruling the original array
715 * @param bool $addKeys If set to FALSE, keys that are NOT found in $original will not be set. Thus only existing value can/will be overruled from overrule array.
716 * @param bool $includeEmptyValues If set, values from $overrule will overrule if they are empty or zero.
717 * @param bool $enableUnsetFeature If set, special values "__UNSET" can be used in the overrule array in order to unset array keys in the original array.
720 protected function mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys = TRUE, $includeEmptyValues = TRUE, $enableUnsetFeature = TRUE) {
721 foreach ($overrule as $key => $_) {
722 if ($enableUnsetFeature && $overrule[$key] === '__UNSET') {
723 unset($original[$key]);
726 if (isset($original[$key]) && is_array($original[$key])) {
727 if (is_array($overrule[$key])) {
728 self
::mergeRecursiveWithOverrule($original[$key], $overrule[$key], $addKeys, $includeEmptyValues, $enableUnsetFeature);
731 ($addKeys ||
isset($original[$key])) &&
732 ($includeEmptyValues ||
$overrule[$key])
734 $original[$key] = $overrule[$key];
737 // This line is kept for backward compatibility reasons.