[BUGFIX] Make Functional tests work on fast machines
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Functional / DataHandling / Framework / ActionService.php
index b786fc4..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
         */
@@ -80,8 +66,10 @@ class ActionService {
                $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) {
@@ -93,6 +81,7 @@ class ActionService {
                        $previousTableName = $tableName;
                        $previousUid = $currentUid;
                }
+               $this->createDataHandler();
                $this->dataHandler->start($dataMap, array());
                $this->dataHandler->process_datamap();
 
@@ -127,6 +116,7 @@ class ActionService {
                                }
                        }
                }
+               $this->createDataHandler();
                $this->dataHandler->start($dataMap, $commandMap);
                $this->dataHandler->process_datamap();
                if (!empty($commandMap)) {
@@ -164,6 +154,7 @@ class ActionService {
                        $previousTableName = $tableName;
                        $previousUid = $currentUid;
                }
+               $this->createDataHandler();
                $this->dataHandler->start($dataMap, array());
                $this->dataHandler->process_datamap();
        }
@@ -194,6 +185,7 @@ class ActionService {
                                );
                        }
                }
+               $this->createDataHandler();
                $this->dataHandler->start(array(), $commandMap);
                $this->dataHandler->process_cmdmap();
                // Deleting workspace records is actually a copy(!)
@@ -226,6 +218,7 @@ class ActionService {
                                );
                        }
                }
+               $this->createDataHandler();
                $this->dataHandler->start(array(), $commandMap);
                $this->dataHandler->process_cmdmap();
        }
@@ -234,9 +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(
@@ -244,6 +238,14 @@ 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;
@@ -253,8 +255,9 @@ class ActionService {
         * @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(
@@ -262,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();
        }
@@ -280,6 +291,7 @@ class ActionService {
                                ),
                        ),
                );
+               $this->createDataHandler();
                $this->dataHandler->start(array(), $commandMap);
                $this->dataHandler->process_cmdmap();
                return $this->dataHandler->copyMappingArray;
@@ -299,40 +311,70 @@ 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();
        }
 
        /**
@@ -373,39 +415,51 @@ class ActionService {
 
        /**
         * @param string $tableName
-        * @param integer $uid
-        * @param string $fieldName
-        * @return array
+        * @param int $liveUid
+        * @param bool $useDeleteClause
+        * @return NULL|int
         */
-       protected function getRecordValues($tableName, $uid, $fieldName) {
-               $recordValues = array();
-
-               $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
-               if (!empty($recordValue)) {
-                       $recordValues = explode(',', $recordValues);
+       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 $recordValues;
+               return $versionedId;
        }
 
        /**
-        * @param string $tableName
-        * @param integer $uid
-        * @param string $fieldName
-        * @return bool|string|NULL
+        * @return \TYPO3\CMS\Core\DataHandling\DataHandler
         */
-       protected function getRecordValue($tableName, $uid, $fieldName) {
-               $recordValue = FALSE;
-
-               $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
-                       $fieldName, $tableName, 'uid=' . (int)$uid
+       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'];
        }
 
        /**