[BUGFIX] Acceptance tests fails due to viewport changes
[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 * Additional core extensions to load.
36 *
37 * To be used in own acceptance test suites.
38 *
39 * If a test suite needs additional core extensions, for instance as a dependency of
40 * an extension that is tested, those core extension names can be noted here and will
41 * be loaded.
42 *
43 * @var array
44 */
45 protected $coreExtensionsToLoad = [];
46
47 /**
48 * Array of test/fixture extensions paths that should be loaded for a test.
49 *
50 * To be used in own acceptance test suites.
51 *
52 * Given path is expected to be relative to your document root, example:
53 *
54 * array(
55 * 'typo3conf/ext/some_extension/Tests/Functional/Fixtures/Extensions/test_extension',
56 * 'typo3conf/ext/base_extension',
57 * );
58 *
59 * Extensions in this array are linked to the test instance, loaded
60 * and their ext_tables.sql will be applied.
61 *
62 * @var array
63 */
64 protected $testExtensionsToLoad = [];
65
66 /**
67 * Array of test/fixture folder or file paths that should be linked for a test.
68 *
69 * To be used in own acceptance test suites.
70 *
71 * array(
72 * 'link-source' => 'link-destination'
73 * );
74 *
75 * Given paths are expected to be relative to the test instance root.
76 * The array keys are the source paths and the array values are the destination
77 * paths, example:
78 *
79 * array(
80 * 'typo3/sysext/impext/Tests/Functional/Fixtures/Folders/fileadmin/user_upload' =>
81 * 'fileadmin/user_upload',
82 * 'typo3conf/ext/my_own_ext/Tests/Functional/Fixtures/Folders/uploads/tx_myownext' =>
83 * 'uploads/tx_myownext'
84 * );
85 *
86 * To be able to link from my_own_ext the extension path needs also to be registered in
87 * property $testExtensionsToLoad
88 *
89 * @var array
90 */
91 protected $pathsToLinkInTestInstance = [];
92
93 /**
94 * This configuration array is merged with TYPO3_CONF_VARS
95 * that are set in default configuration and factory configuration
96 *
97 * To be used in own acceptance test suites.
98 *
99 * @var array
100 */
101 protected $configurationToUseInTestInstance = [];
102
103 /**
104 * Array of folders that should be created inside the test instance document root.
105 *
106 * To be used in own acceptance test suites.
107 *
108 * Per default the following folder are created
109 * /fileadmin
110 * /typo3temp
111 * /typo3conf
112 * /typo3conf/ext
113 * /uploads
114 *
115 * To create additional folders add the paths to this array. Given paths are expected to be
116 * relative to the test instance root and have to begin with a slash. Example:
117 *
118 * array(
119 * 'fileadmin/user_upload'
120 * );
121 *
122 * @var array
123 */
124 protected $additionalFoldersToCreate = [];
125
126 /**
127 * XML database fixtures to be loaded into database.
128 *
129 * Given paths are expected to be relative to your document root.
130 *
131 * @var array
132 */
133 protected $xmlDatabaseFixtures = [
134 'typo3/sysext/core/Tests/Acceptance/Fixtures/be_users.xml',
135 'typo3/sysext/core/Tests/Acceptance/Fixtures/be_sessions.xml',
136 'typo3/sysext/core/Tests/Acceptance/Fixtures/be_groups.xml',
137 'typo3/sysext/core/Tests/Acceptance/Fixtures/sys_category.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['DB'] = $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['BE']['loginSecurityLevel'] = 'rsa';
201 $localConfiguration['SYS']['isInitialInstallationInProgress'] = false;
202 $localConfiguration['SYS']['isInitialDatabaseImportDone'] = true;
203 $localConfiguration['SYS']['displayErrors'] = false;
204 $localConfiguration['SYS']['debugExceptionHandler'] = '';
205 $localConfiguration['SYS']['trustedHostsPattern'] = 'localhost:8000';
206 $localConfiguration['SYS']['encryptionKey'] = 'iAmInvalid';
207 // @todo: This sql_mode should be enabled as soon as styleguide and dataHandler can cope with it
208 //$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\';';
209 $localConfiguration['SYS']['caching']['cacheConfigurations']['extbase_object']['backend'] = NullBackend::class;
210 $testbase->setUpLocalConfiguration($instancePath, $localConfiguration, $this->configurationToUseInTestInstance);
211 $defaultCoreExtensionsToLoad = [
212 'core',
213 'beuser',
214 'extbase',
215 'fluid',
216 'filelist',
217 'extensionmanager',
218 'lang',
219 'setup',
220 'rsaauth',
221 'saltedpasswords',
222 'backend',
223 'about',
224 'belog',
225 'install',
226 't3skin',
227 'frontend',
228 'recordlist',
229 'reports',
230 'sv',
231 'scheduler'
232 ];
233 $testbase->setUpPackageStates($instancePath, $defaultCoreExtensionsToLoad, $this->coreExtensionsToLoad, $testExtensionsToLoad);
234 $testbase->setUpBasicTypo3Bootstrap($instancePath);
235 $testbase->setUpTestDatabase($localConfiguration['DB']['Connections']['Default']['dbname'], $originalDatabaseName);
236 $testbase->loadExtensionTables();
237 $testbase->createDatabaseStructure();
238
239 // Unset a closure or phpunit kicks in with a 'serialization of \Closure is not allowed'
240 // Alternative solution:
241 // unset($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['GLOBAL']['cliKeys']['extbase']);
242 $suite = $suiteEvent->getSuite();
243 $suite->setBackupGlobals(false);
244
245 foreach ($this->xmlDatabaseFixtures as $fixture) {
246 $testbase->importXmlDatabaseFixture(ORIGINAL_ROOT . $fixture);
247 }
248
249 // styleguide generator uses DataHandler for some parts. DataHandler needs an initialized BE user
250 // with admin right and the live workspace.
251 Bootstrap::getInstance()->initializeBackendUser();
252 $GLOBALS['BE_USER']->user['admin'] = 1;
253 $GLOBALS['BE_USER']->user['uid'] = 1;
254 $GLOBALS['BE_USER']->workspace = 0;
255 Bootstrap::getInstance()->initializeLanguageObject();
256
257 $styleguideGenerator = new Generator();
258 $styleguideGenerator->create();
259
260 // @todo: Find out why that is needed to execute the first test successfully
261 $this->cleanupTypo3Environment();
262 }
263
264 /**
265 * Method executed after each test
266 *
267 * @return void
268 */
269 public function cleanupTypo3Environment()
270 {
271 // Reset uc db field of be_user "admin" to null to reduce
272 // possible side effects between single tests.
273 GeneralUtility::makeInstance(ConnectionPool::class)
274 ->getConnectionForTable('be_users')
275 ->update('be_users', ['uc' => null], ['uid' => 1]);
276 }
277 }