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