[TASK] Streamline DataHandler functional 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 */
155 public function deleteRecord($tableName, $uid) {
156 $commandMap = array(
157 $tableName => array(
158 $uid => array(
159 'delete' => TRUE,
160 ),
161 ),
162 );
163 $this->dataHandler->start(array(), $commandMap);
164 $this->dataHandler->process_cmdmap();
165 }
166
167 /**
168 * @param string $tableName
169 * @param integer $uid
170 * @param integer $pageId
171 */
172 public function copyRecord($tableName, $uid, $pageId) {
173 $commandMap = array(
174 $tableName => array(
175 $uid => array(
176 'copy' => $pageId,
177 ),
178 ),
179 );
180 $this->dataHandler->start(array(), $commandMap);
181 $this->dataHandler->process_cmdmap();
182 return $this->dataHandler->copyMappingArray;
183 }
184
185 /**
186 * @param string $tableName
187 * @param integer $uid
188 * @param integer $pageId
189 */
190 public function moveRecord($tableName, $uid, $pageId) {
191 $commandMap = array(
192 $tableName => array(
193 $uid => array(
194 'move' => $pageId,
195 ),
196 ),
197 );
198 $this->dataHandler->start(array(), $commandMap);
199 $this->dataHandler->process_cmdmap();
200 }
201
202 /**
203 * @param string $tableName
204 * @param integer $uid
205 * @param integer $languageId
206 * @return array
207 */
208 public function localizeRecord($tableName, $uid, $languageId) {
209 $commandMap = array(
210 $tableName => array(
211 $uid => array(
212 'localize' => $languageId,
213 ),
214 ),
215 );
216 $this->dataHandler->start(array(), $commandMap);
217 $this->dataHandler->process_cmdmap();
218 return $this->dataHandler->copyMappingArray;
219 }
220
221 /**
222 * @param string $tableName
223 * @param integer $uid
224 * @param string $fieldName
225 * @param array $referenceIds
226 */
227 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
228 $dataMap = array(
229 $tableName => array(
230 $uid => array(
231 $fieldName => implode(',', $referenceIds),
232 ),
233 )
234 );
235 $this->dataHandler->start($dataMap, array());
236 $this->dataHandler->process_datamap();
237 }
238
239 /**
240 * @param string $tableName
241 * @param integer $uid
242 * @param string $fieldName
243 * @param integer $referenceId
244 */
245 public function addReference($tableName, $uid, $fieldName, $referenceId) {
246 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
247
248 if (!in_array($referenceId, $recordValues)) {
249 $recordValues[] = $referenceId;
250 }
251
252 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
253 }
254
255 /**
256 * @param string $tableName
257 * @param integer $uid
258 * @param string $fieldName
259 * @param integer $referenceId
260 */
261 public function deleteReference($tableName, $uid, $fieldName, $referenceId) {
262 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
263
264 if (($index = array_search($referenceId, $recordValues)) !== FALSE) {
265 unset($recordValues[$index]);
266 }
267
268 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
269 }
270
271 /**
272 * @param array $recordData
273 * @param NULL|int $previousUid
274 * @return array
275 */
276 protected function resolvePreviousUid(array $recordData, $previousUid) {
277 if ($previousUid === NULL) {
278 return $recordData;
279 }
280 foreach ($recordData as $fieldName => $fieldValue) {
281 if (strpos($fieldValue, '__previousUid') === FALSE) {
282 continue;
283 }
284 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
285 }
286 return $recordData;
287 }
288
289 /**
290 * @param string $tableName
291 * @param integer $uid
292 * @param string $fieldName
293 * @return array
294 */
295 protected function getRecordValues($tableName, $uid, $fieldName) {
296 $recordValues = array();
297
298 $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
299 if (!empty($recordValue)) {
300 $recordValues = explode(',', $recordValues);
301 }
302
303 return $recordValues;
304 }
305
306 /**
307 * @param string $tableName
308 * @param integer $uid
309 * @param string $fieldName
310 * @return bool|string|NULL
311 */
312 protected function getRecordValue($tableName, $uid, $fieldName) {
313 $recordValue = FALSE;
314
315 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
316 $fieldName, $tableName, 'uid=' . (int)$uid
317 );
318
319 if (isset($record[$fieldName])) {
320 $recordValue = $record[$fieldName];
321 }
322
323 return $recordValue;
324 }
325
326 /**
327 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
328 */
329 protected function getDatabaseConnection() {
330 return $GLOBALS['TYPO3_DB'];
331 }
332
333 }