TYPO3 CMS  TYPO3_6-2
AbstractTestCase.php
Go to the documentation of this file.
1 <?php
3 
19 
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 
42  private $expectedLogEntries = 0;
43 
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 
68  public function tearDown() {
69  $this->assertNoLogEntries();
70 
71  $this->expectedLogEntries = 0;
72 
73  parent::tearDown();
74  }
75 
82  protected function setExpectedLogEntries($count) {
83  $count = (int)$count;
84 
85  if ($count > 0) {
86  $this->expectedLogEntries = $count;
87  }
88  }
89 
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 
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 
131  protected function simulateCommand($command, $value, array $tables) {
132  return $this->simulateCommandByStructure(
133  $this->getElementStructureForCommands($command, $value, $tables)
134  );
135  }
136 
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 
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 
175  protected function getAllRecords($table, $indexField = 'uid') {
176  return $this->getDatabaseConnection()->exec_SELECTgetRows('*', $table, '1=1', '', '', '', $indexField);
177  }
178 
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 
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 
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 
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 
247  protected function getLoadDbGroup() {
248  $loadDbGroup = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Database\\RelationHandler');
249 
250  return $loadDbGroup;
251  }
252 
258  protected function getTceMain() {
259  $tceMain = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
260  return $tceMain;
261  }
262 
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 
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 
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 
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 
369  protected function getLogEntries() {
370  return $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_log', 'error IN (1,2)');
371  }
372 
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 
409  protected function elementToString($element) {
410  $result = preg_replace(
411  '#\n+#',
412  ' ',
413  var_export($element, TRUE)
414  );
415 
416  return $result;
417  }
418 
422  protected function combine() {
423  return implode(':', func_get_args());
424  }
425 
429  protected function getBackendUser() {
430  return $GLOBALS['BE_USER'];
431  }
432 }
setTcaFieldConfiguration($tableName, $fieldName, $propertyName, $value)
assertChildren($parentTableName, $parentId, $parentFieldName, array $assertions, $mmTable='', $expected=TRUE)
static trimExplode($delim, $string, $removeEmptyValues=FALSE, $limit=0)
setTcaFieldConfigurationBehaviour($tableName, $fieldName, $behaviourName, $value)
if($list_of_literals) if(!empty($literals)) if(!empty($literals)) $result
Analyse literals to prepend the N char to them if their contents aren&#39;t numeric.
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]
assertSortingOrder($table, $field, $expectedOrderOfIds, $message)