[BUGFIX] Discard vs. Delete workspace changes in 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 foreach ($tableRecordData as $tableName => $recordData) {
80 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
81 $recordData['pid'] = $pageId;
82 $currentUid = uniqid('NEW');
83 $newTableIds[$tableName][] = $currentUid;
84 $dataMap[$tableName][$currentUid] = $recordData;
85 }
86 $this->dataHandler->start($dataMap, array());
87 $this->dataHandler->process_datamap();
88
89 foreach ($newTableIds as $tableName => &$ids) {
90 foreach ($ids as &$id) {
91 if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
92 $id = $this->dataHandler->substNEWwithIDs[$id];
93 }
94 }
95 }
96
97 return $newTableIds;
98 }
99
100 /**
101 * @param string $tableName
102 * @param integer $uid
103 * @param array $recordData
104 * @param NULL|array $deleteTableRecordIds
105 */
106 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
107 $dataMap = array(
108 $tableName => array(
109 $uid => $recordData,
110 ),
111 );
112 $commandMap = array();
113 if (!empty($deleteTableRecordIds)) {
114 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
115 foreach ($recordIds as $recordId) {
116 $commandMap[$tableName][$recordId]['delete'] = TRUE;
117 }
118 }
119 }
120 $this->dataHandler->start($dataMap, $commandMap);
121 $this->dataHandler->process_datamap();
122 if (!empty($commandMap)) {
123 $this->dataHandler->process_cmdmap();
124 }
125 }
126
127 /**
128 * @param integer $pageId
129 * @param array $tableRecordData
130 */
131 public function modifyRecords($pageId, array $tableRecordData) {
132 $dataMap = array();
133 $currentUid = NULL;
134 foreach ($tableRecordData as $tableName => $recordData) {
135 if (empty($recordData['uid'])) {
136 continue;
137 }
138 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
139 $currentUid = $recordData['uid'];
140 if ($recordData['uid'] === '__NEW') {
141 $recordData['pid'] = $pageId;
142 $currentUid = uniqid('NEW');
143 }
144 unset($recordData['uid']);
145 $dataMap[$tableName][$currentUid] = $recordData;
146 }
147 $this->dataHandler->start($dataMap, array());
148 $this->dataHandler->process_datamap();
149 }
150
151 /**
152 * @param string $tableName
153 * @param integer $uid
154 * @return array
155 */
156 public function deleteRecord($tableName, $uid) {
157 return $this->deleteRecords(
158 array(
159 $tableName => array($uid),
160 )
161 );
162 }
163
164 /**
165 * @param array $tableRecordIds
166 * @return array
167 */
168 public function deleteRecords(array $tableRecordIds) {
169 $commandMap = array();
170 foreach ($tableRecordIds as $tableName => $ids) {
171 foreach ($ids as $uid) {
172 $commandMap[$tableName][$uid] = array(
173 'delete' => TRUE,
174 );
175 }
176 }
177 $this->dataHandler->start(array(), $commandMap);
178 $this->dataHandler->process_cmdmap();
179 // Deleting workspace records is actually a copy(!)
180 return $this->dataHandler->copyMappingArray;
181 }
182
183 /**
184 * @param string $tableName
185 * @param integer $uid
186 */
187 public function clearWorkspaceRecord($tableName, $uid) {
188 $this->clearWorkspaceRecords(
189 array(
190 $tableName => array($uid),
191 )
192 );
193 }
194
195 /**
196 * @param array $tableRecordIds
197 */
198 public function clearWorkspaceRecords(array $tableRecordIds) {
199 $commandMap = array();
200 foreach ($tableRecordIds as $tableName => $ids) {
201 foreach ($ids as $uid) {
202 $commandMap[$tableName][$uid] = array(
203 'version' => array(
204 'action' => 'clearWSID',
205 )
206 );
207 }
208 }
209 $this->dataHandler->start(array(), $commandMap);
210 $this->dataHandler->process_cmdmap();
211 }
212
213 /**
214 * @param string $tableName
215 * @param integer $uid
216 * @param integer $pageId
217 * @return array
218 */
219 public function copyRecord($tableName, $uid, $pageId) {
220 $commandMap = array(
221 $tableName => array(
222 $uid => array(
223 'copy' => $pageId,
224 ),
225 ),
226 );
227 $this->dataHandler->start(array(), $commandMap);
228 $this->dataHandler->process_cmdmap();
229 return $this->dataHandler->copyMappingArray;
230 }
231
232 /**
233 * @param string $tableName
234 * @param integer $uid
235 * @param integer $pageId
236 */
237 public function moveRecord($tableName, $uid, $pageId) {
238 $commandMap = array(
239 $tableName => array(
240 $uid => array(
241 'move' => $pageId,
242 ),
243 ),
244 );
245 $this->dataHandler->start(array(), $commandMap);
246 $this->dataHandler->process_cmdmap();
247 }
248
249 /**
250 * @param string $tableName
251 * @param integer $uid
252 * @param integer $languageId
253 * @return array
254 */
255 public function localizeRecord($tableName, $uid, $languageId) {
256 $commandMap = array(
257 $tableName => array(
258 $uid => array(
259 'localize' => $languageId,
260 ),
261 ),
262 );
263 $this->dataHandler->start(array(), $commandMap);
264 $this->dataHandler->process_cmdmap();
265 return $this->dataHandler->copyMappingArray;
266 }
267
268 /**
269 * @param string $tableName
270 * @param integer $uid
271 * @param string $fieldName
272 * @param array $referenceIds
273 */
274 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
275 $dataMap = array(
276 $tableName => array(
277 $uid => array(
278 $fieldName => implode(',', $referenceIds),
279 ),
280 )
281 );
282 $this->dataHandler->start($dataMap, array());
283 $this->dataHandler->process_datamap();
284 }
285
286 /**
287 * @param string $tableName
288 * @param integer $uid
289 * @param string $fieldName
290 * @param integer $referenceId
291 */
292 public function addReference($tableName, $uid, $fieldName, $referenceId) {
293 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
294
295 if (!in_array($referenceId, $recordValues)) {
296 $recordValues[] = $referenceId;
297 }
298
299 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
300 }
301
302 /**
303 * @param string $tableName
304 * @param integer $uid
305 * @param string $fieldName
306 * @param integer $referenceId
307 */
308 public function deleteReference($tableName, $uid, $fieldName, $referenceId) {
309 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
310
311 if (($index = array_search($referenceId, $recordValues)) !== FALSE) {
312 unset($recordValues[$index]);
313 }
314
315 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
316 }
317
318 /**
319 * @param array $recordData
320 * @param NULL|int $previousUid
321 * @return array
322 */
323 protected function resolvePreviousUid(array $recordData, $previousUid) {
324 if ($previousUid === NULL) {
325 return $recordData;
326 }
327 foreach ($recordData as $fieldName => $fieldValue) {
328 if (strpos($fieldValue, '__previousUid') === FALSE) {
329 continue;
330 }
331 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
332 }
333 return $recordData;
334 }
335
336 /**
337 * @param string $tableName
338 * @param integer $uid
339 * @param string $fieldName
340 * @return array
341 */
342 protected function getRecordValues($tableName, $uid, $fieldName) {
343 $recordValues = array();
344
345 $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
346 if (!empty($recordValue)) {
347 $recordValues = explode(',', $recordValues);
348 }
349
350 return $recordValues;
351 }
352
353 /**
354 * @param string $tableName
355 * @param integer $uid
356 * @param string $fieldName
357 * @return bool|string|NULL
358 */
359 protected function getRecordValue($tableName, $uid, $fieldName) {
360 $recordValue = FALSE;
361
362 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
363 $fieldName, $tableName, 'uid=' . (int)$uid
364 );
365
366 if (isset($record[$fieldName])) {
367 $recordValue = $record[$fieldName];
368 }
369
370 return $recordValue;
371 }
372
373 /**
374 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
375 */
376 protected function getDatabaseConnection() {
377 return $GLOBALS['TYPO3_DB'];
378 }
379
380 }