‪TYPO3CMS  11.5
CategoryRegistryTest.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 
22 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
23 
27 class ‪CategoryRegistryTest extends UnitTestCase
28 {
30 
31  protected array ‪$tables;
32 
36  protected function ‪setUp(): void
37  {
38  parent::setUp();
39  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = 'pages';
40  ‪$GLOBALS['TCA']['pages']['columns'] = [];
41  $this->subject = new ‪CategoryRegistry();
42  $this->tables = [
43  'first' => ‪StringUtility::getUniqueId('first'),
44  'second' => ‪StringUtility::getUniqueId('second'),
45  ];
46  foreach ($this->tables as $tableName) {
47  ‪$GLOBALS['TCA'][$tableName] = [
48  'ctrl' => [],
49  'columns' => [],
50  'types' => [
51  '0' => [
52  'showitem' => '',
53  ],
54  '1' => [
55  'showitem' => '',
56  ],
57  ],
58  ];
59  }
60  }
61 
65  public function ‪doesAddReturnTrueOnDefinedTable(): void
66  {
67  self::assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
68  }
69 
74  {
75  self::assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
76  self::assertFalse($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
77  }
78 
83  {
84  $this->expectException(\InvalidArgumentException::class);
85  $this->expectExceptionCode(1369122038);
86 
87  $this->subject->add('test_extension_a', '', 'categories');
88  }
89 
94  {
95  $this->expectException(\InvalidArgumentException::class);
96  $this->expectExceptionCode(1397836158);
97 
98  $this->subject->add('', 'foo', 'categories');
99  }
100 
105  {
106  $this->expectException(\InvalidArgumentException::class);
107  $this->expectExceptionCode(1369122038);
108 
109  $this->subject->add('test_extension_a', '', 'categories');
110  }
111 
116  {
117  $this->expectException(\InvalidArgumentException::class);
118  $this->expectExceptionCode(1397836158);
119 
120  $this->subject->add('', 'foo', 'categories');
121  }
122 
127  {
128  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
129  $this->subject->add('test_extension_a', $this->tables['second'], 'categories');
130  $this->subject->applyTcaForPreRegisteredTables();
131 
132  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
133  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
134  }
135 
140  {
141  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
142  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
143  $this->subject->applyTcaForPreRegisteredTables();
144 
145  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
146  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
147  }
148 
153  {
154  $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
155  $this->subject->add('test_extension_b', $this->tables['first'], 'categories2');
156  $this->subject->applyTcaForPreRegisteredTables();
157 
158  self::assertArrayHasKey('categories1', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
159  self::assertArrayHasKey('categories2', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
160  }
161 
166  {
167  $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
168  $this->subject->add('test_extension_a', $this->tables['first'], 'categories2');
169  $this->subject->applyTcaForPreRegisteredTables();
170 
171  self::assertArrayHasKey('categories1', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
172  self::assertArrayHasKey('categories2', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
173  }
174 
179  {
180  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
181  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
182  $this->subject->add('test_extension_c', $this->tables['first'], 'categories');
183  $definitions = $this->subject->getDatabaseTableDefinitions();
184  $matches = [];
185  preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
186  self::assertCount(2, $matches[0]);
187  self::assertEquals($matches[1][0], $this->tables['first']);
188  self::assertEquals('categories', $matches[2][0]);
189  self::assertEquals($matches[1][1], $this->tables['second']);
190  self::assertEquals('categories', $matches[2][1]);
191  }
192 
197  {
198  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
199  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
200  $definitions = $this->subject->getDatabaseTableDefinition('test_extension_a');
201  $matches = [];
202  preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
203  self::assertCount(1, $matches[0]);
204  self::assertEquals($matches[1][0], $this->tables['first']);
205  self::assertEquals('categories', $matches[2][0]);
206  }
207 
211  public function ‪areDefaultCategorizedTablesLoaded(): void
212  {
213  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
214  $this->subject->applyTcaForPreRegisteredTables();
215 
216  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
217  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
218  }
219 
223  public function ‪canApplyTca(): void
224  {
225  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
226  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
227  $this->subject->applyTcaForPreRegisteredTables();
228 
229  self::assertNotEmpty(‪$GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
230  self::assertNotEmpty(‪$GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
231  }
232 
237  {
238  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
239  self::assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
240  }
241 
246  {
247  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
248  self::assertFalse($this->subject->isRegistered($this->tables['first'], '_not_registered'));
249  self::assertFalse($this->subject->isRegistered($this->tables['second'], 'categories'));
250  }
251 
255  public function ‪tabIsAddedForElement(): void
256  {
257  $this->subject->add('text_extension_a', $this->tables['first']);
258  $this->subject->applyTcaForPreRegisteredTables();
259 
260  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
261  self::assertStringContainsString('--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
262  }
263  }
264 
269  {
270  $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['fieldList' => 'categories']);
271  $this->subject->applyTcaForPreRegisteredTables();
272 
273  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
274  self::assertStringNotContainsString('--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
275  }
276  }
277 
282  {
283  $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['typesList' => '0']);
284  $this->subject->applyTcaForPreRegisteredTables();
285  self::assertSame('', ‪$GLOBALS['TCA'][$this->tables['first']]['types'][1]['showitem']);
286  }
287 
291  public function ‪tabIsAddedOnlyOncePerTable(): void
292  {
293  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
294  $this->subject->add('text_extension_a', $this->tables['first'], 'categories2');
295  $this->subject->applyTcaForPreRegisteredTables();
296 
297  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
298  self::assertSame(
299  1,
300  substr_count($typeConfig['showitem'], '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
301  );
302  }
303  }
304 
309  {
310  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
311  $result = $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
312  self::assertTrue($result);
313  }
314 
318  public function ‪addInitializesMissingTypes(): void
319  {
320  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
321  ‪$GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd'] = ['showitem' => ''];
322  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
323  self::assertSame(
324  1,
325  substr_count(‪$GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd']['showitem'], '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
326  );
327  }
328 }
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\tabIsAddedForElement
‪tabIsAddedForElement()
Definition: CategoryRegistryTest.php:255
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\canApplyTca
‪canApplyTca()
Definition: CategoryRegistryTest.php:223
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\tabIsAddedOnlyOncePerTable
‪tabIsAddedOnlyOncePerTable()
Definition: CategoryRegistryTest.php:291
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\areMultipleElementsOfSameExtensionRegistered
‪areMultipleElementsOfSameExtensionRegistered()
Definition: CategoryRegistryTest.php:126
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\addAllowsSettingOfTheSameTableFieldTwice
‪addAllowsSettingOfTheSameTableFieldTwice()
Definition: CategoryRegistryTest.php:308
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\tabIsOnlyAddedForTypesThatAreSpecifiedInTypesList
‪tabIsOnlyAddedForTypesThatAreSpecifiedInTypesList()
Definition: CategoryRegistryTest.php:281
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\areElementsOfDifferentExtensionsRegistered
‪areElementsOfDifferentExtensionsRegistered()
Definition: CategoryRegistryTest.php:139
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\addInitializesMissingTypes
‪addInitializesMissingTypes()
Definition: CategoryRegistryTest.php:318
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\areDatabaseDefinitionsOfAllElementsAvailable
‪areDatabaseDefinitionsOfAllElementsAvailable()
Definition: CategoryRegistryTest.php:178
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\areDefaultCategorizedTablesLoaded
‪areDefaultCategorizedTablesLoaded()
Definition: CategoryRegistryTest.php:211
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\$tables
‪array $tables
Definition: CategoryRegistryTest.php:31
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category
Definition: CategoryRegistryTest.php:18
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest
Definition: CategoryRegistryTest.php:28
‪TYPO3\CMS\Core\Category\CategoryRegistry
Definition: CategoryRegistry.php:30
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\doesAddThrowExceptionOnEmptyExtensionKey
‪doesAddThrowExceptionOnEmptyExtensionKey()
Definition: CategoryRegistryTest.php:93
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\$subject
‪CategoryRegistry $subject
Definition: CategoryRegistryTest.php:29
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\tabIsNotAddedForElementIfFieldListIsSpecified
‪tabIsNotAddedForElementIfFieldListIsSpecified()
Definition: CategoryRegistryTest.php:268
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\areElementsOfSameExtensionOnSameTableRegistered
‪areElementsOfSameExtensionOnSameTableRegistered()
Definition: CategoryRegistryTest.php:165
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\doesAddThrowExceptionOnInvalidTablename
‪doesAddThrowExceptionOnInvalidTablename()
Definition: CategoryRegistryTest.php:104
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\doesAddReturnTrueOnDefinedTable
‪doesAddReturnTrueOnDefinedTable()
Definition: CategoryRegistryTest.php:65
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:128
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime
‪doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime()
Definition: CategoryRegistryTest.php:73
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\isRegisteredReturnsTrueIfElementIsAlreadyRegistered
‪isRegisteredReturnsTrueIfElementIsAlreadyRegistered()
Definition: CategoryRegistryTest.php:236
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\doesAddThrowExceptionOnEmptyTablename
‪doesAddThrowExceptionOnEmptyTablename()
Definition: CategoryRegistryTest.php:82
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\isRegisteredReturnsFalseIfElementIsNotRegistered
‪isRegisteredReturnsFalseIfElementIsNotRegistered()
Definition: CategoryRegistryTest.php:245
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\setUp
‪setUp()
Definition: CategoryRegistryTest.php:36
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\areDatabaseDefinitionsOfParticularExtensionAvailable
‪areDatabaseDefinitionsOfParticularExtensionAvailable()
Definition: CategoryRegistryTest.php:196
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\doesAddThrowExceptionOnInvalidExtensionKey
‪doesAddThrowExceptionOnInvalidExtensionKey()
Definition: CategoryRegistryTest.php:115
‪TYPO3\CMS\Core\Tests\UnitDeprecated\Category\CategoryRegistryTest\areElementsOfDifferentExtensionsOnSameTableRegistered
‪areElementsOfDifferentExtensionsOnSameTableRegistered()
Definition: CategoryRegistryTest.php:152