TYPO3 CMS  TYPO3_7-6
AbstractTestCase.php
Go to the documentation of this file.
1 <?php
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 
19 
25 {
26  const VALUE_LanguageId = 2;
27 
28  const TABLE_Pages = 'pages';
29 
30  const COMMAND_Copy = 'copy';
31  const COMMAND_Localize = 'localize';
32  const COMMAND_Delete = 'delete';
33 
34  const PROPERTY_LocalizeReferencesAtParentLocalization = 'localizeReferencesAtParentLocalization';
35  const BEHAVIOUR_LocalizeChildrenAtParentLocalization = 'localizeChildrenAtParentLocalization';
36  const BEHAVIOUR_LocalizationMode = 'localizationMode';
37 
38  protected $testExtensionsToLoad = ['typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial'];
39 
43  private $expectedLogEntries = 0;
44 
50  protected function setUp()
51  {
52  parent::setUp();
53 
54  $this->setUpBackendUserFromFixture(1);
55  \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeLanguageObject();
56 
57  $this->expectedLogEntries = 0;
58 
59  $GLOBALS['TYPO3_CONF_VARS']['SYS']['sqlDebug'] = 1;
60 
61  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/pages.xml');
62  $this->importDataSet(ORIGINAL_ROOT . 'typo3/sysext/core/Tests/Functional/Fixtures/sys_language.xml');
63  }
64 
70  protected function tearDown()
71  {
72  $this->assertNoLogEntries();
73 
74  $this->expectedLogEntries = 0;
75 
76  parent::tearDown();
77  }
78 
85  protected function setExpectedLogEntries($count)
86  {
87  $count = (int)$count;
88 
89  if ($count > 0) {
90  $this->expectedLogEntries = $count;
91  }
92  }
93 
100  protected function getElementStructureForCommands($command, $value, array $tables)
101  {
102  $commandStructure = [];
103 
104  foreach ($tables as $tableName => $idList) {
105  $ids = GeneralUtility::trimExplode(',', $idList, true);
106  foreach ($ids as $id) {
107  $commandStructure[$tableName][$id] = [
108  $command => $value
109  ];
110  }
111  }
112 
113  return $commandStructure;
114  }
115 
122  protected function simulateCommandByStructure(array $elements)
123  {
124  $tceMain = $this->getTceMain();
125  $tceMain->start([], $elements);
126  $tceMain->process_cmdmap();
127 
128  return $tceMain;
129  }
130 
137  protected function simulateCommand($command, $value, array $tables)
138  {
139  return $this->simulateCommandByStructure(
140  $this->getElementStructureForCommands($command, $value, $tables)
141  );
142  }
143 
149  protected function getLastLogEntryMessage()
150  {
151  $message = '';
152 
153  $logEntries = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_log', 'error IN (1,2)', '', '', 1);
154 
155  if (is_array($logEntries) && !empty($logEntries)) {
156  $message = $logEntries[0]['details'];
157  }
158 
159  return $message;
160  }
161 
166  protected function getElementsByItemArray(array $itemArray)
167  {
168  $elements = [];
169 
170  foreach ($itemArray as $item) {
171  $elements[$item['table']][$item['id']] = BackendUtility::getRecord($item['table'], $item['id']);
172  }
173 
174  return $elements;
175  }
176 
184  protected function getAllRecords($table, $indexField = 'uid')
185  {
186  return $this->getDatabaseConnection()->exec_SELECTgetRows('*', $table, '1=1', '', '', '', $indexField);
187  }
188 
196  protected function getTcaFieldConfiguration($tableName, $fieldName)
197  {
198  if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) {
199  $this->fail('TCA definition for field ' . $tableName . '.' . $fieldName . ' not available');
200  }
201 
202  return $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'];
203  }
204 
212  protected function setTcaFieldConfiguration($tableName, $fieldName, $propertyName, $value)
213  {
214  if (isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) {
215  $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'][$propertyName] = $value;
216  }
217  }
218 
226  protected function setTcaFieldConfigurationBehaviour($tableName, $fieldName, $behaviourName, $value)
227  {
228  if (isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config'])) {
229  if (!isset($GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['behaviour'])) {
230  $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['behaviour'] = [];
231  }
232 
233  $GLOBALS['TCA'][$tableName]['columns'][$fieldName]['config']['behaviour'][$behaviourName] = $value;
234  }
235  }
236 
245  protected function getFieldValue($tableName, $id, $fieldName)
246  {
247  $record = BackendUtility::getRecord($tableName, $id, $fieldName);
248 
249  if (!is_array($record)) {
250  $this->fail('Record ' . $tableName . ':' . $id . ' not available');
251  }
252 
253  return $record[$fieldName];
254  }
255 
261  protected function getLoadDbGroup()
262  {
263  $loadDbGroup = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\RelationHandler::class);
264 
265  return $loadDbGroup;
266  }
267 
273  protected function getTceMain()
274  {
275  $tceMain = GeneralUtility::makeInstance(\TYPO3\CMS\Core\DataHandling\DataHandler::class);
276  return $tceMain;
277  }
278 
284  protected function assertNoLogEntries()
285  {
286  $logEntries = $this->getLogEntries();
287 
288  if (count($logEntries) > $this->expectedLogEntries) {
289  var_dump(array_values($logEntries));
290  ob_flush();
291  $this->fail('The sys_log table contains unexpected entries.');
292  } elseif (count($logEntries) < $this->expectedLogEntries) {
293  $this->fail('Expected count of sys_log entries no reached.');
294  }
295  }
296 
306  protected function assertSortingOrder($table, $field, $expectedOrderOfIds, $message)
307  {
308  $expectedOrderOfIdsCount = count($expectedOrderOfIds);
309  $elements = $this->getAllRecords($table);
310 
311  for ($i = 0; $i < $expectedOrderOfIdsCount-1; $i++) {
312  $this->assertLessThan(
313  $elements[$expectedOrderOfIds[$i+1]][$field],
314  $elements[$expectedOrderOfIds[$i]][$field],
315  $message
316  );
317  }
318  }
319 
326  protected function assertReferenceIndex(array $assertions, $expected = true)
327  {
328  $references = $this->getAllRecords('sys_refindex', 'hash');
329 
330  foreach ($assertions as $parent => $children) {
331  foreach ($children as $child) {
332  $parentItems = explode(':', $parent);
333  $childItems = explode(':', $child);
334 
335  $assertion = [
336  'tablename' => $parentItems[0],
337  'recuid' => $parentItems[1],
338  'field' => $parentItems[2],
339  'ref_table' => $childItems[0],
340  'ref_uid' => $childItems[1],
341  ];
342 
343  $this->assertTrue(
344  ($expected === $this->executeAssertionOnElements($assertion, $references)),
345  'Expected reference index element for ' . $parent . ' -> ' . $child
346  );
347  }
348  }
349  }
350 
360  protected function assertChildren($parentTableName, $parentId, $parentFieldName, array $assertions, $mmTable = '', $expected = true)
361  {
362  $tcaFieldConfiguration = $this->getTcaFieldConfiguration($parentTableName, $parentFieldName);
363 
364  $loadDbGroup = $this->getLoadDbGroup();
365  $loadDbGroup->start(
366  $this->getFieldValue($parentTableName, $parentId, $parentFieldName),
367  $tcaFieldConfiguration['foreign_table'],
368  $mmTable,
369  $parentId,
370  $parentTableName,
371  $tcaFieldConfiguration
372  );
373 
374  $elements = $this->getElementsByItemArray($loadDbGroup->itemArray);
375 
376  foreach ($assertions as $index => $assertion) {
377  $this->assertTrue(
378  ($expected === $this->executeAssertionOnElements($assertion, $elements)),
379  'Assertion #' . $index . ' failed'
380  );
381  }
382  }
383 
389  protected function getLogEntries()
390  {
391  return $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_log', 'error IN (1,2)');
392  }
393 
399  protected function executeAssertionOnElements(array $assertion, array $elements)
400  {
401  if (!empty($assertion['tableName'])) {
402  $tableName = $assertion['tableName'];
403  unset($assertion['tableName']);
404  $elements = (array)$elements[$tableName];
405  }
406 
407  foreach ($elements as $element) {
408  $result = false;
409 
410  foreach ($assertion as $field => $value) {
411  if ($element[$field] == $value) {
412  $result = true;
413  } else {
414  $result = false;
415  break;
416  }
417  }
418 
419  if ($result === true) {
420  return true;
421  }
422  }
423 
424  return false;
425  }
426 
431  protected function elementToString($element)
432  {
433  $result = preg_replace(
434  '#\n+#',
435  ' ',
436  var_export($element, true)
437  );
438 
439  return $result;
440  }
441 
445  protected function combine()
446  {
447  return implode(':', func_get_args());
448  }
449 
453  protected function getBackendUser()
454  {
455  return $GLOBALS['BE_USER'];
456  }
457 }
setTcaFieldConfiguration($tableName, $fieldName, $propertyName, $value)
static trimExplode($delim, $string, $removeEmptyValues=false, $limit=0)
setTcaFieldConfigurationBehaviour($tableName, $fieldName, $behaviourName, $value)
static getRecord($table, $uid, $fields=' *', $where='', $useDeleteClause=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
assertSortingOrder($table, $field, $expectedOrderOfIds, $message)
assertChildren($parentTableName, $parentId, $parentFieldName, array $assertions, $mmTable='', $expected=true)