[TASK] Add prefix "class." to all unit test files which contain classes.
[Packages/TYPO3.CMS.git] / tests / t3lib / class.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 * Test wheter replacing other TCA fields works as promissed
259 * @test
260 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
261 */
262 public function canAddFieldsToTCATypeAndReplaceExistingOnes() {
263 $table = uniqid('tx_coretest_table');
264 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
265
266 $typesBefore = $GLOBALS['TCA'][$table]['types'];
267
268 t3lib_extMgm::addToAllTCAtypes($table, 'fieldZ', '', 'replace:fieldX');
269
270 $this->assertEquals(
271 $typesBefore,
272 $GLOBALS['TCA'][$table]['types'],
273 'It\'s wrong that the "types" array changes here - the replaced field is only on palettes'
274 );
275
276 // unchanged because the palette is not used
277 $this->assertEquals(
278 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
279 );
280 // unchanged because the palette is not used
281 $this->assertEquals(
282 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
283 );
284
285 $this->assertEquals(
286 'fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
287 );
288
289 $this->assertEquals(
290 'fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
291 );
292 }
293
294
295
296 ///////////////////////////////////////////////////
297 // Tests concerning addFieldsToAllPalettesOfField
298 ///////////////////////////////////////////////////
299
300 /**
301 * Tests whether fields can be added to a palette before existing elements.
302 * @test
303 * @see t3lib_extMgm::addFieldsToPalette()
304 */
305 public function canAddFieldsToPaletteBeforeExistingOnes() {
306 $table = uniqid('tx_coretest_table');
307 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
308
309 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
310
311 $this->assertEquals(
312 'fieldX, newA, newB, fieldY',
313 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
314 );
315 }
316
317 /**
318 * Tests whether fields can be added to a palette after existing elements.
319 * @test
320 * @see t3lib_extMgm::addFieldsToPalette()
321 */
322 public function canAddFieldsToPaletteAfterExistingOnes() {
323 $table = uniqid('tx_coretest_table');
324 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
325
326 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
327
328 $this->assertEquals(
329 'fieldX, newA, newB, fieldY',
330 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
331 );
332 }
333
334 /**
335 * Tests whether fields can be added to a palette after a not existing elements.
336 * @test
337 * @see t3lib_extMgm::addFieldsToPalette()
338 */
339 public function canAddFieldsToPaletteAfterNotExistingOnes() {
340 $table = uniqid('tx_coretest_table');
341 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
342
343 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
344
345 $this->assertEquals(
346 'fieldX, fieldY, newA, newB',
347 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
348 );
349 }
350
351 /**
352 * Tests whether fields can be added to all palettes of a regular field before existing ones.
353 * @test
354 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
355 */
356 public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
357 $table = uniqid('tx_coretest_table');
358 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
359
360 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
361
362 $this->assertEquals(
363 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
364 );
365 $this->assertEquals(
366 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
367 );
368 $this->assertEquals(
369 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
370 );
371 $this->assertEquals(
372 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
373 );
374 }
375
376 /**
377 * Tests whether fields can be added to all palettes of a regular field after existing ones.
378 * @test
379 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
380 */
381 public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
382 $table = uniqid('tx_coretest_table');
383 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
384
385 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
386
387 $this->assertEquals(
388 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
389 );
390 $this->assertEquals(
391 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
392 );
393 $this->assertEquals(
394 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
395 );
396 $this->assertEquals(
397 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
398 );
399 }
400
401 /**
402 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
403 * @test
404 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
405 */
406 public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
407 $table = uniqid('tx_coretest_table');
408 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
409
410 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
411
412 $this->assertEquals(
413 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
414 );
415 $this->assertEquals(
416 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
417 );
418 $this->assertEquals(
419 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
420 );
421 $this->assertEquals(
422 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
423 );
424 }
425
426 /**
427 * Tests whether fields are added to a new palette that did not exist before.
428 * @test
429 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
430 */
431 public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
432 $table = uniqid('tx_coretest_table');
433 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
434
435 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
436
437 $this->assertEquals(
438 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
439 );
440 $this->assertEquals(
441 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
442 );
443 $this->assertEquals(
444 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
445 );
446 $this->assertEquals(
447 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
448 );
449 $this->assertEquals(
450 'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
451 );
452 }
453
454
455 /////////////////////////////////////////
456 // Tests concerning getExtensionVersion
457 /////////////////////////////////////////
458
459 /**
460 * Data provider for negative getExtensionVersion() tests.
461 *
462 * @return array
463 */
464 public function getExtensionVersionFaultyDataProvider() {
465 return array(
466 array(''),
467 array(0),
468 array(new stdClass()),
469 array(TRUE),
470 );
471 }
472
473 /**
474 * @test
475 * @expectedException InvalidArgumentException
476 * @dataProvider getExtensionVersionFaultyDataProvider
477 */
478 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
479 t3lib_extMgm::getExtensionVersion($key);
480 }
481
482 /**
483 * @test
484 */
485 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
486 t3lib_extMgm::clearExtensionKeyMap();
487
488 $uniqueSuffix = uniqid('test');
489 $extensionKey = 'unloadedextension' . $uniqueSuffix;
490
491 $this->assertEquals(
492 '',
493 t3lib_extMgm::getExtensionVersion($extensionKey)
494 );
495 }
496
497 /**
498 * @test
499 */
500 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
501 t3lib_extMgm::clearExtensionKeyMap();
502
503 $uniqueSuffix = uniqid('test');
504 $extensionKey = 'unloadedextension' . $uniqueSuffix;
505
506 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
507 'siteRelPath' => $this->determineFixturesPath(),
508 );
509 $this->assertEquals(
510 '1.2.3',
511 t3lib_extMgm::getExtensionVersion($extensionKey)
512 );
513 }
514
515 /**
516 * @test
517 */
518 public function getEnabledExtensionListConsidersRequiredExtensions() {
519 $testrequiRedExtension = uniqid('test');
520 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testrequiRedExtension;
521
522 $extensions = explode(',', t3lib_extMgm::getEnabledExtensionList());
523 $this->assertTrue(in_array($testrequiRedExtension, $extensions));
524 }
525
526 /**
527 * @test
528 */
529 public function getEnabledExtensionListConsidersRequiredAndIgnoredExtensions() {
530 $testRequiredExtension = uniqid('test');
531 $testIgnoredExtension = uniqid('test');
532 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testRequiredExtension . ',' . $testIgnoredExtension;
533 $GLOBALS['TYPO3_CONF_VARS']['EXT']['ignoredExt'] = $testIgnoredExtension;
534
535 $extensions = explode(',', t3lib_extMgm::getEnabledExtensionList());
536 $this->assertTrue(in_array($testRequiredExtension, $extensions));
537 $this->assertFalse(in_array($testIgnoredExtension, $extensions));
538 }
539 }
540 ?>