f50ca225016e12a6ba0421dfd0e427d5d8bba0b3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / AbstractDataHandlerActionTestCase.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2014 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28 use TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\DataSet;
29 use TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\Response;
30 use TYPO3\CMS\Core\Tests\Functional\Framework\Frontend\ResponseContent;
31
32 /**
33 * Functional test for the DataHandler
34 */
35 abstract class AbstractDataHandlerActionTestCase extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
36
37 const VALUE_BackendUserId = 1;
38
39 /**
40 * @var string
41 */
42 protected $dataSetDirectory;
43
44 /**
45 * @var array
46 */
47 protected $testExtensionsToLoad = array(
48 'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial',
49 // 'typo3conf/ext/datahandler',
50 );
51
52 /**
53 * @var array
54 */
55 protected $pathsToLinkInTestInstance = array(
56 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/AdditionalConfiguration.php' => 'typo3conf/AdditionalConfiguration.php',
57 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/extTables.php' => 'typo3conf/extTables.php',
58 );
59
60 /**
61 * @var \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\ActionService
62 */
63 protected $actionService;
64
65 /**
66 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
67 */
68 protected $backendUser;
69
70 public function setUp() {
71 parent::setUp();
72
73 $this->backendUser = $this->setUpBackendUserFromFixture(self::VALUE_BackendUserId);
74 // By default make tests on live workspace
75 $this->backendUser->workspace = 0;
76
77 $this->actionService = $this->getActionService();
78 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeLanguageObject();
79 }
80
81 public function tearDown() {
82 unset($this->actionService);
83 }
84
85 /**
86 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
87 */
88 protected function getDataHandler() {
89 $dataHandler = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
90 return $dataHandler;
91 }
92
93 /**
94 * @return \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\ActionService
95 */
96 protected function getActionService() {
97 return GeneralUtility::makeInstance(
98 'TYPO3\\CMS\\Core\\Tests\\Functional\\DataHandling\\Framework\\ActionService',
99 $this->getDataHandler()
100 );
101 }
102
103 /**
104 * @param string $dataSetName
105 */
106 protected function importScenarioDataSet($dataSetName) {
107 $fileName = rtrim($this->dataSetDirectory, '/') . '/Scenario/' . $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->getDatabase()->exec_INSERTquery(
115 $tableName,
116 $element
117 );
118 $sqlError = $this->getDatabase()->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->dataSetDirectory, '/') . '/Assertion/' . $dataSetName . '.csv';
128 $fileName = GeneralUtility::getFileAbsFileName($fileName);
129
130 $dataSet = DataSet::read($fileName);
131
132 foreach ($dataSet->getTableNames() as $tableName) {
133 $hasUidField = ($dataSet->getIdIndex($tableName) !== NULL);
134 $records = $this->getAllRecords($tableName, $hasUidField);
135 foreach ($dataSet->getElements($tableName) as $assertion) {
136 $result = $this->assertInRecords($assertion, $records);
137 if ($result === FALSE) {
138 if ($hasUidField && empty($records[$assertion['uid']])) {
139 $this->fail('Record "' . $tableName . ':' . $assertion['uid'] . '" not found in database');
140 }
141 $recordIdentifier = $tableName . ($hasUidField ? ':' . $assertion['uid'] : '');
142 $additionalInformation = ($hasUidField ? $this->renderRecords($assertion, $records[$assertion['uid']]) : $this->arrayToString($assertion));
143 $this->fail('Assertion in data-set failed for "' . $recordIdentifier . '":' . LF . $additionalInformation);
144 } else {
145 // Unset asserted record
146 unset($records[$result]);
147 // Increase assertion counter
148 $this->assertTrue($result !== FALSE);
149 }
150 }
151 }
152 }
153
154 /**
155 * @param array $assertion
156 * @param array $records
157 * @return bool|int|string
158 */
159 protected function assertInRecords(array $assertion, array $records) {
160 foreach ($records as $index => $record) {
161 $differentFields = $this->getDifferentFields($assertion, $record);
162
163 if (empty($differentFields)) {
164 return $index;
165 }
166 }
167
168 return FALSE;
169 }
170
171 /**
172 * @param string $tableName
173 * @param bool $hasUidField
174 * @return array
175 */
176 protected function getAllRecords($tableName, $hasUidField = FALSE) {
177 $allRecords = array();
178
179 $records = $this->getDatabase()->exec_SELECTgetRows(
180 '*',
181 $tableName,
182 '1=1',
183 '',
184 '',
185 '',
186 ($hasUidField ? 'uid' : '')
187 );
188
189 if (!empty($records)) {
190 $allRecords = $records;
191 }
192
193 return $allRecords;
194 }
195
196 /**
197 * @param array $array
198 * @return string
199 */
200 protected function arrayToString(array $array) {
201 $elements = array();
202 foreach ($array as $key => $value) {
203 if (is_array($value)) {
204 $value = $this->arrayToString($value);
205 }
206 $elements[] = "'" . $key . "' => '" . $value . "'";
207 }
208 return 'array(' . implode(', ', $elements) . ')';
209 }
210
211 /**
212 * @param array $assertion
213 * @param array $record
214 * @return string
215 */
216 protected function renderRecords(array $assertion, array $record) {
217 $differentFields = $this->getDifferentFields($assertion, $record);
218 $columns = array(
219 'fields' => array('Fields'),
220 'assertion' => array('Assertion'),
221 'record' => array('Record'),
222 );
223 $lines = array();
224 $result = '';
225
226 foreach ($differentFields as $differentField) {
227 $columns['fields'][] = $differentField;
228 $columns['assertion'][] = ($assertion[$differentField] === NULL ? 'NULL' : $assertion[$differentField]);
229 $columns['record'][] = ($record[$differentField] === NULL ? 'NULL' : $record[$differentField]);
230 }
231
232 foreach ($columns as $columnIndex => $column) {
233 $columnLength = NULL;
234 foreach ($column as $value) {
235 $valueLength = strlen($value);
236 if (empty($columnLength) || $valueLength > $columnLength) {
237 $columnLength = $valueLength;
238 }
239 }
240 foreach ($column as $valueIndex => $value) {
241 $lines[$valueIndex][$columnIndex] = str_pad($value, $columnLength, ' ');
242 }
243 }
244
245 foreach ($lines as $line) {
246 $result .= implode('|', $line) . PHP_EOL;
247 }
248
249 return $result;
250 }
251
252 /**
253 * @param array $assertion
254 * @param array $record
255 * @return array
256 */
257 protected function getDifferentFields(array $assertion, array $record) {
258 $differentFields = array();
259
260 foreach ($assertion as $field => $value) {
261 if (strpos($value, '\\*') === 0) {
262 continue;
263 } elseif ((string)$record[$field] !== (string)$value) {
264 $differentFields[] = $field;
265 }
266 }
267
268 return $differentFields;
269 }
270
271 /**
272 * @param ResponseContent $responseContent
273 * @param string $structureRecordIdentifier
274 * @param string $structureFieldName
275 * @param string $tableName
276 * @param string $fieldName
277 * @param string|array $values
278 */
279 protected function assertResponseContentStructureHasRecords(ResponseContent $responseContent, $structureRecordIdentifier, $structureFieldName, $tableName, $fieldName, $values) {
280 $nonMatchingVariants = array();
281
282 foreach ($responseContent->findStructures($structureRecordIdentifier, $structureFieldName) as $path => $structure) {
283 $nonMatchingValues = $this->getNonMatchingValuesFrontendResponseRecords($structure, $tableName, $fieldName, $values);
284
285 if (empty($nonMatchingValues)) {
286 // Increase assertion counter
287 $this->assertEmpty($nonMatchingValues);
288 return;
289 }
290
291 $nonMatchingVariants[$path] = $nonMatchingValues;
292 }
293
294 $nonMatchingMessage = '';
295 foreach ($nonMatchingVariants as $path => $nonMatchingValues) {
296 $nonMatchingMessage .= '* ' . $path . ': ' . implode(', ', $nonMatchingValues);
297 }
298
299 $this->fail('Could not assert all values for "' . $tableName . '.' . $fieldName . '"' . LF . $nonMatchingMessage);
300 }
301
302 /**
303 * @param ResponseContent $responseContent
304 * @param string $structureRecordIdentifier
305 * @param string $structureFieldName
306 * @param string $tableName
307 * @param string $fieldName
308 * @param string|array $values
309 */
310 protected function assertResponseContentStructureDoesNotHaveRecords(ResponseContent $responseContent, $structureRecordIdentifier, $structureFieldName, $tableName, $fieldName, $values) {
311 if (is_string($values)) {
312 $values = array($values);
313 }
314
315 $matchingVariants = array();
316
317 foreach ($responseContent->findStructures($structureRecordIdentifier, $structureFieldName) as $path => $structure) {
318 $nonMatchingValues = $this->getNonMatchingValuesFrontendResponseRecords($structure, $tableName, $fieldName, $values);
319 $matchingValues = array_diff($values, $nonMatchingValues);
320
321 if (!empty($matchingValues)) {
322 $matchingVariants[$path] = $matchingValues;
323 }
324 }
325
326 if (empty($matchingVariants)) {
327 // Increase assertion counter
328 $this->assertEmpty($matchingVariants);
329 return;
330 }
331
332 $matchingMessage = '';
333 foreach ($matchingVariants as $path => $matchingValues) {
334 $matchingMessage .= '* ' . $path . ': ' . implode(', ', $matchingValues);
335 }
336
337 $this->fail('Could not assert not having values for "' . $tableName . '.' . $fieldName . '"' . LF . $matchingMessage);
338 }
339
340 /**
341 * @param ResponseContent $responseContent
342 * @param string $tableName
343 * @param string $fieldName
344 * @param string|array $values
345 */
346 protected function assertResponseContentHasRecords(ResponseContent $responseContent, $tableName, $fieldName, $values) {
347 $nonMatchingValues = $this->getNonMatchingValuesFrontendResponseRecords($responseContent->getRecords(), $tableName, $fieldName, $values);
348
349 if (!empty($nonMatchingValues)) {
350 $this->fail('Could not assert all values for "' . $tableName . '.' . $fieldName . '": ' . implode(', ', $nonMatchingValues));
351 }
352
353 // Increase assertion counter
354 $this->assertEmpty($nonMatchingValues);
355 }
356
357 /**
358 * @param ResponseContent $responseContent
359 * @param string $tableName
360 * @param string $fieldName
361 * @param string|array $values
362 */
363 protected function assertResponseContentDoesNotHaveRecords(ResponseContent $responseContent, $tableName, $fieldName, $values) {
364 if (is_string($values)) {
365 $values = array($values);
366 }
367
368 $nonMatchingValues = $this->getNonMatchingValuesFrontendResponseRecords($responseContent->getRecords(), $tableName, $fieldName, $values);
369 $matchingValues = array_diff($values, $nonMatchingValues);
370
371 if (!empty($matchingValues)) {
372 $this->fail('Could not assert not having values for "' . $tableName . '.' . $fieldName . '": ' . implode(', ', $matchingValues));
373 }
374
375 // Increase assertion counter
376 $this->assertTrue(TRUE);
377 }
378
379 /**
380 * @param string|array $data
381 * @param string $tableName
382 * @param string $fieldName
383 * @param string|array $values
384 * @return array
385 */
386 protected function getNonMatchingValuesFrontendResponseRecords($data, $tableName, $fieldName, $values) {
387 if (empty($data) || !is_array($data)) {
388 $this->fail('Frontend Response data does not have any records');
389 }
390
391 if (is_string($values)) {
392 $values = array($values);
393 }
394
395 foreach ($data as $recordIdentifier => $recordData) {
396 if (strpos($recordIdentifier, $tableName . ':') !== 0) {
397 continue;
398 }
399
400 if (($foundValueIndex = array_search($recordData[$fieldName], $values)) !== FALSE) {
401 unset($values[$foundValueIndex]);
402 }
403 }
404
405 return $values;
406 }
407
408 }