[TASK] Acceptance tests: Reset be_user uc after test
[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\Event\SuiteEvent;
18 use Codeception\Events;
19 use Codeception\Extension;
20 use TYPO3\CMS\Core\Cache\Backend\NullBackend;
21 use TYPO3\CMS\Core\Core\Bootstrap;
22 use TYPO3\CMS\Core\Database\ConnectionPool;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\CMS\Styleguide\TcaDataGenerator\Generator;
25
26 /**
27 * This codeception extension creates a full TYPO3 instance within
28 * typo3temp. Own acceptance test suites may extend from this class
29 * and change the properties. This can be used to not copy the whole
30 * bootstrapTypo3Environment() method but reuse it instead.
31 */
32 class AcceptanceCoreEnvironment extends Extension
33 {
34
35 /**
36 * Additional core extensions to load.
37 *
38 * To be used in own acceptance test suites.
39 *
40 * If a test suite needs additional core extensions, for instance as a dependency of
41 * an extension that is tested, those core extension names can be noted here and will
42 * be loaded.
43 *
44 * @var array
45 */
46 protected $coreExtensionsToLoad = [];
47
48 /**
49 * Array of test/fixture extensions paths that should be loaded for a test.
50 *
51 * To be used in own acceptance test suites.
52 *
53 * Given path is expected to be relative to your document root, example:
54 *
55 * array(
56 * 'typo3conf/ext/some_extension/Tests/Functional/Fixtures/Extensions/test_extension',
57 * 'typo3conf/ext/base_extension',
58 * );
59 *
60 * Extensions in this array are linked to the test instance, loaded
61 * and their ext_tables.sql will be applied.
62 *
63 * @var array
64 */
65 protected $testExtensionsToLoad = [];
66
67 /**
68 * Array of test/fixture folder or file paths that should be linked for a test.
69 *
70 * To be used in own acceptance test suites.
71 *
72 * array(
73 * 'link-source' => 'link-destination'
74 * );
75 *
76 * Given paths are expected to be relative to the test instance root.
77 * The array keys are the source paths and the array values are the destination
78 * paths, example:
79 *
80 * array(
81 * 'typo3/sysext/impext/Tests/Functional/Fixtures/Folders/fileadmin/user_upload' =>
82 * 'fileadmin/user_upload',
83 * 'typo3conf/ext/my_own_ext/Tests/Functional/Fixtures/Folders/uploads/tx_myownext' =>
84 * 'uploads/tx_myownext'
85 * );
86 *
87 * To be able to link from my_own_ext the extension path needs also to be registered in
88 * property $testExtensionsToLoad
89 *
90 * @var array
91 */
92 protected $pathsToLinkInTestInstance = [];
93
94 /**
95 * This configuration array is merged with TYPO3_CONF_VARS
96 * that are set in default configuration and factory configuration
97 *
98 * To be used in own acceptance test suites.
99 *
100 * @var array
101 */
102 protected $configurationToUseInTestInstance = [];
103
104 /**
105 * Array of folders that should be created inside the test instance document root.
106 *
107 * To be used in own acceptance test suites.
108 *
109 * Per default the following folder are created
110 * /fileadmin
111 * /typo3temp
112 * /typo3conf
113 * /typo3conf/ext
114 * /uploads
115 *
116 * To create additional folders add the paths to this array. Given paths are expected to be
117 * relative to the test instance root and have to begin with a slash. Example:
118 *
119 * array(
120 * 'fileadmin/user_upload'
121 * );
122 *
123 * @var array
124 */
125 protected $additionalFoldersToCreate = [];
126
127 /**
128 * XML database fixtures to be loaded into database.
129 *
130 * Given paths are expected to be relative to your document root.
131 *
132 * @var array
133 */
134 protected $xmlDatabaseFixtures = [
135 'typo3/sysext/core/Tests/Acceptance/Fixtures/be_users.xml',
136 'typo3/sysext/core/Tests/Acceptance/Fixtures/be_sessions.xml',
137 'typo3/sysext/core/Tests/Acceptance/Fixtures/be_groups.xml',
138 ];
139
140 /**
141 * Events to listen to
142 */
143 public static $events = [
144 Events::SUITE_BEFORE => 'bootstrapTypo3Environment',
145 Events::TEST_AFTER => 'cleanupTypo3Environment'
146 ];
147
148 /**
149 * Handle SUITE_BEFORE event.
150 *
151 * Create a full standalone TYPO3 instance within typo3temp/var/tests/acceptance,
152 * create a database and create database schema.
153 *
154 * @param SuiteEvent $suiteEvent
155 * @throws Exception
156 */
157 public function bootstrapTypo3Environment(SuiteEvent $suiteEvent)
158 {
159 $testbase = new Testbase();
160 $testbase->enableDisplayErrors();
161 $testbase->defineBaseConstants();
162 $testbase->defineOriginalRootPath();
163 $testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/tests/acceptance');
164 $testbase->createDirectory(ORIGINAL_ROOT . 'typo3temp/var/transient');
165
166 $instancePath = ORIGINAL_ROOT . 'typo3temp/var/tests/acceptance';
167
168 $testbase->defineTypo3ModeBe();
169 $testbase->setTypo3TestingContext();
170 $testbase->removeOldInstanceIfExists($instancePath);
171 // Basic instance directory structure
172 $testbase->createDirectory($instancePath . '/fileadmin');
173 $testbase->createDirectory($instancePath . '/typo3temp/var/transient');
174 $testbase->createDirectory($instancePath . '/typo3temp/assets');
175 $testbase->createDirectory($instancePath . '/typo3conf/ext');
176 $testbase->createDirectory($instancePath . '/uploads');
177 // Additionally requested directories
178 foreach ($this->additionalFoldersToCreate as $directory) {
179 $testbase->createDirectory($instancePath . '/' . $directory);
180 }
181 $testbase->createLastRunTextfile($instancePath);
182 $testbase->setUpInstanceCoreLinks($instancePath);
183 // ext:styleguide is always loaded
184 $testExtensionsToLoad = array_merge(
185 [ 'typo3conf/ext/styleguide' ],
186 $this->testExtensionsToLoad
187 );
188 $testbase->linkTestExtensionsToInstance($instancePath, $testExtensionsToLoad);
189 $testbase->linkPathsInTestInstance($instancePath, $this->pathsToLinkInTestInstance);
190 $localConfiguration = $testbase->getOriginalDatabaseSettingsFromEnvironmentOrLocalConfiguration();
191 $originalDatabaseName = $localConfiguration['DB']['Connections']['Default']['dbname'];
192 // Append the unique identifier to the base database name to end up with a single database per test case
193 $localConfiguration['DB']['Connections']['Default']['dbname'] = $originalDatabaseName . '_at';
194 $testbase->testDatabaseNameIsNotTooLong($originalDatabaseName, $localConfiguration);
195 // Set some hard coded base settings for the instance. Those could be overruled by
196 // $this->configurationToUseInTestInstance if needed again.
197 $localConfiguration['BE']['debug'] = true;
198 $localConfiguration['BE']['lockHashKeyWords'] = '';
199 $localConfiguration['BE']['installToolPassword'] = '$P$notnotnotnotnotnot.validvalidva';
200 $localConfiguration['SYS']['isInitialInstallationInProgress'] = false;
201 $localConfiguration['SYS']['isInitialDatabaseImportDone'] = true;
202 $localConfiguration['SYS']['displayErrors'] = false;
203 $localConfiguration['SYS']['debugExceptionHandler'] = '';
204 $localConfiguration['SYS']['trustedHostsPattern'] = 'localhost:8000';
205 $localConfiguration['SYS']['encryptionKey'] = 'iAmInvalid';
206 // @todo: This sql_mode should be enabled as soon as styleguide and dataHandler can cope with it
207 //$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\';';
208 $localConfiguration['SYS']['caching']['cacheConfigurations']['extbase_object']['backend'] = NullBackend::class;
209 $testbase->setUpLocalConfiguration($instancePath, $localConfiguration, $this->configurationToUseInTestInstance);
210 $defaultCoreExtensionsToLoad = [
211 'core',
212 'extbase',
213 'fluid',
214 'filelist',
215 'extensionmanager',
216 'lang',
217 'setup',
218 'rsaauth',
219 'saltedpasswords',
220 'backend',
221 'belog',
222 'install',
223 't3skin',
224 'frontend',
225 'recordlist',
226 'reports',
227 'sv',
228 'scheduler'
229 ];
230 $testbase->setUpPackageStates($instancePath, $defaultCoreExtensionsToLoad, $this->coreExtensionsToLoad, $testExtensionsToLoad);
231 $testbase->setUpBasicTypo3Bootstrap($instancePath);
232 $testbase->setUpTestDatabase($localConfiguration['DB']['Connections']['Default']['dbname'], $originalDatabaseName);
233 $testbase->loadExtensionTables();
234 $testbase->createDatabaseStructure();
235
236 // Unset a closure or phpunit kicks in with a 'serialization of \Closure is not allowed'
237 // Alternative solution:
238 // unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['extbase']);
239 $suite = $suiteEvent->getSuite();
240 $suite->setBackupGlobals(false);
241
242 foreach ($this->xmlDatabaseFixtures as $fixture) {
243 $testbase->importXmlDatabaseFixture(ORIGINAL_ROOT . $fixture);
244 }
245
246 // styleguide generator uses DataHandler for some parts. DataHandler needs an initialized BE user
247 // with admin right and the live workspace.
248 Bootstrap::getInstance()->initializeBackendUser();
249 $GLOBALS['BE_USER']->user['admin'] = 1;
250 $GLOBALS['BE_USER']->user['uid'] = 1;
251 $GLOBALS['BE_USER']->workspace = 0;
252 Bootstrap::getInstance()->initializeLanguageObject();
253
254 $styleguideGenerator = new Generator();
255 $styleguideGenerator->create();
256 }
257
258 /**
259 * Method executed after each test
260 *
261 * @return void
262 */
263 public function cleanupTypo3Environment()
264 {
265 // Reset uc db field of be_user "admin" to null to reduce
266 // possible side effects between single tests.
267 GeneralUtility::makeInstance(ConnectionPool::class)
268 ->getConnectionForTable('be_users')
269 ->update('be_users', ['uc' => null], ['uid' => 1]);
270 }
271 }