[FEATURE] Allow multiple category fields per table
[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 $fixture;
39
40 /**
41 * @var array
42 */
43 protected $tables;
44
45 /**
46 * Sets up this test suite.
47 */
48 protected function setUp() {
49 $this->fixture = 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 * Tears down this test suite.
67 */
68 protected function tearDown() {
69 unset($this->tables);
70 unset($this->fixture);
71 }
72
73 /**
74 * @test
75 */
76 public function doesAddReturnTrueOnDefinedTable() {
77 $this->assertTrue($this->fixture->add('test_extension_a', $this->tables['first'], 'categories'));
78 }
79
80 /**
81 * @test
82 */
83 public function doesAddReturnFalseOnUndefinedTable() {
84 $this->assertFalse($this->fixture->add('test_extension_a', 'undefined_table', 'categories'));
85 }
86
87 /**
88 * @test
89 * @expectedException \InvalidArgumentException
90 * @expectedExceptionCode 1369122038
91 */
92 public function doesAddThrowExceptionOnEmptyTablename() {
93 $this->fixture->add('test_extension_a', '', 'categories');
94 }
95
96 /**
97 * @test
98 * @expectedException \InvalidArgumentException
99 * @expectedExceptionCode 1378976970
100 */
101 public function doesAddThrowExceptionIfOptionsIsNotAnArray() {
102 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories', 'not_an_array');
103 }
104
105 /**
106 * @test
107 */
108 public function areMultipleElementsOfSameExtensionRegistered() {
109 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
110 $this->fixture->add('test_extension_a', $this->tables['second'], 'categories');
111 $this->fixture->applyTca();
112
113 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
114 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
115 }
116
117 /**
118 * @test
119 */
120 public function areElementsOfDifferentExtensionsRegistered() {
121 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
122 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
123 $this->fixture->applyTca();
124
125 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
126 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
127 }
128
129 /**
130 * @test
131 */
132 public function areElementsOfDifferentExtensionsOnSameTableRegistered() {
133 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories1');
134 $this->fixture->add('test_extension_b', $this->tables['first'], 'categories2');
135 $this->fixture->applyTca();
136
137 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
138 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
139 }
140
141 /**
142 * @test
143 */
144 public function areElementsOfSameExtensionOnSameTableRegistered() {
145 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories1');
146 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories2');
147 $this->fixture->applyTca();
148
149 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
150 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
151 }
152
153 /**
154 * @test
155 */
156 public function areDatabaseDefinitionsOfAllElementsAvailable() {
157 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
158 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
159 $this->fixture->add('test_extension_c', $this->tables['first'], 'categories');
160 $definitions = $this->fixture->getDatabaseTableDefinitions();
161 $matches = array();
162 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
163 $this->assertEquals(2, count($matches[0]));
164 $this->assertEquals($matches[1][0], $this->tables['first']);
165 $this->assertEquals($matches[2][0], 'categories');
166 $this->assertEquals($matches[1][1], $this->tables['second']);
167 $this->assertEquals($matches[2][1], 'categories');
168 }
169
170 /**
171 * @test
172 */
173 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
174 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
175 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
176 $definitions = $this->fixture->getDatabaseTableDefinition('test_extension_a');
177 $matches = array();
178 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
179 $this->assertEquals(1, count($matches[0]));
180 $this->assertEquals($matches[1][0], $this->tables['first']);
181 $this->assertEquals($matches[2][0], 'categories');
182 }
183
184 /**
185 * @test
186 */
187 public function areDefaultCategorizedTablesLoaded() {
188 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
189 $this->fixture->applyTca();
190
191 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
192 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
193 }
194
195 /**
196 * @test
197 */
198 public function canApplyTca() {
199 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
200 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
201 $this->fixture->applyTca();
202
203 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
204 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
205 }
206
207 /**
208 * @test
209 */
210 public function isRegisteredReturnsTrueIfElementIsAlreadyRegistered() {
211 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
212 $this->assertTrue($this->fixture->isRegistered($this->tables['first'], 'categories'));
213 }
214
215 /**
216 * @test
217 */
218 public function isRegisteredReturnsFalseIfElementIsNotRegistered() {
219 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
220 $this->assertFalse($this->fixture->isRegistered($this->tables['first'], '_not_registered'));
221 $this->assertFalse($this->fixture->isRegistered($this->tables['second'], 'categories'));
222 }
223
224 /**
225 * @test
226 */
227 public function tabIsAddedForElement() {
228 $this->fixture->add('text_extension_a', $this->tables['first']);
229 $this->fixture->applyTca();
230
231 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
232 $this->assertContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
233 }
234 }
235
236 /**
237 * @test
238 */
239 public function tabIsNotAddedForElementIfFieldListIsSpecified() {
240 $this->fixture->add('text_extension_a', $this->tables['first'], 'categories', array('fieldList' => 'categories'));
241 $this->fixture->applyTca();
242
243 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
244 $this->assertNotContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
245 }
246 }
247
248 /**
249 * @test
250 */
251 public function tabIsAddedOnlyOncePerTable() {
252 $this->fixture->add('text_extension_a', $this->tables['first'], 'categories1');
253 $this->fixture->add('text_extension_a', $this->tables['first'], 'categories2');
254 $this->fixture->applyTca();
255
256 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
257 $this->assertSame(
258 1, substr_count($typeConfig['showitem'], '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category')
259 );
260 }
261
262 }
263
264 }
265
266 ?>