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