[TASK] FormEngine: Replace placeholder resolving with a data provider
[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\Tests\UnitTestCase;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 $this->subject = new TcaInputPlaceholders();
36 }
37
38 /**
39 * @test
40 */
41 public function addDataRemovesEmptyPlaceholderOption() {
42 $input = [
43 'tableName' => 'aTable',
44 'databaseRow' => [],
45 'processedTca' => [
46 'columns' => [
47 'aField' => [
48 'config' => [
49 'type' => 'input',
50 'placeholder' => '',
51 ]
52 ]
53 ],
54 ],
55 ];
56
57 $expected = $input;
58 unset($expected['processedTca']['columns']['aField']['config']['placeholder']);
59
60 $this->assertSame($expected, $this->subject->addData($input));
61 }
62
63 /**
64 * @test
65 */
66 public function addDataReturnsUnmodifiedSimpleStringPlaceholder() {
67 $input = [
68 'tableName' => 'aTable',
69 'databaseRow' => [],
70 'processedTca' => [
71 'columns' => [
72 'aField' => [
73 'config' => [
74 'type' => 'input',
75 'placeholder' => 'aPlaceholder',
76 ]
77 ]
78 ],
79 ],
80 ];
81
82 $expected = $input;
83
84 $this->assertSame($expected, $this->subject->addData($input));
85 }
86
87 /**
88 * @test
89 */
90 public function addDataReturnsValueFromDatabaseRowAsPlaceholder() {
91 $input = [
92 'tableName' => 'aTable',
93 'databaseRow' => [
94 'anotherField' => 'anotherPlaceholder'
95 ],
96 'processedTca' => [
97 'columns' => [
98 'aField' => [
99 'config' => [
100 'type' => 'input',
101 'placeholder' => '__row|anotherField',
102 ]
103 ]
104 ],
105 ],
106 ];
107
108 $expected = $input;
109 $expected['processedTca']['columns']['aField']['config']['placeholder'] = 'anotherPlaceholder';
110
111 $this->assertSame($expected, $this->subject->addData($input));
112 }
113
114 /**
115 * @test
116 */
117 public function addDataReturnsValueFromSelectTypeRelation() {
118 $input = [
119 'tableName' => 'aTable',
120 'databaseRow' => [
121 'aField' => '',
122 'aRelationField' => ['42'],
123 ],
124 'processedTca' => [
125 'columns' => [
126 'aField' => [
127 'config' => [
128 'type' => 'input',
129 'placeholder' => '__row|aRelationField|aForeignField',
130 ]
131 ],
132 'aRelationField' => [
133 'config' => [
134 'type' => 'select',
135 'foreign_table' => 'aForeignTable'
136 ]
137 ]
138 ],
139 ],
140 ];
141
142 $aForeignTableInput = [
143 'tableName' => 'aForeignTable',
144 'databaseRow' => [
145 'aForeignField' => 'aForeignValue',
146 ],
147 'processedTca' => [
148 'columns' => [
149 'aForeignField' => [
150 'config' => [
151 'type' => 'input',
152 ]
153 ],
154 ],
155 ],
156 ];
157
158 /** @var TcaInputPlaceholderRecord $languageService */
159 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
160 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
161 $formDataCompilerProphecy->compile(['command' => 'edit', 'vanillaUid' => 42, 'tableName' => 'aForeignTable'])
162 ->shouldBeCalled()
163 ->willReturn($aForeignTableInput);
164
165 $expected = $input;
166 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $aForeignTableInput['databaseRow']['aForeignField'];
167
168 $this->assertSame($expected, $this->subject->addData($input));
169 }
170
171 /**
172 * @test
173 */
174 public function addDataReturnsValueFromGroupTypeRelation() {
175 $input = [
176 'tableName' => 'aTable',
177 'databaseRow' => [
178 'aField' => '',
179 'uid_local' => 'sys_file_3|aLabel,sys_file_5|anotherLabel',
180 ],
181 'processedTca' => [
182 'columns' => [
183 'aField' => [
184 'config' => [
185 'type' => 'input',
186 'placeholder' => '__row|uid_local|sha1',
187 ]
188 ],
189 'uid_local' => [
190 'config' => [
191 'type' => 'group',
192 'internal_type' => 'db',
193 'allowed' => 'sys_file'
194 ]
195 ]
196 ],
197 ],
198 ];
199
200 $sysFileProphecyResult = [
201 'tableName' => 'sys_file',
202 'databaseRow' => [
203 'sha1' => 'aSha1Value',
204 ],
205 'processedTca' => [
206 'columns' => [
207 'sha1' => [
208 'config' => [
209 'type' => 'input',
210 ]
211 ],
212 ],
213 ],
214 ];
215
216 /** @var TcaInputPlaceholderRecord $languageService */
217 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
218 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
219 $formDataCompilerProphecy->compile(['command' => 'edit', 'vanillaUid' => 3, 'tableName' => 'sys_file'])
220 ->shouldBeCalled()
221 ->willReturn($sysFileProphecyResult);
222
223 $expected = $input;
224 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileProphecyResult['databaseRow']['sha1'];
225
226 $this->assertSame($expected, $this->subject->addData($input));
227 }
228
229 /**
230 * @test
231 */
232 public function addDataReturnsValueFromInlineTypeRelation() {
233 $input = [
234 'tableName' => 'aTable',
235 'databaseRow' => [
236 'aField' => '',
237 'metadata' => '2',
238 ],
239 'processedTca' => [
240 'columns' => [
241 'aField' => [
242 'config' => [
243 'type' => 'input',
244 'placeholder' => '__row|metadata|title',
245 ]
246 ],
247 'metadata' => [
248 'config' => [
249 'readOnly' => 1,
250 'type' => 'inline',
251 'foreign_table' => 'sys_file_metadata',
252 'foreign_field' => 'file',
253 ]
254 ]
255 ],
256 ],
257 ];
258
259 $sysFileMetadataProphecyResult = [
260 'tableName' => 'sys_file_metadata',
261 'databaseRow' => [
262 'title' => 'aTitle',
263 ],
264 'processedTca' => [
265 'columns' => [
266 'sha1' => [
267 'config' => [
268 'type' => 'input',
269 ]
270 ],
271 ],
272 ],
273 ];
274
275 /** @var TcaInputPlaceholderRecord $languageService */
276 $formDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
277 GeneralUtility::addInstance(FormDataCompiler::class, $formDataCompilerProphecy->reveal());
278 $formDataCompilerProphecy->compile(['command' => 'edit', 'vanillaUid' => 2, 'tableName' => 'sys_file_metadata'])
279 ->shouldBeCalled()
280 ->willReturn($sysFileMetadataProphecyResult);
281
282 $expected = $input;
283 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataProphecyResult['databaseRow']['title'];
284
285 $this->assertSame($expected, $this->subject->addData($input));
286 }
287
288 /**
289 * @test
290 */
291 public function addDataReturnsValueFromRelationsRecursively() {
292 $input = [
293 'tableName' => 'aTable',
294 'databaseRow' => [
295 'aField' => '',
296 'uid_local' => 'sys_file_3|aLabel,sys_file_5|anotherLabel',
297 ],
298 'processedTca' => [
299 'columns' => [
300 'aField' => [
301 'config' => [
302 'type' => 'input',
303 'placeholder' => '__row|uid_local|metadata|title',
304 ]
305 ],
306 'uid_local' => [
307 'config' => [
308 'type' => 'group',
309 'internal_type' => 'db',
310 'allowed' => 'sys_file'
311 ]
312 ]
313 ],
314 ],
315 ];
316
317 $sysFileProphecyResult = [
318 'tableName' => 'sys_file',
319 'databaseRow' => [
320 'metadata' => '7',
321 ],
322 'processedTca' => [
323 'columns' => [
324 'metadata' => [
325 'config' => [
326 'readOnly' => 1,
327 'type' => 'inline',
328 'foreign_table' => 'sys_file_metadata',
329 'foreign_field' => 'file',
330 ]
331 ]
332 ],
333 ],
334 ];
335
336 $sysFileMetadataProphecyResult = [
337 'tableName' => 'sys_file_metadata',
338 'databaseRow' => [
339 'title' => 'aTitle',
340 ],
341 'processedTca' => [
342 'columns' => [
343 'sha1' => [
344 'config' => [
345 'type' => 'input',
346 ]
347 ],
348 ],
349 ],
350 ];
351
352 $sysFileFormDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
353 GeneralUtility::addInstance(FormDataCompiler::class, $sysFileFormDataCompilerProphecy->reveal());
354 $sysFileFormDataCompilerProphecy->compile(['command' => 'edit', 'vanillaUid' => 3, 'tableName' => 'sys_file'])
355 ->shouldBeCalled()
356 ->willReturn($sysFileProphecyResult);
357
358 $sysFileMetaDataFormDataCompilerProphecy = $this->prophesize(FormDataCompiler::class);
359 GeneralUtility::addInstance(FormDataCompiler::class, $sysFileMetaDataFormDataCompilerProphecy->reveal());
360 $sysFileMetaDataFormDataCompilerProphecy->compile(['command' => 'edit', 'vanillaUid' => 7, 'tableName' => 'sys_file_metadata'])
361 ->shouldBeCalled()
362 ->willReturn($sysFileMetadataProphecyResult);
363
364 $expected = $input;
365 $expected['processedTca']['columns']['aField']['config']['placeholder'] = $sysFileMetadataProphecyResult['databaseRow']['title'];
366
367 $this->assertSame($expected, $this->subject->addData($input));
368 }
369
370 }