[BUGFIX] Fix oddities of the CategoryRegistry
[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 * Copyright notice
6 *
7 * (c) 2012-2013 Oliver Hader <oliver.hader@typo3.org>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 /**
28 * Testcase for CategoryRegistry
29 *
30 * @author Oliver Hader <oliver.hader@typo3.org>
31 * @author Fabien Udriot <fabien.udriot@typo3.org>
32 */
33 class CategoryRegistryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
34
35 /**
36 * @var \TYPO3\CMS\Core\Category\CategoryRegistry
37 */
38 protected $subject;
39
40 /**
41 * @var array
42 */
43 protected $tables;
44
45 /**
46 * Sets up this test suite.
47 */
48 protected function setUp() {
49 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = 'pages';
50 $GLOBALS['TCA']['pages']['columns'] = array();
51 $this->subject = new \TYPO3\CMS\Core\Category\CategoryRegistry();
52 $this->tables = array(
53 'first' => uniqid('first'),
54 'second' => uniqid('second')
55 );
56 foreach ($this->tables as $tableName) {
57 $GLOBALS['TCA'][$tableName] = array(
58 'ctrl' => array(),
59 'columns' => array(),
60 'types' => array(
61 '1' => array()
62 ),
63 );
64 }
65 }
66
67 /**
68 * @test
69 */
70 public function doesAddReturnTrueOnDefinedTable() {
71 $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
72 }
73
74 /**
75 * @test
76 */
77 public function doesAddReturnTrueOnDefinedTableTheFirstTimeAndFalseTheSecondTime() {
78 $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
79 $this->assertFalse($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
80 }
81
82 /**
83 * @test
84 * @expectedException \InvalidArgumentException
85 * @expectedExceptionCode 1369122038
86 */
87 public function doesAddThrowExceptionOnEmptyTablename() {
88 $this->subject->add('test_extension_a', '', 'categories');
89 }
90
91 /**
92 * @test
93 * @expectedException \InvalidArgumentException
94 * @expectedExceptionCode 1397836158
95 */
96 public function doesAddThrowExceptionOnEmptyExtensionKey() {
97 $this->subject->add('', 'foo', 'categories');
98 }
99
100 /**
101 * @test
102 * @expectedException \InvalidArgumentException
103 * @expectedExceptionCode 1369122038
104 */
105 public function doesAddThrowExceptionOnInvalidTablename() {
106 $this->subject->add('test_extension_a', array(), 'categories');
107 }
108
109 /**
110 * @test
111 * @expectedException \InvalidArgumentException
112 * @expectedExceptionCode 1397836158
113 */
114 public function doesAddThrowExceptionOnInvalidExtensionKey() {
115 $this->subject->add(array(), 'foo', 'categories');
116 }
117
118 /**
119 * @test
120 * @expectedException \RuntimeException
121 * @expectedExceptionCode 1397838817
122 */
123 public function throwsExceptionIfAddIsCalledForNotExistentTableAfterTcaHasBeenApplied() {
124 $this->subject->applyTcaForPreRegisteredTables();
125 $this->subject->add('test_extension_a', uniqid('foo'));
126 }
127
128 /**
129 * @expectedException \RuntimeException
130 * @expectedExceptionCode 1397841334
131 * @test
132 */
133 public function throwsExceptionIfRegistryIsAppliedTwice() {
134 $this->subject->applyTcaForPreRegisteredTables();
135 $this->subject->applyTcaForPreRegisteredTables();
136 }
137
138 /**
139 * @test
140 */
141 public function areMultipleElementsOfSameExtensionRegistered() {
142 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
143 $this->subject->add('test_extension_a', $this->tables['second'], 'categories');
144 $this->subject->applyTcaForPreRegisteredTables();
145
146 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
147 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
148 }
149
150 /**
151 * @test
152 */
153 public function areElementsOfDifferentExtensionsRegistered() {
154 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
155 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
156 $this->subject->applyTcaForPreRegisteredTables();
157
158 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
159 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
160 }
161
162 /**
163 * @test
164 */
165 public function areElementsOfDifferentExtensionsOnSameTableRegistered() {
166 $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
167 $this->subject->add('test_extension_b', $this->tables['first'], 'categories2');
168 $this->subject->applyTcaForPreRegisteredTables();
169
170 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
171 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
172 }
173
174 /**
175 * @test
176 */
177 public function areElementsOfSameExtensionOnSameTableRegistered() {
178 $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
179 $this->subject->add('test_extension_a', $this->tables['first'], 'categories2');
180 $this->subject->applyTcaForPreRegisteredTables();
181
182 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
183 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
184 }
185
186 /**
187 * @test
188 */
189 public function areDatabaseDefinitionsOfAllElementsAvailable() {
190 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
191 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
192 $this->subject->add('test_extension_c', $this->tables['first'], 'categories');
193 $definitions = $this->subject->getDatabaseTableDefinitions();
194 $matches = array();
195 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
196 $this->assertEquals(2, count($matches[0]));
197 $this->assertEquals($matches[1][0], $this->tables['first']);
198 $this->assertEquals($matches[2][0], 'categories');
199 $this->assertEquals($matches[1][1], $this->tables['second']);
200 $this->assertEquals($matches[2][1], 'categories');
201 }
202
203 /**
204 * @test
205 */
206 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
207 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
208 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
209 $definitions = $this->subject->getDatabaseTableDefinition('test_extension_a');
210 $matches = array();
211 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
212 $this->assertEquals(1, count($matches[0]));
213 $this->assertEquals($matches[1][0], $this->tables['first']);
214 $this->assertEquals($matches[2][0], 'categories');
215 }
216
217 /**
218 * @test
219 */
220 public function areDefaultCategorizedTablesLoaded() {
221 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
222 $this->subject->applyTcaForPreRegisteredTables();
223
224 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
225 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
226 }
227
228 /**
229 * @test
230 */
231 public function canApplyTca() {
232 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
233 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
234 $this->subject->applyTcaForPreRegisteredTables();
235
236 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
237 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
238 }
239
240 /**
241 * @test
242 */
243 public function isRegisteredReturnsTrueIfElementIsAlreadyRegistered() {
244 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
245 $this->assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
246 }
247
248 /**
249 * @test
250 */
251 public function isRegisteredReturnsFalseIfElementIsNotRegistered() {
252 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
253 $this->assertFalse($this->subject->isRegistered($this->tables['first'], '_not_registered'));
254 $this->assertFalse($this->subject->isRegistered($this->tables['second'], 'categories'));
255 }
256
257 /**
258 * @test
259 */
260 public function tabIsAddedForElement() {
261 $this->subject->add('text_extension_a', $this->tables['first']);
262 $this->subject->applyTcaForPreRegisteredTables();
263
264 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
265 $this->assertContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
266 }
267 }
268
269 /**
270 * @test
271 */
272 public function tabIsNotAddedForElementIfFieldListIsSpecified() {
273 $this->subject->add('text_extension_a', $this->tables['first'], 'categories', array('fieldList' => 'categories'));
274 $this->subject->applyTcaForPreRegisteredTables();
275
276 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
277 $this->assertNotContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
278 }
279 }
280
281 /**
282 * @test
283 */
284 public function tabIsAddedOnlyOncePerTable() {
285 $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
286 $this->subject->add('text_extension_a', $this->tables['first'], 'categories2');
287 $this->subject->applyTcaForPreRegisteredTables();
288
289 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
290 $this->assertSame(
291 1, substr_count($typeConfig['showitem'], '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category')
292 );
293 }
294
295 }
296
297 }