‪TYPO3CMS  9.5
CategoryRegistryTest.php
Go to the documentation of this file.
1 <?php
2 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 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
20 
24 class ‪CategoryRegistryTest extends UnitTestCase
25 {
29  protected ‪$subject;
30 
34  protected ‪$tables;
35 
39  protected function ‪setUp(): void
40  {
41  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = 'pages';
42  ‪$GLOBALS['TCA']['pages']['columns'] = [];
43  $this->subject = new ‪CategoryRegistry();
44  $this->tables = [
45  'first' => $this->getUniqueId('first'),
46  'second' => $this->getUniqueId('second')
47  ];
48  foreach ($this->tables as $tableName) {
49  ‪$GLOBALS['TCA'][$tableName] = [
50  'ctrl' => [],
51  'columns' => [],
52  'types' => [
53  '0' => [
54  'showitem' => ''
55  ],
56  '1' => [
57  'showitem' => ''
58  ]
59  ],
60  ];
61  }
62  }
63 
67  public function ‪doesAddReturnTrueOnDefinedTable(): void
68  {
69  $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
70  }
71 
76  {
77  $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
78  $this->assertFalse($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
79  }
80 
84  public function ‪doesAddThrowExceptionOnEmptyTablename(): void
85  {
86  $this->expectException(\InvalidArgumentException::class);
87  $this->expectExceptionCode(1369122038);
88 
89  $this->subject->add('test_extension_a', '', 'categories');
90  }
91 
95  public function ‪doesAddThrowExceptionOnEmptyExtensionKey(): void
96  {
97  $this->expectException(\InvalidArgumentException::class);
98  $this->expectExceptionCode(1397836158);
99 
100  $this->subject->add('', 'foo', 'categories');
101  }
102 
106  public function ‪doesAddThrowExceptionOnInvalidTablename(): void
107  {
108  $this->expectException(\InvalidArgumentException::class);
109  $this->expectExceptionCode(1369122038);
110 
111  $this->subject->add('test_extension_a', '', 'categories');
112  }
113 
117  public function ‪doesAddThrowExceptionOnInvalidExtensionKey(): void
118  {
119  $this->expectException(\InvalidArgumentException::class);
120  $this->expectExceptionCode(1397836158);
121 
122  $this->subject->add('', 'foo', 'categories');
123  }
124 
129  {
130  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
131  $this->subject->add('test_extension_a', $this->tables['second'], 'categories');
132  $this->subject->applyTcaForPreRegisteredTables();
133 
134  $this->assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
135  $this->assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
136  }
137 
141  public function ‪areElementsOfDifferentExtensionsRegistered(): void
142  {
143  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
144  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
145  $this->subject->applyTcaForPreRegisteredTables();
146 
147  $this->assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
148  $this->assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
149  }
150 
155  {
156  $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
157  $this->subject->add('test_extension_b', $this->tables['first'], 'categories2');
158  $this->subject->applyTcaForPreRegisteredTables();
159 
160  $this->assertArrayHasKey('categories1', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
161  $this->assertArrayHasKey('categories2', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
162  }
163 
168  {
169  $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
170  $this->subject->add('test_extension_a', $this->tables['first'], 'categories2');
171  $this->subject->applyTcaForPreRegisteredTables();
172 
173  $this->assertArrayHasKey('categories1', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
174  $this->assertArrayHasKey('categories2', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
175  }
176 
181  {
182  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
183  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
184  $this->subject->add('test_extension_c', $this->tables['first'], 'categories');
185  $definitions = $this->subject->getDatabaseTableDefinitions();
186  $matches = [];
187  preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
188  $this->assertCount(2, $matches[0]);
189  $this->assertEquals($matches[1][0], $this->tables['first']);
190  $this->assertEquals($matches[2][0], 'categories');
191  $this->assertEquals($matches[1][1], $this->tables['second']);
192  $this->assertEquals($matches[2][1], 'categories');
193  }
194 
199  {
200  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
201  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
202  $definitions = $this->subject->getDatabaseTableDefinition('test_extension_a');
203  $matches = [];
204  preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
205  $this->assertCount(1, $matches[0]);
206  $this->assertEquals($matches[1][0], $this->tables['first']);
207  $this->assertEquals($matches[2][0], 'categories');
208  }
209 
213  public function ‪areDefaultCategorizedTablesLoaded(): void
214  {
215  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
216  $this->subject->applyTcaForPreRegisteredTables();
217 
218  $this->assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
219  $this->assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
220  }
221 
225  public function ‪canApplyTca(): void
226  {
227  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
228  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
229  $this->subject->applyTcaForPreRegisteredTables();
230 
231  $this->assertNotEmpty(‪$GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
232  $this->assertNotEmpty(‪$GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
233  }
234 
239  {
240  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
241  $this->assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
242  }
243 
248  {
249  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
250  $this->assertFalse($this->subject->isRegistered($this->tables['first'], '_not_registered'));
251  $this->assertFalse($this->subject->isRegistered($this->tables['second'], 'categories'));
252  }
253 
257  public function ‪tabIsAddedForElement(): void
258  {
259  $this->subject->add('text_extension_a', $this->tables['first']);
260  $this->subject->applyTcaForPreRegisteredTables();
261 
262  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
263  $this->assertContains('--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
264  }
265  }
266 
271  {
272  $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['fieldList' => 'categories']);
273  $this->subject->applyTcaForPreRegisteredTables();
274 
275  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
276  $this->assertNotContains('--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
277  }
278  }
279 
284  {
285  $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['typesList' => '0']);
286  $this->subject->applyTcaForPreRegisteredTables();
287  $this->assertSame('', ‪$GLOBALS['TCA'][$this->tables['first']]['types'][1]['showitem']);
288  }
289 
293  public function ‪tabIsAddedOnlyOncePerTable(): void
294  {
295  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
296  $this->subject->add('text_extension_a', $this->tables['first'], 'categories2');
297  $this->subject->applyTcaForPreRegisteredTables();
298 
299  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
300  $this->assertSame(
301  1,
302  substr_count($typeConfig['showitem'], '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
303  );
304  }
305  }
306 
310  public function ‪addAllowsSettingOfTheSameTableFieldTwice(): void
311  {
312  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
313  $result = $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
314  $this->assertTrue($result);
315  }
316 
320  public function ‪addInitializesMissingTypes(): void
321  {
322  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
323  ‪$GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd'] = ['showitem' => ''];
324  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
325  $this->assertSame(
326  1,
327  substr_count(‪$GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd']['showitem'], '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
328  );
329  }
330 }
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\$subject
‪CategoryRegistry $subject
Definition: CategoryRegistryTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\setUp
‪setUp()
Definition: CategoryRegistryTest.php:37
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest
Definition: CategoryRegistryTest.php:25
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\isRegisteredReturnsFalseIfElementIsNotRegistered
‪isRegisteredReturnsFalseIfElementIsNotRegistered()
Definition: CategoryRegistryTest.php:245
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsNotAddedForElementIfFieldListIsSpecified
‪tabIsNotAddedForElementIfFieldListIsSpecified()
Definition: CategoryRegistryTest.php:268
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnInvalidExtensionKey
‪doesAddThrowExceptionOnInvalidExtensionKey()
Definition: CategoryRegistryTest.php:115
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddReturnTrueOnDefinedTable
‪doesAddReturnTrueOnDefinedTable()
Definition: CategoryRegistryTest.php:65
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areDatabaseDefinitionsOfParticularExtensionAvailable
‪areDatabaseDefinitionsOfParticularExtensionAvailable()
Definition: CategoryRegistryTest.php:196
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\canApplyTca
‪canApplyTca()
Definition: CategoryRegistryTest.php:223
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsAddedOnlyOncePerTable
‪tabIsAddedOnlyOncePerTable()
Definition: CategoryRegistryTest.php:291
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areDatabaseDefinitionsOfAllElementsAvailable
‪areDatabaseDefinitionsOfAllElementsAvailable()
Definition: CategoryRegistryTest.php:178
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\addAllowsSettingOfTheSameTableFieldTwice
‪addAllowsSettingOfTheSameTableFieldTwice()
Definition: CategoryRegistryTest.php:308
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areElementsOfSameExtensionOnSameTableRegistered
‪areElementsOfSameExtensionOnSameTableRegistered()
Definition: CategoryRegistryTest.php:165
‪TYPO3\CMS\Core\Category\CategoryRegistry
Definition: CategoryRegistry.php:27
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsAddedForElement
‪tabIsAddedForElement()
Definition: CategoryRegistryTest.php:255
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsOnlyAddedForTypesThatAreSpecifiedInTypesList
‪tabIsOnlyAddedForTypesThatAreSpecifiedInTypesList()
Definition: CategoryRegistryTest.php:281
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime
‪doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime()
Definition: CategoryRegistryTest.php:73
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\isRegisteredReturnsTrueIfElementIsAlreadyRegistered
‪isRegisteredReturnsTrueIfElementIsAlreadyRegistered()
Definition: CategoryRegistryTest.php:236
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\addInitializesMissingTypes
‪addInitializesMissingTypes()
Definition: CategoryRegistryTest.php:318
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areElementsOfDifferentExtensionsOnSameTableRegistered
‪areElementsOfDifferentExtensionsOnSameTableRegistered()
Definition: CategoryRegistryTest.php:152
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnEmptyTablename
‪doesAddThrowExceptionOnEmptyTablename()
Definition: CategoryRegistryTest.php:82
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areDefaultCategorizedTablesLoaded
‪areDefaultCategorizedTablesLoaded()
Definition: CategoryRegistryTest.php:211
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areMultipleElementsOfSameExtensionRegistered
‪areMultipleElementsOfSameExtensionRegistered()
Definition: CategoryRegistryTest.php:126
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnInvalidTablename
‪doesAddThrowExceptionOnInvalidTablename()
Definition: CategoryRegistryTest.php:104
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\$tables
‪array $tables
Definition: CategoryRegistryTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnEmptyExtensionKey
‪doesAddThrowExceptionOnEmptyExtensionKey()
Definition: CategoryRegistryTest.php:93
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areElementsOfDifferentExtensionsRegistered
‪areElementsOfDifferentExtensionsRegistered()
Definition: CategoryRegistryTest.php:139
‪TYPO3\CMS\Core\Tests\Unit\Category
Definition: CategoryRegistryTest.php:3