a02dc9199f0da2348dc1d500930a538fe5343b7f
[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
19 /**
20 * Test case
21 */
22 class DatabaseRecordTypeValueTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
23 {
24 /**
25 * Subject is not notice free, disable E_NOTICES
26 */
27 protected static $suppressNotices = true;
28
29 /**
30 * @var DatabaseRecordTypeValue|\PHPUnit_Framework_MockObject_MockObject
31 */
32 protected $subject;
33
34 protected function setUp()
35 {
36 $this->subject = $this->getMockBuilder(DatabaseRecordTypeValue::class)
37 ->setMethods(['getDatabaseRow'])
38 ->getMock();
39 }
40
41 /**
42 * @test
43 */
44 public function addDataThrowsExceptionIfTcaTypesAreEmpty()
45 {
46 $input = [
47 'processedTca' => [
48 'types' => [],
49 ],
50 ];
51
52 $this->expectException(\UnexpectedValueException::class);
53 $this->expectExceptionCode(1438185331);
54
55 $this->subject->addData($input);
56 }
57
58 /**
59 * @test
60 */
61 public function addDataKeepsExistingTcaRecordTypeValue()
62 {
63 $input = [
64 'recordTypeValue' => 'egon',
65 'processedTca' => [
66 'types' => [
67 '1' => 'foo',
68 ],
69 ],
70 ];
71 $expected = $input;
72 $this->assertSame($expected, $this->subject->addData($input));
73 }
74
75 /**
76 * @test
77 */
78 public function addDataKeepsExistingTcaRecordTypeValueWithValueZero()
79 {
80 $input = [
81 'recordTypeValue' => 0,
82 'processedTca' => [
83 'types' => [
84 '1' => 'foo',
85 ],
86 ],
87 ];
88 $expected = $input;
89 $this->assertSame($expected, $this->subject->addData($input));
90 }
91
92 /**
93 * @test
94 */
95 public function addDataSetsRecordTypeValueToHistoricalOneIfTypeZeroIsNotDefined()
96 {
97 $input = [
98 'recordTypeValue' => '',
99 'processedTca' => [
100 'types' => [
101 '1' => 'foo',
102 ],
103 ],
104 ];
105 $expected = $input;
106 $expected['recordTypeValue'] = '1';
107 $this->assertSame($expected, $this->subject->addData($input));
108 }
109
110 /**
111 * @test
112 */
113 public function addDataSetsRecordTypeValueToZero()
114 {
115 $input = [
116 'recordTypeValue' => '',
117 'processedTca' => [
118 'types' => [
119 '0' => 'foo',
120 ],
121 ],
122 ];
123
124 $expected = $input;
125 $expected['recordTypeValue'] = '0';
126
127 $this->assertSame($expected, $this->subject->addData($input));
128 }
129
130 /**
131 * @test
132 */
133 public function addDataThrowsExceptionIfTypePointsToANotExistingField()
134 {
135 $input = [
136 'recordTypeValue' => '',
137 'processedTca' => [
138 'ctrl' => [
139 'type' => 'notExists',
140 ],
141 'types' => [
142 '0' => 'foo',
143 ],
144 ],
145 'databaseRow' => [
146 'uid' => 23,
147 ],
148 ];
149
150 $this->expectException(\UnexpectedValueException::class);
151 $this->expectExceptionCode(1438183881);
152
153 $this->subject->addData($input);
154 }
155
156 /**
157 * @test
158 */
159 public function addDataSetsRecordTypeValueToValueOfDatabaseField()
160 {
161 $input = [
162 'recordTypeValue' => '',
163 'processedTca' => [
164 'ctrl' => [
165 'type' => 'aField',
166 ],
167 'types' => [
168 '3' => 'foo',
169 ],
170 ],
171 'databaseRow' => [
172 'aField' => 3,
173 ],
174 ];
175
176 $expected = $input;
177 $expected['recordTypeValue'] = '3';
178
179 $this->assertSame($expected, $this->subject->addData($input));
180 }
181
182 /**
183 * @test
184 */
185 public function addDataSetsRecordTypeValueToZeroIfValueOfDatabaseFieldIsNotDefinedInTca()
186 {
187 $input = [
188 'recordTypeValue' => '',
189 'processedTca' => [
190 'ctrl' => [
191 'type' => 'aField',
192 ],
193 'types' => [
194 '0' => 'foo',
195 ],
196 ],
197 'databaseRow' => [
198 'aField' => 3,
199 ],
200 ];
201
202 $expected = $input;
203 $expected['recordTypeValue'] = '0';
204
205 $this->assertSame($expected, $this->subject->addData($input));
206 }
207
208 /**
209 * @test
210 */
211 public function addDataSetsRecordTypeValueToZeroIfValueOfDatabaseFieldIsEmptyString()
212 {
213 $input = [
214 'recordTypeValue' => '',
215 'processedTca' => [
216 'ctrl' => [
217 'type' => 'aField',
218 ],
219 'types' => [
220 '0' => 'foo',
221 ],
222 ],
223 'databaseRow' => [
224 'aField' => '',
225 ],
226 ];
227
228 $expected = $input;
229 $expected['recordTypeValue'] = '0';
230
231 $this->assertSame($expected, $this->subject->addData($input));
232 }
233
234 /**
235 * @test
236 */
237 public function addDataThrowsExceptionIfValueTypesNotExistsAndNoFallbackExists()
238 {
239 $input = [
240 'recordTypeValue' => '',
241 'processedTca' => [
242 'ctrl' => [
243 'type' => 'aField',
244 ],
245 'types' => [
246 '42' => 'foo',
247 ],
248 ],
249 'databaseRow' => [
250 'aField' => 23,
251 ],
252 ];
253
254 $this->expectException(\UnexpectedValueException::class);
255 $this->expectExceptionCode(1438185437);
256
257 $this->subject->addData($input);
258 }
259
260 /**
261 * @test
262 */
263 public function addDataThrowsExceptionForForeignTypeConfigurationNotAsSelectOrGroup()
264 {
265 $input = [
266 'recordTypeValue' => '',
267 'processedTca' => [
268 'ctrl' => [
269 'type' => 'localField:foreignField',
270 ],
271 'columns' => [
272 'localField' => [
273 'config' => [
274 'type' => 'input',
275 ],
276 ],
277 ],
278 'types' => [
279 '3' => 'foo',
280 ],
281 ],
282 ];
283
284 $this->expectException(\UnexpectedValueException::class);
285 $this->expectExceptionCode(1325862241);
286
287 $this->subject->addData($input);
288 }
289
290 /**
291 * @test
292 */
293 public function addDataThrowsExceptionForForeignTypeIfPointerConfigurationHasNoTable()
294 {
295 $input = [
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 }