Fixed #11206: Cleaned up test "t3lib_extmgm_testcase" (Thanks to Oliver Klee)
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_extmgm_testcase.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for class t3lib_extMgm
27 *
28 * @author Oliver Hader <oliver@typo3.org>
29 * @author Oliver Klee <typo3-coding@oliverklee.de>
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 */
34 class t3lib_extmgm_testcase extends tx_phpunit_testcase {
35 /**
36 * backup of defined GLOBALS
37 *
38 * @var array
39 */
40 protected $globals = array();
41
42 public function setUp() {
43 $this->globals = array(
44 'TYPO3_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
45 'TCA' => serialize($GLOBALS['TCA']),
46 );
47 }
48
49 public function tearDown() {
50 t3lib_extMgm::clearExtensionKeyMap();
51
52 foreach ($this->globals as $key => $value) {
53 $GLOBALS[$key] = unserialize($value);
54 }
55 }
56
57
58 //////////////////////
59 // Utility functions
60 //////////////////////
61
62 /**
63 * Generates a basic TCA for a given table.
64 *
65 * @param string $table name of the table, must not be empty
66 * @return array generated TCA for the given table, will not be empty
67 */
68 private function generateTCAForTable($table) {
69 $tca = array();
70 $tca[$table] = array();
71 $tca[$table]['columns'] = array(
72 'fieldA' => array(),
73 'fieldC' => array(),
74 );
75 $tca[$table]['types'] = array(
76 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
77 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
78 'typeC' => array('showitem' => 'fieldC;;paletteD'),
79 );
80 $tca[$table]['palettes'] = array(
81 'paletteA' => array('showitem' => 'fieldX, fieldY'),
82 'paletteB' => array('showitem' => 'fieldX, fieldY'),
83 'paletteC' => array('showitem' => 'fieldX, fieldY'),
84 'paletteD' => array('showitem' => 'fieldX, fieldY'),
85 );
86
87 return $tca;
88 }
89
90
91 /////////////////////////////////////////////
92 // Tests concerning getExtensionKeyByPrefix
93 /////////////////////////////////////////////
94
95 /**
96 * @test
97 * @see t3lib_extMgm::getExtensionKeyByPrefix
98 */
99 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
100 t3lib_extMgm::clearExtensionKeyMap();
101
102 $uniqueSuffix = uniqid('test');
103 $extensionKey = 'tt_news' . $uniqueSuffix;
104 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
105
106 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
107
108 $this->assertEquals(
109 $extensionKey,
110 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
111 );
112 }
113
114 /**
115 * @test
116 * @see t3lib_extMgm::getExtensionKeyByPrefix
117 */
118 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
119 t3lib_extMgm::clearExtensionKeyMap();
120
121 $uniqueSuffix = uniqid('test');
122 $extensionKey = 'kickstarter' . $uniqueSuffix;
123 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
124
125 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
126
127 $this->assertEquals(
128 $extensionKey,
129 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
130 );
131 }
132
133 /**
134 * @test
135 * @see t3lib_extMgm::getExtensionKeyByPrefix
136 */
137 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse(){
138 t3lib_extMgm::clearExtensionKeyMap();
139
140 $uniqueSuffix = uniqid('test');
141 $extensionKey = 'unloadedextension' . $uniqueSuffix;
142 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
143
144 $this->assertFalse(
145 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
146 );
147 }
148
149
150 //////////////////////////////////////
151 // Tests concerning addToAllTCAtypes
152 //////////////////////////////////////
153
154 /**
155 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
156 * @test
157 * @see t3lib_extMgm::addToAllTCAtypes()
158 */
159 public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
160 $table = uniqid('tx_coretest_table');
161 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
162
163 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
164
165 // Checking typeA:
166 $this->assertEquals(
167 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
168 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
169 );
170 // Checking typeB:
171 $this->assertEquals(
172 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
173 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
174 );
175 }
176
177 /**
178 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
179 * @test
180 * @see t3lib_extMgm::addToAllTCAtypes()
181 */
182 public function canAddFieldsToAllTCATypesAfterExistingOnes() {
183 $table = uniqid('tx_coretest_table');
184 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
185
186 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
187
188 // Checking typeA:
189 $this->assertEquals(
190 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
191 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
192 );
193 // Checking typeB:
194 $this->assertEquals(
195 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
196 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
197 );
198 }
199
200 /**
201 * Tests whether fields can be add to a TCA type before existing ones
202 * @test
203 * @see t3lib_extMgm::addToAllTCAtypes()
204 */
205 public function canAddFieldsToTCATypeBeforeExistingOnes() {
206 $table = uniqid('tx_coretest_table');
207 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
208
209 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
210
211 // Checking typeA:
212 $this->assertEquals(
213 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
214 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
215 );
216 // Checking typeB:
217 $this->assertEquals(
218 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
219 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
220 );
221 }
222
223 /**
224 * Tests whether fields can be add to a TCA type after existing ones
225 * @test
226 * @see t3lib_extMgm::addToAllTCAtypes()
227 */
228 public function canAddFieldsToTCATypeAfterExistingOnes() {
229 $table = uniqid('tx_coretest_table');
230 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
231
232 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
233
234 // Checking typeA:
235 $this->assertEquals(
236 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
237 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
238 );
239 // Checking typeB:
240 $this->assertEquals(
241 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
242 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
243 );
244 }
245
246
247 ///////////////////////////////////////////////////
248 // Tests concerning addFieldsToAllPalettesOfField
249 ///////////////////////////////////////////////////
250
251 /**
252 * Tests whether fields can be added to a palette before existing elements.
253 * @test
254 * @see t3lib_extMgm::addFieldsToPalette()
255 */
256 public function canAddFieldsToPaletteBeforeExistingOnes() {
257 $table = uniqid('tx_coretest_table');
258 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
259
260 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
261
262 $this->assertEquals(
263 'fieldX, newA, newB, fieldY',
264 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
265 );
266 }
267
268 /**
269 * Tests whether fields can be added to a palette after existing elements.
270 * @test
271 * @see t3lib_extMgm::addFieldsToPalette()
272 */
273 public function canAddFieldsToPaletteAfterExistingOnes() {
274 $table = uniqid('tx_coretest_table');
275 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
276
277 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
278
279 $this->assertEquals(
280 'fieldX, newA, newB, fieldY',
281 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
282 );
283 }
284
285 /**
286 * Tests whether fields can be added to a palette after a not existing elements.
287 * @test
288 * @see t3lib_extMgm::addFieldsToPalette()
289 */
290 public function canAddFieldsToPaletteAfterNotExistingOnes() {
291 $table = uniqid('tx_coretest_table');
292 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
293
294 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
295
296 $this->assertEquals(
297 'fieldX, fieldY, newA, newB',
298 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
299 );
300 }
301
302 /**
303 * Tests whether fields can be added to all palettes of a regular field before existing ones.
304 * @test
305 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
306 */
307 public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
308 $table = uniqid('tx_coretest_table');
309 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
310
311 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
312
313 $this->assertEquals(
314 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
315 );
316 $this->assertEquals(
317 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
318 );
319 $this->assertEquals(
320 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
321 );
322 $this->assertEquals(
323 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
324 );
325 }
326
327 /**
328 * Tests whether fields can be added to all palettes of a regular field after existing ones.
329 * @test
330 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
331 */
332 public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
333 $table = uniqid('tx_coretest_table');
334 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
335
336 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
337
338 $this->assertEquals(
339 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
340 );
341 $this->assertEquals(
342 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
343 );
344 $this->assertEquals(
345 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
346 );
347 $this->assertEquals(
348 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
349 );
350 }
351
352 /**
353 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
354 * @test
355 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
356 */
357 public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
358 $table = uniqid('tx_coretest_table');
359 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
360
361 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
362
363 $this->assertEquals(
364 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
365 );
366 $this->assertEquals(
367 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
368 );
369 $this->assertEquals(
370 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
371 );
372 $this->assertEquals(
373 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
374 );
375 }
376
377 /**
378 * Tests whether fields are added to a new palette that did not exist before.
379 * @test
380 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
381 */
382 public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
383 $table = uniqid('tx_coretest_table');
384 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
385
386 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
387
388 $this->assertEquals(
389 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
390 );
391 $this->assertEquals(
392 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
393 );
394 $this->assertEquals(
395 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
396 );
397 $this->assertEquals(
398 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
399 );
400 $this->assertEquals(
401 'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
402 );
403 }
404 }
405 ?>