[BUGFIX] Remove references to extListArray
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / ExtensionManagementUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2009-2013 Oliver Hader <oliver@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 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
28
29 /**
30 * Testcase for ExtensionManagementUtility
31 *
32 * @author Oliver Hader <oliver@typo3.org>
33 * @author Oliver Klee <typo3-coding@oliverklee.de>
34 */
35 class ExtensionManagementUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
36
37 /**
38 * @var array A backup of registered singleton instances
39 */
40 protected $singletonInstances = array();
41
42 /**
43 * phpunit still needs some globals that are
44 * reconstructed before $backupGlobals is handled. Those
45 * important globals are handled in tearDown() directly.
46 *
47 * @var array
48 */
49 protected $globals = array();
50
51 /**
52 * Absolute path to files that must be removed
53 * after a test - handled in tearDown
54 *
55 * @TODO : Check if the tests can use vfs:// instead
56 */
57 protected $testFilesToDelete = array();
58
59 /**
60 * @var \TYPO3\CMS\Core\Package\PackageManager
61 */
62 protected $backUpPackageManager;
63
64 public function setUp() {
65 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
66 $this->createAccessibleProxyClass();
67 $this->testFilesToDelete = array();
68 $this->backUpPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
69 $this->singletonInstances = \TYPO3\CMS\Core\Utility\GeneralUtility::getSingletonInstances();
70 }
71
72 public function tearDown() {
73 ExtensionManagementUtility::clearExtensionKeyMap();
74 foreach ($this->globals as $key => $value) {
75 $GLOBALS[$key] = unserialize($value);
76 }
77 foreach ($this->testFilesToDelete as $absoluteFileName) {
78 \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($absoluteFileName);
79 }
80 if (file_exists(PATH_site . 'typo3temp/test_ext/')) {
81 \TYPO3\CMS\Core\Utility\GeneralUtility::rmdir(PATH_site . 'typo3temp/test_ext/', TRUE);
82 }
83 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backUpPackageManager);
84 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->backUpPackageManager);
85 \TYPO3\CMS\Core\Utility\GeneralUtility::resetSingletonInstances($this->singletonInstances);
86 }
87
88 /**
89 * Create a subclass with protected methods made public
90 *
91 * @return void
92 * @TODO: Move this to a fixture file
93 */
94 protected function createAccessibleProxyClass() {
95 $className = 'ExtensionManagementUtilityAccessibleProxy';
96 if (!class_exists(__NAMESPACE__ . '\\' . $className, FALSE)) {
97 eval(
98 'namespace ' . __NAMESPACE__ . ';' .
99 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
100 ' public static function getPackageManager() {' .
101 ' return static::$packageManager;' .
102 ' }' .
103 ' public static function createTypo3LoadedExtensionInformationArray() {' .
104 ' return parent::createTypo3LoadedExtensionInformationArray();' .
105 ' }' .
106 ' public static function getTypo3LoadedExtensionInformationCacheIdentifier() {' .
107 ' return parent::getTypo3LoadedExtensionInformationCacheIdentifier();' .
108 ' }' .
109 ' public static function getExtLocalconfCacheIdentifier() {' .
110 ' return parent::getExtLocalconfCacheIdentifier();' .
111 ' }' .
112 ' public static function loadSingleExtLocalconfFiles() {' .
113 ' return parent::loadSingleExtLocalconfFiles();' .
114 ' }' .
115 ' public static function getBaseTcaCacheIdentifier() {' .
116 ' return parent::getBaseTcaCacheIdentifier();' .
117 ' }' .
118 ' public static function resetExtTablesWasReadFromCacheOnceBoolean() {' .
119 ' self::$extTablesWasReadFromCacheOnce = FALSE;' .
120 ' }' .
121 ' public static function createExtLocalconfCacheEntry() {' .
122 ' return parent::createExtLocalconfCacheEntry();' .
123 ' }' .
124 ' public static function createExtTablesCacheEntry() {' .
125 ' return parent::createExtTablesCacheEntry();' .
126 ' }' .
127 ' public static function getExtTablesCacheIdentifier() {' .
128 ' return parent::getExtTablesCacheIdentifier();' .
129 ' }' .
130 '}'
131 );
132 }
133 }
134
135 /**
136 * @param string $packageKey
137 * @param array $packageMethods
138 * @return object
139 */
140 protected function createMockPackageManagerWithMockPackage($packageKey, $packageMethods = array('getPackagePath', 'getPackageKey')) {
141 $packagePath = PATH_site . 'typo3temp/test_ext/' . $packageKey . '/';
142 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir_deep($packagePath);
143 $package = $this->getMockBuilder('TYPO3\\CMS\\Core\\Package\\Package')
144 ->disableOriginalConstructor()
145 ->setMethods($packageMethods)
146 ->getMock();
147 $packageManager = $this->getMock(
148 'TYPO3\\CMS\\Core\\Package\\PackageManager',
149 array('isPackageActive', 'getPackage', 'getActivePackages')
150 );
151 $package->expects($this->any())
152 ->method('getPackagePath')
153 ->will($this->returnValue($packagePath));
154 $package->expects($this->any())
155 ->method('getPackageKey')
156 ->will($this->returnValue($packageKey));
157 $packageManager->expects($this->any())
158 ->method('isPackageActive')
159 ->will($this->returnValueMap(array(
160 array(NULL, FALSE),
161 array($packageKey, TRUE)
162 )));
163 $packageManager->expects($this->any())
164 ->method('getPackage')
165 ->with($this->equalTo($packageKey))
166 ->will($this->returnValue($package));
167 $packageManager->expects($this->any())
168 ->method('getActivePackages')
169 ->will($this->returnValue(array($packageKey => $package)));
170 return $packageManager;
171 }
172
173 ///////////////////////////////
174 // Tests concerning isLoaded
175 ///////////////////////////////
176 /**
177 * @test
178 */
179 public function isLoadedReturnsTrueIfExtensionIsLoaded() {
180 $this->assertTrue(ExtensionManagementUtility::isLoaded('cms'));
181 }
182
183 /**
184 * @test
185 */
186 public function isLoadedReturnsFalseIfExtensionIsNotLoadedAndExitIsDisabled() {
187 $this->assertFalse(ExtensionManagementUtility::isLoaded(uniqid('foobar'), FALSE));
188 }
189
190 /**
191 * @test
192 * @expectedException \BadFunctionCallException
193 */
194 public function isLoadedThrowsExceptionIfExtensionIsNotLoaded() {
195 $this->assertFalse(ExtensionManagementUtility::isLoaded(uniqid('foobar'), TRUE));
196 }
197
198 ///////////////////////////////
199 // Tests concerning extPath
200 ///////////////////////////////
201 /**
202 * @test
203 * @expectedException \BadFunctionCallException
204 */
205 public function extPathThrowsExceptionIfExtensionIsNotLoaded() {
206 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('isPackageActive'));
207 $packageManager->expects($this->once())
208 ->method('isPackageActive')
209 ->with($this->equalTo('bar'))
210 ->will($this->returnValue(FALSE));
211 ExtensionManagementUtility::setPackageManager($packageManager);
212 ExtensionManagementUtility::extPath('bar');
213 }
214
215 /**
216 * @test
217 */
218 public function extPathAppendsScriptNameToPath() {
219 $package = $this->getMockBuilder('TYPO3\\CMS\\Core\\Package\\Package')
220 ->disableOriginalConstructor()
221 ->setMethods(array('getPackagePath'))
222 ->getMock();
223 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('isPackageActive', 'getPackage'));
224 $package->expects($this->once())
225 ->method('getPackagePath')
226 ->will($this->returnValue(PATH_site . 'foo/'));
227 $packageManager->expects($this->once())
228 ->method('isPackageActive')
229 ->with($this->equalTo('foo'))
230 ->will($this->returnValue(TRUE));
231 $packageManager->expects($this->once())
232 ->method('getPackage')
233 ->with('foo')
234 ->will($this->returnValue($package));
235 ExtensionManagementUtility::setPackageManager($packageManager);
236 $this->assertSame(PATH_site . 'foo/bar.txt', ExtensionManagementUtility::extPath('foo', 'bar.txt'));
237 }
238
239 /**
240 * @test
241 * @expectedException \BadFunctionCallException
242 */
243 public function extPathThrowsExceptionIfExtensionIsNotLoadedAndTypo3LoadedExtensionsIsEmpty() {
244 unset($GLOBALS['TYPO3_LOADED_EXT']);
245 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = '';
246 ExtensionManagementUtility::extPath('bar');
247 }
248
249 /**
250 * @test
251 */
252 public function extPathSearchesForPathOfExtensionInRequiredExtensionList() {
253 $this->setExpectedException('BadFunctionCallException', '', 1365429656);
254 unset($GLOBALS['TYPO3_LOADED_EXT']);
255 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = 'foo';
256 ExtensionManagementUtility::extPath('foo');
257 }
258
259 /**
260 * @test
261 */
262 public function extPathSearchesForPathOfExtensionInExtList() {
263 $this->setExpectedException('BadFunctionCallException', '', 1365429656);
264 unset($GLOBALS['TYPO3_LOADED_EXT']);
265 $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = '';
266 ExtensionManagementUtility::extPath('foo');
267 }
268
269 //////////////////////
270 // Utility functions
271 //////////////////////
272 /**
273 * Generates a basic TCA for a given table.
274 *
275 * @param string $table name of the table, must not be empty
276 * @return array generated TCA for the given table, will not be empty
277 */
278 private function generateTCAForTable($table) {
279 $tca = array();
280 $tca[$table] = array();
281 $tca[$table]['columns'] = array(
282 'fieldA' => array(),
283 'fieldC' => array()
284 );
285 $tca[$table]['types'] = array(
286 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
287 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
288 'typeC' => array('showitem' => 'fieldC;;paletteD')
289 );
290 $tca[$table]['palettes'] = array(
291 'paletteA' => array('showitem' => 'fieldX, fieldY'),
292 'paletteB' => array('showitem' => 'fieldX, fieldY'),
293 'paletteC' => array('showitem' => 'fieldX, fieldY'),
294 'paletteD' => array('showitem' => 'fieldX, fieldY')
295 );
296 return $tca;
297 }
298
299 /////////////////////////////////////////////
300 // Tests concerning getExtensionKeyByPrefix
301 /////////////////////////////////////////////
302 /**
303 * @test
304 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
305 */
306 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
307 ExtensionManagementUtility::clearExtensionKeyMap();
308 $uniqueSuffix = uniqid('test');
309 $extensionKey = 'tt_news' . $uniqueSuffix;
310 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
311 $package = $this->getMockBuilder('TYPO3\\CMS\\Core\\Package\\Package')
312 ->disableOriginalConstructor()
313 ->setMethods(array('getPackageKey'))
314 ->getMock();
315 $package->expects($this->exactly(2))
316 ->method('getPackageKey')
317 ->will($this->returnValue($extensionKey));
318 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('getActivePackages'));
319 $packageManager->expects($this->once())
320 ->method('getActivePackages')
321 ->will($this->returnValue(array($extensionKey => $package)));
322 ExtensionManagementUtility::setPackageManager($packageManager);
323 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
324 }
325
326 /**
327 * @test
328 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
329 */
330 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
331 ExtensionManagementUtility::clearExtensionKeyMap();
332 $uniqueSuffix = uniqid('test');
333 $extensionKey = 'kickstarter' . $uniqueSuffix;
334 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
335 $package = $this->getMockBuilder('TYPO3\\CMS\\Core\\Package\\Package')
336 ->disableOriginalConstructor()
337 ->setMethods(array('getPackageKey'))
338 ->getMock();
339 $package->expects($this->exactly(2))
340 ->method('getPackageKey')
341 ->will($this->returnValue($extensionKey));
342 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('getActivePackages'));
343 $packageManager->expects($this->once())
344 ->method('getActivePackages')
345 ->will($this->returnValue(array($extensionKey => $package)));
346 ExtensionManagementUtility::setPackageManager($packageManager);
347 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
348 }
349
350 /**
351 * @test
352 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
353 */
354 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse() {
355 ExtensionManagementUtility::clearExtensionKeyMap();
356 $uniqueSuffix = uniqid('test');
357 $extensionKey = 'unloadedextension' . $uniqueSuffix;
358 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
359 $this->assertFalse(ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
360 }
361
362 //////////////////////////////////////
363 // Tests concerning addToAllTCAtypes
364 //////////////////////////////////////
365 /**
366 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
367 *
368 * @test
369 * @see ExtensionManagementUtility::addToAllTCAtypes()
370 */
371 public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
372 $table = uniqid('tx_coretest_table');
373 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
374 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
375 // Checking typeA:
376 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
377 // Checking typeB:
378 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
379 }
380
381 /**
382 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
383 *
384 * @test
385 * @see ExtensionManagementUtility::addToAllTCAtypes()
386 */
387 public function canAddFieldsToAllTCATypesAfterExistingOnes() {
388 $table = uniqid('tx_coretest_table');
389 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
390 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
391 // Checking typeA:
392 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
393 // Checking typeB:
394 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
395 }
396
397 /**
398 * Tests whether fields can be add to a TCA type before existing ones
399 *
400 * @test
401 * @see ExtensionManagementUtility::addToAllTCAtypes()
402 */
403 public function canAddFieldsToTCATypeBeforeExistingOnes() {
404 $table = uniqid('tx_coretest_table');
405 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
406 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
407 // Checking typeA:
408 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
409 // Checking typeB:
410 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
411 }
412
413 /**
414 * Tests whether fields can be add to a TCA type after existing ones
415 *
416 * @test
417 * @see ExtensionManagementUtility::addToAllTCAtypes()
418 */
419 public function canAddFieldsToTCATypeAfterExistingOnes() {
420 $table = uniqid('tx_coretest_table');
421 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
422 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
423 // Checking typeA:
424 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
425 // Checking typeB:
426 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
427 }
428
429 /**
430 * Test wheter replacing other TCA fields works as promissed
431 *
432 * @test
433 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
434 */
435 public function canAddFieldsToTCATypeAndReplaceExistingOnes() {
436 $table = uniqid('tx_coretest_table');
437 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
438 $typesBefore = $GLOBALS['TCA'][$table]['types'];
439 ExtensionManagementUtility::addToAllTCAtypes($table, 'fieldZ', '', 'replace:fieldX');
440 $this->assertEquals($typesBefore, $GLOBALS['TCA'][$table]['types'], 'It\'s wrong that the "types" array changes here - the replaced field is only on palettes');
441 // unchanged because the palette is not used
442 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
443 // unchanged because the palette is not used
444 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
445 $this->assertEquals('fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
446 $this->assertEquals('fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
447 }
448
449 ///////////////////////////////////////////////////
450 // Tests concerning addFieldsToAllPalettesOfField
451 ///////////////////////////////////////////////////
452 /**
453 * Tests whether fields can be added to a palette before existing elements.
454 *
455 * @test
456 * @see ExtensionManagementUtility::addFieldsToPalette()
457 */
458 public function canAddFieldsToPaletteBeforeExistingOnes() {
459 $table = uniqid('tx_coretest_table');
460 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
461 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
462 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
463 }
464
465 /**
466 * Tests whether fields can be added to a palette after existing elements.
467 *
468 * @test
469 * @see ExtensionManagementUtility::addFieldsToPalette()
470 */
471 public function canAddFieldsToPaletteAfterExistingOnes() {
472 $table = uniqid('tx_coretest_table');
473 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
474 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
475 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
476 }
477
478 /**
479 * Tests whether fields can be added to a palette after a not existing elements.
480 *
481 * @test
482 * @see ExtensionManagementUtility::addFieldsToPalette()
483 */
484 public function canAddFieldsToPaletteAfterNotExistingOnes() {
485 $table = uniqid('tx_coretest_table');
486 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
487 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
488 $this->assertEquals('fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
489 }
490
491 /**
492 * Tests whether fields can be added to all palettes of a regular field before existing ones.
493 *
494 * @test
495 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
496 */
497 public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
498 $table = uniqid('tx_coretest_table');
499 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
500 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
501 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
502 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
503 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
504 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
505 }
506
507 /**
508 * Tests whether fields can be added to all palettes of a regular field after existing ones.
509 *
510 * @test
511 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
512 */
513 public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
514 $table = uniqid('tx_coretest_table');
515 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
516 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
517 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
518 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
519 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
520 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
521 }
522
523 /**
524 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
525 *
526 * @test
527 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
528 */
529 public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
530 $table = uniqid('tx_coretest_table');
531 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
532 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
533 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
534 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
535 $this->assertEquals('fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
536 $this->assertEquals('fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
537 }
538
539 /**
540 * Tests whether fields are added to a new palette that did not exist before.
541 *
542 * @test
543 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
544 */
545 public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
546 $table = uniqid('tx_coretest_table');
547 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
548 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
549 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
550 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
551 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
552 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
553 $this->assertEquals('newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']);
554 }
555
556 /////////////////////////////////////////
557 // Tests concerning addTcaSelectItem
558 /////////////////////////////////////////
559 /**
560 * @test
561 * @expectedException \InvalidArgumentException
562 */
563 public function addTcaSelectItemThrowsExceptionIfTableIsNotOfTypeString() {
564 ExtensionManagementUtility::addTcaSelectItem(array(), 'foo', array());
565 }
566
567 /**
568 * @test
569 * @expectedException \InvalidArgumentException
570 */
571 public function addTcaSelectItemThrowsExceptionIfFieldIsNotOfTypeString() {
572 ExtensionManagementUtility::addTcaSelectItem('foo', array(), array());
573 }
574
575 /**
576 * @test
577 * @expectedException \InvalidArgumentException
578 */
579 public function addTcaSelectItemThrowsExceptionIfRelativeToFieldIsNotOfTypeString() {
580 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), array());
581 }
582
583 /**
584 * @test
585 * @expectedException \InvalidArgumentException
586 */
587 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOfTypeString() {
588 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', array());
589 }
590
591 /**
592 * @test
593 * @expectedException \InvalidArgumentException
594 */
595 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOneOfValidKeywords() {
596 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', 'not allowed keyword');
597 }
598
599 /**
600 * @test
601 * @expectedException \RuntimeException
602 */
603 public function addTcaSelectItemThrowsExceptionIfFieldIsNotFoundInTca() {
604 $GLOBALS['TCA'] = array();
605 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array());
606 }
607
608 /**
609 * Data provider for addTcaSelectItemInsertsItemAtSpecifiedPosition
610 */
611 public function addTcaSelectItemDataProvider() {
612 // Every array splits into:
613 // - relativeToField
614 // - relativePosition
615 // - expectedResultArray
616 return array(
617 'add at end of array' => array(
618 '',
619 '',
620 array(
621 0 => array('firstElement'),
622 1 => array('matchMe'),
623 2 => array('thirdElement'),
624 3 => array('insertedElement')
625 )
626 ),
627 'replace element' => array(
628 'matchMe',
629 'replace',
630 array(
631 0 => array('firstElement'),
632 1 => array('insertedElement'),
633 2 => array('thirdElement')
634 )
635 ),
636 'add element after' => array(
637 'matchMe',
638 'after',
639 array(
640 0 => array('firstElement'),
641 1 => array('matchMe'),
642 2 => array('insertedElement'),
643 3 => array('thirdElement')
644 )
645 ),
646 'add element before' => array(
647 'matchMe',
648 'before',
649 array(
650 0 => array('firstElement'),
651 1 => array('insertedElement'),
652 2 => array('matchMe'),
653 3 => array('thirdElement')
654 )
655 ),
656 'add at end if relative position was not found' => array(
657 'notExistingItem',
658 'after',
659 array(
660 0 => array('firstElement'),
661 1 => array('matchMe'),
662 2 => array('thirdElement'),
663 3 => array('insertedElement')
664 )
665 )
666 );
667 }
668
669 /**
670 * @test
671 * @dataProvider addTcaSelectItemDataProvider
672 */
673 public function addTcaSelectItemInsertsItemAtSpecifiedPosition($relativeToField, $relativePosition, $expectedResultArray) {
674 $GLOBALS['TCA'] = array(
675 'testTable' => array(
676 'columns' => array(
677 'testField' => array(
678 'config' => array(
679 'items' => array(
680 '0' => array('firstElement'),
681 '1' => array('matchMe'),
682 2 => array('thirdElement')
683 )
684 )
685 )
686 )
687 )
688 );
689 ExtensionManagementUtility::addTcaSelectItem('testTable', 'testField', array('insertedElement'), $relativeToField, $relativePosition);
690 $this->assertEquals($expectedResultArray, $GLOBALS['TCA']['testTable']['columns']['testField']['config']['items']);
691 }
692
693 /////////////////////////////////////////
694 // Tests concerning loadExtLocalconf
695 /////////////////////////////////////////
696 /**
697 * @test
698 */
699 public function loadExtLocalconfDoesNotReadFromCacheIfCachingIsDenied() {
700 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
701 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
702 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
703 ExtensionManagementUtility::loadExtLocalconf(FALSE);
704 }
705
706 /**
707 * @test
708 */
709 public function loadExtLocalconfRequiresCacheFileIfExistsAndCachingIsAllowed() {
710 $mockCache = $this->getMock(
711 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
712 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
713 array(),
714 '',
715 FALSE
716 );
717 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
718 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
719 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
720 $mockCache->expects($this->once())->method('requireOnce');
721 ExtensionManagementUtility::loadExtLocalconf(TRUE);
722 }
723
724 /////////////////////////////////////////
725 // Tests concerning loadSingleExtLocalconfFiles
726 /////////////////////////////////////////
727 /**
728 * @test
729 * @expectedException \RuntimeException
730 */
731 public function loadSingleExtLocalconfFilesRequiresExtLocalconfFileRegisteredInGlobalTypo3LoadedExt() {
732 $extensionName = uniqid('foo');
733 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
734 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
735 file_put_contents($extLocalconfLocation, "<?php\n\nthrow new RuntimeException('', 1340559079);\n\n?>");
736 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
737 ExtensionManagementUtilityAccessibleProxy::loadSingleExtLocalconfFiles();
738 }
739
740 /////////////////////////////////////////
741 // Tests concerning createExtLocalconfCacheEntry
742 /////////////////////////////////////////
743 /**
744 * @test
745 */
746 public function createExtLocalconfCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtLocalconf() {
747 $extensionName = uniqid('foo');
748 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
749 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
750 $this->testFilesToDelete[] = $extLocalconfLocation;
751 $uniqueStringInLocalconf = uniqid('foo');
752 file_put_contents($extLocalconfLocation, "<?php\n\n" . $uniqueStringInLocalconf . "\n\n?>");
753 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
754 $mockCache = $this->getMock(
755 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
756 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
757 array(),
758 '',
759 FALSE
760 );
761 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
762 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
763 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInLocalconf), $this->anything());
764 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
765 }
766
767 /**
768 * @test
769 */
770 public function createExtLocalconfCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtLocalconfExists() {
771 $extensionName = uniqid('foo');
772 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
773 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
774 $mockCache = $this->getMock(
775 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
776 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
777 array(),
778 '',
779 FALSE
780 );
781 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
782 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
783 $mockCache->expects($this->once())
784 ->method('set')
785 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
786 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
787 }
788
789 /**
790 * @test
791 */
792 public function createExtLocalconfCacheEntryWritesCacheEntryWithNoTags() {
793 $mockCache = $this->getMock(
794 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
795 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
796 array(),
797 '',
798 FALSE
799 );
800 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
801 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
802 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
803 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
804 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
805 }
806
807 /////////////////////////////////////////
808 // Tests concerning getExtLocalconfCacheIdentifier
809 /////////////////////////////////////////
810 /**
811 * @test
812 */
813 public function getExtLocalconfCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
814 $prefix = 'ext_localconf_';
815 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtLocalconfCacheIdentifier();
816 $this->assertStringStartsWith($prefix, $identifier);
817 $sha1 = str_replace($prefix, '', $identifier);
818 $this->assertEquals(40, strlen($sha1));
819 }
820
821 /////////////////////////////////////////
822 // Tests concerning loadBaseTca
823 /////////////////////////////////////////
824
825 /**
826 * @test
827 */
828 public function loadBaseTcaDoesNotReadFromCacheIfCachingIsDenied() {
829 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
830 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
831 ExtensionManagementUtility::loadBaseTca(FALSE);
832 }
833
834 /**
835 * @test
836 */
837 public function loadBaseTcaRequiresCacheFileIfExistsAndCachingIsAllowed() {
838 $mockCache = $this->getMock(
839 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
840 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
841 array(),
842 '',
843 FALSE
844 );
845 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
846 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
847 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
848 $mockCache->expects($this->once())->method('requireOnce');
849 ExtensionManagementUtility::loadBaseTca(TRUE);
850 }
851
852 /**
853 * @test
854 */
855 public function loadBaseTcaCreatesCacheFileWithContentOfAnExtensionsConfigurationTcaPhpFile() {
856 $extensionName = uniqid('test_baseTca_');
857 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
858 $packagePath = $packageManager->getPackage($extensionName)->getPackagePath();
859 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath);
860 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/');
861 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/TCA/');
862 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
863 ExtensionManagementUtility::setPackageManager($packageManager);
864 $uniqueTableName = uniqid('table_name_');
865 $uniqueStringInTableConfiguration = uniqid('table_configuration_');
866 $tableConfiguration = '<?php return array(\'foo\' => \'' . $uniqueStringInTableConfiguration . '\'); ?>';
867 file_put_contents($packagePath . 'Configuration/TCA/' . $uniqueTableName . '.php', $tableConfiguration);
868 $mockCache = $this->getMock(
869 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
870 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
871 array(),
872 '',
873 FALSE
874 );
875 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
876 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
877 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
878 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTableConfiguration), $this->anything());
879 ExtensionManagementUtility::loadBaseTca(TRUE);
880 }
881
882 /**
883 * @test
884 */
885 public function loadBaseTcaWritesCacheEntryWithNoTags() {
886 $mockCache = $this->getMock(
887 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
888 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
889 array(),
890 '',
891 FALSE
892 );
893 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
894 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
895 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
896 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
897 ExtensionManagementUtility::loadBaseTca();
898 }
899
900 /////////////////////////////////////////
901 // Tests concerning getBaseTcaCacheIdentifier
902 /////////////////////////////////////////
903
904 /**
905 * @test
906 */
907 public function getBaseTcaCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
908 $prefix = 'tca_base_';
909 $identifier = ExtensionManagementUtilityAccessibleProxy::getBaseTcaCacheIdentifier();
910 $this->assertStringStartsWith($prefix, $identifier);
911 $sha1 = str_replace($prefix, '', $identifier);
912 $this->assertEquals(40, strlen($sha1));
913 }
914
915 /////////////////////////////////////////
916 // Tests concerning loadExtTables
917 /////////////////////////////////////////
918 /**
919 * @test
920 */
921 public function loadExtTablesDoesNotReadFromCacheIfCachingIsDenied() {
922 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
923 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
924 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
925 ExtensionManagementUtility::loadExtLocalconf(FALSE);
926 }
927
928 /**
929 * @test
930 */
931 public function loadExtTablesRequiresCacheFileIfExistsAndCachingIsAllowed() {
932 $mockCache = $this->getMock(
933 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
934 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
935 array(),
936 '',
937 FALSE
938 );
939 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
940 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
941 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
942 $mockCache->expects($this->once())->method('requireOnce');
943 // Reset the internal cache access tracking variable of extMgm
944 // This method is only in the ProxyClass!
945 ExtensionManagementUtilityAccessibleProxy::resetExtTablesWasReadFromCacheOnceBoolean();
946 ExtensionManagementUtility::loadExtTables(TRUE);
947 }
948
949 /////////////////////////////////////////
950 // Tests concerning createExtTablesCacheEntry
951 /////////////////////////////////////////
952 /**
953 * @test
954 */
955 public function createExtTablesCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtTables() {
956 $extensionName = uniqid('foo');
957 $extTablesLocation = PATH_site . 'typo3temp/' . uniqid('test_ext_tables') . '.php';
958 $this->testFilesToDelete[] = $extTablesLocation;
959 $uniqueStringInTables = uniqid('foo');
960 file_put_contents($extTablesLocation, "<?php\n\n$uniqueStringInTables\n\n?>");
961 $GLOBALS['TYPO3_LOADED_EXT'] = array(
962 $extensionName => array(
963 'ext_tables.php' => $extTablesLocation
964 )
965 );
966 $mockCache = $this->getMock(
967 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
968 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
969 array(),
970 '',
971 FALSE
972 );
973 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
974 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
975 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTables), $this->anything());
976 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
977 }
978
979 /**
980 * @test
981 */
982 public function createExtTablesCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtTablesExists() {
983 $extensionName = uniqid('foo');
984 $GLOBALS['TYPO3_LOADED_EXT'] = array(
985 $extensionName => array(),
986 );
987 $mockCache = $this->getMock(
988 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
989 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
990 array(),
991 '',
992 FALSE
993 );
994 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
995 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
996 $mockCache->expects($this->once())
997 ->method('set')
998 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
999 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1000 }
1001
1002 /**
1003 * @test
1004 */
1005 public function createExtTablesCacheEntryWritesCacheEntryWithNoTags() {
1006 $mockCache = $this->getMock(
1007 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1008 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1009 array(),
1010 '',
1011 FALSE
1012 );
1013 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1014 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1015 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1016 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
1017 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1018 }
1019
1020 /////////////////////////////////////////
1021 // Tests concerning getExtTablesCacheIdentifier
1022 /////////////////////////////////////////
1023 /**
1024 * @test
1025 */
1026 public function getExtTablesCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
1027 $prefix = 'ext_tables_';
1028 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtTablesCacheIdentifier();
1029 $this->assertStringStartsWith($prefix, $identifier);
1030 $sha1 = str_replace($prefix, '', $identifier);
1031 $this->assertEquals(40, strlen($sha1));
1032 }
1033
1034 /////////////////////////////////////////
1035 // Tests concerning removeCacheFiles
1036 /////////////////////////////////////////
1037 /**
1038 * @test
1039 */
1040 public function removeCacheFilesFlushesCache() {
1041 $mockCache = $this->getMock(
1042 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1043 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1044 array(),
1045 '',
1046 FALSE
1047 );
1048 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1049 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1050 $mockCache->expects($this->once())->method('flush');
1051 ExtensionManagementUtility::removeCacheFiles();
1052 }
1053
1054 /////////////////////////////////////////
1055 // Tests concerning loadNewTcaColumnsConfigFiles
1056 /////////////////////////////////////////
1057
1058 /**
1059 * @test
1060 * @expectedException \RuntimeException
1061 */
1062 public function loadNewTcaColumnsConfigFilesIncludesDefinedDynamicConfigFileIfNoColumnsExist() {
1063 $GLOBALS['TCA'] = array(
1064 'test' => array(
1065 'ctrl' => array(
1066 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1067 ),
1068 ),
1069 );
1070 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1071 }
1072
1073 /**
1074 * @test
1075 */
1076 public function loadNewTcaColumnsConfigFilesDoesNotIncludeFileIfColumnsExist() {
1077 $GLOBALS['TCA'] = array(
1078 'test' => array(
1079 'ctrl' => array(
1080 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1081 ),
1082 'columns' => array(
1083 'foo' => 'bar',
1084 ),
1085 ),
1086 );
1087 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1088 }
1089
1090 /////////////////////////////////////////
1091 // Tests concerning getExtensionVersion
1092 /////////////////////////////////////////
1093 /**
1094 * Data provider for negative getExtensionVersion() tests.
1095 *
1096 * @return array
1097 */
1098 public function getExtensionVersionFaultyDataProvider() {
1099 return array(
1100 array(''),
1101 array(0),
1102 array(new \stdClass()),
1103 array(TRUE)
1104 );
1105 }
1106
1107 /**
1108 * @test
1109 * @expectedException \InvalidArgumentException
1110 * @dataProvider getExtensionVersionFaultyDataProvider
1111 */
1112 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
1113 ExtensionManagementUtility::getExtensionVersion($key);
1114 }
1115
1116 /**
1117 * @test
1118 */
1119 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
1120 ExtensionManagementUtility::clearExtensionKeyMap();
1121 $uniqueSuffix = uniqid('test');
1122 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1123 $this->assertEquals('', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1124 }
1125
1126 /**
1127 * @test
1128 */
1129 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
1130 ExtensionManagementUtility::clearExtensionKeyMap();
1131 $className = uniqid('ExtensionManagementUtility');
1132 eval(
1133 'namespace ' . __NAMESPACE__ .';' .
1134 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
1135 ' public static function isLoaded() {' .
1136 ' return TRUE;' .
1137 ' }' .
1138 '}'
1139 );
1140 $className = __NAMESPACE__ . '\\' . $className;
1141 ExtensionManagementUtility::clearExtensionKeyMap();
1142 $uniqueSuffix = uniqid('test');
1143 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1144 $packageMetaData = $this->getMock('TYPO3\\Flow\\Package\\MetaData', array('getVersion'), array($extensionKey));
1145 $packageMetaData->expects($this->any())->method('getVersion')->will($this->returnValue('1.2.3'));
1146 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey, array('getPackagePath', 'getPackageKey', 'getPackageMetaData'));
1147 /** @var \PHPUnit_Framework_MockObject_MockObject $package */
1148 $package = $packageManager->getPackage($extensionKey);
1149 $package->expects($this->any())
1150 ->method('getPackageMetaData')
1151 ->will($this->returnValue($packageMetaData));
1152 ExtensionManagementUtility::setPackageManager($packageManager);
1153 $this->assertEquals('1.2.3', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1154 }
1155
1156 /////////////////////////////////////////
1157 // Tests concerning loadExtension
1158 /////////////////////////////////////////
1159 /**
1160 * @test
1161 * @expectedException \RuntimeException
1162 */
1163 public function loadExtensionThrowsExceptionIfExtensionIsLoaded() {
1164 $extensionKey = uniqid('test');
1165 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey);
1166 ExtensionManagementUtility::setPackageManager($packageManager);
1167 ExtensionManagementUtility::loadExtension($extensionKey);
1168 }
1169
1170 /////////////////////////////////////////
1171 // Tests concerning unloadExtension
1172 /////////////////////////////////////////
1173 /**
1174 * @test
1175 * @expectedException \RuntimeException
1176 */
1177 public function unloadExtensionThrowsExceptionIfExtensionIsNotLoaded() {
1178 $className = uniqid('ExtensionManagementUtility');
1179 eval(
1180 'namespace ' . __NAMESPACE__ . ';' .
1181 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
1182 ' public static function isLoaded() {' .
1183 ' return FALSE;' .
1184 ' }' .
1185 '}'
1186 );
1187 $className = __NAMESPACE__ . '\\' . $className;
1188 $className::unloadExtension('test');
1189 }
1190
1191 /**
1192 * @test
1193 * @expectedException \RuntimeException
1194 */
1195 public function unloadExtensionThrowsExceptionIfExtensionIsRequired() {
1196 $extensionKey = uniqid('test');
1197 $className = uniqid('ExtensionManagementUtility');
1198 eval(
1199 'namespace ' . __NAMESPACE__ . ';' .
1200 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
1201 ' public static function isLoaded() {' .
1202 ' return TRUE;' .
1203 ' }' .
1204 ' public static function getRequiredExtensionListArray() {' .
1205 ' return array(\'' . $extensionKey . '\');' .
1206 ' }' .
1207 '}'
1208 );
1209 $className = __NAMESPACE__ . '\\' . $className;
1210 $className::unloadExtension($extensionKey);
1211 }
1212
1213 /**
1214 * @test
1215 * @expectedException \RuntimeException
1216 */
1217 public function unloadExtensionRemovesExtensionFromExtList() {
1218 if (!file_exists((PATH_typo3conf . 'LocalConfiguration.php'))) {
1219 $this->markTestSkipped('Test is not available until update wizard to transform localconf.php was run.');
1220 }
1221 $extensionKeyToUnload = uniqid('unloadMe');
1222 $className = uniqid('ExtensionManagementUtility');
1223 eval(
1224 'namespace ' . __NAMESPACE__ . ';' .
1225 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
1226 ' public static function isLoaded() {' .
1227 ' return TRUE;' .
1228 ' }' .
1229 ' public static function writeNewExtensionList($extList) {' .
1230 ' if (!in_array(' . $extensionKeyToUnload . ', $extList)) {' .
1231 ' throw new \\RuntimeException(\'test\');' .
1232 ' }' .
1233 ' }' .
1234 '}'
1235 );
1236 $className = __NAMESPACE__ . '\\' . $className;
1237 $className::unloadExtension($extensionKeyToUnload);
1238 }
1239
1240 /////////////////////////////////////////
1241 // Tests concerning makeCategorizable
1242 /////////////////////////////////////////
1243 /**
1244 * @test
1245 */
1246 public function isMakeCategorizableAvailableInRegistryWithDefaultField() {
1247 $extensionKey = uniqid('extension');
1248 $tableName = uniqid('table');
1249 $GLOBALS['TCA'][$tableName] = array(
1250 'ctrl' => array(),
1251 'columns' => array()
1252 );
1253 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1254 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1255 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName);
1256 $registryMock->applyTca();
1257 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns']['categories']);
1258 }
1259
1260 /**
1261 * @test
1262 */
1263 public function isMakeCategorizableAvailableInRegistryWithSpecifictField() {
1264 $extensionKey = uniqid('extension');
1265 $tableName = uniqid('table');
1266 $fieldName = uniqid('field');
1267 $GLOBALS['TCA'][$tableName] = array(
1268 'ctrl' => array(),
1269 'columns' => array()
1270 );
1271 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1272 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1273 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName, $fieldName);
1274 $registryMock->applyTca();
1275 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns'][$fieldName]);
1276 }
1277
1278 }