[BUGFIX] mssql: ext:impexp test fixes
[Packages/TYPO3.CMS.git] / typo3 / sysext / impexp / Tests / Functional / AbstractImportExportTestCase.php
1 <?php
2 namespace TYPO3\CMS\Impexp\Tests\Functional;
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 TYPO3\CMS\Backend\Tree\View\PageTreeView;
18 use TYPO3\CMS\Core\Core\Bootstrap;
19 use TYPO3\CMS\Core\Database\ConnectionPool;
20 use TYPO3\CMS\Core\Database\Query\QueryHelper;
21 use TYPO3\CMS\Core\Database\Query\Restriction\DeletedRestriction;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23 use TYPO3\CMS\Impexp\Export;
24 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
25
26 /**
27 * Abstract used by ext:impexp functional tests
28 */
29 abstract class AbstractImportExportTestCase extends FunctionalTestCase
30 {
31 /**
32 * @var array
33 */
34 protected $coreExtensionsToLoad = [
35 'impexp',
36 ];
37
38 /**
39 * Absolute path to files that must be removed
40 * after a test - handled in tearDown
41 *
42 * @var array
43 */
44 protected $testFilesToDelete = [];
45
46 /**
47 * Different DBMS export different field types, the result XML is thus slightly different.
48 * This var is used to select a suitable XML export fixture to compare with.
49 *
50 * @var string
51 */
52 protected $databasePlatform;
53
54 /**
55 * Set up for set up the backend user, initialize the language object
56 * and creating the Export instance
57 */
58 protected function setUp()
59 {
60 parent::setUp();
61
62 $this->databasePlatform = $this->getConnectionPool()
63 ->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME)
64 ->getDatabasePlatform()
65 ->getName();
66
67 $backendUser = $this->setUpBackendUserFromFixture(1);
68 $backendUser->workspace = 0;
69 Bootstrap::getInstance()->initializeLanguageObject();
70 }
71
72 /**
73 * Tear down for remove of the test files
74 */
75 protected function tearDown()
76 {
77 foreach ($this->testFilesToDelete as $absoluteFileName) {
78 if (@is_file($absoluteFileName)) {
79 unlink($absoluteFileName);
80 }
81 }
82 parent::tearDown();
83 }
84
85 /**
86 * Builds a flat array containing the page tree with the PageTreeView
87 * based on given start pid and depth and set it in the Export object.
88 *
89 * Used in export tests
90 *
91 * @param $export Export instance
92 * @param int $pidToStart
93 * @param int $depth
94 */
95 protected function setPageTree(Export $export, $pidToStart, $depth = 1)
96 {
97 $permsClause = $GLOBALS['BE_USER']->getPagePermsClause(1);
98
99 $tree = GeneralUtility::makeInstance(PageTreeView::class);
100 $tree->init('AND ' . $permsClause);
101 $tree->tree[] = ['row' => $pidToStart];
102 $tree->buffer_idH = [];
103 if ($depth > 0) {
104 $tree->getTree($pidToStart, $depth, '');
105 }
106
107 $idH[$pidToStart]['uid'] = $pidToStart;
108 if (!empty($tree->buffer_idH)) {
109 $idH[$pidToStart]['subrow'] = $tree->buffer_idH;
110 }
111
112 $export->setPageTree($idH);
113 }
114
115 /**
116 * Adds records to the export object for a specific page id.
117 *
118 * Used in export tests.
119 *
120 * @param $export Export instance
121 * @param int $pid Page id for which to select records to add
122 * @param array $tables Array of table names to select from
123 */
124 protected function addRecordsForPid(Export $export, $pid, array $tables)
125 {
126 foreach ($GLOBALS['TCA'] as $table => $value) {
127 if ($table !== 'pages' && (in_array($table, $tables) || in_array('_ALL', $tables))) {
128 if ($GLOBALS['BE_USER']->check('tables_select', $table) && !$GLOBALS['TCA'][$table]['ctrl']['is_static']) {
129 $orderBy = $GLOBALS['TCA'][$table]['ctrl']['sortby'] ?: $GLOBALS['TCA'][$table]['ctrl']['default_sortby'];
130
131 $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
132 ->getQueryBuilderForTable($table);
133
134 $queryBuilder->getRestrictions()
135 ->removeAll()
136 ->add(GeneralUtility::makeInstance(DeletedRestriction::class));
137
138 $queryBuilder
139 ->select('*')
140 ->from($table)
141 ->where(
142 $queryBuilder->expr()->eq(
143 'pid',
144 $queryBuilder->createNamedParameter($pid, \PDO::PARAM_INT)
145 )
146 );
147
148 foreach (QueryHelper::parseOrderBy((string)$orderBy) as $orderPair) {
149 list($fieldName, $order) = $orderPair;
150 $queryBuilder->addOrderBy($fieldName, $order);
151 }
152
153 $result = $queryBuilder->execute();
154 while ($row = $result->fetch()) {
155 $export->export_addRecord($table, $row);
156 }
157 }
158 }
159 }
160 }
161
162 /**
163 * Test if the local filesystem is case sensitive.
164 * Needed for some export related tests
165 *
166 * @return bool
167 */
168 protected function isCaseSensitiveFilesystem()
169 {
170 $caseSensitive = true;
171 $path = GeneralUtility::tempnam('aAbB');
172
173 // do the actual sensitivity check
174 if (@file_exists(strtoupper($path)) && @file_exists(strtolower($path))) {
175 $caseSensitive = false;
176 }
177
178 // clean filesystem
179 unlink($path);
180 return $caseSensitive;
181 }
182 }