7f4a6d4c2f21a7a0099a3ab7f2f189beeb78737e
[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 addDataSetsDefaultDataFormGetIfColumnIsDenfinedInTca() {
174 $input = [
175 'command' => 'new',
176 'tableName' => 'aTable',
177 'vanillaUid' => 23,
178 'databaseRow' => [],
179 'vanillaTableTca' => [
180 'columns' => [
181 'aField' => [],
182 ],
183 ]
184 ];
185 $GLOBALS['_GET'] = [
186 'defVals' => [
187 'aTable' => [
188 'aField' => 'getValue',
189 ],
190 ],
191 ];
192 $expected = [
193 'aField' => 'getValue',
194 'pid' => 23,
195 ];
196 $result = $this->subject->addData($input);
197 $this->assertSame($expected, $result['databaseRow']);
198 }
199
200 /**
201 * @test
202 */
203 public function addDataSetsDefaultDataFormPostIfColumnIsDenfinedInTca() {
204 $input = [
205 'command' => 'new',
206 'tableName' => 'aTable',
207 'vanillaUid' => 23,
208 'databaseRow' => [],
209 'vanillaTableTca' => [
210 'columns' => [
211 'aField' => [],
212 ],
213 ]
214 ];
215 $GLOBALS['_POST'] = [
216 'defVals' => [
217 'aTable' => [
218 'aField' => 'postValue',
219 ],
220 ],
221 ];
222 $expected = [
223 'aField' => 'postValue',
224 'pid' => 23,
225 ];
226 $result = $this->subject->addData($input);
227 $this->assertSame($expected, $result['databaseRow']);
228 }
229
230 /**
231 * @test
232 */
233 public function addDataSetsPrioritizesDefaultPostOverDefaultGet() {
234 $input = [
235 'command' => 'new',
236 'tableName' => 'aTable',
237 'vanillaUid' => 23,
238 'databaseRow' => [],
239 'vanillaTableTca' => [
240 'columns' => [
241 'aField' => [],
242 ],
243 ]
244 ];
245 $GLOBALS['_GET'] = [
246 'defVals' => [
247 'aTable' => [
248 'aField' => 'getValue',
249 ],
250 ],
251 ];
252 $GLOBALS['_POST'] = [
253 'defVals' => [
254 'aTable' => [
255 'aField' => 'postValue',
256 ],
257 ],
258 ];
259 $expected = [
260 'aField' => 'postValue',
261 'pid' => 23,
262 ];
263 $result = $this->subject->addData($input);
264 $this->assertSame($expected, $result['databaseRow']);
265 }
266
267 /**
268 * @test
269 */
270 public function addDataDoesNotSetDefaultDataFormGetPostIfColumnIsMissingInTca() {
271 $input = [
272 'command' => 'new',
273 'tableName' => 'aTable',
274 'vanillaUid' => 23,
275 'databaseRow' => [],
276 'userTsConfig' => [
277 'TCAdefaults.' => [
278 'aTable.' => [
279 'aField' => 'pageTsValue',
280 ],
281 ],
282 ],
283 'vanillaTableTca' => [
284 'columns' => [],
285 ]
286 ];
287 $GLOBALS['_GET'] = [
288 'defVals' => [
289 'aTable' => [
290 'aField' => 'getValue',
291 ],
292 ],
293 ];
294 $GLOBALS['_POST'] = [
295 'defVals' => [
296 'aTable' => [
297 'aField' => 'postValue',
298 ],
299 ],
300 ];
301 $expected = [
302 'pid' => 23,
303 ];
304 $result = $this->subject->addData($input);
305 $this->assertSame($expected, $result['databaseRow']);
306 }
307
308 /**
309 * @test
310 */
311 public function addDataSetsDefaultFromNeighborRow() {
312 $input = [
313 'command' => 'new',
314 'tableName' => 'aTable',
315 'vanillaUid' => 23,
316 'databaseRow' => [],
317 'neighborRow' => [
318 'aField' => 'valueFromNeighbor',
319 ],
320 'vanillaTableTca' => [
321 'ctrl' => [
322 'useColumnsForDefaultValues' => 'aField',
323 ],
324 'columns' => [
325 'aField' => [],
326 ],
327 ],
328 ];
329 $expected = [
330 'aField' => 'valueFromNeighbor',
331 'pid' => 23,
332 ];
333 $result = $this->subject->addData($input);
334 $this->assertSame($expected, $result['databaseRow']);
335 }
336
337 /**
338 * @test
339 */
340 public function addDataSetsDoesNotOverrideDefaultFromNeighborRowIfOtherDefaultHasSetDataAlready() {
341 $input = [
342 'command' => 'new',
343 'tableName' => 'aTable',
344 'vanillaUid' => 23,
345 'databaseRow' => [],
346 'neighborRow' => [
347 'aField' => 'valueFromNeighbor',
348 ],
349 'vanillaTableTca' => [
350 'ctrl' => [
351 'useColumnsForDefaultValues' => 'aField',
352 ],
353 'columns' => [
354 'aField' => [],
355 ],
356 ],
357 ];
358 $GLOBALS['_POST'] = [
359 'defVals' => [
360 'aTable' => [
361 'aField' => 'postValue',
362 ],
363 ],
364 ];
365 $expected = [
366 'aField' => 'postValue',
367 'pid' => 23,
368 ];
369 $result = $this->subject->addData($input);
370 $this->assertSame($expected, $result['databaseRow']);
371 }
372
373 /**
374 * @test
375 */
376 public function addDataSetsPidToVanillaUid() {
377 $input = [
378 'command' => 'new',
379 'tableName' => 'aTable',
380 'vanillaUid' => 23,
381 'databaseRow' => [],
382 ];
383 $expected['pid'] = 23;
384 $result = $this->subject->addData($input);
385 $this->assertSame($expected, $result['databaseRow']);
386 }
387
388 }