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