TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
CategoryRegistryTest.php
Go to the documentation of this file.
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 
21 {
25  protected $subject;
26 
30  protected $tables;
31 
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 
64  {
65  $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
66  }
67 
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 
81  {
82  $this->expectException(\InvalidArgumentException::class);
83  $this->expectExceptionCode(1369122038);
84 
85  $this->subject->add('test_extension_a', '', 'categories');
86  }
87 
92  {
93  $this->expectException(\InvalidArgumentException::class);
94  $this->expectExceptionCode(1397836158);
95 
96  $this->subject->add('', 'foo', 'categories');
97  }
98 
103  {
104  $this->expectException(\InvalidArgumentException::class);
105  $this->expectExceptionCode(1369122038);
106 
107  $this->subject->add('test_extension_a', [], 'categories');
108  }
109 
114  {
115  $this->expectException(\InvalidArgumentException::class);
116  $this->expectExceptionCode(1397836158);
117 
118  $this->subject->add([], 'foo', 'categories');
119  }
120 
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 
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 
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 
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 
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 
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 
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 
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 
235  {
236  $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
237  $this->assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
238  }
239 
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 
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 
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 
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 
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 
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 
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 
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 }
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']