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