‪TYPO3CMS  11.5
TcaCategoryTest.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 
23 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
24 
25 class ‪TcaCategoryTest extends FunctionalTestCase
26 {
27  protected function ‪setUp(): void
28  {
29  parent::setUp();
30  $this->importCSVDataSet(__DIR__ . '/../Fixtures/CategoryRelations.csv');
31  $this->setUpBackendUserFromFixture(1);
33  }
34 
38  public function ‪addDataOnlyWorksForTypeCategory(): void
39  {
40  $input = [
41  'command' => 'edit',
42  'tableName' => 'tt_content',
43  'effectivePid' => 89,
44  'databaseRow' => [
45  'uid' => 298,
46  'categories' => '2',
47  ],
48  'processedTca' => [
49  'columns' => [
50  'categories' => [
51  'config' => [
52  'type' => 'input',
53  ],
54  ],
55  ],
56  ],
57  ];
58 
59  // We expect no change to the input data
60  $expected = $input;
61 
62  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
63  }
64 
68  public function ‪addDataChecksForTargetRenderType(): void
69  {
70  $input = [
71  'command' => 'edit',
72  'tableName' => 'tt_content',
73  'effectivePid' => 89,
74  'databaseRow' => [
75  'uid' => 298,
76  'categories' => '2',
77  ],
78  'processedTca' => [
79  'columns' => [
80  'categories' => [
81  'config' => $this->‪getFieldConfiguration([
82  'type' => 'category',
83  'renderType' => 'someRenderType',
84  ]),
85  ],
86  ],
87  ],
88  ];
89 
90  // We expect no change to the input data
91  $expected = $input;
92 
93  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
94  }
95 
100  {
101  $input = [
102  'command' => 'edit',
103  'tableName' => 'tt_content',
104  'effectivePid' => 89,
105  'databaseRow' => [
106  'uid' => 298,
107  'categories' => '2',
108  ],
109  'processedTca' => [
110  'columns' => [
111  'categories' => [
112  'config' => $this->‪getFieldConfiguration(['type' => 'category']),
113  ],
114  ],
115  ],
116  ];
117 
118  $expected = $input;
119  $expected['databaseRow']['categories'] = ['2'];
120  $expected['processedTca']['columns']['categories']['config']['treeConfig'] = [
121  'parentField' => 'parent',
122  'appearance' => [
123  'expandAll' => true,
124  'showHeader' => true,
125  'maxLevels' => 99,
126  ],
127  ];
128 
129  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
130  }
131 
136  {
137  $input = [
138  'command' => 'edit',
139  'tableName' => 'tt_content',
140  'effectivePid' => 89,
141  'databaseRow' => [
142  'uid' => 298,
143  'categories' => '2',
144  ],
145  'processedTca' => [
146  'columns' => [
147  'categories' => [
148  'config' => $this->‪getFieldConfiguration(['type' => 'category']),
149  ],
150  ],
151  ],
152  'pageTsConfig' => [
153  'TCEFORM.' => [
154  'tt_content.' => [
155  'categories.' => [
156  'config.' => [
157  'treeConfig.' => [
158  'startingPoints' => '1',
159  'appearance.' => [
160  'expandAll' => false,
161  'maxLevels' => 10,
162  ],
163  ],
164  ],
165  ],
166  ],
167  ],
168  ],
169  ];
170 
171  $expected = $input;
172  $expected['databaseRow']['categories'] = ['2'];
173  $expected['processedTca']['columns']['categories']['config']['treeConfig'] = [
174  'parentField' => 'parent',
175  'startingPoints' => '1',
176  'appearance' => [
177  'expandAll' => false,
178  'showHeader' => true,
179  'maxLevels' => 10,
180  ],
181  ];
182 
183  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
184  }
185 
187  {
188  return [
189  'one setting' => [
190  'inputStartingPoints' => '42,###SITE:categories.contentCategory###,12',
191  'expectedStartingPoints' => '42,4711,12',
192  'site' => new ‪Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategory' => 4711]]),
193  ],
194  'one setting, multiple categories as array' => [
195  'inputStartingPoints' => '###SITE:categories.contentCategories###',
196  'expectedStartingPoints' => '4711,4712,42',
197  'site' => new ‪Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategories' => [4711, 4712, 42]]]),
198  ],
199  'one setting, multiple categories as csv' => [
200  'inputStartingPoints' => '###SITE:categories.contentCategories###',
201  'expectedStartingPoints' => '4711,4712,42',
202  'site' => new ‪Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategories' => '4711,4712,42']]),
203  ],
204  'two settings' => [
205  'inputStartingPoints' => '42,###SITE:categories.contentCategory###,12,###SITE:foobar###',
206  'expectedStartingPoints' => '42,4711,12,1',
207  'site' => new ‪Site('some-site', 1, ['rootPageId' => 1, 'foobar' => 1, 'categories' => ['contentCategory' => 4711]]),
208  ],
209  'one invalid settings' => [
210  'inputStartingPoints' => '42,12,###SITE:invalid###',
211  'expectedStartingPoints' => '42,12',
212  'site' => new ‪Site('some-site', 1, ['rootPageId' => 1]),
213  ],
214  'one valid and one invalid setting' => [
215  'inputStartingPoints' => '42,###SITE:invalid###,12,###SITE:categories.contentCategory###',
216  'expectedStartingPoints' => '42,12,4711,4712',
217  'site' => new ‪Site('some-site', 1, ['rootPageId' => 1, 'categories' => ['contentCategory' => '4711,4712']]),
218  ],
219  ];
220  }
221 
226  public function ‪addDataOverridesDefaultFieldConfigurationBySiteConfig(string $inputStartingPoints, string $expectedStartingPoints, ‪Site $site): void
227  {
228  $input = [
229  'command' => 'edit',
230  'tableName' => 'tt_content',
231  'effectivePid' => 89,
232  'databaseRow' => [
233  'uid' => 298,
234  'categories' => '2',
235  ],
236  'processedTca' => [
237  'columns' => [
238  'categories' => [
239  'config' => $this->‪getFieldConfiguration([
240  'type' => 'category',
241  'treeConfig' => [
242  'startingPoints' => $inputStartingPoints,
243  ],
244  ]),
245  ],
246  ],
247  ],
248  'site' => $site,
249  ];
250 
251  $expected = $input;
252  $expected['databaseRow']['categories'] = ['2'];
253  $expected['processedTca']['columns']['categories']['config']['treeConfig'] = [
254  'parentField' => 'parent',
255  'startingPoints' => $expectedStartingPoints,
256  'appearance' => [
257  'expandAll' => true,
258  'showHeader' => true,
259  'maxLevels' => 99,
260  ],
261  ];
262 
263  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
264  }
265 
270  {
271  $input = [
272  'command' => 'edit',
273  'tableName' => 'tt_content',
274  'effectivePid' => 89,
275  'databaseRow' => [
276  'uid' => 298,
277  'categories' => '2',
278  ],
279  'processedTca' => [
280  'columns' => [
281  'categories' => [
282  'config' => $this->‪getFieldConfiguration(['type' => 'category', 'relationship' => 'manyToMany']),
283  ],
284  ],
285  ],
286  ];
287 
288  $expected = $input;
289  $expected['databaseRow']['categories'] = [
290  '29',
291  '30',
292  ];
293  $expected['processedTca']['columns']['categories']['config']['treeConfig'] = [
294  'parentField' => 'parent',
295  'appearance' => [
296  'expandAll' => true,
297  'showHeader' => true,
298  'maxLevels' => 99,
299  ],
300  ];
301 
302  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
303  }
304 
309  {
310  $input = [
311  'command' => 'edit',
312  'tableName' => 'tt_content',
313  'selectTreeCompileItems' => true,
314  'effectivePid' => 89,
315  'databaseRow' => [
316  'uid' => 298,
317  'categories' => '31',
318  ],
319  'processedTca' => [
320  'columns' => [
321  'categories' => [
322  'config' => $this->‪getFieldConfiguration([
323  'type' => 'category',
324  'items' => [
325  [
326  'Static item',
327  1234,
328  'icon-identifier',
329  ],
330  ],
331  ]),
332  ],
333  ],
334  ],
335  ];
336 
337  $this->expectExceptionCode(1627336557);
338  $this->expectException(\RuntimeException::class);
339 
340  (new ‪TcaCategory())->addData($input);
341  }
342 
346  public function ‪addDataBuildsTreeForSingle(): void
347  {
348  $input = [
349  'command' => 'edit',
350  'tableName' => 'tt_content',
351  'selectTreeCompileItems' => true,
352  'effectivePid' => 89,
353  'databaseRow' => [
354  'uid' => 298,
355  'categories' => '31',
356  ],
357  'processedTca' => [
358  'columns' => [
359  'categories' => [
360  'config' => $this->‪getFieldConfiguration([
361  'type' => 'category',
362  'relationship' => 'oneToOne',
363  ]),
364  ],
365  ],
366  ],
367  'rootline' => [],
368  'site' => null,
369  ];
370 
371  $expected = $input;
372  $expected['databaseRow']['categories'] = ['31'];
373  $expected['processedTca']['columns']['categories']['config']['treeConfig'] = [
374  'parentField' => 'parent',
375  'appearance' => [
376  'expandAll' => true,
377  'showHeader' => true,
378  'maxLevels' => 99,
379  ],
380  ];
381  // Expect fetched category items
382  $expected['processedTca']['columns']['categories']['config']['items'] = $this->‪getExpectedCategoryItems([31]);
383 
384  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
385  }
386 
390  public function ‪addDataBuildsTreeForCsv(): void
391  {
392  $input = [
393  'command' => 'edit',
394  'tableName' => 'tt_content',
395  'selectTreeCompileItems' => true,
396  'effectivePid' => 89,
397  'databaseRow' => [
398  'uid' => 298,
399  'categories' => '29,30',
400  ],
401  'processedTca' => [
402  'columns' => [
403  'categories' => [
404  'config' => $this->‪getFieldConfiguration([
405  'type' => 'category',
406  'relationship' => 'oneToMany',
407  'maxitems' => 123,
408  'treeConfig' => [
409  'appearance' => [
410  'expandAll' => false,
411  'showHeader' => false,
412  ],
413  ],
414  ]),
415  ],
416  ],
417  ],
418  'pageTsConfig' => [
419  'TCEFORM.' => [
420  'tt_content.' => [
421  'categories.' => [
422  'removeItems' => '31',
423  ],
424  ],
425  ],
426  ],
427  'rootline' => [],
428  'site' => null,
429  ];
430 
431  $expected = $input;
432  $expected['databaseRow']['categories'] = [
433  '29',
434  '30',
435  ];
436  $expected['processedTca']['columns']['categories']['config']['treeConfig'] = [
437  'parentField' => 'parent',
438  'appearance' => [
439  'expandAll' => false,
440  'showHeader' => false,
441  'maxLevels' => 99,
442  ],
443  ];
444  // Expect fetched category items
445  $expected['processedTca']['columns']['categories']['config']['items'] = $this->‪getExpectedCategoryItems([29, 30]);
446 
447  // TSconfig "removeItems" should be respected
448  unset($expected['processedTca']['columns']['categories']['config']['items'][2]);
449  $expected['processedTca']['columns']['categories']['config']['items'][1]['hasChildren'] = false;
450  $expected['processedTca']['columns']['categories']['config']['items'] = array_values(
451  $expected['processedTca']['columns']['categories']['config']['items']
452  );
453 
454  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
455  }
456 
460  public function ‪addDataBuildsTreeForMM(): void
461  {
462  $input = [
463  'command' => 'edit',
464  'tableName' => 'tt_content',
465  'selectTreeCompileItems' => true,
466  'effectivePid' => 89,
467  'databaseRow' => [
468  'uid' => 298,
469  'categories' => '2',
470  ],
471  'processedTca' => [
472  'columns' => [
473  'categories' => [
474  'config' => $this->‪getFieldConfiguration(['type' => 'category', 'relationship' => 'manyToMany']),
475  ],
476  ],
477  ],
478  'pageTsConfig' => [
479  'TCEFORM.' => [
480  'tt_content.' => [
481  'categories.' => [
482  'keepItems' => '28,29,30',
483  ],
484  ],
485  ],
486  ],
487  'rootline' => [],
488  'site' => null,
489  ];
490 
491  $expected = $input;
492  $expected['databaseRow']['categories'] = [
493  '29',
494  '30',
495  ];
496  $expected['processedTca']['columns']['categories']['config']['treeConfig'] = [
497  'parentField' => 'parent',
498  'appearance' => [
499  'expandAll' => true,
500  'showHeader' => true,
501  'maxLevels' => 99,
502  ],
503  ];
504  // Expect fetched category items
505  $expected['processedTca']['columns']['categories']['config']['items'] = $this->‪getExpectedCategoryItems([29, 30]);
506 
507  // TSconfig "keepItems" should be respected
508  unset($expected['processedTca']['columns']['categories']['config']['items'][2]);
509  $expected['processedTca']['columns']['categories']['config']['items'][1]['hasChildren'] = false;
510  $expected['processedTca']['columns']['categories']['config']['items'] = array_values(
511  $expected['processedTca']['columns']['categories']['config']['items']
512  );
513 
514  self::assertEquals($expected, (new ‪TcaCategory())->addData($input));
515  }
516 
524  protected function ‪getFieldConfiguration(array $input): array
525  {
526  $default = [
527  'relationship' => 'oneToOne',
528  'foreign_table' => 'sys_category',
529  'foreign_table_where' => ' AND {#sys_category}.{#sys_language_uid} IN (-1, 0)',
530  'size' => 20,
531  'default' => 0,
532  'maxitems' => 1,
533  ];
534 
535  if (($input['relationship'] ?? '') === 'manyToMany') {
536  $default['MM'] = 'sys_category_record_mm';
537  $default['MM_opposite_field'] = 'items';
538  $default['MM_match_fields'] = [
539  'tablenames' => 'tt_content',
540  'fieldname' => 'categories',
541  ];
542  $default['maxitems'] = 99999;
543  }
544 
545  return array_replace_recursive($default, $input);
546  }
547 
554  protected function ‪getExpectedCategoryItems(array $checked = []): array
555  {
556  return [
557  [
558  'identifier' => '0',
559  'name' => 'Category',
560  'icon' => 'mimetypes-x-sys_category',
561  'overlayIcon' => '',
562  'depth' => 0,
563  'hasChildren' => true,
564  'selectable' => false,
565  'checked' => false,
566  ],
567  [
568  'identifier' => '28',
569  'name' => 'Category A',
570  'icon' => 'mimetypes-x-sys_category',
571  'overlayIcon' => '',
572  'depth' => 1,
573  'hasChildren' => true,
574  'selectable' => true,
575  'checked' => in_array(28, $checked, true),
576  ],
577  [
578  'identifier' => '31',
579  'name' => 'Category A.A',
580  'icon' => 'mimetypes-x-sys_category',
581  'overlayIcon' => '',
582  'depth' => 2,
583  'hasChildren' => false,
584  'selectable' => true,
585  'checked' => in_array(31, $checked, true),
586  ],
587  [
588  'identifier' => '29',
589  'name' => 'Category B',
590  'icon' => 'mimetypes-x-sys_category',
591  'overlayIcon' => '',
592  'depth' => 1,
593  'hasChildren' => false,
594  'selectable' => true,
595  'checked' => in_array(29, $checked, true),
596  ],
597  [
598  'identifier' => '30',
599  'name' => 'Category C',
600  'icon' => 'mimetypes-x-sys_category',
601  'overlayIcon' => '',
602  'depth' => 1,
603  'hasChildren' => false,
604  'selectable' => true,
605  'checked' => in_array(30, $checked, true),
606  ],
607  ];
608  }
609 }
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataInitializesDefaultFieldConfiguration
‪addDataInitializesDefaultFieldConfiguration()
Definition: TcaCategoryTest.php:99
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataBuildsTreeForSingle
‪addDataBuildsTreeForSingle()
Definition: TcaCategoryTest.php:346
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataThorwsExceptionForStaticItems
‪addDataThorwsExceptionForStaticItems()
Definition: TcaCategoryTest.php:308
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataOverridesDefaultFieldConfigurationBySiteConfig
‪addDataOverridesDefaultFieldConfigurationBySiteConfig(string $inputStartingPoints, string $expectedStartingPoints, Site $site)
Definition: TcaCategoryTest.php:226
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataProcessesCategoryFieldValue
‪addDataProcessesCategoryFieldValue()
Definition: TcaCategoryTest.php:269
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataBuildsTreeForCsv
‪addDataBuildsTreeForCsv()
Definition: TcaCategoryTest.php:390
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataChecksForTargetRenderType
‪addDataChecksForTargetRenderType()
Definition: TcaCategoryTest.php:68
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataBuildsTreeForMM
‪addDataBuildsTreeForMM()
Definition: TcaCategoryTest.php:460
‪TYPO3\CMS\Core\Site\Entity\Site
Definition: Site.php:42
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\getExpectedCategoryItems
‪array[] getExpectedCategoryItems(array $checked=[])
Definition: TcaCategoryTest.php:554
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataOverridesDefaultFieldConfigurationBySiteConfigDataProvider
‪addDataOverridesDefaultFieldConfigurationBySiteConfigDataProvider()
Definition: TcaCategoryTest.php:186
‪TYPO3\CMS\Core\Core\Bootstrap\initializeLanguageObject
‪static initializeLanguageObject()
Definition: Bootstrap.php:598
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataOverridesDefaultFieldConfigurationByTSconfig
‪addDataOverridesDefaultFieldConfigurationByTSconfig()
Definition: TcaCategoryTest.php:135
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\setUp
‪setUp()
Definition: TcaCategoryTest.php:27
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest
Definition: TcaCategoryTest.php:26
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\getFieldConfiguration
‪array getFieldConfiguration(array $input)
Definition: TcaCategoryTest.php:524
‪TYPO3\CMS\Backend\Form\FormDataProvider\TcaCategory
Definition: TcaCategory.php:39
‪TYPO3\CMS\Core\Core\Bootstrap
Definition: Bootstrap.php:70
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider
Definition: TcaCategoryTest.php:18
‪TYPO3\CMS\Backend\Tests\Functional\Form\FormDataProvider\TcaCategoryTest\addDataOnlyWorksForTypeCategory
‪addDataOnlyWorksForTypeCategory()
Definition: TcaCategoryTest.php:38