[BUGFIX] Notice free FormEngine testing
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / DatabaseRecordTypeValueTest.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\Backend\Form\FormDataProvider\DatabaseRecordTypeValue;
18 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
19
20 /**
21 * Test case
22 */
23 class DatabaseRecordTypeValueTest extends UnitTestCase
24 {
25 /**
26 * @var DatabaseRecordTypeValue|\PHPUnit_Framework_MockObject_MockObject
27 */
28 protected $subject;
29
30 protected function setUp()
31 {
32 $this->subject = $this->getMockBuilder(DatabaseRecordTypeValue::class)
33 ->setMethods(['getDatabaseRow'])
34 ->getMock();
35 }
36
37 /**
38 * @test
39 */
40 public function addDataThrowsExceptionIfTcaTypesAreEmpty()
41 {
42 $input = [
43 'tableName' => 'aTable',
44 'processedTca' => [
45 'types' => [],
46 ],
47 ];
48
49 $this->expectException(\UnexpectedValueException::class);
50 $this->expectExceptionCode(1438185331);
51
52 $this->subject->addData($input);
53 }
54
55 /**
56 * @test
57 */
58 public function addDataKeepsExistingTcaRecordTypeValue()
59 {
60 $input = [
61 'recordTypeValue' => 'egon',
62 'processedTca' => [
63 'types' => [
64 '1' => 'foo',
65 ],
66 ],
67 ];
68 $expected = $input;
69 $this->assertSame($expected, $this->subject->addData($input));
70 }
71
72 /**
73 * @test
74 */
75 public function addDataKeepsExistingTcaRecordTypeValueWithValueZero()
76 {
77 $input = [
78 'recordTypeValue' => 0,
79 'processedTca' => [
80 'types' => [
81 '1' => 'foo',
82 ],
83 ],
84 ];
85 $expected = $input;
86 $this->assertSame($expected, $this->subject->addData($input));
87 }
88
89 /**
90 * @test
91 */
92 public function addDataSetsRecordTypeValueToHistoricalOneIfTypeZeroIsNotDefined()
93 {
94 $input = [
95 'recordTypeValue' => '',
96 'processedTca' => [
97 'types' => [
98 '1' => 'foo',
99 ],
100 ],
101 ];
102 $expected = $input;
103 $expected['recordTypeValue'] = '1';
104 $this->assertSame($expected, $this->subject->addData($input));
105 }
106
107 /**
108 * @test
109 */
110 public function addDataSetsRecordTypeValueToZero()
111 {
112 $input = [
113 'recordTypeValue' => '',
114 'processedTca' => [
115 'types' => [
116 '0' => 'foo',
117 ],
118 ],
119 ];
120
121 $expected = $input;
122 $expected['recordTypeValue'] = '0';
123
124 $this->assertSame($expected, $this->subject->addData($input));
125 }
126
127 /**
128 * @test
129 */
130 public function addDataThrowsExceptionIfTypePointsToANotExistingField()
131 {
132 $input = [
133 'tableName' => 'aTable',
134 'recordTypeValue' => '',
135 'processedTca' => [
136 'ctrl' => [
137 'type' => 'notExists',
138 ],
139 'types' => [
140 '0' => 'foo',
141 ],
142 ],
143 'databaseRow' => [
144 'uid' => 23,
145 ],
146 ];
147
148 $this->expectException(\UnexpectedValueException::class);
149 $this->expectExceptionCode(1438183881);
150
151 $this->subject->addData($input);
152 }
153
154 /**
155 * @test
156 */
157 public function addDataSetsRecordTypeValueToValueOfDatabaseField()
158 {
159 $input = [
160 'recordTypeValue' => '',
161 'processedTca' => [
162 'ctrl' => [
163 'type' => 'aField',
164 ],
165 'types' => [
166 '3' => 'foo',
167 ],
168 ],
169 'databaseRow' => [
170 'aField' => 3,
171 ],
172 ];
173
174 $expected = $input;
175 $expected['recordTypeValue'] = '3';
176
177 $this->assertSame($expected, $this->subject->addData($input));
178 }
179
180 /**
181 * @test
182 */
183 public function addDataSetsRecordTypeValueToZeroIfValueOfDatabaseFieldIsNotDefinedInTca()
184 {
185 $input = [
186 'recordTypeValue' => '',
187 'processedTca' => [
188 'ctrl' => [
189 'type' => 'aField',
190 ],
191 'types' => [
192 '0' => 'foo',
193 ],
194 ],
195 'databaseRow' => [
196 'aField' => 3,
197 ],
198 ];
199
200 $expected = $input;
201 $expected['recordTypeValue'] = '0';
202
203 $this->assertSame($expected, $this->subject->addData($input));
204 }
205
206 /**
207 * @test
208 */
209 public function addDataSetsRecordTypeValueToZeroIfValueOfDatabaseFieldIsEmptyString()
210 {
211 $input = [
212 'recordTypeValue' => '',
213 'processedTca' => [
214 'ctrl' => [
215 'type' => 'aField',
216 ],
217 'types' => [
218 '0' => 'foo',
219 ],
220 ],
221 'databaseRow' => [
222 'aField' => '',
223 ],
224 ];
225
226 $expected = $input;
227 $expected['recordTypeValue'] = '0';
228
229 $this->assertSame($expected, $this->subject->addData($input));
230 }
231
232 /**
233 * @test
234 */
235 public function addDataThrowsExceptionIfValueTypesNotExistsAndNoFallbackExists()
236 {
237 $input = [
238 'tableName' => 'aTable',
239 'recordTypeValue' => '',
240 'processedTca' => [
241 'ctrl' => [
242 'type' => 'aField',
243 ],
244 'types' => [
245 '42' => 'foo',
246 ],
247 ],
248 'databaseRow' => [
249 'aField' => 23,
250 ],
251 ];
252
253 $this->expectException(\UnexpectedValueException::class);
254 $this->expectExceptionCode(1438185437);
255
256 $this->subject->addData($input);
257 }
258
259 /**
260 * @test
261 */
262 public function addDataThrowsExceptionForForeignTypeConfigurationNotAsSelectOrGroup()
263 {
264 $input = [
265 'recordTypeValue' => '',
266 'processedTca' => [
267 'ctrl' => [
268 'type' => 'localField:foreignField',
269 ],
270 'columns' => [
271 'localField' => [
272 'config' => [
273 'type' => 'input',
274 ],
275 ],
276 ],
277 'types' => [
278 '3' => 'foo',
279 ],
280 ],
281 ];
282
283 $this->expectException(\UnexpectedValueException::class);
284 $this->expectExceptionCode(1325862241);
285
286 $this->subject->addData($input);
287 }
288
289 /**
290 * @test
291 */
292 public function addDataThrowsExceptionForForeignTypeIfPointerConfigurationHasNoTable()
293 {
294 $input = [
295 'tableName' => 'aTable',
296 'recordTypeValue' => '',
297 'processedTca' => [
298 'ctrl' => [
299 'type' => 'localField:foreignField',
300 ],
301 'columns' => [
302 'localField' => [
303 'config' => [
304 'type' => 'select',
305 ],
306 ],
307 ],
308 'types' => [
309 '3' => 'foo',
310 ],
311 ],
312 'databaseRow' => [
313 'localField' => 3,
314 ],
315 ];
316
317 $this->expectException(\UnexpectedValueException::class);
318 $this->expectExceptionCode(1438253614);
319
320 $this->subject->addData($input);
321 }
322
323 /**
324 * @test
325 */
326 public function addDataSetsTypeValueFromForeignTableRecord()
327 {
328 $input = [
329 'recordTypeValue' => '',
330 'processedTca' => [
331 'ctrl' => [
332 'type' => 'localField:foreignField',
333 ],
334 'columns' => [
335 'localField' => [
336 'config' => [
337 'type' => 'select',
338 'foreign_table' => 'foreignTable',
339 ],
340 ],
341 ],
342 'types' => [
343 '3' => 'foo',
344 ],
345 ],
346 'databaseRow' => [
347 // Point to record 42 in foreignTable
348 'localField' => 42,
349 ],
350 ];
351
352 $foreignRecordResult = [
353 'foreignField' => 3,
354 ];
355
356 $this->subject->expects($this->once())
357 ->method('getDatabaseRow')
358 ->with('foreignTable', 42, 'foreignField')
359 ->willReturn($foreignRecordResult);
360
361 $expected = $input;
362 $expected['recordTypeValue'] = '3';
363
364 $this->assertSame($expected, $this->subject->addData($input));
365 }
366
367 /**
368 * @test
369 */
370 public function addDataSetsTypeValueFromNestedTcaGroupField()
371 {
372 $input = [
373 'recordTypeValue' => '',
374 'processedTca' => [
375 'ctrl' => [
376 'type' => 'uid_local:type',
377 ],
378 'columns' => [
379 'uid_local' => [
380 'config' => [
381 'type' => 'group',
382 'internal_type' => 'db',
383 'size' => 1,
384 'maxitems' => 1,
385 'minitems' => 0,
386 'allowed' => 'sys_file'
387 ],
388 ],
389 ],
390 'types' => [
391 '2' => 'foo',
392 ],
393 ],
394 'databaseRow' => [
395 // Processed group field
396 'uid_local' => [
397 [
398 'uid' => 222,
399 ],
400 ],
401 ],
402 ];
403
404 $foreignRecordResult = [
405 'type' => 2,
406 ];
407
408 $this->subject->expects($this->once())
409 ->method('getDatabaseRow')
410 ->with('sys_file', 222, 'type')
411 ->willReturn($foreignRecordResult);
412
413 $expected = $input;
414 $expected['recordTypeValue'] = '2';
415
416 $this->assertSame($expected, $this->subject->addData($input));
417 }
418 }