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