‪TYPO3CMS  10.4
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 {
32  protected ‪$subject;
33 
37  protected ‪$tables;
38 
42  protected function ‪setUp(): void
43  {
44  parent::setUp();
45  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = 'pages';
46  ‪$GLOBALS['TCA']['pages']['columns'] = [];
47  $this->subject = new ‪CategoryRegistry();
48  $this->tables = [
49  'first' => ‪StringUtility::getUniqueId('first'),
50  'second' => ‪StringUtility::getUniqueId('second')
51  ];
52  foreach ($this->tables as $tableName) {
53  ‪$GLOBALS['TCA'][$tableName] = [
54  'ctrl' => [],
55  'columns' => [],
56  'types' => [
57  '0' => [
58  'showitem' => ''
59  ],
60  '1' => [
61  'showitem' => ''
62  ]
63  ],
64  ];
65  }
66  }
67 
71  public function ‪doesAddReturnTrueOnDefinedTable(): void
72  {
73  self::assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
74  }
75 
80  {
81  self::assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
82  self::assertFalse($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
83  }
84 
88  public function ‪doesAddThrowExceptionOnEmptyTablename(): void
89  {
90  $this->expectException(\InvalidArgumentException::class);
91  $this->expectExceptionCode(1369122038);
92 
93  $this->subject->add('test_extension_a', '', 'categories');
94  }
95 
99  public function ‪doesAddThrowExceptionOnEmptyExtensionKey(): void
100  {
101  $this->expectException(\InvalidArgumentException::class);
102  $this->expectExceptionCode(1397836158);
103 
104  $this->subject->add('', 'foo', 'categories');
105  }
106 
110  public function ‪doesAddThrowExceptionOnInvalidTablename(): void
111  {
112  $this->expectException(\InvalidArgumentException::class);
113  $this->expectExceptionCode(1369122038);
114 
115  $this->subject->add('test_extension_a', '', 'categories');
116  }
117 
121  public function ‪doesAddThrowExceptionOnInvalidExtensionKey(): void
122  {
123  $this->expectException(\InvalidArgumentException::class);
124  $this->expectExceptionCode(1397836158);
125 
126  $this->subject->add('', 'foo', 'categories');
127  }
128 
133  {
134  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
135  $this->subject->add('test_extension_a', $this->tables['second'], 'categories');
136  $this->subject->applyTcaForPreRegisteredTables();
137 
138  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
139  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
140  }
141 
145  public function ‪areElementsOfDifferentExtensionsRegistered(): void
146  {
147  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
148  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
149  $this->subject->applyTcaForPreRegisteredTables();
150 
151  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
152  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
153  }
154 
159  {
160  $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
161  $this->subject->add('test_extension_b', $this->tables['first'], 'categories2');
162  $this->subject->applyTcaForPreRegisteredTables();
163 
164  self::assertArrayHasKey('categories1', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
165  self::assertArrayHasKey('categories2', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
166  }
167 
172  {
173  $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
174  $this->subject->add('test_extension_a', $this->tables['first'], 'categories2');
175  $this->subject->applyTcaForPreRegisteredTables();
176 
177  self::assertArrayHasKey('categories1', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
178  self::assertArrayHasKey('categories2', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
179  }
180 
185  {
186  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
187  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
188  $this->subject->add('test_extension_c', $this->tables['first'], 'categories');
189  $definitions = $this->subject->getDatabaseTableDefinitions();
190  $matches = [];
191  preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
192  self::assertCount(2, $matches[0]);
193  self::assertEquals($matches[1][0], $this->tables['first']);
194  self::assertEquals($matches[2][0], 'categories');
195  self::assertEquals($matches[1][1], $this->tables['second']);
196  self::assertEquals($matches[2][1], 'categories');
197  }
198 
203  {
204  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
205  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
206  $definitions = $this->subject->getDatabaseTableDefinition('test_extension_a');
207  $matches = [];
208  preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
209  self::assertCount(1, $matches[0]);
210  self::assertEquals($matches[1][0], $this->tables['first']);
211  self::assertEquals($matches[2][0], 'categories');
212  }
213 
217  public function ‪areDefaultCategorizedTablesLoaded(): void
218  {
219  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
220  $this->subject->applyTcaForPreRegisteredTables();
221 
222  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['first']]['columns']);
223  self::assertArrayHasKey('categories', ‪$GLOBALS['TCA'][$this->tables['second']]['columns']);
224  }
225 
229  public function ‪canApplyTca(): void
230  {
231  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
232  $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
233  $this->subject->applyTcaForPreRegisteredTables();
234 
235  self::assertNotEmpty(‪$GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
236  self::assertNotEmpty(‪$GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
237  }
238 
243  {
244  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
245  self::assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
246  }
247 
252  {
253  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
254  self::assertFalse($this->subject->isRegistered($this->tables['first'], '_not_registered'));
255  self::assertFalse($this->subject->isRegistered($this->tables['second'], 'categories'));
256  }
257 
261  public function ‪tabIsAddedForElement(): void
262  {
263  $this->subject->add('text_extension_a', $this->tables['first']);
264  $this->subject->applyTcaForPreRegisteredTables();
265 
266  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
267  self::assertStringContainsString('--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
268  }
269  }
270 
275  {
276  $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['fieldList' => 'categories']);
277  $this->subject->applyTcaForPreRegisteredTables();
278 
279  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
280  self::assertStringNotContainsString('--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
281  }
282  }
283 
288  {
289  $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['typesList' => '0']);
290  $this->subject->applyTcaForPreRegisteredTables();
291  self::assertSame('', ‪$GLOBALS['TCA'][$this->tables['first']]['types'][1]['showitem']);
292  }
293 
297  public function ‪tabIsAddedOnlyOncePerTable(): void
298  {
299  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
300  $this->subject->add('text_extension_a', $this->tables['first'], 'categories2');
301  $this->subject->applyTcaForPreRegisteredTables();
302 
303  foreach (‪$GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
304  self::assertSame(
305  1,
306  substr_count($typeConfig['showitem'], '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
307  );
308  }
309  }
310 
314  public function ‪addAllowsSettingOfTheSameTableFieldTwice(): void
315  {
316  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
317  $result = $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
318  self::assertTrue($result);
319  }
320 
324  public function ‪addInitializesMissingTypes(): void
325  {
326  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
327  ‪$GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd'] = ['showitem' => ''];
328  $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
329  self::assertSame(
330  1,
331  substr_count(‪$GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd']['showitem'], '--div--;LLL:EXT:core/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
332  );
333  }
334 }
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\$subject
‪CategoryRegistry $subject
Definition: CategoryRegistryTest.php:31
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\setUp
‪setUp()
Definition: CategoryRegistryTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest
Definition: CategoryRegistryTest.php:28
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\isRegisteredReturnsFalseIfElementIsNotRegistered
‪isRegisteredReturnsFalseIfElementIsNotRegistered()
Definition: CategoryRegistryTest.php:249
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsNotAddedForElementIfFieldListIsSpecified
‪tabIsNotAddedForElementIfFieldListIsSpecified()
Definition: CategoryRegistryTest.php:272
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnInvalidExtensionKey
‪doesAddThrowExceptionOnInvalidExtensionKey()
Definition: CategoryRegistryTest.php:119
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddReturnTrueOnDefinedTable
‪doesAddReturnTrueOnDefinedTable()
Definition: CategoryRegistryTest.php:69
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areDatabaseDefinitionsOfParticularExtensionAvailable
‪areDatabaseDefinitionsOfParticularExtensionAvailable()
Definition: CategoryRegistryTest.php:200
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\canApplyTca
‪canApplyTca()
Definition: CategoryRegistryTest.php:227
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsAddedOnlyOncePerTable
‪tabIsAddedOnlyOncePerTable()
Definition: CategoryRegistryTest.php:295
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areDatabaseDefinitionsOfAllElementsAvailable
‪areDatabaseDefinitionsOfAllElementsAvailable()
Definition: CategoryRegistryTest.php:182
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\addAllowsSettingOfTheSameTableFieldTwice
‪addAllowsSettingOfTheSameTableFieldTwice()
Definition: CategoryRegistryTest.php:312
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areElementsOfSameExtensionOnSameTableRegistered
‪areElementsOfSameExtensionOnSameTableRegistered()
Definition: CategoryRegistryTest.php:169
‪TYPO3\CMS\Core\Category\CategoryRegistry
Definition: CategoryRegistry.php:29
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsAddedForElement
‪tabIsAddedForElement()
Definition: CategoryRegistryTest.php:259
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\tabIsOnlyAddedForTypesThatAreSpecifiedInTypesList
‪tabIsOnlyAddedForTypesThatAreSpecifiedInTypesList()
Definition: CategoryRegistryTest.php:285
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime
‪doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime()
Definition: CategoryRegistryTest.php:77
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\isRegisteredReturnsTrueIfElementIsAlreadyRegistered
‪isRegisteredReturnsTrueIfElementIsAlreadyRegistered()
Definition: CategoryRegistryTest.php:240
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\addInitializesMissingTypes
‪addInitializesMissingTypes()
Definition: CategoryRegistryTest.php:322
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areElementsOfDifferentExtensionsOnSameTableRegistered
‪areElementsOfDifferentExtensionsOnSameTableRegistered()
Definition: CategoryRegistryTest.php:156
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnEmptyTablename
‪doesAddThrowExceptionOnEmptyTablename()
Definition: CategoryRegistryTest.php:86
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areDefaultCategorizedTablesLoaded
‪areDefaultCategorizedTablesLoaded()
Definition: CategoryRegistryTest.php:215
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areMultipleElementsOfSameExtensionRegistered
‪areMultipleElementsOfSameExtensionRegistered()
Definition: CategoryRegistryTest.php:130
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnInvalidTablename
‪doesAddThrowExceptionOnInvalidTablename()
Definition: CategoryRegistryTest.php:108
‪TYPO3\CMS\Core\Utility\StringUtility\getUniqueId
‪static string getUniqueId($prefix='')
Definition: StringUtility.php:92
‪$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:35
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\doesAddThrowExceptionOnEmptyExtensionKey
‪doesAddThrowExceptionOnEmptyExtensionKey()
Definition: CategoryRegistryTest.php:97
‪TYPO3\CMS\Core\Tests\Unit\Category\CategoryRegistryTest\areElementsOfDifferentExtensionsRegistered
‪areElementsOfDifferentExtensionsRegistered()
Definition: CategoryRegistryTest.php:143
‪TYPO3\CMS\Core\Utility\StringUtility
Definition: StringUtility.php:22
‪TYPO3\CMS\Core\Tests\Unit\Category
Definition: CategoryRegistryTest.php:18