[BUGFIX] Make category TCA changes cacheable
[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 $this->subject = new \TYPO3\CMS\Core\Category\CategoryRegistry();
50 $this->tables = array(
51 'first' => uniqid('first'),
52 'second' => uniqid('second')
53 );
54 foreach ($this->tables as $tableName) {
55 $GLOBALS['TCA'][$tableName] = array(
56 'ctrl' => array(),
57 'columns' => array(),
58 'types' => array(
59 '1' => array()
60 ),
61 );
62 }
63 }
64
65 /**
66 * @test
67 */
68 public function doesAddReturnTrueOnDefinedTable() {
69 $this->assertTrue($this->subject->add('test_extension_a', $this->tables['first'], 'categories'));
70 }
71
72 /**
73 * @test
74 * @expectedException \InvalidArgumentException
75 * @expectedExceptionCode 1369122038
76 */
77 public function doesAddThrowExceptionOnEmptyTablename() {
78 $this->subject->add('test_extension_a', '', 'categories');
79 }
80
81 /**
82 * @test
83 */
84 public function addCategoryRegistryTcaChangesReturnsGlobalTcaArray() {
85 $GLOBALS['TCA'] = array(
86 'foo' => array(
87 'bar' => 'baz'
88 )
89 );
90 list($tca) = $this->subject->addCategoryRegistryTcaChanges($GLOBALS['TCA']);
91 $this->assertSame($tca, $GLOBALS['TCA']);
92 }
93
94 /**
95 * @test
96 */
97 public function areMultipleElementsOfSameExtensionRegistered() {
98 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
99 $this->subject->add('test_extension_a', $this->tables['second'], 'categories');
100 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
101
102 $this->assertArrayHasKey('categories', $tca[$this->tables['first']]['columns']);
103 $this->assertArrayHasKey('categories', $tca[$this->tables['second']]['columns']);
104 }
105
106 /**
107 * @test
108 */
109 public function areElementsOfDifferentExtensionsRegistered() {
110 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
111 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
112 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
113
114 $this->assertArrayHasKey('categories', $tca[$this->tables['first']]['columns']);
115 $this->assertArrayHasKey('categories', $tca[$this->tables['second']]['columns']);
116 }
117
118 /**
119 * @test
120 */
121 public function areElementsOfDifferentExtensionsOnSameTableRegistered() {
122 $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
123 $this->subject->add('test_extension_b', $this->tables['first'], 'categories2');
124 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
125
126 $this->assertArrayHasKey('categories1', $tca[$this->tables['first']]['columns']);
127 $this->assertArrayHasKey('categories2', $tca[$this->tables['first']]['columns']);
128 }
129
130 /**
131 * @test
132 */
133 public function areElementsOfSameExtensionOnSameTableRegistered() {
134 $this->subject->add('test_extension_a', $this->tables['first'], 'categories1');
135 $this->subject->add('test_extension_a', $this->tables['first'], 'categories2');
136 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
137
138 $this->assertArrayHasKey('categories1', $tca[$this->tables['first']]['columns']);
139 $this->assertArrayHasKey('categories2', $tca[$this->tables['first']]['columns']);
140 }
141
142 /**
143 * @test
144 */
145 public function areDatabaseDefinitionsOfAllElementsAvailable() {
146 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
147 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
148 $this->subject->add('test_extension_c', $this->tables['first'], 'categories');
149 $definitions = $this->subject->getDatabaseTableDefinitions();
150 $matches = array();
151 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
152 $this->assertEquals(2, count($matches[0]));
153 $this->assertEquals($matches[1][0], $this->tables['first']);
154 $this->assertEquals($matches[2][0], 'categories');
155 $this->assertEquals($matches[1][1], $this->tables['second']);
156 $this->assertEquals($matches[2][1], 'categories');
157 }
158
159 /**
160 * @test
161 */
162 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
163 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
164 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
165 $definitions = $this->subject->getDatabaseTableDefinition('test_extension_a');
166 $matches = array();
167 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
168 $this->assertEquals(1, count($matches[0]));
169 $this->assertEquals($matches[1][0], $this->tables['first']);
170 $this->assertEquals($matches[2][0], 'categories');
171 }
172
173 /**
174 * @test
175 */
176 public function areDefaultCategorizedTablesLoaded() {
177 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
178 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
179
180 $this->assertArrayHasKey('categories', $tca[$this->tables['first']]['columns']);
181 $this->assertArrayHasKey('categories', $tca[$this->tables['second']]['columns']);
182 }
183
184 /**
185 * @test
186 */
187 public function canApplyTca() {
188 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
189 $this->subject->add('test_extension_b', $this->tables['second'], 'categories');
190 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
191
192 $this->assertNotEmpty($tca[$this->tables['first']]['columns']['categories']);
193 $this->assertNotEmpty($tca[$this->tables['second']]['columns']['categories']);
194 }
195
196 /**
197 * @test
198 */
199 public function isRegisteredReturnsTrueIfElementIsAlreadyRegistered() {
200 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
201 $this->assertTrue($this->subject->isRegistered($this->tables['first'], 'categories'));
202 }
203
204 /**
205 * @test
206 */
207 public function isRegisteredReturnsFalseIfElementIsNotRegistered() {
208 $this->subject->add('test_extension_a', $this->tables['first'], 'categories');
209 $this->assertFalse($this->subject->isRegistered($this->tables['first'], '_not_registered'));
210 $this->assertFalse($this->subject->isRegistered($this->tables['second'], 'categories'));
211 }
212
213 /**
214 * @test
215 */
216 public function tabIsAddedForElement() {
217 $this->subject->add('text_extension_a', $this->tables['first']);
218 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
219
220 foreach($tca[$this->tables['first']]['types'] as $typeConfig) {
221 $this->assertContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
222 }
223 }
224
225 /**
226 * @test
227 */
228 public function tabIsNotAddedForElementIfFieldListIsSpecified() {
229 $this->subject->add('text_extension_a', $this->tables['first'], 'categories', array('fieldList' => 'categories'));
230 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
231
232 foreach($tca[$this->tables['first']]['types'] as $typeConfig) {
233 $this->assertNotContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
234 }
235 }
236
237 /**
238 * @test
239 */
240 public function tabIsAddedOnlyOncePerTable() {
241 $this->subject->add('text_extension_a', $this->tables['first'], 'categories1');
242 $this->subject->add('text_extension_a', $this->tables['first'], 'categories2');
243 list($tca) = $this->subject->addCategoryRegistryTcaChanges(array());
244
245 foreach($tca[$this->tables['first']]['types'] as $typeConfig) {
246 $this->assertSame(
247 1, substr_count($typeConfig['showitem'], '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category')
248 );
249 }
250
251 }
252
253 }