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