2 /***************************************************************
5 * (c) 2009-2011 Oliver Hader <oliver@typo3.org>
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.
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
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.
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
26 * Testcase for class t3lib_extMgm
28 * @author Oliver Hader <oliver@typo3.org>
29 * @author Oliver Klee <typo3-coding@oliverklee.de>
34 class t3lib_extmgmTest
extends tx_phpunit_testcase
{
36 * backup of defined GLOBALS
40 protected $globals = array();
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']),
50 public function tearDown() {
51 t3lib_extMgm
::clearExtensionKeyMap();
53 foreach ($this->globals
as $key => $value) {
54 $GLOBALS[$key] = unserialize($value);
59 //////////////////////
61 //////////////////////
64 * Generates a basic TCA for a given table.
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
69 private function generateTCAForTable($table) {
71 $tca[$table] = array();
72 $tca[$table]['columns'] = array(
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'),
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'),
92 * Returns the fixtures path for this testcase relative to PATH_site.
94 * @return string the fixtures path for this testcase, will not be empty
96 private function determineFixturesPath() {
97 return t3lib_div
::makeInstance('Tx_Phpunit_Service_TestFinder')
98 ->getRelativeCoreTestsPath() . 't3lib/fixtures/';
102 /////////////////////////////////////////////
103 // Tests concerning getExtensionKeyByPrefix
104 /////////////////////////////////////////////
108 * @see t3lib_extMgm::getExtensionKeyByPrefix
110 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
111 t3lib_extMgm
::clearExtensionKeyMap();
113 $uniqueSuffix = uniqid('test');
114 $extensionKey = 'tt_news' . $uniqueSuffix;
115 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
117 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
121 t3lib_extMgm
::getExtensionKeyByPrefix($extensionPrefix)
127 * @see t3lib_extMgm::getExtensionKeyByPrefix
129 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
130 t3lib_extMgm
::clearExtensionKeyMap();
132 $uniqueSuffix = uniqid('test');
133 $extensionKey = 'kickstarter' . $uniqueSuffix;
134 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
136 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
140 t3lib_extMgm
::getExtensionKeyByPrefix($extensionPrefix)
146 * @see t3lib_extMgm::getExtensionKeyByPrefix
148 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse(){
149 t3lib_extMgm
::clearExtensionKeyMap();
151 $uniqueSuffix = uniqid('test');
152 $extensionKey = 'unloadedextension' . $uniqueSuffix;
153 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
156 t3lib_extMgm
::getExtensionKeyByPrefix($extensionPrefix)
161 //////////////////////////////////////
162 // Tests concerning addToAllTCAtypes
163 //////////////////////////////////////
166 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
168 * @see t3lib_extMgm::addToAllTCAtypes()
170 public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
171 $table = uniqid('tx_coretest_table');
172 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
174 t3lib_extMgm
::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
178 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
179 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
183 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
184 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
189 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
191 * @see t3lib_extMgm::addToAllTCAtypes()
193 public function canAddFieldsToAllTCATypesAfterExistingOnes() {
194 $table = uniqid('tx_coretest_table');
195 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
197 t3lib_extMgm
::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
201 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
202 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
206 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
207 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
212 * Tests whether fields can be add to a TCA type before existing ones
214 * @see t3lib_extMgm::addToAllTCAtypes()
216 public function canAddFieldsToTCATypeBeforeExistingOnes() {
217 $table = uniqid('tx_coretest_table');
218 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
220 t3lib_extMgm
::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
224 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
225 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
229 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
230 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
235 * Tests whether fields can be add to a TCA type after existing ones
237 * @see t3lib_extMgm::addToAllTCAtypes()
239 public function canAddFieldsToTCATypeAfterExistingOnes() {
240 $table = uniqid('tx_coretest_table');
241 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
243 t3lib_extMgm
::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
247 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
248 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
252 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
253 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
258 * Test wheter replacing other TCA fields works as promissed
260 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
262 public function canAddFieldsToTCATypeAndReplaceExistingOnes() {
263 $table = uniqid('tx_coretest_table');
264 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
266 $typesBefore = $GLOBALS['TCA'][$table]['types'];
268 t3lib_extMgm
::addToAllTCAtypes($table, 'fieldZ', '', 'replace:fieldX');
272 $GLOBALS['TCA'][$table]['types'],
273 'It\'s wrong that the "types" array changes here - the replaced field is only on palettes'
276 // unchanged because the palette is not used
278 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
280 // unchanged because the palette is not used
282 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
286 'fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
290 'fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
296 ///////////////////////////////////////////////////
297 // Tests concerning addFieldsToAllPalettesOfField
298 ///////////////////////////////////////////////////
301 * Tests whether fields can be added to a palette before existing elements.
303 * @see t3lib_extMgm::addFieldsToPalette()
305 public function canAddFieldsToPaletteBeforeExistingOnes() {
306 $table = uniqid('tx_coretest_table');
307 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
309 t3lib_extMgm
::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
312 'fieldX, newA, newB, fieldY',
313 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
318 * Tests whether fields can be added to a palette after existing elements.
320 * @see t3lib_extMgm::addFieldsToPalette()
322 public function canAddFieldsToPaletteAfterExistingOnes() {
323 $table = uniqid('tx_coretest_table');
324 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
326 t3lib_extMgm
::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
329 'fieldX, newA, newB, fieldY',
330 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
335 * Tests whether fields can be added to a palette after a not existing elements.
337 * @see t3lib_extMgm::addFieldsToPalette()
339 public function canAddFieldsToPaletteAfterNotExistingOnes() {
340 $table = uniqid('tx_coretest_table');
341 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
343 t3lib_extMgm
::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
346 'fieldX, fieldY, newA, newB',
347 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
352 * Tests whether fields can be added to all palettes of a regular field before existing ones.
354 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
356 public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
357 $table = uniqid('tx_coretest_table');
358 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
360 t3lib_extMgm
::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
363 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
366 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
369 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
372 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
377 * Tests whether fields can be added to all palettes of a regular field after existing ones.
379 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
381 public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
382 $table = uniqid('tx_coretest_table');
383 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
385 t3lib_extMgm
::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
388 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
391 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
394 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
397 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
402 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
404 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
406 public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
407 $table = uniqid('tx_coretest_table');
408 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
410 t3lib_extMgm
::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
413 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
416 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
419 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
422 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
427 * Tests whether fields are added to a new palette that did not exist before.
429 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
431 public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
432 $table = uniqid('tx_coretest_table');
433 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
435 t3lib_extMgm
::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
438 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
441 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
444 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
447 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
450 'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
455 /////////////////////////////////////////
456 // Tests concerning addTcaSelectItem
457 /////////////////////////////////////////
461 * @expectedException InvalidArgumentException
463 public function addTcaSelectItemThrowsExceptionIfTableIsNotOfTypeString() {
464 t3lib_extMgm
::addTcaSelectItem(array(), 'foo', array());
469 * @expectedException InvalidArgumentException
471 public function addTcaSelectItemThrowsExceptionIfFieldIsNotOfTypeString() {
472 t3lib_extMgm
::addTcaSelectItem('foo', array(), array());
477 * @expectedException InvalidArgumentException
479 public function addTcaSelectItemThrowsExceptionIfRelativeToFieldIsNotOfTypeString() {
480 t3lib_extMgm
::addTcaSelectItem('foo', 'bar', array(), array());
485 * @expectedException InvalidArgumentException
487 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOfTypeString() {
488 t3lib_extMgm
::addTcaSelectItem('foo', 'bar', array(), 'foo', array());
493 * @expectedException InvalidArgumentException
495 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOneOfValidKeywords() {
496 t3lib_extMgm
::addTcaSelectItem('foo', 'bar', array(), 'foo', 'not allowed keyword');
501 * @expectedException RuntimeException
503 public function addTcaSelectItemThrowsExceptionIfFieldIsNotFoundInTca() {
504 $GLOBALS['TCA'] = array();
505 t3lib_extMgm
::addTcaSelectItem('foo', 'bar', array());
509 * Data provider for addTcaSelectItemInsertsItemAtSpecifiedPosition
511 public function addTcaSelectItemDataProvider() {
512 // Every array splits into:
514 // - relativePosition
515 // - expectedResultArray
517 'add at end of array' => array(
521 0 => array('firstElement'),
522 1 => array('matchMe'),
523 2 => array('thirdElement'),
524 3 => array('insertedElement'),
527 'replace element' => array(
531 0 => array('firstElement'),
532 1 => array('insertedElement'),
533 2 => array('thirdElement'),
536 'add element after' => array(
540 0 => array('firstElement'),
541 1 => array('matchMe'),
542 2 => array('insertedElement'),
543 3 => array('thirdElement'),
546 'add element before' => array(
550 0 => array('firstElement'),
551 1 => array('insertedElement'),
552 2 => array('matchMe'),
553 3 => array('thirdElement'),
556 'add at end if relative position was not found' => array(
560 0 => array('firstElement'),
561 1 => array('matchMe'),
562 2 => array('thirdElement'),
563 3 => array('insertedElement'),
571 * @dataProvider addTcaSelectItemDataProvider
573 public function addTcaSelectItemInsertsItemAtSpecifiedPosition($relativeToField, $relativePosition, $expectedResultArray) {
574 $GLOBALS['TCA'] = array(
575 'testTable' => array(
577 'testField' => array(
580 '0' => array('firstElement'),
581 '1' => array('matchMe'),
582 2 => array('thirdElement'),
589 t3lib_extMgm
::addTcaSelectItem('testTable', 'testField', array('insertedElement'), $relativeToField, $relativePosition);
590 $this->assertEquals($expectedResultArray, $GLOBALS['TCA']['testTable']['columns']['testField']['config']['items']);
594 /////////////////////////////////////////
595 // Tests concerning getExtensionVersion
596 /////////////////////////////////////////
599 * Data provider for negative getExtensionVersion() tests.
603 public function getExtensionVersionFaultyDataProvider() {
607 array(new stdClass()),
614 * @expectedException InvalidArgumentException
615 * @dataProvider getExtensionVersionFaultyDataProvider
617 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
618 t3lib_extMgm
::getExtensionVersion($key);
624 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
625 t3lib_extMgm
::clearExtensionKeyMap();
627 $uniqueSuffix = uniqid('test');
628 $extensionKey = 'unloadedextension' . $uniqueSuffix;
632 t3lib_extMgm
::getExtensionVersion($extensionKey)
639 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
640 t3lib_extMgm
::clearExtensionKeyMap();
642 $uniqueSuffix = uniqid('test');
643 $extensionKey = 'unloadedextension' . $uniqueSuffix;
645 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
646 'siteRelPath' => $this->determineFixturesPath(),
650 t3lib_extMgm
::getExtensionVersion($extensionKey)
657 public function getEnabledExtensionListConsidersRequiredExtensions() {
658 $testrequiRedExtension = uniqid('test');
659 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testrequiRedExtension;
661 $extensions = explode(',', t3lib_extMgm
::getEnabledExtensionList());
662 $this->assertTrue(in_array($testrequiRedExtension, $extensions));
668 public function getEnabledExtensionListConsidersRequiredAndIgnoredExtensions() {
669 $testRequiredExtension = uniqid('test');
670 $testIgnoredExtension = uniqid('test');
671 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = $testRequiredExtension . ',' . $testIgnoredExtension;
672 $GLOBALS['TYPO3_CONF_VARS']['EXT']['ignoredExt'] = $testIgnoredExtension;
674 $extensions = explode(',', t3lib_extMgm
::getEnabledExtensionList());
675 $this->assertTrue(in_array($testRequiredExtension, $extensions));
676 $this->assertFalse(in_array($testIgnoredExtension, $extensions));