[BUGFIX] Unnatural processing order in IRRE tests
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / Framework / ActionService.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework;
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\DataHandling\DataHandler;
28
29 /**
30 * DataHandler Actions
31 */
32 class ActionService {
33
34 /**
35 * @var DataHandler
36 */
37 protected $dataHandler;
38
39 /**
40 * @param DataHandler $dataHandler
41 */
42 public function __construct(DataHandler $dataHandler) {
43 $this->setDataHandler($dataHandler);
44 }
45
46 /**
47 * @param DataHandler $dataHandler
48 */
49 public function setDataHandler(DataHandler $dataHandler) {
50 $this->dataHandler = $dataHandler;
51 }
52
53 /**
54 * @return DataHandler
55 */
56 public function getDataHander() {
57 return $this->dataHandler;
58 }
59
60 /**
61 * @param string $tableName
62 * @param integer $pageId
63 * @param array $recordData
64 * @return array
65 */
66 public function createNewRecord($tableName, $pageId, array $recordData) {
67 return $this->createNewRecords($pageId, array($tableName => $recordData));
68 }
69
70 /**
71 * @param integer $pageId
72 * @param array $tableRecordData
73 * @return array
74 */
75 public function createNewRecords($pageId, array $tableRecordData) {
76 $dataMap = array();
77 $newTableIds = array();
78 $currentUid = NULL;
79 $previousTableName = NULL;
80 $previousUid = NULL;
81 foreach ($tableRecordData as $tableName => $recordData) {
82 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
83 $recordData['pid'] = $pageId;
84 $currentUid = uniqid('NEW');
85 $newTableIds[$tableName][] = $currentUid;
86 $dataMap[$tableName][$currentUid] = $recordData;
87 if ($previousTableName !== NULL && $previousUid !== NULL) {
88 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
89 $dataMap[$previousTableName][$previousUid],
90 $currentUid
91 );
92 }
93 $previousTableName = $tableName;
94 $previousUid = $currentUid;
95 }
96 $this->dataHandler->start($dataMap, array());
97 $this->dataHandler->process_datamap();
98
99 foreach ($newTableIds as $tableName => &$ids) {
100 foreach ($ids as &$id) {
101 if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
102 $id = $this->dataHandler->substNEWwithIDs[$id];
103 }
104 }
105 }
106
107 return $newTableIds;
108 }
109
110 /**
111 * @param string $tableName
112 * @param integer $uid
113 * @param array $recordData
114 * @param NULL|array $deleteTableRecordIds
115 */
116 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
117 $dataMap = array(
118 $tableName => array(
119 $uid => $recordData,
120 ),
121 );
122 $commandMap = array();
123 if (!empty($deleteTableRecordIds)) {
124 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
125 foreach ($recordIds as $recordId) {
126 $commandMap[$tableName][$recordId]['delete'] = TRUE;
127 }
128 }
129 }
130 $this->dataHandler->start($dataMap, $commandMap);
131 $this->dataHandler->process_datamap();
132 if (!empty($commandMap)) {
133 $this->dataHandler->process_cmdmap();
134 }
135 }
136
137 /**
138 * @param integer $pageId
139 * @param array $tableRecordData
140 */
141 public function modifyRecords($pageId, array $tableRecordData) {
142 $dataMap = array();
143 $currentUid = NULL;
144 $previousTableName = NULL;
145 $previousUid = NULL;
146 foreach ($tableRecordData as $tableName => $recordData) {
147 if (empty($recordData['uid'])) {
148 continue;
149 }
150 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
151 $currentUid = $recordData['uid'];
152 if ($recordData['uid'] === '__NEW') {
153 $recordData['pid'] = $pageId;
154 $currentUid = uniqid('NEW');
155 }
156 unset($recordData['uid']);
157 $dataMap[$tableName][$currentUid] = $recordData;
158 if ($previousTableName !== NULL && $previousUid !== NULL) {
159 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
160 $dataMap[$previousTableName][$previousUid],
161 $currentUid
162 );
163 }
164 $previousTableName = $tableName;
165 $previousUid = $currentUid;
166 }
167 $this->dataHandler->start($dataMap, array());
168 $this->dataHandler->process_datamap();
169 }
170
171 /**
172 * @param string $tableName
173 * @param integer $uid
174 * @return array
175 */
176 public function deleteRecord($tableName, $uid) {
177 return $this->deleteRecords(
178 array(
179 $tableName => array($uid),
180 )
181 );
182 }
183
184 /**
185 * @param array $tableRecordIds
186 * @return array
187 */
188 public function deleteRecords(array $tableRecordIds) {
189 $commandMap = array();
190 foreach ($tableRecordIds as $tableName => $ids) {
191 foreach ($ids as $uid) {
192 $commandMap[$tableName][$uid] = array(
193 'delete' => TRUE,
194 );
195 }
196 }
197 $this->dataHandler->start(array(), $commandMap);
198 $this->dataHandler->process_cmdmap();
199 // Deleting workspace records is actually a copy(!)
200 return $this->dataHandler->copyMappingArray;
201 }
202
203 /**
204 * @param string $tableName
205 * @param integer $uid
206 */
207 public function clearWorkspaceRecord($tableName, $uid) {
208 $this->clearWorkspaceRecords(
209 array(
210 $tableName => array($uid),
211 )
212 );
213 }
214
215 /**
216 * @param array $tableRecordIds
217 */
218 public function clearWorkspaceRecords(array $tableRecordIds) {
219 $commandMap = array();
220 foreach ($tableRecordIds as $tableName => $ids) {
221 foreach ($ids as $uid) {
222 $commandMap[$tableName][$uid] = array(
223 'version' => array(
224 'action' => 'clearWSID',
225 )
226 );
227 }
228 }
229 $this->dataHandler->start(array(), $commandMap);
230 $this->dataHandler->process_cmdmap();
231 }
232
233 /**
234 * @param string $tableName
235 * @param integer $uid
236 * @param integer $pageId
237 * @return array
238 */
239 public function copyRecord($tableName, $uid, $pageId) {
240 $commandMap = array(
241 $tableName => array(
242 $uid => array(
243 'copy' => $pageId,
244 ),
245 ),
246 );
247 $this->dataHandler->start(array(), $commandMap);
248 $this->dataHandler->process_cmdmap();
249 return $this->dataHandler->copyMappingArray;
250 }
251
252 /**
253 * @param string $tableName
254 * @param integer $uid
255 * @param integer $pageId
256 */
257 public function moveRecord($tableName, $uid, $pageId) {
258 $commandMap = array(
259 $tableName => array(
260 $uid => array(
261 'move' => $pageId,
262 ),
263 ),
264 );
265 $this->dataHandler->start(array(), $commandMap);
266 $this->dataHandler->process_cmdmap();
267 }
268
269 /**
270 * @param string $tableName
271 * @param integer $uid
272 * @param integer $languageId
273 * @return array
274 */
275 public function localizeRecord($tableName, $uid, $languageId) {
276 $commandMap = array(
277 $tableName => array(
278 $uid => array(
279 'localize' => $languageId,
280 ),
281 ),
282 );
283 $this->dataHandler->start(array(), $commandMap);
284 $this->dataHandler->process_cmdmap();
285 return $this->dataHandler->copyMappingArray;
286 }
287
288 /**
289 * @param string $tableName
290 * @param integer $uid
291 * @param string $fieldName
292 * @param array $referenceIds
293 */
294 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
295 $dataMap = array(
296 $tableName => array(
297 $uid => array(
298 $fieldName => implode(',', $referenceIds),
299 ),
300 )
301 );
302 $this->dataHandler->start($dataMap, array());
303 $this->dataHandler->process_datamap();
304 }
305
306 /**
307 * @param string $tableName
308 * @param integer $uid
309 * @param string $fieldName
310 * @param integer $referenceId
311 */
312 public function addReference($tableName, $uid, $fieldName, $referenceId) {
313 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
314
315 if (!in_array($referenceId, $recordValues)) {
316 $recordValues[] = $referenceId;
317 }
318
319 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
320 }
321
322 /**
323 * @param string $tableName
324 * @param integer $uid
325 * @param string $fieldName
326 * @param integer $referenceId
327 */
328 public function deleteReference($tableName, $uid, $fieldName, $referenceId) {
329 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
330
331 if (($index = array_search($referenceId, $recordValues)) !== FALSE) {
332 unset($recordValues[$index]);
333 }
334
335 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
336 }
337
338 /**
339 * @param array $recordData
340 * @param NULL|string|int $previousUid
341 * @return array
342 */
343 protected function resolvePreviousUid(array $recordData, $previousUid) {
344 if ($previousUid === NULL) {
345 return $recordData;
346 }
347 foreach ($recordData as $fieldName => $fieldValue) {
348 if (strpos($fieldValue, '__previousUid') === FALSE) {
349 continue;
350 }
351 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
352 }
353 return $recordData;
354 }
355
356 /**
357 * @param array $recordData
358 * @param NULL|string|int $nextUid
359 * @return array
360 */
361 protected function resolveNextUid(array $recordData, $nextUid) {
362 if ($nextUid === NULL) {
363 return $recordData;
364 }
365 foreach ($recordData as $fieldName => $fieldValue) {
366 if (strpos($fieldValue, '__nextUid') === FALSE) {
367 continue;
368 }
369 $recordData[$fieldName] = str_replace('__nextUid', $nextUid, $fieldValue);
370 }
371 return $recordData;
372 }
373
374 /**
375 * @param string $tableName
376 * @param integer $uid
377 * @param string $fieldName
378 * @return array
379 */
380 protected function getRecordValues($tableName, $uid, $fieldName) {
381 $recordValues = array();
382
383 $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
384 if (!empty($recordValue)) {
385 $recordValues = explode(',', $recordValues);
386 }
387
388 return $recordValues;
389 }
390
391 /**
392 * @param string $tableName
393 * @param integer $uid
394 * @param string $fieldName
395 * @return bool|string|NULL
396 */
397 protected function getRecordValue($tableName, $uid, $fieldName) {
398 $recordValue = FALSE;
399
400 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
401 $fieldName, $tableName, 'uid=' . (int)$uid
402 );
403
404 if (isset($record[$fieldName])) {
405 $recordValue = $record[$fieldName];
406 }
407
408 return $recordValue;
409 }
410
411 /**
412 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
413 */
414 protected function getDatabaseConnection() {
415 return $GLOBALS['TYPO3_DB'];
416 }
417
418 }