[CLEANUP] Minor code improvements in DatabaseRowInitializeNew
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseRowInitializeNewTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Tests\UnitTestCase;
18 use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew;
19
20 /**
21 * Test case
22 */
23 class DatabaseRowInitializeNewTest extends UnitTestCase {
24
25 /**
26 * @var DatabaseRowInitializeNew
27 */
28 protected $subject;
29
30 public function setUp() {
31 $this->subject = new DatabaseRowInitializeNew();
32 }
33
34 /**
35 * @test
36 */
37 public function addDataReturnSameDataIfCommandIsEdit() {
38 $input = [
39 'command' => 'edit',
40 'tableName' => 'aTable',
41 'vanillaUid' => 23,
42 'databaseRow' => [
43 'uid' => 42,
44 ],
45 'userTsConfig' => [
46 'TCAdefaults.' => [
47 'aTable.' => [
48 'uid' => 23,
49 ],
50 ],
51 ],
52 ];
53 $this->assertSame($input, $this->subject->addData($input));
54 }
55
56 /**
57 * @test
58 */
59 public function addDataSetsDefaultDataFormUserTsIfColumnIsDenfinedInTca() {
60 $input = [
61 'command' => 'new',
62 'tableName' => 'aTable',
63 'vanillaUid' => 23,
64 'databaseRow' => [],
65 'userTsConfig' => [
66 'TCAdefaults.' => [
67 'aTable.' => [
68 'aField' => 'userTsValue',
69 ],
70 ],
71 ],
72 'vanillaTableTca' => [
73 'columns' => [
74 'aField' => [],
75 ],
76 ]
77 ];
78 $expected = [
79 'aField' => 'userTsValue',
80 'pid' => 23,
81 ];
82 $result = $this->subject->addData($input);
83 $this->assertSame($expected, $result['databaseRow']);
84 }
85
86 /**
87 * @test
88 */
89 public function addDataDoesNotSetDefaultDataFormUserTsIfColumnIsMissingInTca() {
90 $input = [
91 'command' => 'new',
92 'tableName' => 'aTable',
93 'vanillaUid' => 23,
94 'databaseRow' => [],
95 'userTsConfig' => [
96 'TCAdefaults.' => [
97 'aTable.' => [
98 'aField' => 'userTsValue',
99 ],
100 ],
101 ],
102 'vanillaTableTca' => [
103 'columns' => [],
104 ]
105 ];
106 $expected = [
107 'pid' => 23,
108 ];
109 $result = $this->subject->addData($input);
110 $this->assertSame($expected, $result['databaseRow']);
111 }
112
113 /**
114 * @test
115 */
116 public function addDataSetsDefaultDataFormPageTsIfColumnIsDenfinedInTca() {
117 $input = [
118 'command' => 'new',
119 'tableName' => 'aTable',
120 'vanillaUid' => 23,
121 'databaseRow' => [],
122 'pageTsConfig' => [
123 'TCAdefaults.' => [
124 'aTable.' => [
125 'aField' => 'pageTsValue',
126 ],
127 ],
128 ],
129 'vanillaTableTca' => [
130 'columns' => [
131 'aField' => [],
132 ],
133 ]
134 ];
135 $expected = [
136 'aField' => 'pageTsValue',
137 'pid' => 23,
138 ];
139 $result = $this->subject->addData($input);
140 $this->assertSame($expected, $result['databaseRow']);
141 }
142
143 /**
144 * @test
145 */
146 public function addDataDoesNotSetDefaultDataFormPageTsIfColumnIsMissingInTca() {
147 $input = [
148 'command' => 'new',
149 'tableName' => 'aTable',
150 'vanillaUid' => 23,
151 'databaseRow' => [],
152 'pageTsConfig' => [
153 'TCAdefaults.' => [
154 'aTable.' => [
155 'aField' => 'pageTsValue',
156 ],
157 ],
158 ],
159 'vanillaTableTca' => [
160 'columns' => [],
161 ]
162 ];
163 $expected = [
164 'pid' => 23,
165 ];
166 $result = $this->subject->addData($input);
167 $this->assertSame($expected, $result['databaseRow']);
168 }
169
170 /**
171 * @test
172 */
173 public function addDataSetsDefaultDataOverrulingFormPageTs() {
174 $input = [
175 'command' => 'new',
176 'tableName' => 'aTable',
177 'vanillaUid' => 23,
178 'databaseRow' => [],
179 'pageTsConfig' => [
180 'TCAdefaults.' => [
181 'aTable.' => [
182 'aField' => 'pageTsValue',
183 ],
184 ],
185 ],
186 'userTsConfig' => [
187 'TCAdefaults.' => [
188 'aTable.' => [
189 'aField' => 'userTsValue',
190 ],
191 ],
192 ],
193 'vanillaTableTca' => [
194 'columns' => [
195 'aField' => [],
196 ],
197 ]
198 ];
199 $expected = [
200 'aField' => 'pageTsValue',
201 'pid' => 23,
202 ];
203 $result = $this->subject->addData($input);
204 $this->assertSame($expected, $result['databaseRow']);
205 }
206
207 /**
208 * @test
209 */
210 public function addDataSetsDefaultFromNeighborRow() {
211 $input = [
212 'command' => 'new',
213 'tableName' => 'aTable',
214 'vanillaUid' => 23,
215 'databaseRow' => [],
216 'neighborRow' => [
217 'aField' => 'valueFromNeighbor',
218 ],
219 'vanillaTableTca' => [
220 'ctrl' => [
221 'useColumnsForDefaultValues' => 'aField',
222 ],
223 'columns' => [
224 'aField' => [],
225 ],
226 ],
227 ];
228 $expected = [
229 'aField' => 'valueFromNeighbor',
230 'pid' => 23,
231 ];
232 $result = $this->subject->addData($input);
233 $this->assertSame($expected, $result['databaseRow']);
234 }
235
236 /**
237 * @test
238 */
239 public function addDataSetsDefaultDataOverrulingFromNeighborRow() {
240 $input = [
241 'command' => 'new',
242 'tableName' => 'aTable',
243 'vanillaUid' => 23,
244 'databaseRow' => [],
245 'neighborRow' => [
246 'aField' => 'valueFromNeighbor',
247 ],
248 'pageTsConfig' => [
249 'TCAdefaults.' => [
250 'aTable.' => [
251 'aField' => 'pageTsValue',
252 ],
253 ],
254 ],
255 'userTsConfig' => [
256 'TCAdefaults.' => [
257 'aTable.' => [
258 'aField' => 'userTsValue',
259 ],
260 ],
261 ],
262 'vanillaTableTca' => [
263 'ctrl' => [
264 'useColumnsForDefaultValues' => 'aField',
265 ],
266 'columns' => [
267 'aField' => [],
268 ],
269 ],
270 ];
271 $expected = [
272 'aField' => 'valueFromNeighbor',
273 'pid' => 23,
274 ];
275 $result = $this->subject->addData($input);
276 $this->assertSame($expected, $result['databaseRow']);
277 }
278
279 /**
280 * @test
281 */
282 public function addDataSetsDefaultDataFormGetIfColumnIsDenfinedInTca() {
283 $input = [
284 'command' => 'new',
285 'tableName' => 'aTable',
286 'vanillaUid' => 23,
287 'databaseRow' => [],
288 'vanillaTableTca' => [
289 'columns' => [
290 'aField' => [],
291 ],
292 ]
293 ];
294 $GLOBALS['_GET'] = [
295 'defVals' => [
296 'aTable' => [
297 'aField' => 'getValue',
298 ],
299 ],
300 ];
301 $expected = [
302 'aField' => 'getValue',
303 'pid' => 23,
304 ];
305 $result = $this->subject->addData($input);
306 $this->assertSame($expected, $result['databaseRow']);
307 }
308
309 /**
310 * @test
311 */
312 public function addDataSetsDefaultDataFormPostIfColumnIsDenfinedInTca() {
313 $input = [
314 'command' => 'new',
315 'tableName' => 'aTable',
316 'vanillaUid' => 23,
317 'databaseRow' => [],
318 'vanillaTableTca' => [
319 'columns' => [
320 'aField' => [],
321 ],
322 ]
323 ];
324 $GLOBALS['_POST'] = [
325 'defVals' => [
326 'aTable' => [
327 'aField' => 'postValue',
328 ],
329 ],
330 ];
331 $expected = [
332 'aField' => 'postValue',
333 'pid' => 23,
334 ];
335 $result = $this->subject->addData($input);
336 $this->assertSame($expected, $result['databaseRow']);
337 }
338
339 /**
340 * @test
341 */
342 public function addDataSetsPrioritizesDefaultPostOverDefaultGet() {
343 $input = [
344 'command' => 'new',
345 'tableName' => 'aTable',
346 'vanillaUid' => 23,
347 'databaseRow' => [],
348 'vanillaTableTca' => [
349 'columns' => [
350 'aField' => [],
351 ],
352 ]
353 ];
354 $GLOBALS['_GET'] = [
355 'defVals' => [
356 'aTable' => [
357 'aField' => 'getValue',
358 ],
359 ],
360 ];
361 $GLOBALS['_POST'] = [
362 'defVals' => [
363 'aTable' => [
364 'aField' => 'postValue',
365 ],
366 ],
367 ];
368 $expected = [
369 'aField' => 'postValue',
370 'pid' => 23,
371 ];
372 $result = $this->subject->addData($input);
373 $this->assertSame($expected, $result['databaseRow']);
374 }
375
376 /**
377 * @test
378 */
379 public function addDataDoesNotSetDefaultDataFormGetPostIfColumnIsMissingInTca() {
380 $input = [
381 'command' => 'new',
382 'tableName' => 'aTable',
383 'vanillaUid' => 23,
384 'databaseRow' => [],
385 'userTsConfig' => [
386 'TCAdefaults.' => [
387 'aTable.' => [
388 'aField' => 'pageTsValue',
389 ],
390 ],
391 ],
392 'vanillaTableTca' => [
393 'columns' => [],
394 ]
395 ];
396 $GLOBALS['_GET'] = [
397 'defVals' => [
398 'aTable' => [
399 'aField' => 'getValue',
400 ],
401 ],
402 ];
403 $GLOBALS['_POST'] = [
404 'defVals' => [
405 'aTable' => [
406 'aField' => 'postValue',
407 ],
408 ],
409 ];
410 $expected = [
411 'pid' => 23,
412 ];
413 $result = $this->subject->addData($input);
414 $this->assertSame($expected, $result['databaseRow']);
415 }
416
417 /**
418 * @test
419 */
420 public function addDataSetsDefaultDataOverrulingGetPost() {
421 $input = [
422 'command' => 'new',
423 'tableName' => 'aTable',
424 'vanillaUid' => 23,
425 'databaseRow' => [],
426 'neighborRow' => [
427 'aField' => 'valueFromNeighbor',
428 ],
429 'pageTsConfig' => [
430 'TCAdefaults.' => [
431 'aTable.' => [
432 'aField' => 'pageTsValue',
433 ],
434 ],
435 ],
436 'userTsConfig' => [
437 'TCAdefaults.' => [
438 'aTable.' => [
439 'aField' => 'userTsValue',
440 ],
441 ],
442 ],
443 'vanillaTableTca' => [
444 'ctrl' => [
445 'useColumnsForDefaultValues' => 'aField',
446 ],
447 'columns' => [
448 'aField' => [],
449 ],
450 ],
451 ];
452 $GLOBALS['_POST'] = [
453 'defVals' => [
454 'aTable' => [
455 'aField' => 'postValue',
456 ],
457 ],
458 ];
459 $expected = [
460 'aField' => 'postValue',
461 'pid' => 23,
462 ];
463 $result = $this->subject->addData($input);
464 $this->assertSame($expected, $result['databaseRow']);
465 }
466
467 /**
468 * @test
469 */
470 public function addDataSetsPidToVanillaUid() {
471 $input = [
472 'command' => 'new',
473 'tableName' => 'aTable',
474 'vanillaUid' => 23,
475 'databaseRow' => [],
476 ];
477 $expected['pid'] = 23;
478 $result = $this->subject->addData($input);
479 $this->assertSame($expected, $result['databaseRow']);
480 }
481
482 }