[BUGFIX] Use clean DataHandler for each functional test action
[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 * @return DataHandler
41 */
42 public function getDataHander() {
43 return $this->dataHandler;
44 }
45
46 /**
47 * @param string $tableName
48 * @param integer $pageId
49 * @param array $recordData
50 * @return array
51 */
52 public function createNewRecord($tableName, $pageId, array $recordData) {
53 return $this->createNewRecords($pageId, array($tableName => $recordData));
54 }
55
56 /**
57 * @param integer $pageId
58 * @param array $tableRecordData
59 * @return array
60 */
61 public function createNewRecords($pageId, array $tableRecordData) {
62 $dataMap = array();
63 $newTableIds = array();
64 $currentUid = NULL;
65 $previousTableName = NULL;
66 $previousUid = NULL;
67 foreach ($tableRecordData as $tableName => $recordData) {
68 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
69 $recordData['pid'] = $pageId;
70 $currentUid = uniqid('NEW');
71 $newTableIds[$tableName][] = $currentUid;
72 $dataMap[$tableName][$currentUid] = $recordData;
73 if ($previousTableName !== NULL && $previousUid !== NULL) {
74 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
75 $dataMap[$previousTableName][$previousUid],
76 $currentUid
77 );
78 }
79 $previousTableName = $tableName;
80 $previousUid = $currentUid;
81 }
82 $this->createDataHandler();
83 $this->dataHandler->start($dataMap, array());
84 $this->dataHandler->process_datamap();
85
86 foreach ($newTableIds as $tableName => &$ids) {
87 foreach ($ids as &$id) {
88 if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
89 $id = $this->dataHandler->substNEWwithIDs[$id];
90 }
91 }
92 }
93
94 return $newTableIds;
95 }
96
97 /**
98 * @param string $tableName
99 * @param integer $uid
100 * @param array $recordData
101 * @param NULL|array $deleteTableRecordIds
102 */
103 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
104 $dataMap = array(
105 $tableName => array(
106 $uid => $recordData,
107 ),
108 );
109 $commandMap = array();
110 if (!empty($deleteTableRecordIds)) {
111 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
112 foreach ($recordIds as $recordId) {
113 $commandMap[$tableName][$recordId]['delete'] = TRUE;
114 }
115 }
116 }
117 $this->createDataHandler();
118 $this->dataHandler->start($dataMap, $commandMap);
119 $this->dataHandler->process_datamap();
120 if (!empty($commandMap)) {
121 $this->dataHandler->process_cmdmap();
122 }
123 }
124
125 /**
126 * @param integer $pageId
127 * @param array $tableRecordData
128 */
129 public function modifyRecords($pageId, array $tableRecordData) {
130 $dataMap = array();
131 $currentUid = NULL;
132 $previousTableName = NULL;
133 $previousUid = 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 if ($previousTableName !== NULL && $previousUid !== NULL) {
147 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
148 $dataMap[$previousTableName][$previousUid],
149 $currentUid
150 );
151 }
152 $previousTableName = $tableName;
153 $previousUid = $currentUid;
154 }
155 $this->createDataHandler();
156 $this->dataHandler->start($dataMap, array());
157 $this->dataHandler->process_datamap();
158 }
159
160 /**
161 * @param string $tableName
162 * @param integer $uid
163 * @return array
164 */
165 public function deleteRecord($tableName, $uid) {
166 return $this->deleteRecords(
167 array(
168 $tableName => array($uid),
169 )
170 );
171 }
172
173 /**
174 * @param array $tableRecordIds
175 * @return array
176 */
177 public function deleteRecords(array $tableRecordIds) {
178 $commandMap = array();
179 foreach ($tableRecordIds as $tableName => $ids) {
180 foreach ($ids as $uid) {
181 $commandMap[$tableName][$uid] = array(
182 'delete' => TRUE,
183 );
184 }
185 }
186 $this->createDataHandler();
187 $this->dataHandler->start(array(), $commandMap);
188 $this->dataHandler->process_cmdmap();
189 // Deleting workspace records is actually a copy(!)
190 return $this->dataHandler->copyMappingArray;
191 }
192
193 /**
194 * @param string $tableName
195 * @param integer $uid
196 */
197 public function clearWorkspaceRecord($tableName, $uid) {
198 $this->clearWorkspaceRecords(
199 array(
200 $tableName => array($uid),
201 )
202 );
203 }
204
205 /**
206 * @param array $tableRecordIds
207 */
208 public function clearWorkspaceRecords(array $tableRecordIds) {
209 $commandMap = array();
210 foreach ($tableRecordIds as $tableName => $ids) {
211 foreach ($ids as $uid) {
212 $commandMap[$tableName][$uid] = array(
213 'version' => array(
214 'action' => 'clearWSID',
215 )
216 );
217 }
218 }
219 $this->createDataHandler();
220 $this->dataHandler->start(array(), $commandMap);
221 $this->dataHandler->process_cmdmap();
222 }
223
224 /**
225 * @param string $tableName
226 * @param integer $uid
227 * @param integer $pageId
228 * @return array
229 */
230 public function copyRecord($tableName, $uid, $pageId) {
231 $commandMap = array(
232 $tableName => array(
233 $uid => array(
234 'copy' => $pageId,
235 ),
236 ),
237 );
238 $this->createDataHandler();
239 $this->dataHandler->start(array(), $commandMap);
240 $this->dataHandler->process_cmdmap();
241 return $this->dataHandler->copyMappingArray;
242 }
243
244 /**
245 * @param string $tableName
246 * @param integer $uid
247 * @param integer $pageId
248 */
249 public function moveRecord($tableName, $uid, $pageId) {
250 $commandMap = array(
251 $tableName => array(
252 $uid => array(
253 'move' => $pageId,
254 ),
255 ),
256 );
257 $this->createDataHandler();
258 $this->dataHandler->start(array(), $commandMap);
259 $this->dataHandler->process_cmdmap();
260 }
261
262 /**
263 * @param string $tableName
264 * @param integer $uid
265 * @param integer $languageId
266 * @return array
267 */
268 public function localizeRecord($tableName, $uid, $languageId) {
269 $commandMap = array(
270 $tableName => array(
271 $uid => array(
272 'localize' => $languageId,
273 ),
274 ),
275 );
276 $this->createDataHandler();
277 $this->dataHandler->start(array(), $commandMap);
278 $this->dataHandler->process_cmdmap();
279 return $this->dataHandler->copyMappingArray;
280 }
281
282 /**
283 * @param string $tableName
284 * @param integer $uid
285 * @param string $fieldName
286 * @param array $referenceIds
287 */
288 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
289 $dataMap = array(
290 $tableName => array(
291 $uid => array(
292 $fieldName => implode(',', $referenceIds),
293 ),
294 )
295 );
296 $this->createDataHandler();
297 $this->dataHandler->start($dataMap, array());
298 $this->dataHandler->process_datamap();
299 }
300
301 /**
302 * @param string $tableName
303 * @param int $liveUid
304 * @param bool $throwException
305 */
306 public function publishRecord($tableName, $liveUid, $throwException = TRUE) {
307 $this->publishRecords(array($tableName => array($liveUid)), $throwException);
308 }
309
310 /**
311 * @param array $tableLiveUids
312 * @param bool $throwException
313 * @throws \TYPO3\CMS\Core\Tests\Exception
314 */
315 public function publishRecords(array $tableLiveUids, $throwException = TRUE) {
316 $commandMap = array();
317 foreach ($tableLiveUids as $tableName => $liveUids) {
318 foreach ($liveUids as $liveUid) {
319 $versionedUid = $this->getVersionedId($tableName, $liveUid);
320 if (empty($versionedUid)) {
321 if ($throwException) {
322 throw new \TYPO3\CMS\Core\Tests\Exception('Versioned UID could not be determined');
323 } else {
324 continue;
325 }
326 }
327
328 $commandMap[$tableName][$liveUid] = array(
329 'version' => array(
330 'action' => 'swap',
331 'swapWith' => $versionedUid,
332 'notificationAlternativeRecipients' => array(),
333 ),
334 );
335 }
336 }
337 $this->createDataHandler();
338 $this->dataHandler->start(array(), $commandMap);
339 $this->dataHandler->process_cmdmap();
340 }
341
342 /**
343 * @param int $workspaceId
344 */
345 public function publishWorkspace($workspaceId) {
346 $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, FALSE);
347 $this->createDataHandler();
348 $this->dataHandler->start(array(), $commandMap);
349 $this->dataHandler->process_cmdmap();
350 }
351
352 /**
353 * @param int $workspaceId
354 */
355 public function swapWorkspace($workspaceId) {
356 $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, TRUE);
357 $this->createDataHandler();
358 $this->dataHandler->start(array(), $commandMap);
359 $this->dataHandler->process_cmdmap();
360 }
361
362 /**
363 * @param array $recordData
364 * @param NULL|string|int $previousUid
365 * @return array
366 */
367 protected function resolvePreviousUid(array $recordData, $previousUid) {
368 if ($previousUid === NULL) {
369 return $recordData;
370 }
371 foreach ($recordData as $fieldName => $fieldValue) {
372 if (strpos($fieldValue, '__previousUid') === FALSE) {
373 continue;
374 }
375 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
376 }
377 return $recordData;
378 }
379
380 /**
381 * @param array $recordData
382 * @param NULL|string|int $nextUid
383 * @return array
384 */
385 protected function resolveNextUid(array $recordData, $nextUid) {
386 if ($nextUid === NULL) {
387 return $recordData;
388 }
389 foreach ($recordData as $fieldName => $fieldValue) {
390 if (strpos($fieldValue, '__nextUid') === FALSE) {
391 continue;
392 }
393 $recordData[$fieldName] = str_replace('__nextUid', $nextUid, $fieldValue);
394 }
395 return $recordData;
396 }
397
398 /**
399 * @param string $tableName
400 * @param int $liveUid
401 * @param bool $useDeleteClause
402 * @return NULL|int
403 */
404 protected function getVersionedId($tableName, $liveUid, $useDeleteClause = FALSE) {
405 $versionedId = NULL;
406 $liveUid = (int)$liveUid;
407 $workspaceId = (int)$this->getBackendUser()->workspace;
408 $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
409 'uid',
410 $tableName,
411 'pid=-1 AND t3ver_oid=' . $liveUid . ' AND t3ver_wsid=' . $workspaceId .
412 ($useDeleteClause ? \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tableName) : '')
413 );
414 if (!empty($row['uid'])) {
415 $versionedId = (int)$row['uid'];
416 }
417 return $versionedId;
418 }
419
420 /**
421 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
422 */
423 protected function createDataHandler() {
424 $dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
425 'TYPO3\\CMS\\Core\\DataHandling\\DataHandler'
426 );
427 $this->dataHandler = $dataHandler;
428 return $dataHandler;
429 }
430
431 /**
432 * @return \TYPO3\CMS\Workspaces\Service\WorkspaceService
433 */
434 protected function getWorkspaceService() {
435 return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
436 'TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService'
437 );
438 }
439
440 /**
441 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
442 */
443 protected function getBackendUser() {
444 return $GLOBALS['BE_USER'];
445 }
446
447 /**
448 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
449 */
450 protected function getDatabaseConnection() {
451 return $GLOBALS['TYPO3_DB'];
452 }
453
454 }