TYPO3 CMS  TYPO3_6-2
AbstractDataHandlerActionTestCase.php
Go to the documentation of this file.
1 <?php
3 
19 
24 
26 
31 
36 
43  protected $expectedErrorLogEntries = 0;
44 
48  protected $testExtensionsToLoad = array(
49  'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial',
50  // 'typo3conf/ext/datahandler',
51  );
52 
56  protected $pathsToLinkInTestInstance = array(
57  'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
58  'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/extTables.php' => 'typo3conf/extTables.php',
59  );
60 
64  protected $recordIds = array();
65 
69  protected $actionService;
70 
74  protected $backendUser;
75 
76  public function setUp() {
77  parent::setUp();
78 
79  $this->backendUser = $this->setUpBackendUserFromFixture(self::VALUE_BackendUserId);
80  // By default make tests on live workspace
81  $this->backendUser->workspace = 0;
82 
83  $this->actionService = $this->getActionService();
84  \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeLanguageObject();
85  }
86 
87  public function tearDown() {
88  $this->assertErrorLogEntries();
89  unset($this->actionService);
90  unset($this->recordIds);
91  parent::tearDown();
92  }
93 
97  protected function getActionService() {
99  'TYPO3\\CMS\\Core\\Tests\\Functional\\DataHandling\\Framework\\ActionService'
100  );
101  }
102 
106  protected function importScenarioDataSet($dataSetName) {
107  $fileName = rtrim($this->scenarioDataSetDirectory, '/') . '/' . $dataSetName . '.csv';
108  $fileName = GeneralUtility::getFileAbsFileName($fileName);
109 
110  $dataSet = DataSet::read($fileName);
111 
112  foreach ($dataSet->getTableNames() as $tableName) {
113  foreach ($dataSet->getElements($tableName) as $element) {
114  $this->getDatabaseConnection()->exec_INSERTquery(
115  $tableName,
116  $element
117  );
118  $sqlError = $this->getDatabaseConnection()->sql_error();
119  if (!empty($sqlError)) {
120  $this->fail('SQL Error for table "' . $tableName . '": ' . LF . $sqlError);
121  }
122  }
123  }
124  }
125 
126  protected function assertAssertionDataSet($dataSetName) {
127  $fileName = rtrim($this->assertionDataSetDirectory, '/') . '/' . $dataSetName . '.csv';
128  $fileName = GeneralUtility::getFileAbsFileName($fileName);
129 
130  $dataSet = DataSet::read($fileName);
131  $failMessages = array();
132 
133  foreach ($dataSet->getTableNames() as $tableName) {
134  $hasUidField = ($dataSet->getIdIndex($tableName) !== NULL);
135  $records = $this->getAllRecords($tableName, $hasUidField);
136  foreach ($dataSet->getElements($tableName) as $assertion) {
137  $result = $this->assertInRecords($assertion, $records);
138  if ($result === FALSE) {
139  if ($hasUidField && empty($records[$assertion['uid']])) {
140  $failMessages[] = 'Record "' . $tableName . ':' . $assertion['uid'] . '" not found in database';
141  continue;
142  }
143  $recordIdentifier = $tableName . ($hasUidField ? ':' . $assertion['uid'] : '');
144  $additionalInformation = ($hasUidField ? $this->renderRecords($assertion, $records[$assertion['uid']]) : $this->arrayToString($assertion));
145  $failMessages[] = 'Assertion in data-set failed for "' . $recordIdentifier . '":' . LF . $additionalInformation;
146  // Unset failed asserted record
147  if ($hasUidField) {
148  unset($records[$assertion['uid']]);
149  }
150  } else {
151  // Unset asserted record
152  unset($records[$result]);
153  // Increase assertion counter
154  $this->assertTrue($result !== FALSE);
155  }
156  }
157  if (!empty($records)) {
158  foreach ($records as $record) {
159  $recordIdentifier = $tableName . ':' . $record['uid'];
160  $emptyAssertion = array_fill_keys($dataSet->getFields($tableName), '[none]');
161  $reducedRecord = array_intersect_key($record, $emptyAssertion);
162  $additionalInformation = ($hasUidField ? $this->renderRecords($emptyAssertion, $reducedRecord) : $this->arrayToString($reducedRecord));
163  $failMessages[] = 'Not asserted record found for "' . $recordIdentifier . '":' . LF . $additionalInformation;
164  }
165  }
166  }
167 
168  if (!empty($failMessages)) {
169  $this->fail(implode(LF, $failMessages));
170  }
171  }
172 
178  protected function assertInRecords(array $assertion, array $records) {
179  foreach ($records as $index => $record) {
180  $differentFields = $this->getDifferentFields($assertion, $record);
181 
182  if (empty($differentFields)) {
183  return $index;
184  }
185  }
186 
187  return FALSE;
188  }
189 
195  protected function assertErrorLogEntries() {
196  if ($this->expectedErrorLogEntries === NULL) {
197  return;
198  }
199  $errorLogEntries = $this->getDatabaseConnection()->exec_SELECTgetRows('*', 'sys_log', 'error IN (1,2)');
200  $actualErrorLogEntries = count($errorLogEntries);
201  if ($actualErrorLogEntries === $this->expectedErrorLogEntries) {
202  $this->assertSame($this->expectedErrorLogEntries, $actualErrorLogEntries);
203  } else {
204  $failureMessage = 'Expected ' . $this->expectedErrorLogEntries . ' entries in sys_log, but got ' . $actualErrorLogEntries . LF;
205  foreach ($errorLogEntries as $entry) {
206  $entryData = unserialize($entry['log_data']);
207  $entryMessage = vsprintf($entry['details'], $entryData);
208  $failureMessage .= '* ' . $entryMessage . LF;
209  }
210  $this->fail($failureMessage);
211  }
212  }
213 
219  protected function getAllRecords($tableName, $hasUidField = FALSE) {
220  $allRecords = array();
221 
222  $records = $this->getDatabaseConnection()->exec_SELECTgetRows(
223  '*',
224  $tableName,
225  '1=1',
226  '',
227  '',
228  '',
229  ($hasUidField ? 'uid' : '')
230  );
231 
232  if (!empty($records)) {
233  $allRecords = $records;
234  }
235 
236  return $allRecords;
237  }
238 
243  protected function arrayToString(array $array) {
244  $elements = array();
245  foreach ($array as $key => $value) {
246  if (is_array($value)) {
247  $value = $this->arrayToString($value);
248  }
249  $elements[] = "'" . $key . "' => '" . $value . "'";
250  }
251  return 'array(' . PHP_EOL . ' ' . implode(', ' . PHP_EOL . ' ', $elements) . PHP_EOL . ')' . PHP_EOL;
252  }
253 
259  protected function renderRecords(array $assertion, array $record) {
260  $differentFields = $this->getDifferentFields($assertion, $record);
261  $columns = array(
262  'fields' => array('Fields'),
263  'assertion' => array('Assertion'),
264  'record' => array('Record'),
265  );
266  $lines = array();
267  $linesFromXmlValues = array();
268  $result = '';
269 
270  foreach ($differentFields as $differentField) {
271  $columns['fields'][] = $differentField;
272  $columns['assertion'][] = ($assertion[$differentField] === NULL ? 'NULL' : $assertion[$differentField]);
273  $columns['record'][] = ($record[$differentField] === NULL ? 'NULL' : $record[$differentField]);
274  }
275 
276  foreach ($columns as $columnIndex => $column) {
277  $columnLength = NULL;
278  foreach ($column as $value) {
279  if (strpos($value, '<?xml') === 0) {
280  $value = '[see diff]';
281  }
282  $valueLength = strlen($value);
283  if (empty($columnLength) || $valueLength > $columnLength) {
284  $columnLength = $valueLength;
285  }
286  }
287  foreach ($column as $valueIndex => $value) {
288  if (strpos($value, '<?xml') === 0) {
289  if ($columnIndex === 'assertion') {
290  try {
291  $this->assertXmlStringEqualsXmlString((string)$value, (string)$record[$columns['fields'][$valueIndex]]);
292  } catch(\PHPUnit_Framework_ExpectationFailedException $e) {
293  $linesFromXmlValues[] = 'Diff for field "' . $columns['fields'][$valueIndex] . '":' . PHP_EOL . $e->getComparisonFailure()->getDiff();
294  }
295  }
296  $value = '[see diff]';
297  }
298  $lines[$valueIndex][$columnIndex] = str_pad($value, $columnLength, ' ');
299  }
300  }
301 
302  foreach ($lines as $line) {
303  $result .= implode('|', $line) . PHP_EOL;
304  }
305 
306  foreach ($linesFromXmlValues as $lineFromXmlValues) {
307  $result .= PHP_EOL . $lineFromXmlValues . PHP_EOL;
308  }
309 
310  return $result;
311  }
312 
318  protected function getDifferentFields(array $assertion, array $record) {
319  $differentFields = array();
320 
321  foreach ($assertion as $field => $value) {
322  if (strpos($value, '\\*') === 0) {
323  continue;
324  } elseif (strpos($value, '<?xml') === 0) {
325  try {
326  $this->assertXmlStringEqualsXmlString((string)$value, (string)$record[$field]);
327  } catch (\PHPUnit_Framework_ExpectationFailedException $e) {
328  $differentFields[] = $field;
329  }
330  } elseif ($value === NULL && $record[$field] !== $value) {
331  $differentFields[] = $field;
332  } elseif ((string)$record[$field] !== (string)$value) {
333  $differentFields[] = $field;
334  }
335  }
336 
337  return $differentFields;
338  }
339 
344  return new \TYPO3\CMS\Core\Tests\Functional\Framework\Constraint\RequestSection\HasRecordConstraint();
345  }
346 
351  return new \TYPO3\CMS\Core\Tests\Functional\Framework\Constraint\RequestSection\DoesNotHaveRecordConstraint();
352  }
353 
358  return new \TYPO3\CMS\Core\Tests\Functional\Framework\Constraint\RequestSection\StructureHasRecordConstraint();
359  }
360 
365  return new \TYPO3\CMS\Core\Tests\Functional\Framework\Constraint\RequestSection\StructureDoesNotHaveRecordConstraint();
366  }
367 
368 }
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.
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)