[!!!][TASK] Improve flex and TCA handling in FormEngine
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataProvider / TcaInputPlaceholdersTest.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\FormDataCompiler;
19 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaInputPlaceholderRecord;
20 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaInputPlaceholders;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Lang\LanguageService;
23
24 /**
25 * Test case
26 */
27 class TcaInputPlaceholdersTest extends \TYPO3\CMS\Components\TestingFramework\Core\UnitTestCase
28 {
29 /**
30 * @var TcaInputPlaceholders
31 */
32 protected $subject;
33
34 public function setUp()
35 {
36 $this->subject = new TcaInputPlaceholders();
37 }
38
39 /**
40 * @test
41 */
42 public function addDataRemovesEmptyPlaceholderOption()
43 {
44 $input = [
45 'tableName' => 'aTable',
46 'databaseRow' => [],
47 'processedTca' => [
48 'columns' => [
49 'aField' => [
50 'config' => [
51 'type' => 'input',
52 'placeholder' => '',
53 ]
54 ]
55 ],
56 ],
57 ];
58
59 $expected = $input;
60 unset($expected['processedTca']['columns']['aField']['config']['placeholder']);
61
62 $this->assertSame($expected, $this->subject->addData($input));
63 }
64
65 /**
66 * @test
67 */
68 public function addDataReturnsUnmodifiedSimpleStringPlaceholder()
69 {
70 $input = [
71 'tableName' => 'aTable',
72 'databaseRow' => [],
73 'processedTca' => [
74 'columns' => [
75 'aField' => [
76 'config' => [
77 'type' => 'input',
78 'placeholder' => 'aPlaceholder',
79 ]
80 ]
81 ],
82 ],
83 ];
84
85 $expected = $input;
86
87 $this->assertSame($expected, $this->subject->addData($input));
88 }
89
90 /**
91 * @test
92 */
93 public function addDataReturnsValueFromDatabaseRowAsPlaceholder()
94 {
95 $input = [
96 'tableName' => 'aTable',
97 'databaseRow' => [
98 'anotherField' => 'anotherPlaceholder'
99 ],
100 'processedTca' => [
101 'columns' => [
102 'aField' => [
103 'config' => [
104 'type' => 'input',
105 'placeholder' => '__row|anotherField',
106 ]
107 ]
108 ],
109 ],
110 ];
111
112 $expected = $input;
113 $expected['processedTca']['columns']['aField']['config']['placeholder'] = 'anotherPlaceholder';
114
115 $this->assertSame($expected, $this->subject->addData($input));
116 }
117
118 /**
119 * @test
120 */
121 public function addDataReturnsValueFromSelectTypeRelation()
122 {
123 $input = [
124 'tableName' => 'aTable',
125 'databaseRow' => [
126 'aField' => '',
127 'aRelationField' => ['42'],
128 ],
129 'processedTca' => [
130 'columns' => [
131 'aField' => [
132 'config' => [
133 'type' => 'input',
134 'placeholder' => '__row|aRelationField|aForeignField',
135 ]
136 ],
137 'aRelationField' => [
138 'config' => [
139 'type' => 'select',
140 'foreign_table' => 'aForeignTable'
141 ]
142 ]
143 ],
144 ],
145 ];
146
147 $aForeignTableInput = [
148 'tableName' => 'aForeignTable',
149 'databaseRow' => [
150 'aForeignField' => 'aForeignValue',
151 ],
152 'processedTca' => [
153 'columns' => [
154 'aForeignField' => [
155 'config' => [
156 'type' => 'input',
157 ]
158 ],
159 ],
160 ],
161 ];
162
163 /** @var FormDataCompiler|ObjectProphecy $formDataCompilerProphecy */
164 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
165 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
166 $formDataCompilerProphecy->compile([
167 'command' => 'edit',
168 'vanillaUid' => 42,
169 'tableName' => 'aForeignTable',
170 'inlineCompileExistingChildren' => false,
171 'columnsToProcess' => ['aForeignField']
172 ])
173 ->shouldBeCalled()
174 ->willReturn($aForeignTableInput);
175
176 $expected = $input;
177 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $aForeignTableInput['databaseRow']['aForeignField'];
178
179 $this->assertSame($expected, $this->subject->addData($input));
180 }
181
182 /**
183 * @test
184 */
185 public function addDataReturnsNoPlaceholderForNewSelectTypeRelation()
186 {
187 $input = [
188 'tableName' => 'aTable',
189 'databaseRow' => [
190 'aField' => '',
191 'aRelationField' => [],
192 ],
193 'processedTca' => [
194 'columns' => [
195 'aField' => [
196 'config' => [
197 'type' => 'input',
198 'placeholder' => '__row|aRelationField|aForeignField',
199 ]
200 ],
201 'aRelationField' => [
202 'config' => [
203 'type' => 'select',
204 'foreign_table' => 'aForeignTable'
205 ]
206 ]
207 ],
208 ],
209 ];
210
211 $expected = $input;
212 unset($expected['processedTca']['columns']['aField']['config']['placeholder']);
213
214 $this->assertSame($expected, $this->subject->addData($input));
215 }
216
217 /**
218 * @test
219 */
220 public function addDataReturnsValueFromGroupTypeRelation()
221 {
222 $input = [
223 'tableName' => 'aTable',
224 'databaseRow' => [
225 'aField' => '',
226 'uid_local' => [
227 [
228 'uid' => 3,
229 'table' => 'sys_file',
230 ],
231 [
232 'uid' => 5,
233 'table' => 'sys_file',
234 ],
235 ],
236 ],
237 'processedTca' => [
238 'columns' => [
239 'aField' => [
240 'config' => [
241 'type' => 'input',
242 'placeholder' => '__row|uid_local|sha1',
243 ]
244 ],
245 'uid_local' => [
246 'config' => [
247 'type' => 'group',
248 'internal_type' => 'db',
249 'allowed' => 'sys_file'
250 ]
251 ]
252 ],
253 ],
254 ];
255
256 $sysFileProphecyResult = [
257 'tableName' => 'sys_file',
258 'databaseRow' => [
259 'sha1' => 'aSha1Value',
260 ],
261 'processedTca' => [
262 'columns' => [
263 'sha1' => [
264 'config' => [
265 'type' => 'input',
266 ]
267 ],
268 ],
269 ],
270 ];
271
272 /** @var TcaInputPlaceholderRecord $languageService */
273 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
274 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
275 $formDataCompilerProphecy->compile([
276 'command' => 'edit',
277 'vanillaUid' => 3,
278 'tableName' => 'sys_file',
279 'inlineCompileExistingChildren' => false,
280 'columnsToProcess' => ['sha1']
281 ])
282 ->shouldBeCalled()
283 ->willReturn($sysFileProphecyResult);
284
285 $expected = $input;
286 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileProphecyResult['databaseRow']['sha1'];
287
288 $this->assertSame($expected, $this->subject->addData($input));
289 }
290
291 /**
292 * @test
293 */
294 public function addDataReturnsValueFromInlineTypeRelation()
295 {
296 $input = [
297 'tableName' => 'aTable',
298 'databaseRow' => [
299 'aField' => '',
300 'metadata' => '2',
301 ],
302 'processedTca' => [
303 'columns' => [
304 'aField' => [
305 'config' => [
306 'type' => 'input',
307 'placeholder' => '__row|metadata|title',
308 ]
309 ],
310 'metadata' => [
311 'config' => [
312 'readOnly' => true,
313 'type' => 'inline',
314 'foreign_table' => 'sys_file_metadata',
315 'foreign_field' => 'file',
316 ]
317 ]
318 ],
319 ],
320 ];
321
322 $sysFileMetadataProphecyResult = [
323 'tableName' => 'sys_file_metadata',
324 'databaseRow' => [
325 'title' => 'aTitle',
326 ],
327 'processedTca' => [
328 'columns' => [
329 'sha1' => [
330 'config' => [
331 'type' => 'input',
332 ]
333 ],
334 ],
335 ],
336 ];
337
338 /** @var TcaInputPlaceholderRecord $languageService */
339 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
340 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
341 $formDataCompilerProphecy->compile([
342 'command' => 'edit',
343 'vanillaUid' => 2,
344 'tableName' => 'sys_file_metadata',
345 'inlineCompileExistingChildren' => false,
346 'columnsToProcess' => ['title']
347 ])
348 ->shouldBeCalled()
349 ->willReturn($sysFileMetadataProphecyResult);
350
351 $expected = $input;
352 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataProphecyResult['databaseRow']['title'];
353
354 $this->assertSame($expected, $this->subject->addData($input));
355 }
356
357 /**
358 * @test
359 */
360 public function addDataReturnsValueFromRelationsRecursively()
361 {
362 $input = [
363 'tableName' => 'aTable',
364 'databaseRow' => [
365 'aField' => '',
366 'uid_local' => [
367 [
368 'uid' => 3,
369 'table' => 'sys_file',
370 ],
371 [
372 'uid' => 5,
373 'table' => 'sys_file',
374 ],
375 ],
376 ],
377 'processedTca' => [
378 'columns' => [
379 'aField' => [
380 'config' => [
381 'type' => 'input',
382 'placeholder' => '__row|uid_local|metadata|title',
383 ]
384 ],
385 'uid_local' => [
386 'config' => [
387 'type' => 'group',
388 'internal_type' => 'db',
389 'allowed' => 'sys_file'
390 ]
391 ]
392 ],
393 ],
394 ];
395
396 $sysFileProphecyResult = [
397 'tableName' => 'sys_file',
398 'databaseRow' => [
399 'metadata' => '7',
400 ],
401 'processedTca' => [
402 'columns' => [
403 'metadata' => [
404 'config' => [
405 'readOnly' => true,
406 'type' => 'inline',
407 'foreign_table' => 'sys_file_metadata',
408 'foreign_field' => 'file',
409 ]
410 ]
411 ],
412 ],
413 ];
414
415 $sysFileMetadataProphecyResult = [
416 'tableName' => 'sys_file_metadata',
417 'databaseRow' => [
418 'title' => 'aTitle',
419 ],
420 'processedTca' => [
421 'columns' => [
422 'sha1' => [
423 'config' => [
424 'type' => 'input',
425 ]
426 ],
427 ],
428 ],
429 ];
430
431 $sysFileFormDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
432 GeneralUtility::addInstance(FormDataCompiler::class, $sysFileFormDataCompilerProphecy->reveal());
433 $sysFileFormDataCompilerProphecy->compile([
434 'command' => 'edit',
435 'vanillaUid' => 3,
436 'tableName' => 'sys_file',
437 'inlineCompileExistingChildren' => false,
438 'columnsToProcess' => ['metadata']
439 ])
440 ->shouldBeCalled()
441 ->willReturn($sysFileProphecyResult);
442
443 $sysFileMetaDataFormDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
444 GeneralUtility::addInstance(FormDataCompiler::class, $sysFileMetaDataFormDataCompilerProphecy->reveal());
445 $sysFileMetaDataFormDataCompilerProphecy->compile([
446 'command' => 'edit',
447 'vanillaUid' => 7,
448 'tableName' => 'sys_file_metadata',
449 'inlineCompileExistingChildren' => false,
450 'columnsToProcess' => ['title']
451 ])
452 ->shouldBeCalled()
453 ->willReturn($sysFileMetadataProphecyResult);
454
455 $expected = $input;
456 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataProphecyResult['databaseRow']['title'];
457
458 $this->assertSame($expected, $this->subject->addData($input));
459 }
460
461 /**
462 * @test
463 */
464 public function addDataCallsLanguageServiceForLocalizedPlaceholders()
465 {
466 $labelString = 'LLL:EXT:some_ext/Resources/Private/Language/locallang.xlf:my_placeholder';
467 $localizedString = 'My Placeholder';
468 $input = [
469 'tableName' => 'aTable',
470 'databaseRow' => [],
471 'processedTca' => [
472 'columns' => [
473 'aField' => [
474 'config' => [
475 'type' => 'input',
476 'placeholder' => $labelString,
477 ]
478 ]
479 ],
480 ],
481 ];
482 $expected = $input;
483 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $localizedString;
484
485 /** @var LanguageService|ObjectProphecy $languageService */
486 $languageService = $this->prophesize(LanguageService::class);
487 $GLOBALS['LANG'] = $languageService->reveal();
488 $languageService->sL($labelString)->shouldBeCalled()->willReturn($localizedString);
489
490 $this->assertSame($expected, $this->subject->addData($input));
491 }
492 }