‪TYPO3CMS  ‪main
TcaInputPlaceholdersTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\Test;
26 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
27 
28 final class ‪TcaInputPlaceholdersTest extends UnitTestCase
29 {
30  protected function ‪setUp(): void
31  {
32  parent::setUp();
33  $languageService = $this->createMock(LanguageService::class);
34  $languageService->method('sL')->withAnyParameters()->willReturnArgument(0);
35  ‪$GLOBALS['LANG'] = $languageService;
36  }
37 
38  #[Test]
40  {
41  $input = [
42  'tableName' => 'aTable',
43  'databaseRow' => [],
44  'processedTca' => [
45  'columns' => [
46  'aField' => [
47  'config' => [
48  'type' => 'input',
49  'placeholder' => '',
50  ],
51  ],
52  ],
53  ],
54  ];
55 
56  $expected = $input;
57  unset($expected['processedTca']['columns']['aField']['config']['placeholder']);
58 
59  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
60  }
61 
62  #[Test]
64  {
65  $input = [
66  'tableName' => 'aTable',
67  'databaseRow' => [],
68  'processedTca' => [
69  'columns' => [
70  'aField' => [
71  'config' => [
72  'type' => 'input',
73  'placeholder' => 'aPlaceholder',
74  ],
75  ],
76  ],
77  ],
78  ];
79 
80  $expected = $input;
81 
82  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
83  }
84 
85  #[Test]
87  {
88  $input = [
89  'tableName' => 'aTable',
90  'databaseRow' => [
91  'anotherField' => 'anotherPlaceholder',
92  ],
93  'processedTca' => [
94  'columns' => [
95  'aField' => [
96  'config' => [
97  'type' => 'input',
98  'placeholder' => '__row|anotherField',
99  ],
100  ],
101  ],
102  ],
103  ];
104 
105  $expected = $input;
106  $expected['processedTca']['columns']['aField']['config']['placeholder'] = 'anotherPlaceholder';
107 
108  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
109  }
110 
111  #[Test]
113  {
114  $request = new ‪ServerRequest();
115  $input = [
116  'request' => $request,
117  'tableName' => 'aTable',
118  'databaseRow' => [
119  'aField' => '',
120  'aRelationField' => ['42'],
121  ],
122  'processedTca' => [
123  'columns' => [
124  'aField' => [
125  'config' => [
126  'type' => 'input',
127  'placeholder' => '__row|aRelationField|aForeignField',
128  ],
129  ],
130  'aRelationField' => [
131  'config' => [
132  'type' => 'select',
133  'foreign_table' => 'aForeignTable',
134  ],
135  ],
136  ],
137  ],
138  ];
139 
140  $aForeignTableInput = [
141  'request' => $request,
142  'tableName' => 'aForeignTable',
143  'databaseRow' => [
144  'aForeignField' => 'aForeignValue',
145  ],
146  'processedTca' => [
147  'columns' => [
148  'aForeignField' => [
149  'config' => [
150  'type' => 'input',
151  ],
152  ],
153  ],
154  ],
155  ];
156 
157  $formDataCompilerMock = $this->createMock(FormDataCompiler::class);
158  GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerMock);
159  $formDataCompilerMock->expects(self::atLeastOnce())->method('compile')->with([
160  'request' => $request,
161  'command' => 'edit',
162  'vanillaUid' => 42,
163  'tableName' => 'aForeignTable',
164  'inlineCompileExistingChildren' => false,
165  'columnsToProcess' => ['aForeignField'],
166  ])
167  ->willReturn($aForeignTableInput);
168 
169  $expected = $input;
170  $expected['processedTca']['columns']['aField']['config']['placeholder'] = $aForeignTableInput['databaseRow']['aForeignField'];
171 
172  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
173  }
174 
175  #[Test]
177  {
178  $input = [
179  'tableName' => 'aTable',
180  'databaseRow' => [
181  'aField' => '',
182  'aRelationField' => [],
183  ],
184  'processedTca' => [
185  'columns' => [
186  'aField' => [
187  'config' => [
188  'type' => 'input',
189  'placeholder' => '__row|aRelationField|aForeignField',
190  ],
191  ],
192  'aRelationField' => [
193  'config' => [
194  'type' => 'select',
195  'foreign_table' => 'aForeignTable',
196  ],
197  ],
198  ],
199  ],
200  ];
201 
202  $expected = $input;
203  unset($expected['processedTca']['columns']['aField']['config']['placeholder']);
204 
205  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
206  }
207 
208  #[Test]
210  {
211  $request = new ‪ServerRequest();
212  $input = [
213  'request' => $request,
214  'tableName' => 'aTable',
215  'databaseRow' => [
216  'aField' => '',
217  'uid_local' => [
218  [
219  'uid' => 3,
220  'table' => 'sys_file',
221  ],
222  [
223  'uid' => 5,
224  'table' => 'sys_file',
225  ],
226  ],
227  ],
228  'processedTca' => [
229  'columns' => [
230  'aField' => [
231  'config' => [
232  'type' => 'input',
233  'placeholder' => '__row|uid_local|sha1',
234  ],
235  ],
236  'uid_local' => [
237  'config' => [
238  'type' => 'group',
239  'allowed' => 'sys_file',
240  ],
241  ],
242  ],
243  ],
244  ];
245 
246  $sysFileMockResult = [
247  'request' => $request,
248  'tableName' => 'sys_file',
249  'databaseRow' => [
250  'sha1' => 'aSha1Value',
251  ],
252  'processedTca' => [
253  'columns' => [
254  'sha1' => [
255  'config' => [
256  'type' => 'input',
257  ],
258  ],
259  ],
260  ],
261  ];
262 
263  $formDataCompilerMock = $this->createMock(FormDataCompiler::class);
264  GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerMock);
265  $formDataCompilerMock->expects(self::atLeastOnce())->method('compile')->with([
266  'request' => $request,
267  'command' => 'edit',
268  'vanillaUid' => 3,
269  'tableName' => 'sys_file',
270  'inlineCompileExistingChildren' => false,
271  'columnsToProcess' => ['sha1'],
272  ])
273  ->willReturn($sysFileMockResult);
274 
275  $expected = $input;
276  $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMockResult['databaseRow']['sha1'];
277 
278  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
279  }
280 
281  #[Test]
283  {
284  $request = new ‪ServerRequest();
285  $input = [
286  'request' => $request,
287  'tableName' => 'aTable',
288  'databaseRow' => [
289  'aField' => '',
290  'metadata' => '2',
291  ],
292  'processedTca' => [
293  'columns' => [
294  'aField' => [
295  'config' => [
296  'type' => 'input',
297  'placeholder' => '__row|metadata|title',
298  ],
299  ],
300  'metadata' => [
301  'config' => [
302  'readOnly' => true,
303  'type' => 'inline',
304  'foreign_table' => 'sys_file_metadata',
305  'foreign_field' => 'file',
306  ],
307  ],
308  ],
309  ],
310  ];
311 
312  $sysFileMetadataMockResult = [
313  'request' => $request,
314  'tableName' => 'sys_file_metadata',
315  'databaseRow' => [
316  'title' => 'aTitle',
317  ],
318  'processedTca' => [
319  'columns' => [
320  'sha1' => [
321  'config' => [
322  'type' => 'input',
323  ],
324  ],
325  ],
326  ],
327  ];
328 
329  $formDataCompilerMock = $this->createMock(FormDataCompiler::class);
330  GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerMock);
331  $formDataCompilerMock->expects(self::atLeastOnce())->method('compile')->with([
332  'request' => $request,
333  'command' => 'edit',
334  'vanillaUid' => 2,
335  'tableName' => 'sys_file_metadata',
336  'inlineCompileExistingChildren' => false,
337  'columnsToProcess' => ['title'],
338  ])
339  ->willReturn($sysFileMetadataMockResult);
340 
341  $expected = $input;
342  $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataMockResult['databaseRow']['title'];
343 
344  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
345  }
346 
347  #[Test]
349  {
350  $request = new ‪ServerRequest();
351  $input = [
352  'request' => $request,
353  'tableName' => 'aTable',
354  'databaseRow' => [
355  'aField' => '',
356  'uid_local' => [
357  [
358  'uid' => 3,
359  'table' => 'sys_file',
360  ],
361  [
362  'uid' => 5,
363  'table' => 'sys_file',
364  ],
365  ],
366  ],
367  'processedTca' => [
368  'columns' => [
369  'aField' => [
370  'config' => [
371  'type' => 'input',
372  'placeholder' => '__row|uid_local|metadata|title',
373  ],
374  ],
375  'uid_local' => [
376  'config' => [
377  'type' => 'group',
378  'allowed' => 'sys_file',
379  ],
380  ],
381  ],
382  ],
383  ];
384 
385  $sysFileMockResult = [
386  'request' => $request,
387  'tableName' => 'sys_file',
388  'databaseRow' => [
389  'metadata' => '7',
390  ],
391  'processedTca' => [
392  'columns' => [
393  'metadata' => [
394  'config' => [
395  'readOnly' => true,
396  'type' => 'inline',
397  'foreign_table' => 'sys_file_metadata',
398  'foreign_field' => 'file',
399  ],
400  ],
401  ],
402  ],
403  ];
404 
405  $sysFileMetadataMockResult = [
406  'request' => $request,
407  'tableName' => 'sys_file_metadata',
408  'databaseRow' => [
409  'title' => 'aTitle',
410  ],
411  'processedTca' => [
412  'columns' => [
413  'sha1' => [
414  'config' => [
415  'type' => 'input',
416  ],
417  ],
418  ],
419  ],
420  ];
421 
422  $sysFileFormDataCompilerMock = $this->createMock(FormDataCompiler::class);
423  GeneralUtility::addInstance(FormDataCompiler::class, $sysFileFormDataCompilerMock);
424  $sysFileFormDataCompilerMock->expects(self::atLeastOnce())->method('compile')->with([
425  'request' => $request,
426  'command' => 'edit',
427  'vanillaUid' => 3,
428  'tableName' => 'sys_file',
429  'inlineCompileExistingChildren' => false,
430  'columnsToProcess' => ['metadata'],
431  ])
432  ->willReturn($sysFileMockResult);
433 
434  $sysFileMetaDataFormDataCompilerMock = $this->createMock(FormDataCompiler::class);
435  GeneralUtility::addInstance(FormDataCompiler::class, $sysFileMetaDataFormDataCompilerMock);
436  $sysFileMetaDataFormDataCompilerMock->expects(self::atLeastOnce())->method('compile')->with([
437  'request' => $request,
438  'command' => 'edit',
439  'vanillaUid' => 7,
440  'tableName' => 'sys_file_metadata',
441  'inlineCompileExistingChildren' => false,
442  'columnsToProcess' => ['title'],
443  ])
444  ->willReturn($sysFileMetadataMockResult);
445 
446  $expected = $input;
447  $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataMockResult['databaseRow']['title'];
448 
449  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
450  }
451 
452  #[Test]
454  {
455  $labelString = 'LLL:EXT:some_ext/Resources/Private/Language/locallang.xlf:my_placeholder';
456  $localizedString = 'My Placeholder';
457  $input = [
458  'tableName' => 'aTable',
459  'databaseRow' => [],
460  'processedTca' => [
461  'columns' => [
462  'aField' => [
463  'config' => [
464  'type' => 'input',
465  'placeholder' => $labelString,
466  ],
467  ],
468  ],
469  ],
470  ];
471  $expected = $input;
472  $expected['processedTca']['columns']['aField']['config']['placeholder'] = $localizedString;
473 
474  $languageService = $this->createMock(LanguageService::class);
475  ‪$GLOBALS['LANG'] = $languageService;
476  $languageService->expects(self::atLeastOnce())->method('sL')->with($labelString)->willReturn($localizedString);
477 
478  self::assertSame($expected, (new ‪TcaInputPlaceholders())->addData($input));
479  }
480 }
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataReturnsValueFromSelectTypeRelation
‪addDataReturnsValueFromSelectTypeRelation()
Definition: TcaInputPlaceholdersTest.php:112
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaInputPlaceholders
Definition: TcaInputPlaceholders.php:32
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest
Definition: TcaInputPlaceholdersTest.php:29
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataReturnsUnmodifiedSimpleStringPlaceholder
‪addDataReturnsUnmodifiedSimpleStringPlaceholder()
Definition: TcaInputPlaceholdersTest.php:63
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataRemovesEmptyPlaceholderOption
‪addDataRemovesEmptyPlaceholderOption()
Definition: TcaInputPlaceholdersTest.php:39
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataCallsLanguageServiceForLocalizedPlaceholders
‪addDataCallsLanguageServiceForLocalizedPlaceholders()
Definition: TcaInputPlaceholdersTest.php:453
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\setUp
‪setUp()
Definition: TcaInputPlaceholdersTest.php:30
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataReturnsValueFromRelationsRecursively
‪addDataReturnsValueFromRelationsRecursively()
Definition: TcaInputPlaceholdersTest.php:348
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataReturnsValueFromDatabaseRowAsPlaceholder
‪addDataReturnsValueFromDatabaseRowAsPlaceholder()
Definition: TcaInputPlaceholdersTest.php:86
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataReturnsValueFromGroupTypeRelation
‪addDataReturnsValueFromGroupTypeRelation()
Definition: TcaInputPlaceholdersTest.php:209
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Form\FormDataCompiler
Definition: FormDataCompiler.php:26
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataReturnsValueFromInlineTypeRelation
‪addDataReturnsValueFromInlineTypeRelation()
Definition: TcaInputPlaceholdersTest.php:282
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaInputPlaceholdersTest\addDataReturnsNoPlaceholderForNewSelectTypeRelation
‪addDataReturnsNoPlaceholderForNewSelectTypeRelation()
Definition: TcaInputPlaceholdersTest.php:176
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18