[FEATURE] Integrate preliminary PackageManager API
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / FunctionalTestCaseBootstrapUtility.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Utility class to set up and bootstrap TYPO3 CMS for functional tests
29 */
30 class FunctionalTestCaseBootstrapUtility {
31
32 /**
33 * @var string Identifier calculated from test case class
34 */
35 protected $identifier;
36
37 /**
38 * @var string Absolute path to test instance document root
39 */
40 protected $instancePath;
41
42 /**
43 * @var string Name of test database
44 */
45 protected $databaseName;
46
47 /**
48 * @var string Name of original database
49 */
50 protected $originalDatabaseName;
51
52 /**
53 * Set up creates a test instance and database.
54 *
55 * @param string $testCaseClassName Name of test case class
56 * @param array $coreExtensionsToLoad Array of core extensions to load
57 * @param array $testExtensionsToLoad Array of test extensions to load
58 * @return void
59 */
60 public function setUp(
61 $testCaseClassName,
62 array $coreExtensionsToLoad,
63 array $testExtensionsToLoad
64 ) {
65 $this->setUpIdentifier($testCaseClassName);
66 $this->setUpInstancePath();
67 $this->removeOldInstanceIfExists();
68 $this->setUpInstanceDirectories();
69 $this->setUpInstanceCoreLinks();
70 $this->linkTestExtensionsToInstance($testExtensionsToLoad);
71 $this->setUpLocalConfiguration();
72 $this->setUpPackageStates($coreExtensionsToLoad, $testExtensionsToLoad);
73 $this->setUpBasicTypo3Bootstrap();
74 $this->setUpTestDatabase();
75 $this->createDatabaseStructure();
76 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->loadExtensionTables(TRUE);
77 }
78
79 /**
80 * Tear down destroys the instance and database.
81 *
82 * @throws Exception
83 * @return void
84 */
85 public function tearDown() {
86 if (empty($this->identifier)) {
87 throw new Exception(
88 'Test identifier not set. Is parent::setUp() called in setUp()?',
89 1376739702
90 );
91 }
92 $this->tearDownTestDatabase();
93 $this->removeInstance();
94 }
95
96 /**
97 * Calculate a "unique" identifier for the test database and the
98 * instance patch based on the given test case class name.
99 *
100 * As a result, the database name will be identical between different
101 * test runs, but different between each test case.
102 */
103 protected function setUpIdentifier($testCaseClassName) {
104 // 7 characters of sha1 should be enough for a unique identification
105 $this->identifier = substr(sha1($testCaseClassName), 0, 7);
106 }
107
108 /**
109 * Calculates path to TYPO3 CMS test installation for this test case.
110 *
111 * @return void
112 */
113 protected function setUpInstancePath() {
114 $this->instancePath = ORIGINAL_ROOT . 'typo3temp/functional-' . $this->identifier;
115 }
116
117 /**
118 * Remove test instance folder structure in setUp() if it exists.
119 * This may happen if a functional test before threw a fatal.
120 *
121 * @return void
122 */
123 protected function removeOldInstanceIfExists() {
124 if (is_dir($this->instancePath)) {
125 $this->removeInstance();
126 }
127 }
128
129 /**
130 * Create folder structure of test instance.
131 *
132 * @throws Exception
133 * @return void
134 */
135 protected function setUpInstanceDirectories() {
136 $foldersToCreate = array(
137 '',
138 '/fileadmin',
139 '/typo3temp',
140 '/typo3conf',
141 '/typo3conf/ext',
142 '/uploads'
143 );
144 foreach ($foldersToCreate as $folder) {
145 $success = mkdir($this->instancePath . $folder);
146 if (!$success) {
147 throw new Exception(
148 'Creating directory failed: ' . $this->instancePath . $folder,
149 1376657189
150 );
151 }
152 }
153 }
154
155 /**
156 * Link TYPO3 CMS core from "parent" instance.
157 *
158 * @throws Exception
159 * @return void
160 */
161 protected function setUpInstanceCoreLinks() {
162 $linksToSet = array(
163 ORIGINAL_ROOT . 'typo3' => $this->instancePath . '/typo3',
164 ORIGINAL_ROOT . 'index.php' => $this->instancePath . '/index.php'
165 );
166 foreach ($linksToSet as $from => $to) {
167 $success = symlink($from, $to);
168 if (!$success) {
169 throw new Exception(
170 'Creating link failed: from ' . $from . ' to: ' . $to,
171 1376657199
172 );
173 }
174 }
175 }
176
177 /**
178 * Link test extensions to the typo3conf/ext folder of the instance.
179 *
180 * @param array $extensionPaths Contains paths to extensions relative to document root
181 * @throws Exception
182 * @return void
183 */
184 protected function linkTestExtensionsToInstance(array $extensionPaths) {
185 foreach ($extensionPaths as $extensionPath) {
186 if (!is_dir($extensionPath)) {
187 throw new Exception(
188 'Test extension path ' . $extensionPath . ' not found',
189 1376745645
190 );
191 }
192 $absoluteExtensionPath = ORIGINAL_ROOT . $extensionPath;
193 $destinationPath = $this->instancePath . '/typo3conf/ext/'. basename($absoluteExtensionPath);
194 $success = symlink($absoluteExtensionPath, $destinationPath);
195 if (!$success) {
196 throw new Exception(
197 'Can not link extension folder: ' . $absoluteExtensionPath . ' to ' . $destinationPath,
198 1376657142
199 );
200 }
201 }
202 }
203
204 /**
205 * Create LocalConfiguration.php file in the test instance
206 *
207 * @param array $coreExtensionsToLoad Additional core extensions to load
208 * @param array $testExtensionPaths Paths to extensions relative to document root
209 * @throws Exception
210 * @return void
211 */
212 protected function setUpLocalConfiguration() {
213 $originalConfigurationArray = require ORIGINAL_ROOT . 'typo3conf/LocalConfiguration.php';
214 // Base of final LocalConfiguration is core factory configuration
215 $finalConfigurationArray = require ORIGINAL_ROOT .'typo3/sysext/core/Configuration/FactoryConfiguration.php';
216
217 $finalConfigurationArray['DB'] = $originalConfigurationArray['DB'];
218 // Calculate and set new database name
219 $this->originalDatabaseName = $originalConfigurationArray['DB']['database'];
220 $this->databaseName = $this->originalDatabaseName . '_ft' . $this->identifier;
221
222 // Maximum database name length for mysql is 64 characters
223 if (strlen($this->databaseName) > 64) {
224 $maximumOriginalDatabaseName = 64 - strlen('_ft' . $this->identifier);
225 throw new Exception(
226 'The name of the database that is used for the functional test (' . $this->databaseName . ')' .
227 ' exceeds the maximum length of 64 character allowed by MySQL. You have to shorten your' .
228 ' original database name to ' . $maximumOriginalDatabaseName . ' characters',
229 1377600104
230 );
231 }
232
233 $finalConfigurationArray['DB']['database'] = $this->databaseName;
234
235 $result = $this->writeFile(
236 $this->instancePath . '/typo3conf/LocalConfiguration.php',
237 '<?php' . chr(10) .
238 'return ' .
239 $this->arrayExport(
240 $finalConfigurationArray
241 ) .
242 ';' . chr(10) .
243 '?>'
244 );
245 if (!$result) {
246 throw new Exception('Can not write local configuration', 1376657277);
247 }
248 }
249
250 /**
251 * @param array $coreExtensionsToLoad Additional core extensions to load
252 * @param array $testExtensionPaths Paths to extensions relative to document root
253 * @throws Exception
254 * @TODO Figure out what the intention of the upper arguments is
255 */
256 protected function setUpPackageStates(array $coreExtensionsToLoad, array $testExtensionPaths) {
257 $packageStates = require ORIGINAL_ROOT . 'typo3conf/PackageStates.php';
258 $packageStates['packages']['phpunit']['packagePath'] = '../../' . $packageStates['packages']['phpunit']['packagePath'];
259
260 $result = $this->writeFile(
261 $this->instancePath . '/typo3conf/PackageStates.php',
262 '<?php' . chr(10) .
263 'return ' .
264 $this->arrayExport(
265 $packageStates
266 ) .
267 ';' . chr(10) .
268 '?>'
269 );
270 if (!$result) {
271 throw new Exception('Can not write PackageStates', 1381612729);
272 }
273 }
274
275 /**
276 * Bootstrap basic TYPO3
277 *
278 * @return void
279 */
280 protected function setUpBasicTypo3Bootstrap() {
281 $_SERVER['PWD'] = $this->instancePath;
282 $_SERVER['argv'][0] = 'index.php';
283
284 define('TYPO3_MODE', 'BE');
285 define('TYPO3_cliMode', TRUE);
286
287 require $this->instancePath . '/typo3/sysext/core/Classes/Core/CliBootstrap.php';
288 \TYPO3\CMS\Core\Core\CliBootstrap::checkEnvironmentOrDie();
289
290 require $this->instancePath . '/typo3/sysext/core/Classes/Core/Bootstrap.php';
291 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
292 ->baseSetup('')
293 ->loadConfigurationAndInitialize(FALSE)
294 ->loadTypo3LoadedExtAndExtLocalconf(FALSE)
295 ->applyAdditionalConfigurationSettings();
296 }
297
298 /**
299 * Populate $GLOBALS['TYPO3_DB'] and create test database
300 *
301 * @throws \TYPO3\CMS\Core\Tests\Exception
302 * @return void
303 */
304 protected function setUpTestDatabase() {
305 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeTypo3DbGlobal();
306 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
307 $database = $GLOBALS['TYPO3_DB'];
308 if(!$database->sql_pconnect()) {
309 throw new Exception(
310 'TYPO3 Fatal Error: The current username, password or host was not accepted when the'
311 . ' connection to the database was attempted to be established!',
312 1377620117
313 );
314 }
315
316 // Drop database in case a previous test had a fatal and did not clean up properly
317 $database->admin_query('DROP DATABASE IF EXISTS `' . $this->databaseName . '`');
318 $createDatabaseResult = $database->admin_query('CREATE DATABASE `' . $this->databaseName . '`');
319 if (!$createDatabaseResult) {
320 $user = $GLOBALS['TYPO3_CONF_VARS']['DB']['username'];
321 $host = $GLOBALS['TYPO3_CONF_VARS']['DB']['host'];
322 throw new Exception(
323 'Unable to create database with name ' . $this->databaseName . '. This is probably a permission problem.'
324 . ' For this instance this could be fixed executing'
325 . ' "GRANT ALL ON `' . $this->originalDatabaseName . '_ft%`.* TO `' . $user . '`@`' . $host . '`;"',
326 1376579070
327 );
328 }
329 $database->setDatabaseName($this->databaseName);
330 $database->sql_select_db($this->databaseName);
331 }
332
333 /**
334 * Create tables and import static rows
335 *
336 * @return void
337 */
338 protected function createDatabaseStructure() {
339 /** @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService */
340 $schemaMigrationService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
341 /** @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService $expectedSchemaService */
342 $expectedSchemaService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Install\\Service\\SqlExpectedSchemaService');
343
344 // Raw concatenated ext_tables.sql and friends string
345 $expectedSchemaString = $expectedSchemaService->getTablesDefinitionString(TRUE);
346 $statements = $schemaMigrationService->getStatementArray($expectedSchemaString, TRUE);
347 list($_, $insertCount) = $schemaMigrationService->getCreateTables($statements, TRUE);
348
349 $fieldDefinitionsFile = $schemaMigrationService->getFieldDefinitions_fileContent($expectedSchemaString);
350 $fieldDefinitionsDatabase = $schemaMigrationService->getFieldDefinitions_database();
351 $difference = $schemaMigrationService->getDatabaseExtra($fieldDefinitionsFile, $fieldDefinitionsDatabase);
352 $updateStatements = $schemaMigrationService->getUpdateSuggestions($difference);
353
354 $schemaMigrationService->performUpdateQueries($updateStatements['add'], $updateStatements['add']);
355 $schemaMigrationService->performUpdateQueries($updateStatements['change'], $updateStatements['change']);
356 $schemaMigrationService->performUpdateQueries($updateStatements['create_table'], $updateStatements['create_table']);
357
358 foreach ($insertCount as $table => $count) {
359 $insertStatements = $schemaMigrationService->getTableInsertStatements($statements, $table);
360 foreach ($insertStatements as $insertQuery) {
361 $insertQuery = rtrim($insertQuery, ';');
362 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
363 $database = $GLOBALS['TYPO3_DB'];
364 $database->admin_query($insertQuery);
365 }
366 }
367 }
368
369 /**
370 * Drop test database.
371 *
372 * @throws \TYPO3\CMS\Core\Tests\Exception
373 * @return void
374 */
375 protected function tearDownTestDatabase() {
376 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
377 $database = $GLOBALS['TYPO3_DB'];
378 $result = $database->admin_query('DROP DATABASE `' . $this->databaseName . '`');
379 if (!$result) {
380 throw new Exception(
381 'Dropping test database ' . $this->databaseName . ' failed',
382 1376583188
383 );
384 }
385 }
386
387 /**
388 * Removes instance directories and files
389 *
390 * @throws \TYPO3\CMS\Core\Tests\Exception
391 * @return void
392 */
393 protected function removeInstance() {
394 $success = $this->rmdir($this->instancePath, TRUE);
395 if (!$success) {
396 throw new Exception(
397 'Can not remove folder: ' . $this->instancePath,
398 1376657210
399 );
400 }
401 }
402
403 /**
404 * COPIED FROM GeneralUtility
405 *
406 * Wrapper function for rmdir, allowing recursive deletion of folders and files
407 *
408 * @param string $path Absolute path to folder, see PHP rmdir() function. Removes trailing slash internally.
409 * @param boolean $removeNonEmpty Allow deletion of non-empty directories
410 * @return boolean TRUE if @rmdir went well!
411 */
412 protected function rmdir($path, $removeNonEmpty = FALSE) {
413 $OK = FALSE;
414 // Remove trailing slash
415 $path = preg_replace('|/$|', '', $path);
416 if (file_exists($path)) {
417 $OK = TRUE;
418 if (!is_link($path) && is_dir($path)) {
419 if ($removeNonEmpty == TRUE && ($handle = opendir($path))) {
420 while ($OK && FALSE !== ($file = readdir($handle))) {
421 if ($file == '.' || $file == '..') {
422 continue;
423 }
424 $OK = $this->rmdir($path . '/' . $file, $removeNonEmpty);
425 }
426 closedir($handle);
427 }
428 if ($OK) {
429 $OK = @rmdir($path);
430 }
431 } else {
432 // If $path is a file, simply remove it
433 $OK = unlink($path);
434 }
435 clearstatcache();
436 } elseif (is_link($path)) {
437 $OK = unlink($path);
438 clearstatcache();
439 }
440 return $OK;
441 }
442
443 /**
444 * COPIED FROM GeneralUtility
445 *
446 * Writes $content to the file $file
447 *
448 * @param string $file Filepath to write to
449 * @param string $content Content to write
450 * @return boolean TRUE if the file was successfully opened and written to.
451 */
452 protected function writeFile($file, $content) {
453 if ($fd = fopen($file, 'wb')) {
454 $res = fwrite($fd, $content);
455 fclose($fd);
456 if ($res === FALSE) {
457 return FALSE;
458 }
459 return TRUE;
460 }
461 return FALSE;
462 }
463
464 /**
465 * COPIED FROM ArrayUtility
466 *
467 * Exports an array as string.
468 * Similar to var_export(), but representation follows the TYPO3 core CGL.
469 *
470 * See unit tests for detailed examples
471 *
472 * @param array $array Array to export
473 * @param integer $level Internal level used for recursion, do *not* set from outside!
474 * @return string String representation of array
475 * @throws \RuntimeException
476 */
477 protected function arrayExport(array $array = array(), $level = 0) {
478 $lines = 'array(' . chr(10);
479 $level++;
480 $writeKeyIndex = FALSE;
481 $expectedKeyIndex = 0;
482 foreach ($array as $key => $value) {
483 if ($key === $expectedKeyIndex) {
484 $expectedKeyIndex++;
485 } else {
486 // Found a non integer or non consecutive key, so we can break here
487 $writeKeyIndex = TRUE;
488 break;
489 }
490 }
491 foreach ($array as $key => $value) {
492 // Indention
493 $lines .= str_repeat(chr(9), $level);
494 if ($writeKeyIndex) {
495 // Numeric / string keys
496 $lines .= is_int($key) ? $key . ' => ' : '\'' . $key . '\' => ';
497 }
498 if (is_array($value)) {
499 if (count($value) > 0) {
500 $lines .= $this->arrayExport($value, $level);
501 } else {
502 $lines .= 'array(),' . chr(10);
503 }
504 } elseif (is_int($value) || is_float($value)) {
505 $lines .= $value . ',' . chr(10);
506 } elseif (is_null($value)) {
507 $lines .= 'NULL' . ',' . chr(10);
508 } elseif (is_bool($value)) {
509 $lines .= $value ? 'TRUE' : 'FALSE';
510 $lines .= ',' . chr(10);
511 } elseif (is_string($value)) {
512 // Quote \ to \\
513 $stringContent = str_replace('\\', '\\\\', $value);
514 // Quote ' to \'
515 $stringContent = str_replace('\'', '\\\'', $stringContent);
516 $lines .= '\'' . $stringContent . '\'' . ',' . chr(10);
517 } else {
518 throw new \RuntimeException('Objects are not supported', 1342294986);
519 }
520 }
521 $lines .= str_repeat(chr(9), ($level - 1)) . ')' . ($level - 1 == 0 ? '' : ',' . chr(10));
522 return $lines;
523 }
524 }