[TASK] Re-work/simplify copyright header in PHP files - Part 4
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / IRRE / AbstractTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\IRRE;
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\Utility\BackendUtility;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20 /**
21 * Generic test helpers.
22 *
23 */
24 abstract class AbstractTestCase extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
25 const VALUE_LanguageId = 2;
26
27 const TABLE_Pages = 'pages';
28
29 const COMMAND_Copy = 'copy';
30 const COMMAND_Localize = 'localize';
31 const COMMAND_Delete = 'delete';
32
33 const PROPERTY_LocalizeReferencesAtParentLocalization = 'localizeReferencesAtParentLocalization';
34 const BEHAVIOUR_LocalizeChildrenAtParentLocalization = 'localizeChildrenAtParentLocalization';
35 const BEHAVIOUR_LocalizationMode = 'localizationMode';
36
37 protected $testExtensionsToLoad = array('typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial');
38
39 /**
40 * @var integer
41 */
42 private $expectedLogEntries = 0;
43
44 /**
45 * Sets up this test case.
46 *
47 * @return void
48 */
49 public function setUp() {
50 parent::setUp();
51
52 $this->setUpBackendUserFromFixture(1);
53 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeLanguageObject();
54
55 $this->expectedLogEntries = 0;
56
57 $GLOBALS['TYPO3_CONF_VARS']['SYS']['sqlDebug'] = 1;
58
59 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
60 $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_language.xml');
61 }
62
63 /**
64 * Tears down this test case.
65 *
66 * @return void
67 */
68 public function tearDown() {
69 $this->assertNoLogEntries();
70
71 $this->expectedLogEntries = 0;
72
73 parent::tearDown();
74 }
75
76 /**
77 * Sets the number of expected log entries.
78 *
79 * @param integer $count
80 * @return void
81 */
82 protected function setExpectedLogEntries($count) {
83 $count = (int)$count;
84
85 if ($count > 0) {
86 $this->expectedLogEntries = $count;
87 }
88 }
89
90 /**
91 * @param string $command
92 * @param mixed $value
93 * @param array $tables Table names with list of ids to be edited
94 * @return array
95 */
96 protected function getElementStructureForCommands($command, $value, array $tables) {
97 $commandStructure = array();
98
99 foreach ($tables as $tableName => $idList) {
100 $ids = GeneralUtility::trimExplode(',', $idList, TRUE);
101 foreach ($ids as $id) {
102 $commandStructure[$tableName][$id] = array(
103 $command => $value
104 );
105 }
106 }
107
108 return $commandStructure;
109 }
110
111 /**
112 * Simulates executing commands by using t3lib_TCEmain.
113 *
114 * @param array $elements The cmdmap to be delivered to DataHandler
115 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
116 */
117 protected function simulateCommandByStructure(array $elements) {
118 $tceMain = $this->getTceMain();
119 $tceMain->start(array(), $elements);
120 $tceMain->process_cmdmap();
121
122 return $tceMain;
123 }
124
125 /**
126 * @param string $command
127 * @param mixed $value
128 * @param array $tables Table names with list of ids to be edited
129 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
130 */
131 protected function simulateCommand($command, $value, array $tables) {
132 return $this->simulateCommandByStructure(
133 $this->getElementStructureForCommands($command, $value, $tables)
134 );
135 }
136
137 /**
138 * Gets the last log entry.
139 *
140 * @return array
141 */
142 protected function getLastLogEntryMessage() {
143 $message = '';
144
145 $logEntries = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_log', 'error IN (1,2)', '', '', 1);
146
147 if (is_array($logEntries) && count($logEntries)) {
148 $message = $logEntries[0]['details'];
149 }
150
151 return $message;
152 }
153
154 /**
155 * @param array $itemArray
156 * @return array
157 */
158 protected function getElementsByItemArray(array $itemArray) {
159 $elements = array();
160
161 foreach ($itemArray as $item) {
162 $elements[$item['table']][$item['id']] = BackendUtility::getRecord($item['table'], $item['id']);
163 }
164
165 return $elements;
166 }
167
168 /**
169 * Gets all records of a table.
170 *
171 * @param string $table Name of the table
172 * @param string $indexField
173 * @return array
174 */
175 protected function getAllRecords($table, $indexField = 'uid') {
176 return $this->getDatabaseConnection()->exec_SELECTgetRows('*', $table, '1=1', '', '', '', $indexField);
177 }
178
179 /**
180 * Gets the TCE configuration of a field.
181 *
182 * @param $tableName
183 * @param $fieldName
184 * @return array
185 */
186 protected function getTcaFieldConfiguration($tableName, $fieldName) {
187 if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) {
188 $this->fail('TCA definition for field ' . $tableName . '.' . $fieldName . ' not available');
189 }
190
191 return $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'];
192 }
193
194 /**
195 * @param string $tableName
196 * @param string $fieldName
197 * @param string $propertyName
198 * @param mixed $value
199 * @return void
200 */
201 protected function setTcaFieldConfiguration($tableName, $fieldName, $propertyName, $value) {
202 if (isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) {
203 $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'][$propertyName] = $value;
204 }
205 }
206
207 /**
208 * @param string $tableName
209 * @param string $fieldName
210 * @param string $behaviourName
211 * @param mixed $value
212 * @return void
213 */
214 protected function setTcaFieldConfigurationBehaviour($tableName, $fieldName, $behaviourName, $value) {
215 if (isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) {
216 if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['behaviour'])) {
217 $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['behaviour'] = array();
218 }
219
220 $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['behaviour'][$behaviourName] = $value;
221 }
222 }
223
224 /**
225 * Gets the field value of a record.
226 *
227 * @param $tableName
228 * @param $id
229 * @param $fieldName
230 * @return string
231 */
232 protected function getFieldValue($tableName, $id, $fieldName) {
233 $record = BackendUtility::getRecord($tableName, $id, $fieldName);
234
235 if (!is_array($record)) {
236 $this->fail('Record ' . $tableName . ':' . $id . ' not available');
237 }
238
239 return $record[$fieldName];
240 }
241
242 /**
243 * Gets instance of \TYPO3\CMS\Core\Database\RelationHandler.
244 *
245 * @return \TYPO3\CMS\Core\Database\RelationHandler
246 */
247 protected function getLoadDbGroup() {
248 $loadDbGroup = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
249
250 return $loadDbGroup;
251 }
252
253 /**
254 * Gets an instance of \TYPO3\CMS\Core\DataHandling\DataHandler.
255 *
256 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
257 */
258 protected function getTceMain() {
259 $tceMain = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
260 return $tceMain;
261 }
262
263 /**
264 * Assert that no sys_log entries had been written.
265 *
266 * @return void
267 */
268 protected function assertNoLogEntries() {
269 $logEntries = $this->getLogEntries();
270
271 if (count($logEntries) > $this->expectedLogEntries) {
272 var_dump(array_values($logEntries));
273 ob_flush();
274 $this->fail('The sys_log table contains unexpected entries.');
275 } elseif (count($logEntries) < $this->expectedLogEntries) {
276 $this->fail('Expected count of sys_log entries no reached.');
277 }
278 }
279
280 /**
281 * Asserts the correct order of elements.
282 *
283 * @param string $table
284 * @param string $field
285 * @param array $expectedOrderOfIds
286 * @param string $message
287 * @return void
288 */
289 protected function assertSortingOrder($table, $field, $expectedOrderOfIds, $message) {
290 $expectedOrderOfIdsCount = count($expectedOrderOfIds);
291 $elements = $this->getAllRecords($table);
292
293 for ($i = 0; $i < $expectedOrderOfIdsCount-1; $i++) {
294 $this->assertLessThan(
295 $elements[$expectedOrderOfIds[$i+1]][$field],
296 $elements[$expectedOrderOfIds[$i]][$field],
297 $message
298 );
299 }
300 }
301
302 /**
303 * Asserts reference index elements.
304 *
305 * @param array $assertions
306 * @param boolean $expected
307 */
308 protected function assertReferenceIndex(array $assertions, $expected = TRUE) {
309 $references = $this->getAllRecords('sys_refindex', 'hash');
310
311 foreach ($assertions as $parent => $children) {
312 foreach ($children as $child) {
313 $parentItems = explode(':', $parent);
314 $childItems = explode(':', $child);
315
316 $assertion = array(
317 'tablename' => $parentItems[0],
318 'recuid' => $parentItems[1],
319 'field' => $parentItems[2],
320 'ref_table' => $childItems[0],
321 'ref_uid' => $childItems[1],
322 );
323
324 $this->assertTrue(
325 ($expected === $this->executeAssertionOnElements($assertion, $references)),
326 'Expected reference index element for ' . $parent . ' -> ' . $child
327 );
328 }
329 }
330 }
331
332 /**
333 * @param string $parentTableName
334 * @param integer $parentId
335 * @param string $parentFieldName
336 * @param array $assertions
337 * @param string $mmTable
338 * @param boolean $expected
339 * @return void
340 */
341 protected function assertChildren($parentTableName, $parentId, $parentFieldName, array $assertions, $mmTable = '', $expected = TRUE) {
342 $tcaFieldConfiguration = $this->getTcaFieldConfiguration($parentTableName, $parentFieldName);
343
344 $loadDbGroup = $this->getLoadDbGroup();
345 $loadDbGroup->start(
346 $this->getFieldValue($parentTableName, $parentId, $parentFieldName),
347 $tcaFieldConfiguration['foreign_table'],
348 $mmTable,
349 $parentId,
350 $parentTableName,
351 $tcaFieldConfiguration
352 );
353
354 $elements = $this->getElementsByItemArray($loadDbGroup->itemArray);
355
356 foreach ($assertions as $index => $assertion) {
357 $this->assertTrue(
358 ($expected === $this->executeAssertionOnElements($assertion, $elements)),
359 'Assertion #' . $index . ' failed'
360 );
361 }
362 }
363
364 /**
365 * Gets log entries from the sys_log
366 *
367 * @return array
368 */
369 protected function getLogEntries() {
370 return $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_log', 'error IN (1,2)');
371 }
372
373 /**
374 * @param array $assertion
375 * @param array $elements
376 * @return boolean
377 */
378 protected function executeAssertionOnElements(array $assertion, array $elements) {
379 if (!empty($assertion['tableName'])) {
380 $tableName = $assertion['tableName'];
381 unset($assertion['tableName']);
382 $elements = (array) $elements[$tableName];
383 }
384
385 foreach ($elements as $element) {
386 $result = FALSE;
387
388 foreach ($assertion as $field => $value) {
389 if ($element[$field] == $value) {
390 $result = TRUE;
391 } else {
392 $result = FALSE;
393 break;
394 }
395 }
396
397 if ($result === TRUE) {
398 return TRUE;
399 }
400 }
401
402 return FALSE;
403 }
404
405 /**
406 * @param mixed $element
407 * @return string
408 */
409 protected function elementToString($element) {
410 $result = preg_replace(
411 '#\n+#',
412 ' ',
413 var_export($element, TRUE)
414 );
415
416 return $result;
417 }
418
419 /**
420 * @return string
421 */
422 protected function combine() {
423 return implode(':', func_get_args());
424 }
425
426 /**
427 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
428 */
429 protected function getBackendUser() {
430 return $GLOBALS['BE_USER'];
431 }
432 }