[TASK] Acceptance tests in controlled environment
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / AcceptanceCoreEnvironment.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests;
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 use Codeception\Events;
18 use Codeception\Event\SuiteEvent;
19 use Codeception\Extension;
20 use TYPO3\CMS\Core\Cache\Backend\NullBackend;
21
22 /**
23 * This codeception extension creates a full TYPO3 instance within
24 * typo3temp. Own acceptance test suites may extend from this class
25 * and change the properties. This can be used to not copy the whole
26 * bootstrapTypo3Environment() method but reuse it instead.
27 */
28 class AcceptanceCoreEnvironment extends Extension
29 {
30
31 /**
32 * Additional core extensions to load.
33 *
34 * To be used in own acceptance test suites.
35 *
36 * If a test suite needs additional core extensions, for instance as a dependency of
37 * an extension that is tested, those core extension names can be noted here and will
38 * be loaded.
39 *
40 * @var array
41 */
42 protected $coreExtensionsToLoad = [];
43
44 /**
45 * Array of test/fixture extensions paths that should be loaded for a test.
46 *
47 * To be used in own acceptance test suites.
48 *
49 * Given path is expected to be relative to your document root, example:
50 *
51 * array(
52 * 'typo3conf/ext/some_extension/Tests/Functional/Fixtures/Extensions/test_extension',
53 * 'typo3conf/ext/base_extension',
54 * );
55 *
56 * Extensions in this array are linked to the test instance, loaded
57 * and their ext_tables.sql will be applied.
58 *
59 * @var array
60 */
61 protected $testExtensionsToLoad = [];
62
63 /**
64 * Array of test/fixture folder or file paths that should be linked for a test.
65 *
66 * To be used in own acceptance test suites.
67 *
68 * array(
69 * 'link-source' => 'link-destination'
70 * );
71 *
72 * Given paths are expected to be relative to the test instance root.
73 * The array keys are the source paths and the array values are the destination
74 * paths, example:
75 *
76 * array(
77 * 'typo3/sysext/impext/Tests/Functional/Fixtures/Folders/fileadmin/user_upload' =>
78 * 'fileadmin/user_upload',
79 * 'typo3conf/ext/my_own_ext/Tests/Functional/Fixtures/Folders/uploads/tx_myownext' =>
80 * 'uploads/tx_myownext'
81 * );
82 *
83 * To be able to link from my_own_ext the extension path needs also to be registered in
84 * property $testExtensionsToLoad
85 *
86 * @var array
87 */
88 protected $pathsToLinkInTestInstance = [];
89
90 /**
91 * This configuration array is merged with TYPO3_CONF_VARS
92 * that are set in default configuration and factory configuration
93 *
94 * To be used in own acceptance test suites.
95 *
96 * @var array
97 */
98 protected $configurationToUseInTestInstance = [];
99
100 /**
101 * Array of folders that should be created inside the test instance document root.
102 *
103 * To be used in own acceptance test suites.
104 *
105 * Per default the following folder are created
106 * /fileadmin
107 * /typo3temp
108 * /typo3conf
109 * /typo3conf/ext
110 * /uploads
111 *
112 * To create additional folders add the paths to this array. Given paths are expected to be
113 * relative to the test instance root and have to begin with a slash. Example:
114 *
115 * array(
116 * 'fileadmin/user_upload'
117 * );
118 *
119 * @var array
120 */
121 protected $additionalFoldersToCreate = [];
122
123 /**
124 * Events to listen to
125 */
126 public static $events = [
127 Events::SUITE_BEFORE => 'bootstrapTypo3Environment',
128 ];
129
130 /**
131 * Handle SUITE_BEFORE event.
132 *
133 * Create a full standalone TYPO3 instance within typo3temp/var/tests/acceptance,
134 * create a database and create database schema.
135 *
136 * @param SuiteEvent $suiteEvent
137 * @throws Exception
138 */
139 public function bootstrapTypo3Environment(SuiteEvent $suiteEvent) {
140 $testbase = new Testbase();
141 $testbase->enableDisplayErrors();
142 $testbase->defineBaseConstants();
143 $testbase->defineOriginalRootPath();
144 $testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/tests/acceptance');
145 $testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/transient');
146
147 $instancePath = ORIGINAL_ROOT . 'typo3temp/var/tests/acceptance';
148
149 $testbase = new Testbase();
150 $testbase->defineTypo3ModeBe();
151 $testbase->setTypo3TestingContext();
152 $testbase->removeOldInstanceIfExists($instancePath);
153 // Basic instance directory structure
154 $testbase->createDirectory($instancePath . '/fileadmin');
155 $testbase->createDirectory($instancePath . '/typo3temp/var/transient');
156 $testbase->createDirectory($instancePath . '/typo3temp/assets');
157 $testbase->createDirectory($instancePath . '/typo3conf/ext');
158 $testbase->createDirectory($instancePath . '/uploads');
159 // Additionally requested directories
160 foreach ($this->additionalFoldersToCreate as $directory) {
161 $testbase->createDirectory($instancePath . '/' . $directory);
162 }
163 $testbase->createLastRunTextfile($instancePath);
164 $testbase->setUpInstanceCoreLinks($instancePath);
165 $testbase->linkTestExtensionsToInstance($instancePath, $this->testExtensionsToLoad);
166 $testbase->linkPathsInTestInstance($instancePath, $this->pathsToLinkInTestInstance);
167 $localConfiguration = $testbase->getOriginalDatabaseSettingsFromEnvironmentOrLocalConfiguration();
168 $originalDatabaseName = $localConfiguration['DB']['database'];
169 // Append the unique identifier to the base database name to end up with a single database per test case
170 $localConfiguration['DB']['database'] = $originalDatabaseName . '_at';
171 $testbase->testDatabaseNameIsNotTooLong($originalDatabaseName, $localConfiguration);
172 // Set some hard coded base settings for the instance. Those could be overruled by
173 // $this->configurationToUseInTestInstance if needed again.
174 $localConfiguration['BE']['debug'] = true;
175 $localConfiguration['BE']['installToolPassword'] = '$P$notnotnotnotnotnot.validvalidva';
176 $localConfiguration['SYS']['isInitialInstallationInProgress'] = false;
177 $localConfiguration['SYS']['isInitialDatabaseImportDone'] = true;
178 $localConfiguration['SYS']['displayErrors'] = '1';
179 $localConfiguration['SYS']['debugExceptionHandler'] = '';
180 $localConfiguration['SYS']['trustedHostsPattern'] = '.*';
181 $localConfiguration['SYS']['encryptionKey'] = 'iAmInvalid';
182 $localConfiguration['SYS']['setDBinit'] = 'SET SESSION sql_mode = \'STRICT_ALL_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_VALUE_ON_ZERO,NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE,ONLY_FULL_GROUP_BY\';';
183 $localConfiguration['SYS']['caching']['cacheConfigurations']['extbase_object']['backend'] = NullBackend::class;
184 $testbase->setUpLocalConfiguration($instancePath, $localConfiguration, $this->configurationToUseInTestInstance);
185 $defaultCoreExtensionsToLoad = [
186 'core',
187 'extbase',
188 'fluid',
189 'extensionmanager',
190 'lang',
191 'setup',
192 'rsaauth',
193 'saltedpasswords',
194 'backend',
195 'belog',
196 'install',
197 't3skin',
198 'frontend',
199 'recordlist',
200 'sv',
201 ];
202 $testbase->setUpPackageStates($instancePath, $defaultCoreExtensionsToLoad, $this->coreExtensionsToLoad, $this->testExtensionsToLoad);
203 $testbase->setUpBasicTypo3Bootstrap($instancePath);
204 $testbase->setUpTestDatabase($localConfiguration['DB']['database'], $originalDatabaseName);
205 $testbase->loadExtensionTables();
206 $testbase->createDatabaseStructure();
207
208 // Unset a closure or phpunit kicks in with a 'serialization of \Closure is not allowed'
209 // Alternative solution:
210 // unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['extbase']);
211 $suite = $suiteEvent->getSuite();
212 $suite->setBackupGlobals(FALSE);
213 }
214
215 }