[BUGFIX] Follow-up: Fix broken test in CategoryRegistryTest
[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 */
99 public function areMultipleElementsOfSameExtensionRegistered() {
100 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
101 $this->fixture->add('test_extension_a', $this->tables['second'], 'categories');
102 $this->fixture->applyTca();
103
104 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
105 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
106 }
107
108 /**
109 * @test
110 */
111 public function areElementsOfDifferentExtensionsRegistered() {
112 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
113 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
114 $this->fixture->applyTca();
115
116 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
117 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
118 }
119
120 /**
121 * @test
122 */
123 public function areElementsOfDifferentExtensionsOnSameTableRegistered() {
124 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories1');
125 $this->fixture->add('test_extension_b', $this->tables['first'], 'categories2');
126 $this->fixture->applyTca();
127
128 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
129 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
130 }
131
132 /**
133 * @test
134 */
135 public function areElementsOfSameExtensionOnSameTableRegistered() {
136 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories1');
137 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories2');
138 $this->fixture->applyTca();
139
140 $this->assertArrayHasKey('categories1', $GLOBALS['TCA'][$this->tables['first']]['columns']);
141 $this->assertArrayHasKey('categories2', $GLOBALS['TCA'][$this->tables['first']]['columns']);
142 }
143
144 /**
145 * @test
146 */
147 public function areDatabaseDefinitionsOfAllElementsAvailable() {
148 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
149 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
150 $this->fixture->add('test_extension_c', $this->tables['first'], 'categories');
151 $definitions = $this->fixture->getDatabaseTableDefinitions();
152 $matches = array();
153 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
154 $this->assertEquals(2, count($matches[0]));
155 $this->assertEquals($matches[1][0], $this->tables['first']);
156 $this->assertEquals($matches[2][0], 'categories');
157 $this->assertEquals($matches[1][1], $this->tables['second']);
158 $this->assertEquals($matches[2][1], 'categories');
159 }
160
161 /**
162 * @test
163 */
164 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
165 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
166 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
167 $definitions = $this->fixture->getDatabaseTableDefinition('test_extension_a');
168 $matches = array();
169 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
170 $this->assertEquals(1, count($matches[0]));
171 $this->assertEquals($matches[1][0], $this->tables['first']);
172 $this->assertEquals($matches[2][0], 'categories');
173 }
174
175 /**
176 * @test
177 */
178 public function areDefaultCategorizedTablesLoaded() {
179 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
180 $this->fixture->applyTca();
181
182 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['first']]['columns']);
183 $this->assertArrayHasKey('categories', $GLOBALS['TCA'][$this->tables['second']]['columns']);
184 }
185
186 /**
187 * @test
188 */
189 public function canApplyTca() {
190 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
191 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
192 $this->fixture->applyTca();
193
194 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['first']]['columns']['categories']);
195 $this->assertNotEmpty($GLOBALS['TCA'][$this->tables['second']]['columns']['categories']);
196 }
197
198 /**
199 * @test
200 */
201 public function isRegisteredReturnsTrueIfElementIsAlreadyRegistered() {
202 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
203 $this->assertTrue($this->fixture->isRegistered($this->tables['first'], 'categories'));
204 }
205
206 /**
207 * @test
208 */
209 public function isRegisteredReturnsFalseIfElementIsNotRegistered() {
210 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
211 $this->assertFalse($this->fixture->isRegistered($this->tables['first'], '_not_registered'));
212 $this->assertFalse($this->fixture->isRegistered($this->tables['second'], 'categories'));
213 }
214
215 /**
216 * @test
217 */
218 public function tabIsAddedForElement() {
219 $this->fixture->add('text_extension_a', $this->tables['first']);
220 $this->fixture->applyTca();
221
222 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
223 $this->assertContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
224 }
225 }
226
227 /**
228 * @test
229 */
230 public function tabIsNotAddedForElementIfFieldListIsSpecified() {
231 $this->fixture->add('text_extension_a', $this->tables['first'], 'categories', array('fieldList' => 'categories'));
232 $this->fixture->applyTca();
233
234 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
235 $this->assertNotContains('--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category', $typeConfig['showitem']);
236 }
237 }
238
239 /**
240 * @test
241 */
242 public function tabIsAddedOnlyOncePerTable() {
243 $this->fixture->add('text_extension_a', $this->tables['first'], 'categories1');
244 $this->fixture->add('text_extension_a', $this->tables['first'], 'categories2');
245 $this->fixture->applyTca();
246
247 foreach($GLOBALS['TCA'][$this->tables['first']]['types'] as $typeConfig) {
248 $this->assertSame(
249 1, substr_count($typeConfig['showitem'], '--div--;LLL:EXT:lang/locallang_tca.xlf:sys_category.tabs.category')
250 );
251 }
252
253 }
254
255 }