[TASK] Add functional test cases to DataHandler (live)
[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 */
65 public function createNewRecord($tableName, $pageId, array $recordData) {
66 $this->createNewRecords($pageId, array($tableName => $recordData));
67 }
68
69 /**
70 * @param integer $pageId
71 * @param array $tableRecordData
72 */
73 public function createNewRecords($pageId, array $tableRecordData) {
74 $dataMap = array();
75 $currentUid = NULL;
76 foreach ($tableRecordData as $tableName => $recordData) {
77 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
78 $recordData['pid'] = $pageId;
79 $currentUid = uniqid('NEW');
80 $dataMap[$tableName][$currentUid] = $recordData;
81 }
82 $this->dataHandler->start($dataMap, array());
83 $this->dataHandler->process_datamap();
84 }
85
86 /**
87 * @param string $tableName
88 * @param integer $uid
89 * @param array $recordData
90 * @param NULL|array $deleteTableRecordIds
91 */
92 public function modifyRecord($tableName, $uid, array $recordData, array $deleteTableRecordIds = NULL) {
93 $dataMap = array(
94 $tableName => array(
95 $uid => $recordData,
96 ),
97 );
98 $commandMap = array();
99 if (!empty($deleteTableRecordIds)) {
100 foreach ($deleteTableRecordIds as $tableName => $recordIds) {
101 foreach ($recordIds as $recordId) {
102 $commandMap[$tableName][$recordId]['delete'] = TRUE;
103 }
104 }
105 }
106 $this->dataHandler->start($dataMap, $commandMap);
107 $this->dataHandler->process_datamap();
108 if (!empty($commandMap)) {
109 $this->dataHandler->process_cmdmap();
110 }
111 }
112
113 /**
114 * @param integer $pageId
115 * @param array $tableRecordData
116 */
117 public function modifyRecords($pageId, array $tableRecordData) {
118 $dataMap = array();
119 $currentUid = NULL;
120 foreach ($tableRecordData as $tableName => $recordData) {
121 if (empty($recordData['uid'])) {
122 continue;
123 }
124 $recordData = $this->resolvePreviousUid($recordData, $currentUid);
125 $currentUid = $recordData['uid'];
126 if ($recordData['uid'] === '__NEW') {
127 $recordData['pid'] = $pageId;
128 $currentUid = uniqid('NEW');
129 }
130 unset($recordData['uid']);
131 $dataMap[$tableName][$currentUid] = $recordData;
132 }
133 $this->dataHandler->start($dataMap, array());
134 $this->dataHandler->process_datamap();
135 }
136
137 /**
138 * @param string $tableName
139 * @param integer $uid
140 */
141 public function deleteRecord($tableName, $uid) {
142 $commandMap = array(
143 $tableName => array(
144 $uid => array(
145 'delete' => TRUE,
146 ),
147 ),
148 );
149 $this->dataHandler->start(array(), $commandMap);
150 $this->dataHandler->process_cmdmap();
151 }
152
153 /**
154 * @param string $tableName
155 * @param integer $uid
156 * @param integer $pageId
157 */
158 public function copyRecord($tableName, $uid, $pageId) {
159 $commandMap = array(
160 $tableName => array(
161 $uid => array(
162 'copy' => $pageId,
163 ),
164 ),
165 );
166 $this->dataHandler->start(array(), $commandMap);
167 $this->dataHandler->process_cmdmap();
168 }
169
170 /**
171 * @param string $tableName
172 * @param integer $uid
173 * @param integer $pageId
174 */
175 public function moveRecord($tableName, $uid, $pageId) {
176 $commandMap = array(
177 $tableName => array(
178 $uid => array(
179 'move' => $pageId,
180 ),
181 ),
182 );
183 $this->dataHandler->start(array(), $commandMap);
184 $this->dataHandler->process_cmdmap();
185 }
186
187 /**
188 * @param string $tableName
189 * @param integer $uid
190 * @param integer $languageId
191 */
192 public function localizeRecord($tableName, $uid, $languageId) {
193 $commandMap = array(
194 $tableName => array(
195 $uid => array(
196 'localize' => $languageId,
197 ),
198 ),
199 );
200 $this->dataHandler->start(array(), $commandMap);
201 $this->dataHandler->process_cmdmap();
202 }
203
204 /**
205 * @param string $tableName
206 * @param integer $uid
207 * @param string $fieldName
208 * @param array $referenceIds
209 */
210 public function modifyReferences($tableName, $uid, $fieldName, array $referenceIds) {
211 $dataMap = array(
212 $tableName => array(
213 $uid => array(
214 $fieldName => implode(',', $referenceIds),
215 ),
216 )
217 );
218 $this->dataHandler->start($dataMap, array());
219 $this->dataHandler->process_datamap();
220 }
221
222 /**
223 * @param string $tableName
224 * @param integer $uid
225 * @param string $fieldName
226 * @param integer $referenceId
227 */
228 public function addReference($tableName, $uid, $fieldName, $referenceId) {
229 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
230
231 if (!in_array($referenceId, $recordValues)) {
232 $recordValues[] = $referenceId;
233 }
234
235 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
236 }
237
238 /**
239 * @param string $tableName
240 * @param integer $uid
241 * @param string $fieldName
242 * @param integer $referenceId
243 */
244 public function deleteReference($tableName, $uid, $fieldName, $referenceId) {
245 $recordValues = $this->getRecordValues($tableName, $uid, $fieldName);
246
247 if (($index = array_search($referenceId, $recordValues)) !== FALSE) {
248 unset($recordValues[$index]);
249 }
250
251 $this->modifyReferences($tableName, $uid, $fieldName, $recordValues);
252 }
253
254 /**
255 * @param array $recordData
256 * @param NULL|int $previousUid
257 * @return array
258 */
259 protected function resolvePreviousUid(array $recordData, $previousUid) {
260 if ($previousUid === NULL) {
261 return $recordData;
262 }
263 foreach ($recordData as $fieldName => $fieldValue) {
264 if (strpos($fieldValue, '__previousUid') === FALSE) {
265 continue;
266 }
267 $recordData[$fieldName] = str_replace('__previousUid', $previousUid, $fieldValue);
268 }
269 return $recordData;
270 }
271
272 /**
273 * @param string $tableName
274 * @param integer $uid
275 * @param string $fieldName
276 * @return array
277 */
278 protected function getRecordValues($tableName, $uid, $fieldName) {
279 $recordValues = array();
280
281 $recordValue = $this->getRecordValue($tableName, $uid, $fieldName);
282 if (!empty($recordValue)) {
283 $recordValues = explode(',', $recordValues);
284 }
285
286 return $recordValues;
287 }
288
289 /**
290 * @param string $tableName
291 * @param integer $uid
292 * @param string $fieldName
293 * @return bool|string|NULL
294 */
295 protected function getRecordValue($tableName, $uid, $fieldName) {
296 $recordValue = FALSE;
297
298 $record = $this->getDatabaseConnection()->exec_SELECTgetSingleRow(
299 $fieldName, $tableName, 'uid=' . (int)$uid
300 );
301
302 if (isset($record[$fieldName])) {
303 $recordValue = $record[$fieldName];
304 }
305
306 return $recordValue;
307 }
308
309 /**
310 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
311 */
312 protected function getDatabaseConnection() {
313 return $GLOBALS['TYPO3_DB'];
314 }
315
316 }