[BUGFIX] Make Functional tests work on fast machines
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / Framework / ActionService.php
index 071c65d..264cd58 100644 (file)
@@ -36,20 +36,6 @@ class ActionService {
         */
        protected $dataHandler;
 
-       /**
-        * @param DataHandler $dataHandler
-        */
-       public function __construct(DataHandler $dataHandler) {
-               $this->setDataHandler($dataHandler);
-       }
-
-       /**
-        * @param DataHandler $dataHandler
-        */
-       public function setDataHandler(DataHandler $dataHandler) {
-               $this->dataHandler = $dataHandler;
-       }
-
        /**
         * @return DataHandler
         */
@@ -61,26 +47,53 @@ class ActionService {
         * @param string $tableName
         * @param integer $pageId
         * @param array $recordData
+        * @return array
         */
        public function createNewRecord($tableName, $pageId, array $recordData) {
-               $this->createNewRecords($pageId, array($tableName => $recordData));
+               return $this->createNewRecords($pageId, array($tableName => $recordData));
        }
 
        /**
         * @param integer $pageId
         * @param array $tableRecordData
+        * @return array
         */
        public function createNewRecords($pageId, array $tableRecordData) {
                $dataMap = array();
+               $newTableIds = array();
                $currentUid = NULL;
+               $previousTableName = NULL;
+               $previousUid = NULL;
                foreach ($tableRecordData as $tableName => $recordData) {
                        $recordData = $this->resolvePreviousUid($recordData, $currentUid);
-                       $recordData['pid'] = $pageId;
-                       $currentUid = uniqid('NEW');
+                       if (!isset($recordData['pid'])) {
+                               $recordData['pid'] = $pageId;
+                       }
+                       $currentUid = uniqid('NEW', TRUE);
+                       $newTableIds[$tableName][] = $currentUid;
                        $dataMap[$tableName][$currentUid] = $recordData;
+                       if ($previousTableName !== NULL && $previousUid !== NULL) {
+                               $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
+                                       $dataMap[$previousTableName][$previousUid],
+                                       $currentUid
+                               );
+                       }
+                       $previousTableName = $tableName;
+                       $previousUid = $currentUid;
                }
+               $this->createDataHandler();
                $this->dataHandler->start($dataMap, array());
                $this->dataHandler->process_datamap();
+
+               foreach ($newTableIds as $tableName => &$ids) {
+                       foreach ($ids as &$id) {
+                               if (!empty($this->dataHandler->substNEWwithIDs[$id])) {
+                                       $id = $this->dataHandler->substNEWwithIDs[$id];
+                               }
+                       }
+               }
+
+               return $newTableIds;
        }
 
        /**
@@ -103,6 +116,7 @@ class ActionService {
                                }
                        }
                }
+               $this->createDataHandler();
                $this->dataHandler->start($dataMap, $commandMap);
                $this->dataHandler->process_datamap();
                if (!empty($commandMap)) {
@@ -117,6 +131,8 @@ class ActionService {
        public function modifyRecords($pageId, array $tableRecordData) {
                $dataMap = array();
                $currentUid = NULL;
+               $previousTableName = NULL;
+               $previousUid = NULL;
                foreach ($tableRecordData as $tableName => $recordData) {
                        if (empty($recordData['uid'])) {
                                continue;
@@ -129,7 +145,16 @@ class ActionService {
                        }
                        unset($recordData['uid']);
                        $dataMap[$tableName][$currentUid] = $recordData;
+                       if ($previousTableName !== NULL && $previousUid !== NULL) {
+                               $dataMap[$previousTableName][$previousUid] = $this->resolveNextUid(
+                                       $dataMap[$previousTableName][$previousUid],
+                                       $currentUid
+                               );
+                       }
+                       $previousTableName = $tableName;
+                       $previousUid = $currentUid;
                }
+               $this->createDataHandler();
                $this->dataHandler->start($dataMap, array());
                $this->dataHandler->process_datamap();
        }
@@ -137,15 +162,63 @@ class ActionService {
        /**
         * @param string $tableName
         * @param integer $uid
+        * @return array
         */
        public function deleteRecord($tableName, $uid) {
-               $commandMap = array(
-                       $tableName => array(
-                               $uid => array(
+               return $this->deleteRecords(
+                       array(
+                               $tableName => array($uid),
+                       )
+               );
+       }
+
+       /**
+        * @param array $tableRecordIds
+        * @return array
+        */
+       public function deleteRecords(array $tableRecordIds) {
+               $commandMap = array();
+               foreach ($tableRecordIds as $tableName => $ids) {
+                       foreach ($ids as $uid) {
+                               $commandMap[$tableName][$uid] = array(
                                        'delete' => TRUE,
-                               ),
-                       ),
+                               );
+                       }
+               }
+               $this->createDataHandler();
+               $this->dataHandler->start(array(), $commandMap);
+               $this->dataHandler->process_cmdmap();
+               // Deleting workspace records is actually a copy(!)
+               return $this->dataHandler->copyMappingArray;
+       }
+
+       /**
+        * @param string $tableName
+        * @param integer $uid
+        */
+       public function clearWorkspaceRecord($tableName, $uid) {
+               $this->clearWorkspaceRecords(
+                       array(
+                               $tableName => array($uid),
+                       )
                );
+       }
+
+       /**
+        * @param array $tableRecordIds
+        */
+       public function clearWorkspaceRecords(array $tableRecordIds) {
+               $commandMap = array();
+               foreach ($tableRecordIds as $tableName => $ids) {
+                       foreach ($ids as $uid) {
+                               $commandMap[$tableName][$uid] = array(
+                                       'version' => array(
+                                               'action' => 'clearWSID',
+                                       )
+                               );
+                       }
+               }
+               $this->createDataHandler();
                $this->dataHandler->start(array(), $commandMap);
                $this->dataHandler->process_cmdmap();
        }
@@ -154,8 +227,10 @@ class ActionService {
         * @param string $tableName
         * @param integer $uid
         * @param integer $pageId
+        * @param NULL|array $recordData
+        * @return array
         */
-       public function copyRecord($tableName, $uid, $pageId) {
+       public function copyRecord($tableName, $uid, $pageId, array $recordData = NULL) {
                $commandMap = array(
                        $tableName => array(
                                $uid => array(
@@ -163,16 +238,26 @@ class ActionService {
                                ),
                        ),
                );
+               if ($recordData !== NULL) {
+                       $commandMap[$tableName][$uid]['copy'] = array(
+                               'action' => 'paste',
+                               'target' => $pageId,
+                               'update' => $recordData,
+                       );
+               }
+               $this->createDataHandler();
                $this->dataHandler->start(array(), $commandMap);
                $this->dataHandler->process_cmdmap();
+               return $this->dataHandler->copyMappingArray;
        }
 
        /**
         * @param string $tableName
         * @param integer $uid
         * @param integer $pageId
+        * @param NULL|array $recordData
         */
-       public function moveRecord($tableName, $uid, $pageId) {
+       public function moveRecord($tableName, $uid, $pageId, array $recordData = NULL) {
                $commandMap = array(
                        $tableName => array(
                                $uid => array(
@@ -180,6 +265,14 @@ class ActionService {
                                ),
                        ),
                );
+               if ($recordData !== NULL) {
+                       $commandMap[$tableName][$uid]['move'] = array(
+                               'action' => 'paste',
+                               'target' => $pageId,
+                               'update' => $recordData,
+                       );
+               }
+               $this->createDataHandler();
                $this->dataHandler->start(array(), $commandMap);
                $this->dataHandler->process_cmdmap();
        }
@@ -188,6 +281,7 @@ class ActionService {
         * @param string $tableName
         * @param integer $uid
         * @param integer $languageId
+        * @return array
         */
        public function localizeRecord($tableName, $uid, $languageId) {
                $commandMap = array(
@@ -197,8 +291,10 @@ class ActionService {
                                ),
                        ),
                );
+               $this->createDataHandler();
                $this->dataHandler->start(array(), $commandMap);
                $this->dataHandler->process_cmdmap();
+               return $this->dataHandler->copyMappingArray;
        }
 
        /**
@@ -215,45 +311,75 @@ class ActionService {
                                ),
                        )
                );
+               $this->createDataHandler();
                $this->dataHandler->start($dataMap, array());
                $this->dataHandler->process_datamap();
        }
 
        /**
         * @param string $tableName
-        * @param integer $uid
-        * @param string $fieldName
-        * @param integer $referenceId
+        * @param int $liveUid
+        * @param bool $throwException
         */
-       public function addReference($tableName, $uid, $fieldName, $referenceId) {
-               $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
-
-               if (!in_array($referenceId, $recordValues)) {
-                       $recordValues[] = $referenceId;
-               }
-
-               $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
+       public function publishRecord($tableName, $liveUid, $throwException = TRUE) {
+               $this->publishRecords(array($tableName => array($liveUid)), $throwException);
        }
 
        /**
-        * @param string $tableName
-        * @param integer $uid
-        * @param string $fieldName
-        * @param integer $referenceId
+        * @param array $tableLiveUids
+        * @param bool $throwException
+        * @throws \TYPO3\CMS\Core\Tests\Exception
         */
-       public function deleteReference($tableName, $uid, $fieldName, $referenceId) {
-               $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
+       public function publishRecords(array $tableLiveUids, $throwException = TRUE) {
+               $commandMap = array();
+               foreach ($tableLiveUids as $tableName => $liveUids) {
+                       foreach ($liveUids as $liveUid) {
+                               $versionedUid = $this->getVersionedId($tableName, $liveUid);
+                               if (empty($versionedUid)) {
+                                       if ($throwException) {
+                                               throw new \TYPO3\CMS\Core\Tests\Exception('Versioned UID could not be determined');
+                                       } else {
+                                               continue;
+                                       }
+                               }
 
-               if (($index = array_search($referenceId, $recordValues)) !== FALSE) {
-                       unset($recordValues[$index]);
+                               $commandMap[$tableName][$liveUid] = array(
+                                       'version' => array(
+                                               'action' => 'swap',
+                                               'swapWith' => $versionedUid,
+                                               'notificationAlternativeRecipients' => array(),
+                                       ),
+                               );
+                       }
                }
+               $this->createDataHandler();
+               $this->dataHandler->start(array(), $commandMap);
+               $this->dataHandler->process_cmdmap();
+       }
 
-               $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
+       /**
+        * @param int $workspaceId
+        */
+       public function publishWorkspace($workspaceId) {
+               $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, FALSE);
+               $this->createDataHandler();
+               $this->dataHandler->start(array(), $commandMap);
+               $this->dataHandler->process_cmdmap();
+       }
+
+       /**
+        * @param int $workspaceId
+        */
+       public function swapWorkspace($workspaceId) {
+               $commandMap = $this->getWorkspaceService()->getCmdArrayForPublishWS($workspaceId, TRUE);
+               $this->createDataHandler();
+               $this->dataHandler->start(array(), $commandMap);
+               $this->dataHandler->process_cmdmap();
        }
 
        /**
         * @param array $recordData
-        * @param NULL|int $previousUid
+        * @param NULL|string|int $previousUid
         * @return array
         */
        protected function resolvePreviousUid(array $recordData, $previousUid) {
@@ -270,40 +396,70 @@ class ActionService {
        }
 
        /**
-        * @param string $tableName
-        * @param integer $uid
-        * @param string $fieldName
+        * @param array $recordData
+        * @param NULL|string|int $nextUid
         * @return array
         */
-       protected function getRecordValues($tableName, $uid, $fieldName) {
-               $recordValues = array();
-
-               $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
-               if (!empty($recordValue)) {
-                       $recordValues = explode(',', $recordValues);
+       protected function resolveNextUid(array $recordData, $nextUid) {
+               if ($nextUid === NULL) {
+                       return $recordData;
                }
-
-               return $recordValues;
+               foreach ($recordData as $fieldName => $fieldValue) {
+                       if (strpos($fieldValue, '__nextUid') === FALSE) {
+                               continue;
+                       }
+                       $recordData[$fieldName] = str_replace('__nextUid', $nextUid, $fieldValue);
+               }
+               return $recordData;
        }
 
        /**
         * @param string $tableName
-        * @param integer $uid
-        * @param string $fieldName
-        * @return bool|string|NULL
+        * @param int $liveUid
+        * @param bool $useDeleteClause
+        * @return NULL|int
         */
-       protected function getRecordValue($tableName, $uid, $fieldName) {
-               $recordValue = FALSE;
+       protected function getVersionedId($tableName, $liveUid, $useDeleteClause = FALSE) {
+               $versionedId = NULL;
+               $liveUid = (int)$liveUid;
+               $workspaceId = (int)$this->getBackendUser()->workspace;
+               $row = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
+                       'uid',
+                       $tableName,
+                       'pid=-1 AND t3ver_oid=' . $liveUid . ' AND t3ver_wsid=' . $workspaceId .
+                       ($useDeleteClause ? \TYPO3\CMS\Backend\Utility\BackendUtility::deleteClause($tableName) : '')
+               );
+               if (!empty($row['uid'])) {
+                       $versionedId = (int)$row['uid'];
+               }
+               return $versionedId;
+       }
 
-               $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
-                       $fieldName, $tableName, 'uid=' . (int)$uid
+       /**
+        * @return \TYPO3\CMS\Core\DataHandling\DataHandler
+        */
+       protected function createDataHandler() {
+               $dataHandler = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
+                       'TYPO3\\CMS\\Core\\DataHandling\\DataHandler'
                );
+               $this->dataHandler = $dataHandler;
+               return $dataHandler;
+       }
 
-               if (isset($record[$fieldName])) {
-                       $recordValue = $record[$fieldName];
-               }
+       /**
+        * @return \TYPO3\CMS\Workspaces\Service\WorkspaceService
+        */
+       protected function getWorkspaceService() {
+               return \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
+                       'TYPO3\\CMS\\Workspaces\\Service\\WorkspaceService'
+               );
+       }
 
-               return $recordValue;
+       /**
+        * @return \TYPO3\CMS\Core\Authentication\BackendUserAuthentication
+        */
+       protected function getBackendUser() {
+               return $GLOBALS['BE_USER'];
        }
 
        /**