Fixed bug #17244: Error Message while Updating 4.4.6 > 4.5
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_extmgmTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2011 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_extmgmTest 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_CONF_VARS' => serialize($GLOBALS['TYPO3_CONF_VARS']),
45 'TYPO3_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
46 'TCA' => serialize($GLOBALS['TCA']),
47 );
48 }
49
50 public function tearDown() {
51 t3lib_extMgm::clearExtensionKeyMap();
52
53 foreach ($this->globals as $key => $value) {
54 $GLOBALS[$key] = unserialize($value);
55 }
56 }
57
58
59 //////////////////////
60 // Utility functions
61 //////////////////////
62
63 /**
64 * Generates a basic TCA for a given table.
65 *
66 * @param string $table name of the table, must not be empty
67 * @return array generated TCA for the given table, will not be empty
68 */
69 private function generateTCAForTable($table) {
70 $tca = array();
71 $tca[$table] = array();
72 $tca[$table]['columns'] = array(
73 'fieldA' => array(),
74 'fieldC' => array(),
75 );
76 $tca[$table]['types'] = array(
77 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
78 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
79 'typeC' => array('showitem' => 'fieldC;;paletteD'),
80 );
81 $tca[$table]['palettes'] = array(
82 'paletteA' => array('showitem' => 'fieldX, fieldY'),
83 'paletteB' => array('showitem' => 'fieldX, fieldY'),
84 'paletteC' => array('showitem' => 'fieldX, fieldY'),
85 'paletteD' => array('showitem' => 'fieldX, fieldY'),
86 );
87
88 return $tca;
89 }
90
91 /**
92 * Returns the fixtures path for this testcase relative to PATH_site.
93 *
94 * @return string the fixtures path for this testcase, will not be empty
95 */
96 private function determineFixturesPath() {
97 return t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder')
98 ->getRelativeCoreTestsPath() . 't3lib/fixtures/';
99 }
100
101
102 /////////////////////////////////////////////
103 // Tests concerning getExtensionKeyByPrefix
104 /////////////////////////////////////////////
105
106 /**
107 * @test
108 * @see t3lib_extMgm::getExtensionKeyByPrefix
109 */
110 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
111 t3lib_extMgm::clearExtensionKeyMap();
112
113 $uniqueSuffix = uniqid('test');
114 $extensionKey = 'tt_news' . $uniqueSuffix;
115 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
116
117 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
118
119 $this->assertEquals(
120 $extensionKey,
121 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
122 );
123 }
124
125 /**
126 * @test
127 * @see t3lib_extMgm::getExtensionKeyByPrefix
128 */
129 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
130 t3lib_extMgm::clearExtensionKeyMap();
131
132 $uniqueSuffix = uniqid('test');
133 $extensionKey = 'kickstarter' . $uniqueSuffix;
134 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
135
136 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
137
138 $this->assertEquals(
139 $extensionKey,
140 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
141 );
142 }
143
144 /**
145 * @test
146 * @see t3lib_extMgm::getExtensionKeyByPrefix
147 */
148 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse(){
149 t3lib_extMgm::clearExtensionKeyMap();
150
151 $uniqueSuffix = uniqid('test');
152 $extensionKey = 'unloadedextension' . $uniqueSuffix;
153 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
154
155 $this->assertFalse(
156 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
157 );
158 }
159
160
161 //////////////////////////////////////
162 // Tests concerning addToAllTCAtypes
163 //////////////////////////////////////
164
165 /**
166 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
167 * @test
168 * @see t3lib_extMgm::addToAllTCAtypes()
169 */
170 public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
171 $table = uniqid('tx_coretest_table');
172 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
173
174 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
175
176 // Checking typeA:
177 $this->assertEquals(
178 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
179 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
180 );
181 // Checking typeB:
182 $this->assertEquals(
183 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
184 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
185 );
186 }
187
188 /**
189 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
190 * @test
191 * @see t3lib_extMgm::addToAllTCAtypes()
192 */
193 public function canAddFieldsToAllTCATypesAfterExistingOnes() {
194 $table = uniqid('tx_coretest_table');
195 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
196
197 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
198
199 // Checking typeA:
200 $this->assertEquals(
201 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
202 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
203 );
204 // Checking typeB:
205 $this->assertEquals(
206 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
207 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
208 );
209 }
210
211 /**
212 * Tests whether fields can be add to a TCA type before existing ones
213 * @test
214 * @see t3lib_extMgm::addToAllTCAtypes()
215 */
216 public function canAddFieldsToTCATypeBeforeExistingOnes() {
217 $table = uniqid('tx_coretest_table');
218 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
219
220 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
221
222 // Checking typeA:
223 $this->assertEquals(
224 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
225 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
226 );
227 // Checking typeB:
228 $this->assertEquals(
229 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
230 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
231 );
232 }
233
234 /**
235 * Tests whether fields can be add to a TCA type after existing ones
236 * @test
237 * @see t3lib_extMgm::addToAllTCAtypes()
238 */
239 public function canAddFieldsToTCATypeAfterExistingOnes() {
240 $table = uniqid('tx_coretest_table');
241 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
242
243 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
244
245 // Checking typeA:
246 $this->assertEquals(
247 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
248 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
249 );
250 // Checking typeB:
251 $this->assertEquals(
252 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
253 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
254 );
255 }
256
257
258 ///////////////////////////////////////////////////
259 // Tests concerning addFieldsToAllPalettesOfField
260 ///////////////////////////////////////////////////
261
262 /**
263 * Tests whether fields can be added to a palette before existing elements.
264 * @test
265 * @see t3lib_extMgm::addFieldsToPalette()
266 */
267 public function canAddFieldsToPaletteBeforeExistingOnes() {
268 $table = uniqid('tx_coretest_table');
269 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
270
271 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
272
273 $this->assertEquals(
274 'fieldX, newA, newB, fieldY',
275 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
276 );
277 }
278
279 /**
280 * Tests whether fields can be added to a palette after existing elements.
281 * @test
282 * @see t3lib_extMgm::addFieldsToPalette()
283 */
284 public function canAddFieldsToPaletteAfterExistingOnes() {
285 $table = uniqid('tx_coretest_table');
286 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
287
288 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
289
290 $this->assertEquals(
291 'fieldX, newA, newB, fieldY',
292 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
293 );
294 }
295
296 /**
297 * Tests whether fields can be added to a palette after a not existing elements.
298 * @test
299 * @see t3lib_extMgm::addFieldsToPalette()
300 */
301 public function canAddFieldsToPaletteAfterNotExistingOnes() {
302 $table = uniqid('tx_coretest_table');
303 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
304
305 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
306
307 $this->assertEquals(
308 'fieldX, fieldY, newA, newB',
309 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
310 );
311 }
312
313 /**
314 * Tests whether fields can be added to all palettes of a regular field before existing ones.
315 * @test
316 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
317 */
318 public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
319 $table = uniqid('tx_coretest_table');
320 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
321
322 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
323
324 $this->assertEquals(
325 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
326 );
327 $this->assertEquals(
328 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
329 );
330 $this->assertEquals(
331 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
332 );
333 $this->assertEquals(
334 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
335 );
336 }
337
338 /**
339 * Tests whether fields can be added to all palettes of a regular field after existing ones.
340 * @test
341 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
342 */
343 public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
344 $table = uniqid('tx_coretest_table');
345 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
346
347 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
348
349 $this->assertEquals(
350 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
351 );
352 $this->assertEquals(
353 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
354 );
355 $this->assertEquals(
356 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
357 );
358 $this->assertEquals(
359 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
360 );
361 }
362
363 /**
364 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
365 * @test
366 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
367 */
368 public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
369 $table = uniqid('tx_coretest_table');
370 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
371
372 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
373
374 $this->assertEquals(
375 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
376 );
377 $this->assertEquals(
378 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
379 );
380 $this->assertEquals(
381 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
382 );
383 $this->assertEquals(
384 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
385 );
386 }
387
388 /**
389 * Tests whether fields are added to a new palette that did not exist before.
390 * @test
391 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
392 */
393 public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
394 $table = uniqid('tx_coretest_table');
395 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
396
397 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
398
399 $this->assertEquals(
400 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
401 );
402 $this->assertEquals(
403 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
404 );
405 $this->assertEquals(
406 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
407 );
408 $this->assertEquals(
409 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
410 );
411 $this->assertEquals(
412 'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
413 );
414 }
415
416
417 /////////////////////////////////////////
418 // Tests concerning getExtensionVersion
419 /////////////////////////////////////////
420
421 /**
422 * Data provider for negative getExtensionVersion() tests.
423 *
424 * @return array
425 */
426 public function getExtensionVersionFaultyDataProvider() {
427 return array(
428 array(''),
429 array(0),
430 array(new stdClass()),
431 array(TRUE),
432 );
433 }
434
435 /**
436 * @test
437 * @expectedException InvalidArgumentException
438 * @dataProvider getExtensionVersionFaultyDataProvider
439 */
440 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
441 t3lib_extMgm::getExtensionVersion($key);
442 }
443
444 /**
445 * @test
446 */
447 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
448 t3lib_extMgm::clearExtensionKeyMap();
449
450 $uniqueSuffix = uniqid('test');
451 $extensionKey = 'unloadedextension' . $uniqueSuffix;
452
453 $this->assertEquals(
454 '',
455 t3lib_extMgm::getExtensionVersion($extensionKey)
456 );
457 }
458
459 /**
460 * @test
461 */
462 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
463 t3lib_extMgm::clearExtensionKeyMap();
464
465 $uniqueSuffix = uniqid('test');
466 $extensionKey = 'unloadedextension' . $uniqueSuffix;
467
468 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
469 'siteRelPath' => $this->determineFixturesPath(),
470 );
471 $this->assertEquals(
472 '1.2.3',
473 t3lib_extMgm::getExtensionVersion($extensionKey)
474 );
475 }
476
477 /**
478 * @test
479 */
480 public function getEnabledExtensionListConsidersRequiredExtensions() {
481 $testrequiRedExtension = uniqid('test');
482 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testrequiRedExtension;
483
484 $extensions = explode(',', t3lib_extMgm::getEnabledExtensionList());
485 $this->assertTrue(in_array($testrequiRedExtension, $extensions));
486 }
487
488 /**
489 * @test
490 */
491 public function getEnabledExtensionListConsidersRequiredAndIgnoredExtensions() {
492 $testRequiredExtension = uniqid('test');
493 $testIgnoredExtension = uniqid('test');
494 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testRequiredExtension . ',' . $testIgnoredExtension;
495 $GLOBALS['TYPO3_CONF_VARS']['EXT']['ignoredExt'] = $testIgnoredExtension;
496
497 $extensions = explode(',', t3lib_extMgm::getEnabledExtensionList());
498 $this->assertTrue(in_array($testRequiredExtension, $extensions));
499 $this->assertFalse(in_array($testIgnoredExtension, $extensions));
500 }
501 }
502 ?>