2 namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\Framework
;
4 /***************************************************************
7 * (c) 2014 Oliver Hader <oliver.hader@typo3.org>
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.
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
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.
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
27 use TYPO3\CMS\Core\DataHandling\DataHandler
;
37 protected $dataHandler;
42 public function getDataHander() {
43 return $this->dataHandler
;
47 * @param string $tableName
48 * @param integer $pageId
49 * @param array $recordData
52 public function createNewRecord($tableName, $pageId, array $recordData) {
53 return $this->createNewRecords($pageId, array($tableName => $recordData));
57 * @param integer $pageId
58 * @param array $tableRecordData
61 public function createNewRecords($pageId, array $tableRecordData) {
63 $newTableIds = array();
65 $previousTableName = NULL;
67 foreach ($tableRecordData as $tableName => $recordData) {
68 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
69 if (!isset($recordData['pid'])) {
70 $recordData['pid'] = $pageId;
72 $currentUid = uniqid('NEW', TRUE);
73 $newTableIds[$tableName][] = $currentUid;
74 $dataMap[$tableName][$currentUid] = $recordData;
75 if ($previousTableName !== NULL && $previousUid !== NULL) {
76 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
77 $dataMap[$previousTableName][$previousUid],
81 $previousTableName = $tableName;
82 $previousUid = $currentUid;
84 $this->createDataHandler();
85 $this->dataHandler
->start($dataMap, array());
86 $this->dataHandler
->process_datamap();
88 foreach ($newTableIds as $tableName => &$ids) {
89 foreach ($ids as &$id) {
90 if (!empty($this->dataHandler
->substNEWwithIDs
[$id])) {
91 $id = $this->dataHandler
->substNEWwithIDs
[$id];
100 * @param string $tableName
101 * @param integer $uid
102 * @param array $recordData
103 * @param NULL|array $deleteTableRecordIds
105 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
111 $commandMap = array();
112 if (!empty($deleteTableRecordIds)) {
113 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
114 foreach ($recordIds as $recordId) {
115 $commandMap[$tableName][$recordId]['delete'] = TRUE;
119 $this->createDataHandler();
120 $this->dataHandler
->start($dataMap, $commandMap);
121 $this->dataHandler
->process_datamap();
122 if (!empty($commandMap)) {
123 $this->dataHandler
->process_cmdmap();
128 * @param integer $pageId
129 * @param array $tableRecordData
131 public function modifyRecords($pageId, array $tableRecordData) {
134 $previousTableName = NULL;
136 foreach ($tableRecordData as $tableName => $recordData) {
137 if (empty($recordData['uid'])) {
140 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
141 $currentUid = $recordData['uid'];
142 if ($recordData['uid'] === '__NEW') {
143 $recordData['pid'] = $pageId;
144 $currentUid = uniqid('NEW');
146 unset($recordData['uid']);
147 $dataMap[$tableName][$currentUid] = $recordData;
148 if ($previousTableName !== NULL && $previousUid !== NULL) {
149 $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
150 $dataMap[$previousTableName][$previousUid],
154 $previousTableName = $tableName;
155 $previousUid = $currentUid;
157 $this->createDataHandler();
158 $this->dataHandler
->start($dataMap, array());
159 $this->dataHandler
->process_datamap();
163 * @param string $tableName
164 * @param integer $uid
167 public function deleteRecord($tableName, $uid) {
168 return $this->deleteRecords(
170 $tableName => array($uid),
176 * @param array $tableRecordIds
179 public function deleteRecords(array $tableRecordIds) {
180 $commandMap = array();
181 foreach ($tableRecordIds as $tableName => $ids) {
182 foreach ($ids as $uid) {
183 $commandMap[$tableName][$uid] = array(
188 $this->createDataHandler();
189 $this->dataHandler
->start(array(), $commandMap);
190 $this->dataHandler
->process_cmdmap();
191 // Deleting workspace records is actually a copy(!)
192 return $this->dataHandler
->copyMappingArray
;
196 * @param string $tableName
197 * @param integer $uid
199 public function clearWorkspaceRecord($tableName, $uid) {
200 $this->clearWorkspaceRecords(
202 $tableName => array($uid),
208 * @param array $tableRecordIds
210 public function clearWorkspaceRecords(array $tableRecordIds) {
211 $commandMap = array();
212 foreach ($tableRecordIds as $tableName => $ids) {
213 foreach ($ids as $uid) {
214 $commandMap[$tableName][$uid] = array(
216 'action' => 'clearWSID',
221 $this->createDataHandler();
222 $this->dataHandler
->start(array(), $commandMap);
223 $this->dataHandler
->process_cmdmap();
227 * @param string $tableName
228 * @param integer $uid
229 * @param integer $pageId
230 * @param NULL|array $recordData
233 public function copyRecord($tableName, $uid, $pageId, array $recordData = NULL) {
241 if ($recordData !== NULL) {
242 $commandMap[$tableName][$uid]['copy'] = array(
245 'update' => $recordData,
248 $this->createDataHandler();
249 $this->dataHandler
->start(array(), $commandMap);
250 $this->dataHandler
->process_cmdmap();
251 return $this->dataHandler
->copyMappingArray
;
255 * @param string $tableName
256 * @param integer $uid
257 * @param integer $pageId
258 * @param NULL|array $recordData
260 public function moveRecord($tableName, $uid, $pageId, array $recordData = NULL) {
268 if ($recordData !== NULL) {
269 $commandMap[$tableName][$uid]['move'] = array(
272 'update' => $recordData,
275 $this->createDataHandler();
276 $this->dataHandler
->start(array(), $commandMap);
277 $this->dataHandler
->process_cmdmap();
281 * @param string $tableName
282 * @param integer $uid
283 * @param integer $languageId
286 public function localizeRecord($tableName, $uid, $languageId) {
290 'localize' => $languageId,
294 $this->createDataHandler();
295 $this->dataHandler
->start(array(), $commandMap);
296 $this->dataHandler
->process_cmdmap();
297 return $this->dataHandler
->copyMappingArray
;
301 * @param string $tableName
302 * @param integer $uid
303 * @param string $fieldName
304 * @param array $referenceIds
306 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
310 $fieldName => implode(',', $referenceIds),
314 $this->createDataHandler();
315 $this->dataHandler
->start($dataMap, array());
316 $this->dataHandler
->process_datamap();
320 * @param string $tableName
321 * @param int $liveUid
322 * @param bool $throwException
324 public function publishRecord($tableName, $liveUid, $throwException = TRUE) {
325 $this->publishRecords(array($tableName => array($liveUid)), $throwException);
329 * @param array $tableLiveUids
330 * @param bool $throwException
331 * @throws \TYPO3\CMS\Core\Tests\Exception
333 public function publishRecords(array $tableLiveUids, $throwException = TRUE) {
334 $commandMap = array();
335 foreach ($tableLiveUids as $tableName => $liveUids) {
336 foreach ($liveUids as $liveUid) {
337 $versionedUid = $this->getVersionedId($tableName, $liveUid);
338 if (empty($versionedUid)) {
339 if ($throwException) {
340 throw new \TYPO3\CMS\Core\Tests\
Exception('Versioned UID could not be determined');
346 $commandMap[$tableName][$liveUid] = array(
349 'swapWith' => $versionedUid,
350 'notificationAlternativeRecipients' => array(),
355 $this->createDataHandler();
356 $this->dataHandler
->start(array(), $commandMap);
357 $this->dataHandler
->process_cmdmap();
361 * @param int $workspaceId
363 public function publishWorkspace($workspaceId) {
364 $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, FALSE);
365 $this->createDataHandler();
366 $this->dataHandler
->start(array(), $commandMap);
367 $this->dataHandler
->process_cmdmap();
371 * @param int $workspaceId
373 public function swapWorkspace($workspaceId) {
374 $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, TRUE);
375 $this->createDataHandler();
376 $this->dataHandler
->start(array(), $commandMap);
377 $this->dataHandler
->process_cmdmap();
381 * @param array $recordData
382 * @param NULL|string|int $previousUid
385 protected function resolvePreviousUid(array $recordData, $previousUid) {
386 if ($previousUid === NULL) {
389 foreach ($recordData as $fieldName => $fieldValue) {
390 if (strpos($fieldValue, '__previousUid') === FALSE) {
393 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
399 * @param array $recordData
400 * @param NULL|string|int $nextUid
403 protected function resolveNextUid(array $recordData, $nextUid) {
404 if ($nextUid === NULL) {
407 foreach ($recordData as $fieldName => $fieldValue) {
408 if (strpos($fieldValue, '__nextUid') === FALSE) {
411 $recordData[$fieldName] = str_replace('__nextUid', $nextUid, $fieldValue);
417 * @param string $tableName
418 * @param int $liveUid
419 * @param bool $useDeleteClause
422 protected function getVersionedId($tableName, $liveUid, $useDeleteClause = FALSE) {
424 $liveUid = (int)$liveUid;
425 $workspaceId = (int)$this->getBackendUser()->workspace
;
426 $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
429 'pid=-1 AND t3ver_oid=' . $liveUid . ' AND t3ver_wsid=' . $workspaceId .
430 ($useDeleteClause ? \TYPO3\CMS\Backend\Utility\BackendUtility
::deleteClause($tableName) : '')
432 if (!empty($row['uid'])) {
433 $versionedId = (int)$row['uid'];
439 * @return \TYPO3\CMS\Core\DataHandling\DataHandler
441 protected function createDataHandler() {
442 $dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(
443 'TYPO3\\CMS\\Core\\DataHandling\\DataHandler'
445 $this->dataHandler
= $dataHandler;
450 * @return \TYPO3\CMS\Workspaces\Service\WorkspaceService
452 protected function getWorkspaceService() {
453 return \TYPO3\CMS\Core\Utility\GeneralUtility
::makeInstance(
454 'TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService'
459 * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
461 protected function getBackendUser() {
462 return $GLOBALS['BE_USER'];
466 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
468 protected function getDatabaseConnection() {
469 return $GLOBALS['TYPO3_DB'];