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(
55 * @var array These folder are always created
57 protected $defaultFoldersToCreate = array(
62 '/typo3temp/var/transient',
70 * Calculate a "unique" identifier for the test database and the
71 * instance patch based on the given test case class name.
73 * @param string $testCaseClassName Name of test case class
76 public static function getInstanceIdentifier($testCaseClassName)
78 // 7 characters of sha1 should be enough for a unique identification
79 return substr(sha1($testCaseClassName), 0, 7);
83 * Calculates path to TYPO3 CMS test installation for this test case.
85 * @param string $testCaseClassName Name of test case class
88 public static function getInstancePath($testCaseClassName)
90 return ORIGINAL_ROOT
. 'typo3temp/var/tests/functional-' . static::getInstanceIdentifier($testCaseClassName);
94 * Set up creates a test instance and database.
96 * @param string $testCaseClassName Name of test case class
97 * @param array $coreExtensionsToLoad Array of core extensions to load
98 * @param array $testExtensionsToLoad Array of test extensions to load
99 * @param array $pathsToLinkInTestInstance Array of source => destination path pairs to be linked
100 * @param array $configurationToUse Array of TYPO3_CONF_VARS that need to be overridden
101 * @param array $additionalFoldersToCreate Array of folder paths to be created
102 * @return string Path to TYPO3 CMS test installation for this test case
104 public function setUp(
106 array $coreExtensionsToLoad,
107 array $testExtensionsToLoad,
108 array $pathsToLinkInTestInstance,
109 array $configurationToUse,
110 array $additionalFoldersToCreate
112 $this->setUpIdentifier($testCaseClassName);
113 $this->setUpInstancePath($testCaseClassName);
114 if ($this->recentTestInstanceExists()) {
115 $this->setUpBasicTypo3Bootstrap();
116 $this->initializeTestDatabase();
117 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->loadExtensionTables();
119 $this->removeOldInstanceIfExists();
120 $this->setUpInstanceDirectories($additionalFoldersToCreate);
121 $this->setUpInstanceCoreLinks();
122 $this->linkTestExtensionsToInstance($testExtensionsToLoad);
123 $this->linkPathsInTestInstance($pathsToLinkInTestInstance);
124 $this->setUpLocalConfiguration($configurationToUse);
125 $this->setUpPackageStates($coreExtensionsToLoad, $testExtensionsToLoad);
126 $this->setUpBasicTypo3Bootstrap();
127 $this->setUpTestDatabase();
128 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->loadExtensionTables();
129 $this->createDatabaseStructure();
132 return $this->instancePath
;
136 * Checks whether the current test instance exists and is younger than
141 protected function recentTestInstanceExists()
143 if (@file_get_contents
($this->instancePath
. '/last_run.txt') <= (time() - 300)) {
146 // Test instance exists and is pretty young -> re-use
152 * Calculate a "unique" identifier for the test database and the
153 * instance patch based on the given test case class name.
155 * As a result, the database name will be identical between different
156 * test runs, but different between each test case.
158 * @param string $testCaseClassName Name of test case class
161 protected function setUpIdentifier($testCaseClassName)
163 $this->identifier
= static::getInstanceIdentifier($testCaseClassName);
167 * Calculates path to TYPO3 CMS test installation for this test case.
169 * @param string $testCaseClassName Name of test case class
172 protected function setUpInstancePath($testCaseClassName)
174 $this->instancePath
= static::getInstancePath($testCaseClassName);
178 * Remove test instance folder structure in setUp() if it exists.
179 * This may happen if a functional test before threw a fatal.
183 protected function removeOldInstanceIfExists()
185 if (is_dir($this->instancePath
)) {
186 $this->removeInstance();
191 * Create folder structure of test instance.
193 * @param array $additionalFoldersToCreate Array of additional folders to be created
197 protected function setUpInstanceDirectories(array $additionalFoldersToCreate = array())
199 $foldersToCreate = array_merge($this->defaultFoldersToCreate
, $additionalFoldersToCreate);
200 foreach ($foldersToCreate as $folder) {
201 $success = mkdir($this->instancePath
. $folder);
204 'Creating directory failed: ' . $this->instancePath
. $folder,
210 // Store the time we created this directory
211 file_put_contents($this->instancePath
. '/last_run.txt', time());
215 * Link TYPO3 CMS core from "parent" instance.
220 protected function setUpInstanceCoreLinks()
223 ORIGINAL_ROOT
. 'typo3' => $this->instancePath
. '/typo3',
224 ORIGINAL_ROOT
. 'index.php' => $this->instancePath
. '/index.php'
226 foreach ($linksToSet as $from => $to) {
227 $success = symlink($from, $to);
230 'Creating link failed: from ' . $from . ' to: ' . $to,
238 * Link test extensions to the typo3conf/ext folder of the instance.
240 * @param array $extensionPaths Contains paths to extensions relative to document root
244 protected function linkTestExtensionsToInstance(array $extensionPaths)
246 foreach ($extensionPaths as $extensionPath) {
247 $absoluteExtensionPath = ORIGINAL_ROOT
. $extensionPath;
248 if (!is_dir($absoluteExtensionPath)) {
250 'Test extension path ' . $absoluteExtensionPath . ' not found',
254 $destinationPath = $this->instancePath
. '/typo3conf/ext/' . basename($absoluteExtensionPath);
255 $success = symlink($absoluteExtensionPath, $destinationPath);
258 'Can not link extension folder: ' . $absoluteExtensionPath . ' to ' . $destinationPath,
266 * Link paths inside the test instance, e.g. from a fixture fileadmin subfolder to the
267 * test instance fileadmin folder
269 * @param array $pathsToLinkInTestInstance Contains paths as array of source => destination in key => value pairs of folders relative to test instance root
270 * @throws \TYPO3\CMS\Core\Tests\Exception if a source path could not be found
271 * @throws \TYPO3\CMS\Core\Tests\Exception on failing creating the symlink
273 * @see \TYPO3\CMS\Core\Tests\FunctionalTestCase::$pathsToLinkInTestInstance
275 protected function linkPathsInTestInstance(array $pathsToLinkInTestInstance)
277 foreach ($pathsToLinkInTestInstance as $sourcePathToLinkInTestInstance => $destinationPathToLinkInTestInstance) {
278 $sourcePath = $this->instancePath
. '/' . ltrim($sourcePathToLinkInTestInstance, '/');
279 if (!file_exists($sourcePath)) {
281 'Path ' . $sourcePath . ' not found',
285 $destinationPath = $this->instancePath
. '/' . ltrim($destinationPathToLinkInTestInstance, '/');
286 $success = symlink($sourcePath, $destinationPath);
289 'Can not link the path ' . $sourcePath . ' to ' . $destinationPath,
297 * Create LocalConfiguration.php file in the test instance
299 * @param array $configurationToMerge
303 protected function setUpLocalConfiguration(array $configurationToMerge)
305 $databaseName = trim(getenv('typo3DatabaseName'));
306 $databaseHost = trim(getenv('typo3DatabaseHost'));
307 $databaseUsername = trim(getenv('typo3DatabaseUsername'));
308 $databasePassword = trim(getenv('typo3DatabasePassword'));
309 $databasePort = trim(getenv('typo3DatabasePort'));
310 $databaseSocket = trim(getenv('typo3DatabaseSocket'));
311 if ($databaseName ||
$databaseHost ||
$databaseUsername ||
$databasePassword ||
$databasePort ||
$databaseSocket) {
312 // Try to get database credentials from environment variables first
313 $originalConfigurationArray = array(
317 $originalConfigurationArray['DB']['database'] = $databaseName;
320 $originalConfigurationArray['DB']['host'] = $databaseHost;
322 if ($databaseUsername) {
323 $originalConfigurationArray['DB']['username'] = $databaseUsername;
325 if ($databasePassword) {
326 $originalConfigurationArray['DB']['password'] = $databasePassword;
329 $originalConfigurationArray['DB']['port'] = $databasePort;
331 if ($databaseSocket) {
332 $originalConfigurationArray['DB']['socket'] = $databaseSocket;
334 } elseif (file_exists(ORIGINAL_ROOT
. 'typo3conf/LocalConfiguration.php')) {
335 // See if a LocalConfiguration file exists in "parent" instance to get db credentials from
336 $originalConfigurationArray = require ORIGINAL_ROOT
. 'typo3conf/LocalConfiguration.php';
339 'Database credentials for functional tests are neither set through environment'
340 . ' variables, and can not be found in an existing LocalConfiguration file',
345 // Base of final LocalConfiguration is core factory configuration
346 $finalConfigurationArray = require ORIGINAL_ROOT
. 'typo3/sysext/core/Configuration/FactoryConfiguration.php';
348 $this->mergeRecursiveWithOverrule($finalConfigurationArray, require ORIGINAL_ROOT
. 'typo3/sysext/core/Build/Configuration/FunctionalTestsConfiguration.php');
349 $this->mergeRecursiveWithOverrule($finalConfigurationArray, $configurationToMerge);
350 $finalConfigurationArray['DB'] = $originalConfigurationArray['DB'];
351 // Calculate and set new database name
352 $this->originalDatabaseName
= $originalConfigurationArray['DB']['database'];
353 $this->databaseName
= $this->originalDatabaseName
. '_ft' . $this->identifier
;
355 // Maximum database name length for mysql is 64 characters
356 if (strlen($this->databaseName
) > 64) {
357 $maximumOriginalDatabaseName = 64 - strlen('_ft' . $this->identifier
);
359 'The name of the database that is used for the functional test (' . $this->databaseName
. ')' .
360 ' exceeds the maximum length of 64 character allowed by MySQL. You have to shorten your' .
361 ' original database name to ' . $maximumOriginalDatabaseName . ' characters',
366 $finalConfigurationArray['DB']['database'] = $this->databaseName
;
368 $result = $this->writeFile(
369 $this->instancePath
. '/typo3conf/LocalConfiguration.php',
373 $finalConfigurationArray
379 throw new Exception('Can not write local configuration', 1376657277);
384 * Compile typo3conf/PackageStates.php containing default packages like core,
385 * a functional test specific list of additional core extensions, and a list of
388 * @param array $coreExtensionsToLoad Additional core extensions to load
389 * @param array $testExtensionPaths Paths to extensions relative to document root
391 * @TODO Figure out what the intention of the upper arguments is
393 protected function setUpPackageStates(array $coreExtensionsToLoad, array $testExtensionPaths)
395 $packageStates = array(
396 'packages' => array(),
400 // Register default list of extensions and set active
401 foreach ($this->defaultActivatedCoreExtensions
as $extensionName) {
402 $packageStates['packages'][$extensionName] = array(
404 'packagePath' => 'typo3/sysext/' . $extensionName . '/',
405 'classesPath' => 'Classes/',
409 // Register additional core extensions and set active
410 foreach ($coreExtensionsToLoad as $extensionName) {
411 if (isset($packageSates['packages'][$extensionName])) {
413 $extensionName . ' is already registered as default core extension to load, no need to load it explicitly',
417 $packageStates['packages'][$extensionName] = array(
419 'packagePath' => 'typo3/sysext/' . $extensionName . '/',
420 'classesPath' => 'Classes/',
424 // Activate test extensions that have been symlinked before
425 foreach ($testExtensionPaths as $extensionPath) {
426 $extensionName = basename($extensionPath);
427 if (isset($packageSates['packages'][$extensionName])) {
429 $extensionName . ' is already registered as extension to load, no need to load it explicitly',
433 $packageStates['packages'][$extensionName] = array(
435 'packagePath' => 'typo3conf/ext/' . $extensionName . '/',
436 'classesPath' => 'Classes/',
440 $result = $this->writeFile(
441 $this->instancePath
. '/typo3conf/PackageStates.php',
451 throw new Exception('Can not write PackageStates', 1381612729);
456 * Bootstrap basic TYPO3
460 protected function setUpBasicTypo3Bootstrap()
462 $_SERVER['PWD'] = $this->instancePath
;
463 $_SERVER['argv'][0] = 'index.php';
465 define('TYPO3_MODE', 'BE');
467 $classLoader = require rtrim(realpath($this->instancePath
. '/typo3'), '\\/') . '/../vendor/autoload.php';
468 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()
469 ->initializeClassLoader($classLoader)
470 ->setRequestType(TYPO3_REQUESTTYPE_BE | TYPO3_REQUESTTYPE_CLI
)
472 ->loadConfigurationAndInitialize(true)
473 ->loadTypo3LoadedExtAndExtLocalconf(true)
474 ->setFinalCachingFrameworkCacheConfiguration()
475 ->defineLoggingAndExceptionConstants()
476 ->unsetReservedGlobalVariables();
480 * Populate $GLOBALS['TYPO3_DB'] and create test database
482 * @throws \TYPO3\CMS\Core\Tests\Exception
485 protected function setUpTestDatabase()
487 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->initializeTypo3DbGlobal();
488 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
489 $database = $GLOBALS['TYPO3_DB'];
490 if (!$database->sql_pconnect()) {
492 'TYPO3 Fatal Error: The current username, password or host was not accepted when the'
493 . ' connection to the database was attempted to be established!',
498 // Drop database in case a previous test had a fatal and did not clean up properly
499 $database->admin_query('DROP DATABASE IF EXISTS `' . $this->databaseName
. '`');
500 $createDatabaseResult = $database->admin_query('CREATE DATABASE `' . $this->databaseName
. '` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci');
501 if (!$createDatabaseResult) {
502 $user = $GLOBALS['TYPO3_CONF_VARS']['DB']['username'];
503 $host = $GLOBALS['TYPO3_CONF_VARS']['DB']['host'];
505 'Unable to create database with name ' . $this->databaseName
. '. This is probably a permission problem.'
506 . ' For this instance this could be fixed executing'
507 . ' "GRANT ALL ON `' . $this->originalDatabaseName
. '_ft%`.* TO `' . $user . '`@`' . $host . '`;"',
511 $database->setDatabaseName($this->databaseName
);
512 // On windows, this still works, but throws a warning, which we need to discard.
513 @$database->sql_select_db();
517 * Populate $GLOBALS['TYPO3_DB'] reusing an existing database with
518 * all tables truncated.
520 * @throws \TYPO3\CMS\Core\Tests\Exception
523 protected function initializeTestDatabase()
525 \TYPO3\CMS\Core\Core\Bootstrap
::getInstance()->initializeTypo3DbGlobal();
526 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
527 $database = $GLOBALS['TYPO3_DB'];
528 if (!$database->sql_pconnect()) {
530 'TYPO3 Fatal Error: The current username, password or host was not accepted when the'
531 . ' connection to the database was attempted to be established!',
535 $this->databaseName
= $GLOBALS['TYPO3_CONF_VARS']['DB']['database'];
536 $database->setDatabaseName($this->databaseName
);
537 $database->sql_select_db();
538 foreach ($database->admin_get_tables() as $table) {
539 $database->admin_query('TRUNCATE ' . $table['Name'] . ';');
544 * Create tables and import static rows
548 protected function createDatabaseStructure()
550 /** @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService */
551 $schemaMigrationService = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService
::class);
552 /** @var \TYPO3\CMS\Extbase\Object\ObjectManager $objectManager */
553 $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(\TYPO3\CMS\Extbase\
Object\ObjectManager
::class);
554 /** @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService $expectedSchemaService */
555 $expectedSchemaService = $objectManager->get(\TYPO3\CMS\Install\Service\SqlExpectedSchemaService
::class);
557 // Raw concatenated ext_tables.sql and friends string
558 $expectedSchemaString = $expectedSchemaService->getTablesDefinitionString(true);
559 $statements = $schemaMigrationService->getStatementArray($expectedSchemaString, true);
560 list($_, $insertCount) = $schemaMigrationService->getCreateTables($statements, true);
562 $fieldDefinitionsFile = $schemaMigrationService->getFieldDefinitions_fileContent($expectedSchemaString);
563 $fieldDefinitionsDatabase = $schemaMigrationService->getFieldDefinitions_database();
564 $difference = $schemaMigrationService->getDatabaseExtra($fieldDefinitionsFile, $fieldDefinitionsDatabase);
565 $updateStatements = $schemaMigrationService->getUpdateSuggestions($difference);
567 $schemaMigrationService->performUpdateQueries($updateStatements['add'], $updateStatements['add']);
568 $schemaMigrationService->performUpdateQueries($updateStatements['change'], $updateStatements['change']);
569 $schemaMigrationService->performUpdateQueries($updateStatements['create_table'], $updateStatements['create_table']);
571 foreach ($insertCount as $table => $count) {
572 $insertStatements = $schemaMigrationService->getTableInsertStatements($statements, $table);
573 foreach ($insertStatements as $insertQuery) {
574 $insertQuery = rtrim($insertQuery, ';');
575 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
576 $database = $GLOBALS['TYPO3_DB'];
577 $database->admin_query($insertQuery);
583 * Drop test database.
585 * @throws \TYPO3\CMS\Core\Tests\Exception
588 protected function tearDownTestDatabase()
590 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
591 $database = $GLOBALS['TYPO3_DB'];
592 $result = $database->admin_query('DROP DATABASE `' . $this->databaseName
. '`');
595 'Dropping test database ' . $this->databaseName
. ' failed',
602 * Removes instance directories and files
604 * @throws \TYPO3\CMS\Core\Tests\Exception
607 protected function removeInstance()
609 $success = $this->rmdir($this->instancePath
, true);
612 'Can not remove folder: ' . $this->instancePath
,
619 * COPIED FROM GeneralUtility
621 * Wrapper function for rmdir, allowing recursive deletion of folders and files
623 * @param string $path Absolute path to folder, see PHP rmdir() function. Removes trailing slash internally.
624 * @param bool $removeNonEmpty Allow deletion of non-empty directories
625 * @return bool TRUE if @rmdir went well!
627 protected function rmdir($path, $removeNonEmpty = false)
630 // Remove trailing slash
631 $path = preg_replace('|/$|', '', $path);
632 if (file_exists($path)) {
634 if (!is_link($path) && is_dir($path)) {
635 if ($removeNonEmpty == true && ($handle = opendir($path))) {
636 while ($OK && false !== ($file = readdir($handle))) {
637 if ($file == '.' ||
$file == '..') {
640 $OK = $this->rmdir($path . '/' . $file, $removeNonEmpty);
648 // If $path is a symlink to a folder we need rmdir() on Windows systems
649 if (!stristr(PHP_OS
, 'darwin') && stristr(PHP_OS
, 'win') && is_link($path) && is_dir($path . '/')) {
656 } elseif (is_link($path)) {
664 * COPIED FROM GeneralUtility
666 * Writes $content to the file $file
668 * @param string $file Filepath to write to
669 * @param string $content Content to write
670 * @return bool TRUE if the file was successfully opened and written to.
672 protected function writeFile($file, $content)
674 if ($fd = fopen($file, 'wb')) {
675 $res = fwrite($fd, $content);
677 if ($res === false) {
686 * COPIED FROM ArrayUtility
688 * Exports an array as string.
689 * Similar to var_export(), but representation follows the TYPO3 core CGL.
691 * See unit tests for detailed examples
693 * @param array $array Array to export
694 * @param int $level Internal level used for recursion, do *not* set from outside!
695 * @return string String representation of array
696 * @throws \RuntimeException
698 protected function arrayExport(array $array = array(), $level = 0)
700 $lines = 'array(' . chr(10);
702 $writeKeyIndex = false;
703 $expectedKeyIndex = 0;
704 foreach ($array as $key => $value) {
705 if ($key === $expectedKeyIndex) {
708 // Found a non integer or non consecutive key, so we can break here
709 $writeKeyIndex = true;
713 foreach ($array as $key => $value) {
715 $lines .= str_repeat(chr(9), $level);
716 if ($writeKeyIndex) {
717 // Numeric / string keys
718 $lines .= is_int($key) ?
$key . ' => ' : '\'' . $key . '\' => ';
720 if (is_array($value)) {
721 if (!empty($value)) {
722 $lines .= $this->arrayExport($value, $level);
724 $lines .= 'array(),' . chr(10);
726 } elseif (is_int($value) ||
is_float($value)) {
727 $lines .= $value . ',' . chr(10);
728 } elseif (is_null($value)) {
729 $lines .= 'NULL' . ',' . chr(10);
730 } elseif (is_bool($value)) {
731 $lines .= $value ?
'TRUE' : 'FALSE';
732 $lines .= ',' . chr(10);
733 } elseif (is_string($value)) {
735 $stringContent = str_replace('\\', '\\\\', $value);
737 $stringContent = str_replace('\'', '\\\'', $stringContent);
738 $lines .= '\'' . $stringContent . '\'' . ',' . chr(10);
740 throw new \
RuntimeException('Objects are not supported', 1342294986);
743 $lines .= str_repeat(chr(9), ($level - 1)) . ')' . ($level - 1 == 0 ?
'' : ',' . chr(10));
748 * COPIED FROM ArrayUtility
750 * Merges two arrays recursively and "binary safe" (integer keys are
751 * overridden as well), overruling similar values in the original array
752 * with the values of the overrule array.
753 * In case of identical keys, ie. keeping the values of the overrule array.
755 * This method takes the original array by reference for speed optimization with large arrays
757 * The differences to the existing PHP function array_merge_recursive() are:
758 * * Keys of the original array can be unset via the overrule array. ($enableUnsetFeature)
759 * * Much more control over what is actually merged. ($addKeys, $includeEmptyValues)
760 * * Elements or the original array get overwritten if the same key is present in the overrule array.
762 * @param array $original Original array. It will be *modified* by this method and contains the result afterwards!
763 * @param array $overrule Overrule array, overruling the original array
764 * @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.
765 * @param bool $includeEmptyValues If set, values from $overrule will overrule if they are empty or zero.
766 * @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.
769 protected function mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys = true, $includeEmptyValues = true, $enableUnsetFeature = true)
771 foreach ($overrule as $key => $_) {
772 if ($enableUnsetFeature && $overrule[$key] === '__UNSET') {
773 unset($original[$key]);
776 if (isset($original[$key]) && is_array($original[$key])) {
777 if (is_array($overrule[$key])) {
778 self
::mergeRecursiveWithOverrule($original[$key], $overrule[$key], $addKeys, $includeEmptyValues, $enableUnsetFeature);
781 ($addKeys ||
isset($original[$key])) &&
782 ($includeEmptyValues ||
$overrule[$key])
784 $original[$key] = $overrule[$key];
787 // This line is kept for backward compatibility reasons.