‪TYPO3CMS  ‪main
TcaFlexPrepareTest.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;
21 use Psr\EventDispatcher\EventDispatcherInterface;
28 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
29 
30 final class ‪TcaFlexPrepareTest extends UnitTestCase
31 {
32  protected function ‪setUp(): void
33  {
34  parent::setUp();
35  // Suppress cache foo in xml helpers of GeneralUtility
36  $cacheManagerMock = $this->createMock(CacheManager::class);
37  GeneralUtility::setSingletonInstance(CacheManager::class, $cacheManagerMock);
38  $cacheFrontendMock = $this->createMock(FrontendInterface::class);
39  $cacheManagerMock->method('getCache')->with(self::anything())->willReturn($cacheFrontendMock);
40 
41  $eventDispatcher = new ‪MockEventDispatcher();
42  GeneralUtility::addInstance(EventDispatcherInterface::class, $eventDispatcher);
43  GeneralUtility::addInstance(FlexFormTools::class, new ‪FlexFormTools($eventDispatcher));
44  }
45 
46  protected function ‪tearDown(): void
47  {
48  GeneralUtility::purgeInstances();
49  parent::tearDown();
50  }
51 
52  #[Test]
53  public function ‪addDataKeepsExistingDataStructure(): void
54  {
55  $input = [
56  'systemLanguageRows' => [],
57  'tableName' => 'aTableName',
58  'databaseRow' => [
59  'aField' => [
60  'data' => [],
61  'meta' => [],
62  ],
63  ],
64  'processedTca' => [
65  'columns' => [
66  'aField' => [
67  'config' => [
68  'type' => 'flex',
69  'dataStructureIdentifier' => '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}',
70  'ds' => [
71  'sheets' => [
72  'sDEF' => [
73  'ROOT' => [
74  'type' => 'array',
75  'el' => [
76  'aFlexField' => [
77  'label' => 'aFlexFieldLabel',
78  'config' => [
79  'type' => 'input',
80  ],
81  ],
82  ],
83  ],
84  ],
85  ],
86  'meta' => [],
87  ],
88  ],
89  ],
90  ],
91  ],
92  ];
93  $expected = $input;
94  self::assertEquals($expected, (new ‪TcaFlexPrepare())->addData($input));
95  }
96 
97  #[Test]
99  {
100  $input = [
101  'systemLanguageRows' => [],
102  'tableName' => 'aTableName',
103  'databaseRow' => [
104  'aField' => [
105  'data' => [],
106  'meta' => [],
107  ],
108  ],
109  'processedTca' => [
110  'columns' => [
111  'aField' => [
112  'config' => [
113  'type' => 'flex',
114  'ds' => [
115  'default' => '
116  <T3DataStructure>
117  <ROOT>
118  <type>array</type>
119  <el>
120  <aFlexField>
121  <label>aFlexFieldLabel</label>
122  <config>
123  <type>input</type>
124  </config>
125  </aFlexField>
126  </el>
127  </ROOT>
128  </T3DataStructure>
129  ',
130  ],
131  ],
132  ],
133  ],
134  ],
135  ];
136 
137  ‪$GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
138 
139  $expected = $input;
140  $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
141  = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
142  $expected['processedTca']['columns']['aField']['config']['ds'] = [
143  'sheets' => [
144  'sDEF' => [
145  'ROOT' => [
146  'type' => 'array',
147  'el' => [
148  'aFlexField' => [
149  'label' => 'aFlexFieldLabel',
150  'config' => [
151  'type' => 'input',
152  ],
153  ],
154  ],
155  ],
156  ],
157  ],
158  'meta' => [],
159  ];
160 
161  self::assertEquals($expected, (new ‪TcaFlexPrepare())->addData($input));
162  }
163 
164  #[Test]
166  {
167  $input = [
168  'systemLanguageRows' => [],
169  'tableName' => 'aTableName',
170  'databaseRow' => [
171  'aField' => [
172  'data' => [],
173  'meta' => [],
174  ],
175  ],
176  'processedTca' => [
177  'columns' => [
178  'aField' => [
179  'config' => [
180  'type' => 'flex',
181  'ds' => [
182  'default' => '
183  <T3DataStructure>
184  <sheets>
185  <sDEF>
186  <ROOT>
187  <sheetTitle>aTitle</sheetTitle>
188  <type>array</type>
189  <el>
190  <aFlexField>
191  <label>aFlexFieldLabel</label>
192  <config>
193  <type>input</type>
194  </config>
195  </aFlexField>
196  </el>
197  </ROOT>
198  </sDEF>
199  </sheets>
200  </T3DataStructure>
201  ',
202  ],
203  ],
204  ],
205  ],
206  ],
207  ];
208 
209  ‪$GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
210 
211  $expected = $input;
212  $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
213  = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
214  $expected['processedTca']['columns']['aField']['config']['ds'] = [
215  'sheets' => [
216  'sDEF' => [
217  'ROOT' => [
218  'type' => 'array',
219  'el' => [
220  'aFlexField' => [
221  'label' => 'aFlexFieldLabel',
222  'config' => [
223  'type' => 'input',
224  ],
225  ],
226  ],
227  'sheetTitle' => 'aTitle',
228  ],
229  ],
230  ],
231  'meta' => [],
232  ];
233 
234  self::assertEquals($expected, (new ‪TcaFlexPrepare())->addData($input));
235  }
236 
237  #[Test]
239  {
240  $input = [
241  'databaseRow' => [],
242  'tableName' => 'aTableName',
243  'systemLanguageRows' => [],
244  'processedTca' => [
245  'columns' => [
246  'aField' => [
247  'config' => [
248  'type' => 'flex',
249  'ds' => [
250  'default' => '
251  <T3DataStructure>
252  <ROOT></ROOT>
253  </T3DataStructure>
254  ',
255  ],
256  ],
257  ],
258  ],
259  ],
260  ];
261 
262  ‪$GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
263 
264  $expected = $input;
265  $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
266  = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
267  $expected['processedTca']['columns']['aField']['config']['ds'] = [
268  'ROOT' => '',
269  'meta' => [],
270  ];
271  $expected['databaseRow']['aField'] = [
272  'data' => [],
273  'meta' => [],
274  ];
275 
276  self::assertEquals($expected, (new ‪TcaFlexPrepare())->addData($input));
277  }
278 
279  #[Test]
281  {
282  $input = [
283  'systemLanguageRows' => [],
284  'tableName' => 'aTableName',
285  'databaseRow' => [
286  'aField' => [
287  'data' => [],
288  'meta' => [],
289  ],
290  ],
291  'processedTca' => [
292  'columns' => [
293  'aField' => [
294  'config' => [
295  'type' => 'flex',
296  'ds' => [
297  'default' => '
298  <T3DataStructure>
299  <sheets>
300  <sTree>
301  <ROOT>
302  <type>array</type>
303  <sheetTitle>selectTree</sheetTitle>
304  <el>
305  <select_tree_1>
306  <label>select_tree_1</label>
307  <description>select_tree_1 description</description>
308  <config>
309  <type>select</type>
310  <renderType>selectTree</renderType>
311  </config>
312  </select_tree_1>
313  </el>
314  </ROOT>
315  </sTree>
316  <sSection>
317  <ROOT>
318  <type>array</type>
319  <sheetTitle>section</sheetTitle>
320  <el>
321  <section_1>
322  <title>section_1</title>
323  <type>array</type>
324  <section>1</section>
325  <el>
326  <container_1>
327  <type>array</type>
328  <title>container_1</title>
329  <el>
330  <select_tree_2>
331  <label>select_tree_2</label>
332  <description>select_tree_2 description</description>
333  <config>
334  <type>select</type>
335  <renderType>selectTree</renderType>
336  </config>
337  </select_tree_2>
338  </el>
339  </container_1>
340  </el>
341  </section_1>
342  </el>
343  </ROOT>
344  </sSection>
345  </sheets>
346  </T3DataStructure>
347  ',
348  ],
349  ],
350  ],
351  ],
352  ],
353  ];
354 
355  ‪$GLOBALS['TCA']['aTableName']['columns'] = $input['processedTca']['columns'];
356 
357  $expected = $input;
358  $expected['processedTca']['columns']['aField']['config']['dataStructureIdentifier']
359  = '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}';
360 
361  $expected['processedTca']['columns']['aField']['config']['ds'] = [
362  'sheets' => [
363  'sSection' => [
364  'ROOT' => [
365  'type' => 'array',
366  'sheetTitle' => 'section',
367  'el' => [
368  'section_1' => [
369  'title' => 'section_1',
370  'type' => 'array',
371  'section' => '1',
372  'el' => [
373  'container_1' => [
374  'type' => 'array',
375  'title' => 'container_1',
376  'el' => [
377  'select_tree_2' => [
378  'label' => 'select_tree_2',
379  'description' => 'select_tree_2 description',
380  'config' => [
381  'type' => 'select',
382  'renderType' => 'selectTree',
383  ],
384  ],
385  ],
386  ],
387  ],
388  ],
389  ],
390  ],
391  ],
392  'sTree' => [
393  'ROOT' => [
394  'type' => 'array',
395  'sheetTitle' => 'selectTree',
396  'el' => [
397  'select_tree_1' => [
398  'label' => 'select_tree_1',
399  'description' => 'select_tree_1 description',
400  'config' => [
401  'type' => 'select',
402  'renderType' => 'selectTree',
403  ],
404  ],
405  ],
406  ],
407  ],
408  ],
409  'meta' => [],
410  ];
411 
412  self::assertEquals($expected, (new ‪TcaFlexPrepare())->addData($input));
413  }
414 
420  #[Test]
422  {
423  $columnConfig = [
424  'label' => 'select_section_1',
425  'description' => 'select_section_1 description',
426  'config' => [
427  'type' => 'select',
428  ],
429  ];
430 
431  $input = [
432  'systemLanguageRows' => [],
433  'tableName' => 'aTableName',
434  'databaseRow' => [
435  'aField' => [
436  'data' => [],
437  'meta' => [],
438  ],
439  ],
440  'processedTca' => [
441  'columns' => [
442  'aField' => [
443  'config' => [
444  'type' => 'flex',
445  'ds' => [
446  'sheets' => [
447  'sSection' => [
448  'ROOT' => [
449  'type' => 'array',
450  'el' => [
451  'section_1' => [
452  'section' => 1,
453  'type' => 'array',
454  'el' => [
455  'container_1' => [
456  'type' => 'array',
457  'el' => [
458  'select_section_1' => $columnConfig,
459  ],
460  ],
461  ],
462  ],
463  ],
464  ],
465  ],
466  ],
467  ],
468  'dataStructureIdentifier' => '{"type":"tca","tableName":"aTableName","fieldName":"aField","dataStructureKey":"default"}',
469  ],
470  ],
471  ],
472  ],
473  ];
474 
475  $expected = $input;
476  $expected['processedTca']['columns']['aField']['config']['ds']['meta'] = [];
477  $expected['processedTca']['columns']['aField']['config']['ds']
478  ['sheets']['sSection']['ROOT']['el']
479  ['section_1']['el']
480  ['container_1']['el']
481  ['select_section_1'] = $columnConfig;
482 
483  self::assertEquals($expected, (new ‪TcaFlexPrepare())->addData($input));
484  }
485 }
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaFlexPrepare
Definition: TcaFlexPrepare.php:29
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest
Definition: TcaFlexPrepareTest.php:31
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\addDataSetsParsedDataStructureArray
‪addDataSetsParsedDataStructureArray()
Definition: TcaFlexPrepareTest.php:98
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\setUp
‪setUp()
Definition: TcaFlexPrepareTest.php:32
‪TYPO3\CMS\Core\Configuration\FlexForm\FlexFormTools
Definition: FlexFormTools.php:40
‪TYPO3\CMS\Core\Cache\CacheManager
Definition: CacheManager.php:36
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\addDataKeepsExistingDataStructure
‪addDataKeepsExistingDataStructure()
Definition: TcaFlexPrepareTest.php:53
‪TYPO3\CMS\Core\Cache\Frontend\FrontendInterface
Definition: FrontendInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\Unit\Fixtures\EventDispatcher\MockEventDispatcher
Definition: MockEventDispatcher.php:30
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\tearDown
‪tearDown()
Definition: TcaFlexPrepareTest.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\addDataInitializesDatabaseRowValueIfNoDataStringIsGiven
‪addDataInitializesDatabaseRowValueIfNoDataStringIsGiven()
Definition: TcaFlexPrepareTest.php:238
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\addDataMigratesResolvedFlexformTca
‪addDataMigratesResolvedFlexformTca()
Definition: TcaFlexPrepareTest.php:421
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\addDataSetsParsedDataStructureArrayRecursive
‪addDataSetsParsedDataStructureArrayRecursive()
Definition: TcaFlexPrepareTest.php:280
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider
Definition: DatabaseDefaultLanguagePageRowTest.php:18
‪TYPO3\CMS\Backend\Tests\Unit\Form\FormDataProvider\TcaFlexPrepareTest\addDataSetsParsedDataStructureArrayWithSheets
‪addDataSetsParsedDataStructureArrayWithSheets()
Definition: TcaFlexPrepareTest.php:165