[TASK] Streamline DataHandler functional tests
[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
30 /**
31 * Functional test for the DataHandler
32 */
33 abstract class AbstractDataHandlerActionTestCase extends \TYPO3\CMS\Core\Tests\FunctionalTestCase {
34
35 /**
36 * @var string
37 */
38 protected $dataSetDirectory;
39
40 /**
41 * @var array
42 */
43 protected $testExtensionsToLoad = array(
44 'typo3/sysext/core/Tests/Functional/Fixtures/Extensions/irre_tutorial',
45 // 'typo3conf/ext/datahandler',
46 );
47
48 /**
49 * @var array
50 */
51 protected $pathsToLinkInTestInstance = array(
52 'typo3/sysext/core/Tests/Functional/Fixtures/Frontend/extTables.php' => 'typo3conf/extTables.php',
53 );
54
55 /**
56 * @var \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\ActionService
57 */
58 protected $actionService;
59
60 /**
61 * @var \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
62 */
63 protected $backendUser;
64
65 public function setUp() {
66 parent::setUp();
67
68 $this->backendUser = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Authentication\\BackendUserAuthentication');
69 $this->backendUser->user['admin'] = 1;
70 $this->backendUser->user['uid'] = 1;
71 // By default make tests on live workspace
72 $this->backendUser->workspace = 0;
73 $GLOBALS['BE_USER'] = $this->backendUser;
74
75 $this->actionService = $this->getActionService();
76
77 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()->initializeLanguageObject();
78 }
79
80 public function tearDown() {
81 unset($this->actionService);
82 }
83
84 /**
85 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
86 */
87 protected function getDataHandler() {
88 $dataHandler = GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
89 return $dataHandler;
90 }
91
92 /**
93 * @return \TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework\ActionService
94 */
95 protected function getActionService() {
96 return GeneralUtility::makeInstance(
97 'TYPO3\\CMS\\Core\\Tests\\Functional\\DataHandling\\Framework\\ActionService',
98 $this->getDataHandler()
99 );
100 }
101
102 /**
103 * @param string $dataSetName
104 */
105 protected function importScenarioDataSet($dataSetName) {
106 $fileName = rtrim($this->dataSetDirectory, '/') . '/Scenario/' . $dataSetName . '.csv';
107 $fileName = GeneralUtility::getFileAbsFileName($fileName);
108
109 $dataSet = DataSet::read($fileName);
110
111 foreach ($dataSet->getTableNames() as $tableName) {
112 foreach ($dataSet->getElements($tableName) as $element) {
113 $this->getDatabase()->exec_INSERTquery(
114 $tableName,
115 $element
116 );
117 $sqlError = $this->getDatabase()->sql_error();
118 if (!empty($sqlError)) {
119 $this->fail('SQL Error for table "' . $tableName . '": ' . LF . $sqlError);
120 }
121 }
122 }
123 }
124
125 protected function assertAssertionDataSet($dataSetName) {
126 $fileName = rtrim($this->dataSetDirectory, '/') . '/Assertion/' . $dataSetName . '.csv';
127 $fileName = GeneralUtility::getFileAbsFileName($fileName);
128
129 $dataSet = DataSet::read($fileName);
130
131 foreach ($dataSet->getTableNames() as $tableName) {
132 $hasUidField = ($dataSet->getIdIndex($tableName) !== NULL);
133 $records = $this->getAllRecords($tableName, $hasUidField);
134 foreach ($dataSet->getElements($tableName) as $assertion) {
135 $result = $this->assertInRecords($assertion, $records);
136 if ($result === FALSE) {
137 if ($hasUidField && empty($records[$assertion['uid']])) {
138 $this->fail('Record "' . $tableName . ':' . $assertion['uid'] . '" not found in database');
139 }
140 $recordIdentifier = $tableName . ($hasUidField ? ':' . $assertion['uid'] : '');
141 $additionalInformation = ($hasUidField ? $this->renderRecords($assertion, $records[$assertion['uid']]) : $this->arrayToString($assertion));
142 $this->fail('Assertion in data-set failed for "' . $recordIdentifier . '":' . LF . $additionalInformation);
143 } else {
144 // Unset asserted record
145 unset($records[$result]);
146 // Increase assertion counter
147 $this->assertTrue($result !== FALSE);
148 }
149 }
150 }
151 }
152
153 /**
154 * @param array $assertion
155 * @param array $records
156 * @return bool|int|string
157 */
158 protected function assertInRecords(array $assertion, array $records) {
159 foreach ($records as $index => $record) {
160 $differentFields = $this->getDifferentFields($assertion, $record);
161
162 if (empty($differentFields)) {
163 return $index;
164 }
165 }
166
167 return FALSE;
168 }
169
170 /**
171 * @param string $tableName
172 * @param bool $hasUidField
173 * @return array
174 */
175 protected function getAllRecords($tableName, $hasUidField = FALSE) {
176 $allRecords = array();
177
178 $records = $this->getDatabase()->exec_SELECTgetRows(
179 '*',
180 $tableName,
181 '1=1',
182 '',
183 '',
184 '',
185 ($hasUidField ? 'uid' : '')
186 );
187
188 if (!empty($records)) {
189 $allRecords = $records;
190 }
191
192 return $allRecords;
193 }
194
195 /**
196 * @param array $array
197 * @return string
198 */
199 protected function arrayToString(array $array) {
200 $elements = array();
201 foreach ($array as $key => $value) {
202 if (is_array($value)) {
203 $value = $this->arrayToString($value);
204 }
205 $elements[] = "'" . $key . "' => '" . $value . "'";
206 }
207 return 'array(' . implode(', ', $elements) . ')';
208 }
209
210 /**
211 * @param array $assertion
212 * @param array $record
213 * @return string
214 */
215 protected function renderRecords(array $assertion, array $record) {
216 $differentFields = $this->getDifferentFields($assertion, $record);
217 $columns = array(
218 'fields' => array('Fields'),
219 'assertion' => array('Assertion'),
220 'record' => array('Record'),
221 );
222 $lines = array();
223 $result = '';
224
225 foreach ($differentFields as $differentField) {
226 $columns['fields'][] = $differentField;
227 $columns['assertion'][] = ($assertion[$differentField] === NULL ? 'NULL' : $assertion[$differentField]);
228 $columns['record'][] = ($record[$differentField] === NULL ? 'NULL' : $record[$differentField]);
229 }
230
231 foreach ($columns as $columnIndex => $column) {
232 $columnLength = NULL;
233 foreach ($column as $value) {
234 $valueLength = strlen($value);
235 if (empty($columnLength) || $valueLength > $columnLength) {
236 $columnLength = $valueLength;
237 }
238 }
239 foreach ($column as $valueIndex => $value) {
240 $lines[$valueIndex][$columnIndex] = str_pad($value, $columnLength, ' ');
241 }
242 }
243
244 foreach ($lines as $line) {
245 $result .= implode('|', $line) . PHP_EOL;
246 }
247
248 return $result;
249 }
250
251 /**
252 * @param array $assertion
253 * @param array $record
254 * @return array
255 */
256 protected function getDifferentFields(array $assertion, array $record) {
257 $differentFields = array();
258
259 foreach ($assertion as $field => $value) {
260 if (strpos($value, '\\*') === 0) {
261 continue;
262 } elseif ((string)$record[$field] !== (string)$value) {
263 $differentFields[] = $field;
264 }
265 }
266
267 return $differentFields;
268 }
269
270 }