[BUGFIX] Do not add category to empty table names
[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('ctrl' => array(), 'columns' => array());
56 }
57 }
58
59 /**
60 * Tears down this test suite.
61 */
62 protected function tearDown() {
63 unset($this->tables);
64 unset($this->fixture);
65 }
66
67 /**
68 * @test
69 */
70 public function isRegistryEmptyByDefault() {
71 $this->assertEquals(array(), $this->fixture->get());
72 }
73
74 /**
75 * @test
76 */
77 public function doesAddReturnTrueOnDefinedTable() {
78 $this->assertTrue($this->fixture->add('test_extension_a', $this->tables['first'], 'categories'));
79 }
80
81 /**
82 * @test
83 */
84 public function doesAddReturnFalseOnUndefinedTable() {
85 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
86
87 $this->assertFalse(
88 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories')
89 );
90 }
91
92 /**
93 * @test
94 * @expectedException \RuntimeException
95 * @expectedExceptionCode 1369122038
96 */
97 public function doesAddThrowExceptionOnEmptyAdds() {
98 $this->fixture->add('test_extension_a', '', 'categories');
99 }
100
101 /**
102 * @test
103 */
104 public function areMultipleElementsOfSameExtensionRegistered() {
105 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
106 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
107 $registry = $this->fixture->get();
108 ob_flush();
109
110 $this->assertEquals(
111 'categories',
112 $registry['test_extension_a'][$this->tables['first']]['fieldName']
113 );
114
115 $this->assertEquals(
116 'categories',
117 $registry['test_extension_b'][$this->tables['second']]['fieldName']
118 );
119 }
120
121 /**
122 * @test
123 */
124 public function areElementsOfDifferentExtensionsRegistered() {
125 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
126 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
127 $registry = $this->fixture->get();
128
129 $this->assertEquals(
130 'categories',
131 $registry['test_extension_a'][$this->tables['first']]['fieldName']
132 );
133
134 $this->assertEquals(
135 'categories',
136 $registry['test_extension_b'][$this->tables['second']]['fieldName']
137 );
138 }
139
140 /**
141 * @test
142 */
143 public function areElementsOnSameTableOverridden() {
144 $this->fixture->add('test_extension_a', $this->tables['first'], $this->tables['first']);
145 $this->fixture->add('test_extension_b', $this->tables['second'], $this->tables['second']);
146 $registry = $this->fixture->get();
147
148 $this->assertEquals(
149 $this->tables['first'],
150 $registry['test_extension_a'][$this->tables['first']]['fieldName']
151 );
152 }
153
154 /**
155 * @test
156 */
157 public function areDatabaseDefinitionsOfAllElementsAvailable() {
158 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
159 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
160 $this->fixture->add('test_extension_c', $this->tables['first'], 'categories');
161 $definitions = $this->fixture->getDatabaseTableDefinitions();
162 $matches = array();
163 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
164 $this->assertEquals(2, count($matches[0]));
165 $this->assertEquals($matches[1][0], $this->tables['first']);
166 $this->assertEquals($matches[2][0], 'categories');
167 $this->assertEquals($matches[1][1], $this->tables['second']);
168 $this->assertEquals($matches[2][1], 'categories');
169 }
170
171 /**
172 * @test
173 */
174 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
175 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
176 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
177 $definitions = $this->fixture->getDatabaseTableDefinition('test_extension_a');
178 $matches = array();
179 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
180 $this->assertEquals(1, count($matches[0]));
181 $this->assertEquals($matches[1][0], $this->tables['first']);
182 $this->assertEquals($matches[2][0], 'categories');
183 }
184
185 /**
186 * @test
187 */
188 public function areDefaultCategorizedTablesLoaded() {
189 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
190 $this->fixture->applyTca();
191
192 $registry = $this->fixture->get();
193 $this->assertCount(2, $registry['core']);
194 $this->assertSame(key($registry['core']), $this->tables['first']);
195 }
196
197 /**
198 * @test
199 */
200 public function areEmptyStringDefaultCategorizedTablesLoaded() {
201 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = '';
202 $this->fixture->applyTca();
203
204 $registry = $this->fixture->get();
205 $this->assertNull($registry['core']);
206 }
207
208 /**
209 * @test
210 */
211 public function areNullDefaultCategorizedTablesLoaded() {
212 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = NULL;
213 $this->fixture->applyTca();
214
215 $registry = $this->fixture->get();
216 $this->assertNull($registry['core']);
217 }
218
219 /**
220 * @test
221 */
222 public function canApplyTca() {
223 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
224 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
225 $this->fixture->applyTca();
226 foreach ($this->tables as $table) {
227 $this->assertNotEmpty($GLOBALS['TCA'][$table]['columns']['categories']);
228 }
229 }
230 }
231
232 ?>