This repository has been archived by the owner on Jun 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathAbstractTestSystem.php
926 lines (847 loc) · 35 KB
/
AbstractTestSystem.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
<?php
namespace Nimut\TestingFramework\TestSystem;
/*
* This file is part of the NIMUT testing-framework project.
*
* It is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, either version 2
* of the License, or any later version.
*
* For the full copyright and license information, please read
* LICENSE file that was distributed with this source code.
*/
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Nimut\TestingFramework\Bootstrap\SystemEnvironmentBuilder;
use Nimut\TestingFramework\Exception\Exception;
use Nimut\TestingFramework\File\NtfStreamWrapper;
use TYPO3\CMS\Core\Core\Bootstrap;
use TYPO3\CMS\Core\Core\ClassLoadingInformation;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;
abstract class AbstractTestSystem
{
/**
* Extensions that are always loaded
*
* @var array
*/
protected $defaultActivatedCoreExtensions = [
'core',
'backend',
'frontend',
'extbase',
'fluid',
'install',
'recordlist',
];
/**
* Configuration to set as default
*
* @var array
*/
protected $defaultConfiguration = [
'SYS' => [
'caching' => [
'cacheConfigurations' => [
'extbase_object' => [
'backend' => 'TYPO3\\CMS\\Core\\Cache\\Backend\\NullBackend',
],
],
],
'displayErrors' => '1',
'debugExceptionHandler' => '',
'encryptionKey' => 'i-am-not-a-secure-encryption-key',
'isInitialDatabaseImportDone' => true,
'isInitialInstallationInProgress' => false,
'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\';',
'trustedHostsPattern' => '.*',
],
];
/**
* Folders that are always created
*
* @var array
*/
protected $defaultFoldersToCreate = [
'',
'/fileadmin',
'/typo3',
'/typo3conf/ext',
'/typo3temp/var/build',
'/typo3temp/var/tests',
'/typo3temp/var/transient',
'/uploads',
];
/**
* @var string Identifier calculated from test case class
*/
protected $identifier;
/**
* @var string Absolute path to test system document root
*/
protected $systemPath;
/**
* @param string $identifier Name of test case class
*/
public function __construct($identifier)
{
putenv('TYPO3_CONTEXT=Testing');
$this->identifier = substr(sha1($identifier), 0, 7);
$this->systemPath = ORIGINAL_ROOT . 'typo3temp/var/tests/functional-' . $this->identifier . '/';
}
/**
* Includes the Core Bootstrap class and calls its first few functions
*
* @return void
*/
protected function includeAndStartCoreBootstrap()
{
$classLoaderFilepath = $this->getClassLoaderFilepath();
$classLoader = require $classLoaderFilepath;
SystemEnvironmentBuilder::run(0, SystemEnvironmentBuilder::REQUESTTYPE_BE | SystemEnvironmentBuilder::REQUESTTYPE_CLI);
Bootstrap::init($classLoader);
if (!ClassLoadingInformation::isClassLoadingInformationAvailable()) {
ClassLoadingInformation::dumpClassLoadingInformation();
ClassLoadingInformation::registerClassLoadingInformation();
}
ob_end_clean();
}
/**
* Setup creates a test system and database
*
* @param array $coreExtensionsToLoad Array of core extensions to load
* @param array $testExtensionsToLoad Array of test extensions to load
* @param array $pathsToLinkInTestSystem Array of source => destination path pairs to be linked
* @param array $configurationToUse Array of TYPO3_CONF_VARS that need to be overridden
* @param array $additionalFoldersToCreate Array of folder paths to be created
* @return void
*/
public function setUp(
array $coreExtensionsToLoad,
array $testExtensionsToLoad,
array $pathsToLinkInTestSystem,
array $configurationToUse,
array $additionalFoldersToCreate
) {
$this->registerNtfStreamWrapper();
$this->setTypo3Context();
if ($this->recentTestSystemExists()) {
$this->includeAndStartCoreBootstrap();
$this->initializeTestDatabase();
$this->loadExtensionConfiguration();
} else {
$this->removeOldSystemIfExists();
$this->setUpSystemDirectories($additionalFoldersToCreate);
$this->setUpSystemCoreLinks();
$this->linkTestExtensionsToSystem($testExtensionsToLoad);
$this->linkPathsInTestSystem($pathsToLinkInTestSystem);
$this->setUpLocalConfiguration($configurationToUse);
$this->setUpPackageStates($coreExtensionsToLoad, $testExtensionsToLoad);
$this->includeAndStartCoreBootstrap();
$this->setUpTestDatabase();
$this->loadExtensionConfiguration();
$this->createDatabaseStructure();
}
}
/**
* Returns the calculated identifier
*
* @return string
*/
public function getSystemIdentifier()
{
return $this->identifier;
}
/**
* Calculates path to TYPO3 CMS test installation for this test system
*
* @return string
*/
public function getSystemPath()
{
return $this->systemPath;
}
/**
* Registers the NtfStreamWrapper for ntf:// protocol
*
* @return void
*/
protected function registerNtfStreamWrapper()
{
NtfStreamWrapper::register();
}
/**
* Defines some constants and sets the environment variable TYPO3_CONTEXT
*
* @return void
*/
protected function setTypo3Context()
{
/** @var string */
define('TYPO3_MODE', 'BE');
/** @var string */
define('TYPO3_cliMode', true);
// Disable TYPO3_DLOG
define('TYPO3_DLOG', false);
// Ensure TYPO3_PATH_ROOT and TYPO3_PATH_APP are pointing to the document root of the test environment
// It will be evaluated in the TYPO3 bootstrap and a previously set value may interfere here
$rootPath = rtrim($this->systemPath, '/');
putenv('TYPO3_PATH_ROOT=' . $rootPath);
putenv('TYPO3_PATH_APP=' . $rootPath);
$_SERVER['PWD'] = $this->systemPath;
$_SERVER['argv'][0] = 'index.php';
}
/**
* Checks whether the current test system exists and is younger than 5 minutes
*
* @return bool
*/
protected function recentTestSystemExists()
{
if (@file_get_contents($this->systemPath . 'last_run.txt') <= (time() - 300)) {
return false;
}
return true;
}
/**
* Populate $GLOBALS['TYPO3_DB'] reusing an existing database with all tables truncated
*
* @return void
*/
protected function initializeTestDatabase()
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
$platform = $connection->getDatabasePlatform();
if ($platform instanceof MySqlPlatform) {
$this->truncateAllTablesForMysql();
} else {
$this->truncateAllTablesForOtherDatabases();
}
}
/**
* Truncates all tables for MySQL databases in an optimized way.
*
* This method tries to avoid the (expensive) truncate if possible:
* - If the table has an auto-increment value (which usually is the `uid` column`) and that value has changed,
* this method will truncate the table.
* - If the table does not have an auto-increment value, but it has at least one row (where the exact number does
* not matter), this method will truncate the table.
* - Otherwise, this method will skip the truncate. (For tables without an auto-increment value, this means that
* the table either has not been touched at all beforehand, or that all records have already been deleted).
*/
protected function truncateAllTablesForMysql()
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
$databaseName = $connection->getDatabase();
$tableNames = $connection->getSchemaManager()->listTableNames();
if (empty($tableNames)) {
return;
}
// Build a sub select to get joinable table with information if table has at least one row.
// This is needed because information_schema.table_rows is not reliable enough for innodb engine.
// see https://dev.mysql.com/doc/mysql-infoschema-excerpt/5.7/en/information-schema-tables-table.html TABLE_ROWS
$fromTableUnionSubSelectQuery = [];
foreach ($tableNames as $tableName) {
$fromTableUnionSubSelectQuery[] = sprintf(
' SELECT %s AS table_name, exists(SELECT * FROM %s LIMIT 1) AS has_rows',
$connection->quote($tableName),
$connection->quoteIdentifier($tableName)
);
}
$fromTableUnionSubSelectQuery = implode(' UNION ', $fromTableUnionSubSelectQuery);
$query = sprintf(
'SELECT
table_real_rowcounts.*,
information_schema.tables.AUTO_INCREMENT AS auto_increment
FROM (%s) AS table_real_rowcounts
INNER JOIN information_schema.tables ON (
information_schema.tables.TABLE_SCHEMA = %s
AND information_schema.tables.TABLE_NAME = table_real_rowcounts.table_name
)',
$fromTableUnionSubSelectQuery,
$connection->quote($databaseName)
);
$result = $connection->executeQuery($query)->fetchAll();
foreach ($result as $tableData) {
$hasChangedAutoIncrement = ((int)$tableData['auto_increment']) > 1;
$hasRows = (bool)$tableData['has_rows'];
$isChanged = $hasChangedAutoIncrement || $hasRows;
if ($isChanged) {
$connection->truncate($tableData['table_name']);
}
}
}
/**
* Truncates all tables without any optimizations.
*/
protected function truncateAllTablesForOtherDatabases()
{
$connection = GeneralUtility::makeInstance(ConnectionPool::class)
->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME);
$schemaManager = $connection->getSchemaManager();
foreach ($schemaManager->listTables() as $table) {
$connection->truncate($table->getName());
}
}
/**
* Loads TCA and ext_tables.php files from extensions
*
* @return void
*/
protected function loadExtensionConfiguration()
{
Bootstrap::loadExtTables(true);
}
/**
* Remove test system folder structure in setUp() if it exists
*
* @throws Exception
* @return void
*/
protected function removeOldSystemIfExists()
{
if (is_dir($this->systemPath)) {
if (!$this->rmdir($this->systemPath, true)) {
throw new Exception(
'Can not remove folder: ' . $this->systemPath,
1376657210
);
}
}
}
/**
* Create folder structure of test system
*
* @param array $additionalFoldersToCreate Array of additional folders to be created
* @throws Exception
* @return void
*/
protected function setUpSystemDirectories(array $additionalFoldersToCreate = [])
{
$foldersToCreate = array_merge($this->defaultFoldersToCreate, $additionalFoldersToCreate);
foreach ($foldersToCreate as $folder) {
$folder = ltrim($folder, '/');
clearstatcache();
if (is_dir($this->systemPath . $folder)) {
continue;
}
if (!@mkdir($this->systemPath . $folder, 0777, true) && !is_dir($this->systemPath . $folder)) {
throw new Exception(
'Creating directory failed: ' . $this->systemPath . $folder,
1376657189
);
}
}
// Store the time we created this directory
file_put_contents($this->systemPath . 'last_run.txt', time());
}
/**
* Link TYPO3 CMS core from original system
*
* @throws Exception
* @return void
*/
protected function setUpSystemCoreLinks()
{
// Link sysext folder first
// As entry points can't be symlinked anymore, typo3 can't be linked directly here
$from = ORIGINAL_ROOT . 'typo3/sysext';
$to = $this->systemPath . 'typo3/sysext';
if (!symlink($from, $to)) {
throw new Exception(
'Creating link failed: from ' . $from . ' to: ' . $to,
1376657199
);
}
$entryPointsToSet = [
'typo3/sysext/backend/Resources/Private/Php/backend.php' => 'typo3/index.php',
'typo3/sysext/frontend/Resources/Private/Php/frontend.php' => 'index.php',
'typo3/sysext/install/Resources/Private/Php/install.php' => 'typo3/install.php',
];
$autoloadFilepath = PathUtility::getCanonicalPath($this->getClassLoaderFilepath());
foreach ($entryPointsToSet as $source => $target) {
if (($entryPointContent = file_get_contents($this->systemPath . $source)) === false) {
throw new \UnexpectedValueException(sprintf('Source file (%s) was not found.', $source), 1647288158);
}
$entryPointContent = (string)preg_replace(
'/__DIR__ \. \'[^\']+\'/',
$this->findShortestPathCode($this->systemPath . $target, $autoloadFilepath),
$entryPointContent
);
$entryPointContent = (string)preg_replace(
'/\\\\TYPO3\\\\CMS\\\\Core\\\\Core\\\\SystemEnvironmentBuilder::run\(/',
'\Nimut\TestingFramework\Bootstrap\SystemEnvironmentBuilder::run(',
$entryPointContent
);
if ($entryPointContent === '') {
throw new \UnexpectedValueException(
sprintf('Error while customizing the source file (%s).', $source),
1636244910
);
}
file_put_contents($this->systemPath . $target, $entryPointContent);
}
}
/**
* Link test extensions to the typo3conf/ext folder of the system
*
* @param array $extensionPaths Contains paths to extensions relative to document root
* @throws Exception
* @return void
*/
protected function linkTestExtensionsToSystem(array $extensionPaths)
{
foreach ($extensionPaths as $extensionPath) {
$absoluteExtensionPath = ORIGINAL_ROOT . $extensionPath;
if (!is_dir($absoluteExtensionPath)) {
throw new Exception(
'Test extension path ' . $absoluteExtensionPath . ' not found',
1376745645
);
}
$destinationPath = $this->systemPath . 'typo3conf/ext/' . basename($absoluteExtensionPath);
if (!symlink($absoluteExtensionPath, $destinationPath)) {
throw new Exception(
'Can not link extension folder: ' . $absoluteExtensionPath . ' to ' . $destinationPath,
1376657142
);
}
}
}
/**
* Link paths inside the test system
*
* @param array $pathsToLinkInTestSystem Contains paths as array of source => destination
* @throws Exception if a source path could not be found
* @throws Exception on failing creating the symlink
* @return void
*/
protected function linkPathsInTestSystem(array $pathsToLinkInTestSystem)
{
foreach ($pathsToLinkInTestSystem as $sourcePath => $destinationPath) {
$sourcePath = $this->systemPath . ltrim($sourcePath, '/');
if (!file_exists($sourcePath)) {
throw new Exception(
'Path ' . $sourcePath . ' not found',
1376745645
);
}
$destinationPath = $this->systemPath . ltrim($destinationPath, '/');
if (!symlink($sourcePath, $destinationPath)) {
throw new Exception(
'Can not link the path ' . $sourcePath . ' to ' . $destinationPath,
1389969623
);
}
}
}
/**
* Create LocalConfiguration.php file in the test system
*
* @param array $configurationToMerge
* @throws Exception
* @return void
*/
protected function setUpLocalConfiguration(array $configurationToMerge)
{
$originalConfigurationArray = $this->getDatabaseConfiguration();
if (empty($originalConfigurationArray)) {
if (file_exists(ORIGINAL_ROOT . 'typo3conf/LocalConfiguration.php')) {
// Load configuration from original system
$originalConfigurationArray = require ORIGINAL_ROOT . 'typo3conf/LocalConfiguration.php';
} else {
throw new Exception(
'Database credentials for functional tests are neither set through environment'
. ' variables, and can not be found in an existing LocalConfiguration file',
1397406356
);
}
}
$finalConfigurationArray = require ORIGINAL_ROOT . 'typo3/sysext/core/Configuration/FactoryConfiguration.php';
$configurationToMerge = array_replace_recursive(
$this->defaultConfiguration,
$configurationToMerge
);
$finalConfigurationArray['DB'] = $this->setDatabaseName($originalConfigurationArray['DB']);
$this->mergeRecursiveWithOverrule($finalConfigurationArray, $configurationToMerge);
$content = '<?php' . chr(10) . 'return '
. var_export($finalConfigurationArray, true)
. ';' . chr(10) . '?>';
if (!$this->writeFile($this->systemPath . 'typo3conf/LocalConfiguration.php', $content)) {
throw new Exception('Can not write local configuration', 1376657277);
}
}
/**
* Compile typo3conf/PackageStates.php containing defaultActivatedCoreExtensions, additional core extensions
* and test extensions to load
*
* @param array $coreExtensionsToLoad Additional core extensions to load
* @param array $testExtensionPaths Paths to extensions relative to document root
* @throws Exception
*/
protected function setUpPackageStates(array $coreExtensionsToLoad, array $testExtensionPaths)
{
$packageStates = [
'packages' => [],
'version' => $this->getPackageStatesVersion(),
];
// Register default list of extensions and set active
foreach ($this->defaultActivatedCoreExtensions as $extensionName) {
$packageStates['packages'][$extensionName] = [
'state' => 'active',
'packagePath' => 'typo3/sysext/' . $extensionName . '/',
'classesPath' => 'Classes/',
];
}
// Register additional core extensions and set active
foreach ($coreExtensionsToLoad as $extensionName) {
if (isset($packageStates['packages'][$extensionName])) {
throw new Exception(
$extensionName . ' is already registered as default core extension to load, no need to load it explicitly',
1390913893
);
}
$packageStates['packages'][$extensionName] = [
'state' => 'active',
'packagePath' => 'typo3/sysext/' . $extensionName . '/',
'classesPath' => 'Classes/',
];
}
// Activate test extensions that have been symlinked before
foreach ($testExtensionPaths as $extensionPath) {
$extensionName = basename($extensionPath);
if (isset($packageStates['packages'][$extensionName])) {
throw new Exception(
$extensionName . ' is already registered as extension to load, no need to load it explicitly',
1390913894
);
}
$packageStates['packages'][$extensionName] = [
'state' => 'active',
'packagePath' => 'typo3conf/ext/' . $extensionName . '/',
'classesPath' => 'Classes/',
];
}
$content = '<?php' . chr(10) . 'return '
. var_export($packageStates, true)
. ';' . chr(10) . '?>';
if (!$this->writeFile($this->systemPath . 'typo3conf/PackageStates.php', $content)) {
throw new Exception('Can not write PackageStates', 1381612729);
}
}
/**
* Populate $GLOBALS['TYPO3_DB'] and create test database
*
* @throws Exception
* @return void
*/
protected function setUpTestDatabase()
{
// The TYPO3 core misses to reset its internal connection state
// This means we need to reset all connections to ensure database connection can be initialized
$closure = \Closure::bind(function () {
foreach (ConnectionPool::$connections as $connection) {
$connection->close();
}
ConnectionPool::$connections = [];
}, null, ConnectionPool::class);
$closure();
$connectionParameters = $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default'];
$databaseName = $connectionParameters['dbname'];
unset($connectionParameters['dbname']);
$schemaManager = DriverManager::getConnection($connectionParameters)->getSchemaManager();
if ($schemaManager->getDatabasePlatform()->getName() === 'sqlite') {
// This is the "path" option in sqlite: one file = one db
$databaseName = $connectionParameters['path'];
$schemaManager->dropDatabase($databaseName);
} elseif (in_array($databaseName, $schemaManager->listDatabases(), true)) {
// Suppress listDatabases() call on sqlite which is not implemented there, but
// check db existence on all other platforms before drop call
$schemaManager->dropDatabase($databaseName);
}
try {
$schemaManager->createDatabase($databaseName);
} catch (DBALException $e) {
$user = $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['user'];
$host = $GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['host'];
throw new Exception(
'Unable to create database with name ' . $databaseName . '. This is probably a permission problem.'
. ' For this instance this could be fixed executing:'
. ' GRANT ALL ON `' . substr($databaseName, 0, -10) . '_%`.* TO `' . $user . '`@`' . $host . '`;'
. ' Original message thrown by database layer: ' . $e->getMessage(),
1376579070
);
}
}
/**
* Create tables and import static rows
*
* @return void
*/
protected function createDatabaseStructure()
{
$schemaMigrationService = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\Schema\\SchemaMigrator');
$sqlReader = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\Schema\\SqlReader');
$sqlCode = $sqlReader->getTablesDefinitionString(true);
$createTableStatements = $sqlReader->getCreateTableStatementArray($sqlCode);
$updateResult = $schemaMigrationService->install($createTableStatements);
$failedStatements = array_filter($updateResult);
$result = [];
foreach ($failedStatements as $query => $error) {
$result[] = 'Query "' . $query . '" returned "' . $error . '"';
}
if (!empty($result)) {
throw new \RuntimeException(implode("\n", $result), 1505058450);
}
$insertStatements = $sqlReader->getInsertStatementArray($sqlCode);
$schemaMigrationService->importStaticData($insertStatements);
}
/**
* Returns the current database connection set in environment
*
* @return array
*/
protected function getDatabaseConfiguration()
{
$originalConfigurationArray = [];
$databaseName = trim(getenv('typo3DatabaseName'));
$databaseHost = trim(getenv('typo3DatabaseHost'));
$databaseUsername = trim(getenv('typo3DatabaseUsername'));
$databasePassword = getenv('typo3DatabasePassword');
$databasePasswordTrimmed = trim($databasePassword);
$databasePort = trim(getenv('typo3DatabasePort'));
$databaseSocket = trim(getenv('typo3DatabaseSocket'));
$databaseDriver = trim(getenv('typo3DatabaseDriver'));
if ($databaseName || $databaseHost || $databaseUsername || $databasePassword || $databasePort || $databaseSocket || $databaseDriver) {
// Try to get database credentials from environment variables first
$originalConfigurationArray = [
'DB' => [
'Connections' => [
'Default' => [
'driver' => 'mysqli',
'initCommands' => $this->defaultConfiguration['SYS']['setDBinit'],
],
],
],
];
if ($databaseName) {
$originalConfigurationArray['DB']['Connections']['Default']['dbname'] = $databaseName;
}
if ($databaseHost) {
$originalConfigurationArray['DB']['Connections']['Default']['host'] = $databaseHost;
}
if ($databaseUsername) {
$originalConfigurationArray['DB']['Connections']['Default']['user'] = $databaseUsername;
}
if ($databasePassword !== false) {
$originalConfigurationArray['DB']['Connections']['Default']['password'] = $databasePasswordTrimmed;
}
if ($databasePort) {
$originalConfigurationArray['DB']['Connections']['Default']['port'] = $databasePort;
}
if ($databaseSocket) {
$originalConfigurationArray['DB']['Connections']['Default']['unix_socket'] = $databaseSocket;
}
if ($databaseDriver) {
$originalConfigurationArray['DB']['Connections']['Default']['driver'] = $databaseDriver;
if ($databaseDriver === 'pdo_sqlite') {
$originalConfigurationArray['DB']['Connections']['Default']['path'] = $this->systemPath . 'test.sqlite';
$originalConfigurationArray['DB']['Connections']['Default']['initCommands'] = '';
}
}
}
return $originalConfigurationArray;
}
/**
* Merges two arrays recursively and "binary safe" (integer keys are overridden as well), overruling similar values
* in the original array with the values of the overrule array.
* In case of identical keys, ie. keeping the values of the overrule array.
*
* This method takes the original array by reference for speed optimization with large arrays
*
* The differences to the existing PHP function array_merge_recursive() are:
* * Keys of the original array can be unset via the overrule array. ($enableUnsetFeature)
* * Much more control over what is actually merged. ($addKeys, $includeEmptyValues)
* * Elements or the original array get overwritten if the same key is present in the overrule array.
*
* @param array $original Original array. It will be *modified* by this method and contains the result afterwards!
* @param array $overrule Overrule array, overruling the original array
* @param bool $addKeys If set to FALSE, keys that are NOT found in $original will not be set. Thus only existing value can/will be overruled from overrule array.
* @param bool $includeEmptyValues if set, values from $overrule will overrule if they are empty or zero
* @param bool $enableUnsetFeature if set, special values "__UNSET" can be used in the overrule array in order to unset array keys in the original array
* @return void
*/
protected function mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys = true, $includeEmptyValues = true, $enableUnsetFeature = true)
{
foreach ($overrule as $key => $_) {
if ($enableUnsetFeature && $overrule[$key] === '__UNSET') {
unset($original[$key]);
continue;
}
if (isset($original[$key]) && is_array($original[$key])) {
if (is_array($overrule[$key])) {
$this->mergeRecursiveWithOverrule($original[$key], $overrule[$key], $addKeys, $includeEmptyValues, $enableUnsetFeature);
}
} elseif (
($addKeys || isset($original[$key])) &&
($includeEmptyValues || $overrule[$key])
) {
$original[$key] = $overrule[$key];
}
}
// This line is kept for backward compatibility reasons.
reset($original);
}
/**
* Sets the new database name for the test system in configuration
*
* @param array $databaseConfiguration
* @throws Exception
* @return array
*/
protected function setDatabaseName(array $databaseConfiguration)
{
$originalDatabaseName = $databaseConfiguration['Connections']['Default']['dbname'];
$databaseName = $originalDatabaseName . '_ft' . $this->identifier;
// Maximum database name length for mysql is 64 characters
if (strlen($databaseName) > 64) {
throw new Exception(
'The name of the database that is used for the functional test (' . $databaseName . ')' .
' exceeds the maximum length of 64 character allowed by MySQL. You have to shorten your' .
' original database name to 54 characters',
1488117937
);
}
$databaseConfiguration['Connections']['Default']['dbname'] = strtolower($databaseName);
return $databaseConfiguration;
}
/**
* Returns PHP code that, when executed in $from, will return the path to $to
* Copied from Composer sources and adapted for limited use case here
*
* @see https://github.com/composer/composer
*/
protected function findShortestPathCode(string $from, string $to): string
{
if ($from === $to) {
return '__FILE__';
}
$commonPath = $to;
while (strpos($from . '/', $commonPath . '/') !== 0 && '/' !== $commonPath && preg_match('{^[a-z]:/?$}i', $commonPath) !== false && '.' !== $commonPath) {
$commonPath = dirname($commonPath);
}
if ('/' === $commonPath || '.' === $commonPath || 0 !== strpos($from, $commonPath)) {
return var_export($to, true);
}
$commonPath = rtrim($commonPath, '/') . '/';
if (strpos($to, $from . '/') === 0) {
return '__DIR__ . ' . var_export(substr($to, strlen($from)), true);
}
$sourcePathDepth = substr_count(substr($from, strlen($commonPath)), '/');
$commonPathCode = "__DIR__ . '" . str_repeat('/..', $sourcePathDepth) . "'";
$relTarget = substr($to, strlen($commonPath));
return $commonPathCode . ($relTarget !== '' ? ' . ' . var_export('/' . $relTarget, true) : '');
}
/**
* Wrapper function for rmdir, allowing recursive deletion of folders and files
*
* @param string $path Absolute path to folder, see PHP rmdir() function. Removes trailing slash internally.
* @param bool $removeNonEmpty Allow deletion of non-empty directories
* @return bool true if @rmdir went well!
*/
protected function rmdir($path, $removeNonEmpty = false)
{
$OK = false;
// Remove trailing slash
$path = preg_replace('|/$|', '', $path);
if (file_exists($path)) {
$OK = true;
if (!is_link($path) && is_dir($path)) {
if ($removeNonEmpty == true && ($handle = opendir($path))) {
while ($OK && false !== ($file = readdir($handle))) {
if ($file === '.' || $file === '..') {
continue;
}
$OK = $this->rmdir($path . '/' . $file, $removeNonEmpty);
}
closedir($handle);
}
if ($OK) {
$OK = @rmdir($path);
}
} else {
// If $path is a symlink to a folder we need rmdir() on Windows systems
if (DIRECTORY_SEPARATOR === '\\'
&& is_link($path)
&& is_dir($path . '/')
) {
$OK = rmdir($path);
} else {
$OK = unlink($path);
}
}
clearstatcache();
} elseif (is_link($path)) {
$OK = unlink($path);
clearstatcache();
}
return $OK;
}
/**
* Writes $content to the file $file
*
* @param string $file Filepath to write to
* @param string $content Content to write
* @return bool TRUE if the file was successfully opened and written
*/
protected function writeFile($file, $content)
{
if ($fd = fopen($file, 'wb')) {
$res = fwrite($fd, $content);
fclose($fd);
if ($res === false) {
return false;
}
return true;
}
return false;
}
/**
* Returns the version number of the PackageStates.php file
*
* @return int
*/
protected function getPackageStatesVersion()
{
return 5;
}
/**
* Checks and returns the file path of the autoload.php
*
* @throws Exception
* @return string
*/
protected function getClassLoaderFilepath()
{
$classLoaderFilepath = __DIR__ . '/../../../../../autoload.php';
if (!file_exists($classLoaderFilepath)) {
if (file_exists(__DIR__ . '/../../../.Build/vendor/autoload.php')) {
$classLoaderFilepath = __DIR__ . '/../../../.Build/vendor/autoload.php';
} elseif (file_exists(ORIGINAL_ROOT . '../vendor/autoload.php')) {
$classLoaderFilepath = ORIGINAL_ROOT . '../vendor/autoload.php';
} else {
throw new Exception(
'ClassLoader can\'t be loaded.'
. ' Tried to find "' . $classLoaderFilepath . '".'
. ' Please check your path or set an environment variable \'TYPO3_PATH_ROOT\' to your root path.'
);
}
}
return $classLoaderFilepath;
}
}