[SECURITY] Prevent information disclosure in tests bootstrap
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Build / FunctionalTestsBootstrap.php
1 <?php
2 namespace TYPO3\CMS\Core\Build;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
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.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * This file is defined in FuntionalTests.xml and called by phpunit
19 * before instantiating the test suites, it must also be included
20 * with phpunit parameter --bootstrap if executing single test case classes.
21 */
22 class FunctionalTestsBootstrap {
23 /**
24 * Bootstraps the system for unit tests.
25 *
26 * @return void
27 */
28 public function bootstrapSystem() {
29 $this->enableDisplayErrors()
30 ->loadClassFiles()
31 ->defineOriginalRootPath()
32 ->createNecessaryDirectoriesInDocumentRoot();
33 }
34
35 /**
36 * Makes sure error messages during the tests get displayed no matter what is set in php.ini.
37 *
38 * @return FunctionalTestsBootstrap fluent interface
39 */
40 protected function enableDisplayErrors() {
41 @ini_set('display_errors', 1);
42 return $this;
43 }
44
45 /**
46 * Requires classes the functional test classes extend from or use for further bootstrap.
47 * Only files required for "new TestCaseClass" are required here and a general exception
48 * that is thrown by setUp() code.
49 *
50 * @return FunctionalTestsBootstrap fluent interface
51 */
52 protected function loadClassFiles() {
53 $testsDirectory = __DIR__ . '/../Tests/';
54 if (!class_exists('PHPUnit_Framework_TestCase')) {
55 die('PHPUnit wasn\'t found. Please check your settings and command.');
56 }
57 require_once($testsDirectory . 'BaseTestCase.php');
58 require_once($testsDirectory . 'FunctionalTestCase.php');
59 require_once($testsDirectory . 'FunctionalTestCaseBootstrapUtility.php');
60 require_once($testsDirectory . 'Exception.php');
61
62 return $this;
63 }
64
65 /**
66 * Defines the constant ORIGINAL_ROOT for the path to the original TYPO3 document root.
67 *
68 * If ORIGINAL_ROOT already is defined, this method is a no-op.
69 *
70 * @return FunctionalTestsBootstrap fluent interface
71 */
72 protected function defineOriginalRootPath() {
73 if (!defined('ORIGINAL_ROOT')) {
74 /** @var string */
75 define('ORIGINAL_ROOT', $this->getWebRoot());
76 }
77
78 return $this;
79 }
80
81 /**
82 * Creates the following directories in the TYPO3 core:
83 * - typo3temp
84 *
85 * @return FunctionalTestsBootstrap fluent interface
86 */
87 protected function createNecessaryDirectoriesInDocumentRoot() {
88 $this->createDirectory(ORIGINAL_ROOT . 'typo3temp');
89
90 return $this;
91 }
92
93 /**
94 * Creates the directory $directory (recursively if required).
95 *
96 * If $directory already exists, this method is a no-op.
97 *
98 * @param string $directory absolute path of the directory to be created
99 * @return void
100 * @throws \RuntimeException
101 */
102 protected function createDirectory($directory) {
103 if (is_dir($directory)) {
104 return;
105 }
106
107 if (!mkdir($directory, 0777, TRUE)) {
108 throw new \RuntimeException('Directory "' . $directory . '" could not be created', 1404038665);
109 }
110 }
111
112 /**
113 * Returns the absolute path the TYPO3 document root.
114 *
115 * @return string the TYPO3 document root using Unix path separators
116 */
117 protected function getWebRoot() {
118 if (getenv('TYPO3_PATH_WEB')) {
119 $webRoot = getenv('TYPO3_PATH_WEB') . '/';
120 } else {
121 $webRoot = getcwd() . '/';
122 }
123
124 return strtr($webRoot, '\\', '/');
125 }
126 }
127
128 if (PHP_SAPI !== 'cli') {
129 die('This script supports command line usage only. Please check your command.');
130 }
131
132 $bootstrap = new FunctionalTestsBootstrap();
133 $bootstrap->bootstrapSystem();
134 unset($bootstrap);