289c047dfd7ac4e63417288ec3562f4c37097d2c
[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 TYPO3\CMS\Backend\Form\FormDataCompiler;
18 use TYPO3\CMS\Backend\Form\FormDataGroup\TcaInputPlaceholderRecord;
19 use TYPO3\CMS\Backend\Form\FormDataProvider\TcaInputPlaceholders;
20 use TYPO3\CMS\Core\Tests\UnitTestCase;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Lang\LanguageService;
23
24 /**
25 * Test case
26 */
27 class TcaInputPlaceholdersTest extends 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 TcaInputPlaceholderRecord $languageService */
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 addDataReturnsValueFromGroupTypeRelation()
186 {
187 $input = [
188 'tableName' => 'aTable',
189 'databaseRow' => [
190 'aField' => '',
191 'uid_local' => 'sys_file_3|aLabel,sys_file_5|anotherLabel',
192 ],
193 'processedTca' => [
194 'columns' => [
195 'aField' => [
196 'config' => [
197 'type' => 'input',
198 'placeholder' => '__row|uid_local|sha1',
199 ]
200 ],
201 'uid_local' => [
202 'config' => [
203 'type' => 'group',
204 'internal_type' => 'db',
205 'allowed' => 'sys_file'
206 ]
207 ]
208 ],
209 ],
210 ];
211
212 $sysFileProphecyResult = [
213 'tableName' => 'sys_file',
214 'databaseRow' => [
215 'sha1' => 'aSha1Value',
216 ],
217 'processedTca' => [
218 'columns' => [
219 'sha1' => [
220 'config' => [
221 'type' => 'input',
222 ]
223 ],
224 ],
225 ],
226 ];
227
228 /** @var TcaInputPlaceholderRecord $languageService */
229 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
230 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
231 $formDataCompilerProphecy->compile([
232 'command' => 'edit',
233 'vanillaUid' => 3,
234 'tableName' => 'sys_file',
235 'inlineCompileExistingChildren' => false,
236 'columnsToProcess' => ['sha1']
237 ])
238 ->shouldBeCalled()
239 ->willReturn($sysFileProphecyResult);
240
241 $expected = $input;
242 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileProphecyResult['databaseRow']['sha1'];
243
244 $this->assertSame($expected, $this->subject->addData($input));
245 }
246
247 /**
248 * @test
249 */
250 public function addDataReturnsValueFromInlineTypeRelation()
251 {
252 $input = [
253 'tableName' => 'aTable',
254 'databaseRow' => [
255 'aField' => '',
256 'metadata' => '2',
257 ],
258 'processedTca' => [
259 'columns' => [
260 'aField' => [
261 'config' => [
262 'type' => 'input',
263 'placeholder' => '__row|metadata|title',
264 ]
265 ],
266 'metadata' => [
267 'config' => [
268 'readOnly' => 1,
269 'type' => 'inline',
270 'foreign_table' => 'sys_file_metadata',
271 'foreign_field' => 'file',
272 ]
273 ]
274 ],
275 ],
276 ];
277
278 $sysFileMetadataProphecyResult = [
279 'tableName' => 'sys_file_metadata',
280 'databaseRow' => [
281 'title' => 'aTitle',
282 ],
283 'processedTca' => [
284 'columns' => [
285 'sha1' => [
286 'config' => [
287 'type' => 'input',
288 ]
289 ],
290 ],
291 ],
292 ];
293
294 /** @var TcaInputPlaceholderRecord $languageService */
295 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
296 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
297 $formDataCompilerProphecy->compile([
298 'command' => 'edit',
299 'vanillaUid' => 2,
300 'tableName' => 'sys_file_metadata',
301 'inlineCompileExistingChildren' => false,
302 'columnsToProcess' => ['title']
303 ])
304 ->shouldBeCalled()
305 ->willReturn($sysFileMetadataProphecyResult);
306
307 $expected = $input;
308 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataProphecyResult['databaseRow']['title'];
309
310 $this->assertSame($expected, $this->subject->addData($input));
311 }
312
313 /**
314 * @test
315 */
316 public function addDataReturnsValueFromRelationsRecursively()
317 {
318 $input = [
319 'tableName' => 'aTable',
320 'databaseRow' => [
321 'aField' => '',
322 'uid_local' => 'sys_file_3|aLabel,sys_file_5|anotherLabel',
323 ],
324 'processedTca' => [
325 'columns' => [
326 'aField' => [
327 'config' => [
328 'type' => 'input',
329 'placeholder' => '__row|uid_local|metadata|title',
330 ]
331 ],
332 'uid_local' => [
333 'config' => [
334 'type' => 'group',
335 'internal_type' => 'db',
336 'allowed' => 'sys_file'
337 ]
338 ]
339 ],
340 ],
341 ];
342
343 $sysFileProphecyResult = [
344 'tableName' => 'sys_file',
345 'databaseRow' => [
346 'metadata' => '7',
347 ],
348 'processedTca' => [
349 'columns' => [
350 'metadata' => [
351 'config' => [
352 'readOnly' => 1,
353 'type' => 'inline',
354 'foreign_table' => 'sys_file_metadata',
355 'foreign_field' => 'file',
356 ]
357 ]
358 ],
359 ],
360 ];
361
362 $sysFileMetadataProphecyResult = [
363 'tableName' => 'sys_file_metadata',
364 'databaseRow' => [
365 'title' => 'aTitle',
366 ],
367 'processedTca' => [
368 'columns' => [
369 'sha1' => [
370 'config' => [
371 'type' => 'input',
372 ]
373 ],
374 ],
375 ],
376 ];
377
378 $sysFileFormDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
379 GeneralUtility::addInstance(FormDataCompiler::class, $sysFileFormDataCompilerProphecy->reveal());
380 $sysFileFormDataCompilerProphecy->compile([
381 'command' => 'edit',
382 'vanillaUid' => 3,
383 'tableName' => 'sys_file',
384 'inlineCompileExistingChildren' => false,
385 'columnsToProcess' => ['metadata']
386 ])
387 ->shouldBeCalled()
388 ->willReturn($sysFileProphecyResult);
389
390 $sysFileMetaDataFormDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
391 GeneralUtility::addInstance(FormDataCompiler::class, $sysFileMetaDataFormDataCompilerProphecy->reveal());
392 $sysFileMetaDataFormDataCompilerProphecy->compile([
393 'command' => 'edit',
394 'vanillaUid' => 7,
395 'tableName' => 'sys_file_metadata',
396 'inlineCompileExistingChildren' => false,
397 'columnsToProcess' => ['title']
398 ])
399 ->shouldBeCalled()
400 ->willReturn($sysFileMetadataProphecyResult);
401
402 $expected = $input;
403 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataProphecyResult['databaseRow']['title'];
404
405 $this->assertSame($expected, $this->subject->addData($input));
406 }
407
408 /**
409 * @test
410 */
411 public function addDataCallsLanguageServiceForLocalizedPlaceholders()
412 {
413 $labelString = 'LLL:EXT:some_ext/Resources/Private/Language/locallang.xlf:my_placeholder';
414 $localizedString = 'My Placeholder';
415 $input = [
416 'tableName' => 'aTable',
417 'databaseRow' => [],
418 'processedTca' => [
419 'columns' => [
420 'aField' => [
421 'config' => [
422 'type' => 'input',
423 'placeholder' => $labelString,
424 ]
425 ]
426 ],
427 ],
428 ];
429 $expected = $input;
430 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $localizedString;
431
432 /** @var LanguageService|ObjectProphecy $languageService */
433 $languageService = $this->prophesize(LanguageService::class);
434 $GLOBALS['LANG'] = $languageService->reveal();
435 $languageService->sL($labelString)->shouldBeCalled()->willReturn($localizedString);
436
437 $this->assertSame($expected, $this->subject->addData($input));
438 }
439 }