[TASK] Improve method makeCategorizable to be used everywhere
[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 */
95 public function areMultipleElementsOfSameExtensionRegistered() {
96 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
97 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
98 $registry = $this->fixture->get();
99 ob_flush();
100
101 $this->assertEquals(
102 'categories',
103 $registry['test_extension_a'][$this->tables['first']]['fieldName']
104 );
105
106 $this->assertEquals(
107 'categories',
108 $registry['test_extension_b'][$this->tables['second']]['fieldName']
109 );
110 }
111
112 /**
113 * @test
114 */
115 public function areElementsOfDifferentExtensionsRegistered() {
116 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
117 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
118 $registry = $this->fixture->get();
119
120 $this->assertEquals(
121 'categories',
122 $registry['test_extension_a'][$this->tables['first']]['fieldName']
123 );
124
125 $this->assertEquals(
126 'categories',
127 $registry['test_extension_b'][$this->tables['second']]['fieldName']
128 );
129 }
130
131 /**
132 * @test
133 */
134 public function areElementsOnSameTableOverridden() {
135 $this->fixture->add('test_extension_a', $this->tables['first'], $this->tables['first']);
136 $this->fixture->add('test_extension_b', $this->tables['second'], $this->tables['second']);
137 $registry = $this->fixture->get();
138
139 $this->assertEquals(
140 $this->tables['first'],
141 $registry['test_extension_a'][$this->tables['first']]['fieldName']
142 );
143 }
144
145 /**
146 * @test
147 */
148 public function areDatabaseDefinitionsOfAllElementsAvailable() {
149 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
150 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
151 $this->fixture->add('test_extension_c', $this->tables['first'], 'categories');
152 $definitions = $this->fixture->getDatabaseTableDefinitions();
153 $matches = array();
154 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
155 $this->assertEquals(2, count($matches[0]));
156 $this->assertEquals($matches[1][0], $this->tables['first']);
157 $this->assertEquals($matches[2][0], 'categories');
158 $this->assertEquals($matches[1][1], $this->tables['second']);
159 $this->assertEquals($matches[2][1], 'categories');
160 }
161
162 /**
163 * @test
164 */
165 public function areDatabaseDefinitionsOfParticularExtensionAvailable() {
166 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
167 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
168 $definitions = $this->fixture->getDatabaseTableDefinition('test_extension_a');
169 $matches = array();
170 preg_match_all('#CREATE TABLE\\s*([^ (]+)\\s*\\(\\s*([^ )]+)\\s+int\\(11\\)[^)]+\\);#mis', $definitions, $matches);
171 $this->assertEquals(1, count($matches[0]));
172 $this->assertEquals($matches[1][0], $this->tables['first']);
173 $this->assertEquals($matches[2][0], 'categories');
174 }
175
176 /**
177 * @test
178 */
179 public function areDefaultCategorizedTabledLoaded() {
180 $GLOBALS['TYPO3_CONF_VARS']['SYS']['defaultCategorizedTables'] = $this->tables['first'] . ',' . $this->tables['second'];
181 $this->fixture->applyTca();
182
183 $registry = $this->fixture->get();
184 $this->assertCount(2, $registry['core']);
185 $this->assertSame(key($registry['core']), $this->tables['first']);
186 }
187
188 /**
189 * @test
190 */
191 public function canApplyTca() {
192 $this->fixture->add('test_extension_a', $this->tables['first'], 'categories');
193 $this->fixture->add('test_extension_b', $this->tables['second'], 'categories');
194 $this->fixture->applyTca();
195 foreach ($this->tables as $table) {
196 $this->assertNotEmpty($GLOBALS['TCA'][$table]['columns']['categories']);
197 }
198 }
199 }
200
201 ?>