97bcb374de3620b94bd71090985a4677efb8a29d
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Category / CategoryRegistryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Category;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Testcase for CategoryRegistry
19 */
20 class CategoryRegistryTest extends \TYPO3\Components\TestingFramework\Core\UnitTestCase
21 {
22 /**
23 * @var \TYPO3\CMS\Core\Category\CategoryRegistry
24 */
25 protected $subject;
26
27 /**
28 * @var array
29 */
30 protected $tables;
31
32 /**
33 * Sets up this test suite.
34 */
35 protected function setUp()
36 {
37 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = 'pages';
38 $GLOBALS['TCA']['pages']['columns'] = [];
39 $this->subject = new \TYPO3\CMS\Core\Category\CategoryRegistry();
40 $this->tables = [
41 'first' => $this->getUniqueId('first'),
42 'second' => $this->getUniqueId('second')
43 ];
44 foreach ($this->tables as $tableName) {
45 $GLOBALS['TCA'][$tableName] = [
46 'ctrl' => [],
47 'columns' => [],
48 'types' => [
49 '0' => [
50 'showitem' => ''
51 ],
52 '1' => [
53 'showitem' => ''
54 ]
55 ],
56 ];
57 }
58 }
59
60 /**
61 * @test
62 */
63 public function doesAddReturnTrueOnDefinedTable()
64 {
65 $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
66 }
67
68 /**
69 * @test
70 */
71 public function doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime()
72 {
73 $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
74 $this->assertFalse($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
75 }
76
77 /**
78 * @test
79 */
80 public function doesAddThrowExceptionOnEmptyTablename()
81 {
82 $this->expectException(\InvalidArgumentException::class);
83 $this->expectExceptionCode(1369122038);
84
85 $this->subject->add('test_extension_a', '', 'categories');
86 }
87
88 /**
89 * @test
90 */
91 public function doesAddThrowExceptionOnEmptyExtensionKey()
92 {
93 $this->expectException(\InvalidArgumentException::class);
94 $this->expectExceptionCode(1397836158);
95
96 $this->subject->add('', 'foo', 'categories');
97 }
98
99 /**
100 * @test
101 */
102 public function doesAddThrowExceptionOnInvalidTablename()
103 {
104 $this->expectException(\InvalidArgumentException::class);
105 $this->expectExceptionCode(1369122038);
106
107 $this->subject->add('test_extension_a', [], 'categories');
108 }
109
110 /**
111 * @test
112 */
113 public function doesAddThrowExceptionOnInvalidExtensionKey()
114 {
115 $this->expectException(\InvalidArgumentException::class);
116 $this->expectExceptionCode(1397836158);
117
118 $this->subject->add([], 'foo', 'categories');
119 }
120
121 /**
122 * @test
123 */
124 public function areMultipleElementsOfSameExtensionRegistered()
125 {
126 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
127 $this->subject->add('test_extension_a', $this->tables['second'], 'categories');
128 $this->subject->applyTcaForPreRegisteredTables();
129
130 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
131 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
132 }
133
134 /**
135 * @test
136 */
137 public function areElementsOfDifferentExtensionsRegistered()
138 {
139 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
140 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
141 $this->subject->applyTcaForPreRegisteredTables();
142
143 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
144 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
145 }
146
147 /**
148 * @test
149 */
150 public function areElementsOfDifferentExtensionsOnSameTableRegistered()
151 {
152 $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
153 $this->subject->add('test_extension_b', $this->tables['first'], 'categories2');
154 $this->subject->applyTcaForPreRegisteredTables();
155
156 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
157 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
158 }
159
160 /**
161 * @test
162 */
163 public function areElementsOfSameExtensionOnSameTableRegistered()
164 {
165 $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
166 $this->subject->add('test_extension_a', $this->tables['first'], 'categories2');
167 $this->subject->applyTcaForPreRegisteredTables();
168
169 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
170 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
171 }
172
173 /**
174 * @test
175 */
176 public function areDatabaseDefinitionsOfAllElementsAvailable()
177 {
178 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
179 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
180 $this->subject->add('test_extension_c', $this->tables['first'], 'categories');
181 $definitions = $this->subject->getDatabaseTableDefinitions();
182 $matches = [];
183 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
184 $this->assertEquals(2, count($matches[0]));
185 $this->assertEquals($matches[1][0], $this->tables['first']);
186 $this->assertEquals($matches[2][0], 'categories');
187 $this->assertEquals($matches[1][1], $this->tables['second']);
188 $this->assertEquals($matches[2][1], 'categories');
189 }
190
191 /**
192 * @test
193 */
194 public function areDatabaseDefinitionsOfParticularExtensionAvailable()
195 {
196 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
197 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
198 $definitions = $this->subject->getDatabaseTableDefinition('test_extension_a');
199 $matches = [];
200 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
201 $this->assertEquals(1, count($matches[0]));
202 $this->assertEquals($matches[1][0], $this->tables['first']);
203 $this->assertEquals($matches[2][0], 'categories');
204 }
205
206 /**
207 * @test
208 */
209 public function areDefaultCategorizedTablesLoaded()
210 {
211 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
212 $this->subject->applyTcaForPreRegisteredTables();
213
214 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
215 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
216 }
217
218 /**
219 * @test
220 */
221 public function canApplyTca()
222 {
223 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
224 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
225 $this->subject->applyTcaForPreRegisteredTables();
226
227 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
228 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
229 }
230
231 /**
232 * @test
233 */
234 public function isRegisteredReturnsTrueIfElementIsAlreadyRegistered()
235 {
236 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
237 $this->assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
238 }
239
240 /**
241 * @test
242 */
243 public function isRegisteredReturnsFalseIfElementIsNotRegistered()
244 {
245 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
246 $this->assertFalse($this->subject->isRegistered($this->tables['first'], '_not_registered'));
247 $this->assertFalse($this->subject->isRegistered($this->tables['second'], 'categories'));
248 }
249
250 /**
251 * @test
252 */
253 public function tabIsAddedForElement()
254 {
255 $this->subject->add('text_extension_a', $this->tables['first']);
256 $this->subject->applyTcaForPreRegisteredTables();
257
258 foreach ($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
259 $this->assertContains('--div--;LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
260 }
261 }
262
263 /**
264 * @test
265 */
266 public function tabIsNotAddedForElementIfFieldListIsSpecified()
267 {
268 $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['fieldList' => 'categories']);
269 $this->subject->applyTcaForPreRegisteredTables();
270
271 foreach ($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
272 $this->assertNotContains('--div--;LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
273 }
274 }
275
276 /**
277 * @test
278 */
279 public function tabIsOnlyAddedForTypesThatAreSpecifiedInTypesList()
280 {
281 $this->subject->add('text_extension_a', $this->tables['first'], 'categories', ['typesList' => '0']);
282 $this->subject->applyTcaForPreRegisteredTables();
283 $this->assertSame('', $GLOBALS['TCA'][$this->tables['first']]['types'][1]['showitem']);
284 }
285
286 /**
287 * @test
288 */
289 public function tabIsAddedOnlyOncePerTable()
290 {
291 $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
292 $this->subject->add('text_extension_a', $this->tables['first'], 'categories2');
293 $this->subject->applyTcaForPreRegisteredTables();
294
295 foreach ($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
296 $this->assertSame(
297 1, substr_count($typeConfig['showitem'], '--div--;LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
298 );
299 }
300 }
301
302 /**
303 * @test
304 */
305 public function addAllowsSettingOfTheSameTableFieldTwice()
306 {
307 $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
308 $result = $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
309 $this->assertTrue($result);
310 }
311
312 /**
313 * @test
314 */
315 public function addInitializesMissingTypes()
316 {
317 $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
318 $GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd'] = ['showitem' => ''];
319 $this->subject->add('text_extension_a', $this->tables['first'], 'categories1', [], true);
320 $this->assertSame(
321 1, substr_count($GLOBALS['TCA'][$this->tables['first']]['types']['newtypeafterfirstadd']['showitem'], '--div--;LLL:EXT:lang/Resources/Private/Language/locallang_tca.xlf:sys_category.tabs.category')
322 );
323 }
324
325 /**
326 * @test
327 */
328 public function addAddsOnlyOneSqlString()
329 {
330 $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
331 $this->subject->add('text_extension_b', $this->tables['first'], 'categories1', [], true);
332 $sqlData = $this->subject->addExtensionCategoryDatabaseSchemaToTablesDefinition([], 'text_extension_a');
333 $this->assertEmpty($sqlData['sqlString'][0]);
334 }
335 }