[BUGFIX] addModule after/before not working
[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 $packageName = uniqid('foo');
207 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('isPackageActive'));
208 $packageManager->expects($this->once())
209 ->method('isPackageActive')
210 ->with($this->equalTo($packageName))
211 ->will($this->returnValue(FALSE));
212 ExtensionManagementUtility::setPackageManager($packageManager);
213 ExtensionManagementUtility::extPath($packageName);
214 }
215
216 /**
217 * @test
218 */
219 public function extPathAppendsScriptNameToPath() {
220 $package = $this->getMockBuilder('TYPO3\\CMS\\Core\\Package\\Package')
221 ->disableOriginalConstructor()
222 ->setMethods(array('getPackagePath'))
223 ->getMock();
224 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('isPackageActive', 'getPackage'));
225 $package->expects($this->once())
226 ->method('getPackagePath')
227 ->will($this->returnValue(PATH_site . 'foo/'));
228 $packageManager->expects($this->once())
229 ->method('isPackageActive')
230 ->with($this->equalTo('foo'))
231 ->will($this->returnValue(TRUE));
232 $packageManager->expects($this->once())
233 ->method('getPackage')
234 ->with('foo')
235 ->will($this->returnValue($package));
236 ExtensionManagementUtility::setPackageManager($packageManager);
237 $this->assertSame(PATH_site . 'foo/bar.txt', ExtensionManagementUtility::extPath('foo', 'bar.txt'));
238 }
239
240 //////////////////////
241 // Utility functions
242 //////////////////////
243 /**
244 * Generates a basic TCA for a given table.
245 *
246 * @param string $table name of the table, must not be empty
247 * @return array generated TCA for the given table, will not be empty
248 */
249 private function generateTCAForTable($table) {
250 $tca = array();
251 $tca[$table] = array();
252 $tca[$table]['columns'] = array(
253 'fieldA' => array(),
254 'fieldC' => array()
255 );
256 $tca[$table]['types'] = array(
257 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
258 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
259 'typeC' => array('showitem' => 'fieldC;;paletteD')
260 );
261 $tca[$table]['palettes'] = array(
262 'paletteA' => array('showitem' => 'fieldX, fieldY'),
263 'paletteB' => array('showitem' => 'fieldX, fieldY'),
264 'paletteC' => array('showitem' => 'fieldX, fieldY'),
265 'paletteD' => array('showitem' => 'fieldX, fieldY')
266 );
267 return $tca;
268 }
269
270 /**
271 * Data provider for getClassNamePrefixForExtensionKey.
272 *
273 * @return array
274 */
275 public function extensionKeyDataProvider() {
276 return array(
277 'Without underscores' => array(
278 'testkey',
279 'tx_testkey'
280 ),
281 'With underscores' => array(
282 'this_is_a_test_extension',
283 'tx_thisisatestextension'
284 ),
285 'With user prefix and without underscores' => array(
286 'user_testkey',
287 'user_testkey'
288 ),
289 'With user prefix and with underscores' => array(
290 'user_test_key',
291 'user_testkey'
292 ),
293 );
294 }
295
296 /**
297 * @test
298 * @param string $extensionName
299 * @param string $expectedPrefix
300 * @dataProvider extensionKeyDataProvider
301 */
302 public function getClassNamePrefixForExtensionKey($extensionName, $expectedPrefix) {
303 $this->assertSame($expectedPrefix, ExtensionManagementUtility::getCN($extensionName));
304 }
305
306 /////////////////////////////////////////////
307 // Tests concerning getExtensionKeyByPrefix
308 /////////////////////////////////////////////
309 /**
310 * @test
311 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
312 */
313 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
314 ExtensionManagementUtility::clearExtensionKeyMap();
315 $uniqueSuffix = uniqid('test');
316 $extensionKey = 'tt_news' . $uniqueSuffix;
317 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
318 $package = $this->getMockBuilder('TYPO3\\CMS\\Core\\Package\\Package')
319 ->disableOriginalConstructor()
320 ->setMethods(array('getPackageKey'))
321 ->getMock();
322 $package->expects($this->exactly(2))
323 ->method('getPackageKey')
324 ->will($this->returnValue($extensionKey));
325 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('getActivePackages'));
326 $packageManager->expects($this->once())
327 ->method('getActivePackages')
328 ->will($this->returnValue(array($extensionKey => $package)));
329 ExtensionManagementUtility::setPackageManager($packageManager);
330 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
331 }
332
333 /**
334 * @test
335 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
336 */
337 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
338 ExtensionManagementUtility::clearExtensionKeyMap();
339 $uniqueSuffix = uniqid('test');
340 $extensionKey = 'kickstarter' . $uniqueSuffix;
341 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
342 $package = $this->getMockBuilder('TYPO3\\CMS\\Core\\Package\\Package')
343 ->disableOriginalConstructor()
344 ->setMethods(array('getPackageKey'))
345 ->getMock();
346 $package->expects($this->exactly(2))
347 ->method('getPackageKey')
348 ->will($this->returnValue($extensionKey));
349 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('getActivePackages'));
350 $packageManager->expects($this->once())
351 ->method('getActivePackages')
352 ->will($this->returnValue(array($extensionKey => $package)));
353 ExtensionManagementUtility::setPackageManager($packageManager);
354 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
355 }
356
357 /**
358 * @test
359 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
360 */
361 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse() {
362 ExtensionManagementUtility::clearExtensionKeyMap();
363 $uniqueSuffix = uniqid('test');
364 $extensionKey = 'unloadedextension' . $uniqueSuffix;
365 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
366 $this->assertFalse(ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
367 }
368
369 //////////////////////////////////////
370 // Tests concerning addToAllTCAtypes
371 //////////////////////////////////////
372 /**
373 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
374 *
375 * @test
376 * @see ExtensionManagementUtility::addToAllTCAtypes()
377 */
378 public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
379 $table = uniqid('tx_coretest_table');
380 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
381 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
382 // Checking typeA:
383 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
384 // Checking typeB:
385 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
386 }
387
388 /**
389 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
390 *
391 * @test
392 * @see ExtensionManagementUtility::addToAllTCAtypes()
393 */
394 public function canAddFieldsToAllTCATypesAfterExistingOnes() {
395 $table = uniqid('tx_coretest_table');
396 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
397 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
398 // Checking typeA:
399 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
400 // Checking typeB:
401 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
402 }
403
404 /**
405 * Tests whether fields can be add to a TCA type before existing ones
406 *
407 * @test
408 * @see ExtensionManagementUtility::addToAllTCAtypes()
409 */
410 public function canAddFieldsToTCATypeBeforeExistingOnes() {
411 $table = uniqid('tx_coretest_table');
412 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
413 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
414 // Checking typeA:
415 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
416 // Checking typeB:
417 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
418 }
419
420 /**
421 * Tests whether fields can be add to a TCA type after existing ones
422 *
423 * @test
424 * @see ExtensionManagementUtility::addToAllTCAtypes()
425 */
426 public function canAddFieldsToTCATypeAfterExistingOnes() {
427 $table = uniqid('tx_coretest_table');
428 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
429 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
430 // Checking typeA:
431 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
432 // Checking typeB:
433 $this->assertEquals('fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
434 }
435
436 /**
437 * Test wheter replacing other TCA fields works as promissed
438 *
439 * @test
440 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
441 */
442 public function canAddFieldsToTCATypeAndReplaceExistingOnes() {
443 $table = uniqid('tx_coretest_table');
444 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
445 $typesBefore = $GLOBALS['TCA'][$table]['types'];
446 ExtensionManagementUtility::addToAllTCAtypes($table, 'fieldZ', '', 'replace:fieldX');
447 $this->assertEquals($typesBefore, $GLOBALS['TCA'][$table]['types'], 'It\'s wrong that the "types" array changes here - the replaced field is only on palettes');
448 // unchanged because the palette is not used
449 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
450 // unchanged because the palette is not used
451 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
452 $this->assertEquals('fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
453 $this->assertEquals('fieldZ, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
454 }
455
456 ///////////////////////////////////////////////////
457 // Tests concerning addFieldsToAllPalettesOfField
458 ///////////////////////////////////////////////////
459 /**
460 * Tests whether fields can be added to a palette before existing elements.
461 *
462 * @test
463 * @see ExtensionManagementUtility::addFieldsToPalette()
464 */
465 public function canAddFieldsToPaletteBeforeExistingOnes() {
466 $table = uniqid('tx_coretest_table');
467 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
468 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
469 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
470 }
471
472 /**
473 * Tests whether fields can be added to a palette after existing elements.
474 *
475 * @test
476 * @see ExtensionManagementUtility::addFieldsToPalette()
477 */
478 public function canAddFieldsToPaletteAfterExistingOnes() {
479 $table = uniqid('tx_coretest_table');
480 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
481 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
482 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
483 }
484
485 /**
486 * Tests whether fields can be added to a palette after a not existing elements.
487 *
488 * @test
489 * @see ExtensionManagementUtility::addFieldsToPalette()
490 */
491 public function canAddFieldsToPaletteAfterNotExistingOnes() {
492 $table = uniqid('tx_coretest_table');
493 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
494 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
495 $this->assertEquals('fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
496 }
497
498 /**
499 * Tests whether fields can be added to all palettes of a regular field before existing ones.
500 *
501 * @test
502 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
503 */
504 public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
505 $table = uniqid('tx_coretest_table');
506 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
507 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
508 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
509 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
510 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
511 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
512 }
513
514 /**
515 * Tests whether fields can be added to all palettes of a regular field after existing ones.
516 *
517 * @test
518 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
519 */
520 public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
521 $table = uniqid('tx_coretest_table');
522 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
523 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
524 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
525 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
526 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
527 $this->assertEquals('fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
528 }
529
530 /**
531 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
532 *
533 * @test
534 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
535 */
536 public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
537 $table = uniqid('tx_coretest_table');
538 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
539 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
540 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
541 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
542 $this->assertEquals('fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
543 $this->assertEquals('fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
544 }
545
546 /**
547 * Tests whether fields are added to a new palette that did not exist before.
548 *
549 * @test
550 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
551 */
552 public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
553 $table = uniqid('tx_coretest_table');
554 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
555 ExtensionManagementUtility::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
556 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
557 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
558 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
559 $this->assertEquals('fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
560 $this->assertEquals('newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']);
561 }
562
563 /////////////////////////////////////////
564 // Tests concerning addTcaSelectItem
565 /////////////////////////////////////////
566 /**
567 * @test
568 * @expectedException \InvalidArgumentException
569 */
570 public function addTcaSelectItemThrowsExceptionIfTableIsNotOfTypeString() {
571 ExtensionManagementUtility::addTcaSelectItem(array(), 'foo', array());
572 }
573
574 /**
575 * @test
576 * @expectedException \InvalidArgumentException
577 */
578 public function addTcaSelectItemThrowsExceptionIfFieldIsNotOfTypeString() {
579 ExtensionManagementUtility::addTcaSelectItem('foo', array(), array());
580 }
581
582 /**
583 * @test
584 * @expectedException \InvalidArgumentException
585 */
586 public function addTcaSelectItemThrowsExceptionIfRelativeToFieldIsNotOfTypeString() {
587 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), array());
588 }
589
590 /**
591 * @test
592 * @expectedException \InvalidArgumentException
593 */
594 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOfTypeString() {
595 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', array());
596 }
597
598 /**
599 * @test
600 * @expectedException \InvalidArgumentException
601 */
602 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOneOfValidKeywords() {
603 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', 'not allowed keyword');
604 }
605
606 /**
607 * @test
608 * @expectedException \RuntimeException
609 */
610 public function addTcaSelectItemThrowsExceptionIfFieldIsNotFoundInTca() {
611 $GLOBALS['TCA'] = array();
612 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array());
613 }
614
615 /**
616 * Data provider for addTcaSelectItemInsertsItemAtSpecifiedPosition
617 */
618 public function addTcaSelectItemDataProvider() {
619 // Every array splits into:
620 // - relativeToField
621 // - relativePosition
622 // - expectedResultArray
623 return array(
624 'add at end of array' => array(
625 '',
626 '',
627 array(
628 0 => array('firstElement'),
629 1 => array('matchMe'),
630 2 => array('thirdElement'),
631 3 => array('insertedElement')
632 )
633 ),
634 'replace element' => array(
635 'matchMe',
636 'replace',
637 array(
638 0 => array('firstElement'),
639 1 => array('insertedElement'),
640 2 => array('thirdElement')
641 )
642 ),
643 'add element after' => array(
644 'matchMe',
645 'after',
646 array(
647 0 => array('firstElement'),
648 1 => array('matchMe'),
649 2 => array('insertedElement'),
650 3 => array('thirdElement')
651 )
652 ),
653 'add element before' => array(
654 'matchMe',
655 'before',
656 array(
657 0 => array('firstElement'),
658 1 => array('insertedElement'),
659 2 => array('matchMe'),
660 3 => array('thirdElement')
661 )
662 ),
663 'add at end if relative position was not found' => array(
664 'notExistingItem',
665 'after',
666 array(
667 0 => array('firstElement'),
668 1 => array('matchMe'),
669 2 => array('thirdElement'),
670 3 => array('insertedElement')
671 )
672 )
673 );
674 }
675
676 /**
677 * @test
678 * @dataProvider addTcaSelectItemDataProvider
679 */
680 public function addTcaSelectItemInsertsItemAtSpecifiedPosition($relativeToField, $relativePosition, $expectedResultArray) {
681 $GLOBALS['TCA'] = array(
682 'testTable' => array(
683 'columns' => array(
684 'testField' => array(
685 'config' => array(
686 'items' => array(
687 '0' => array('firstElement'),
688 '1' => array('matchMe'),
689 2 => array('thirdElement')
690 )
691 )
692 )
693 )
694 )
695 );
696 ExtensionManagementUtility::addTcaSelectItem('testTable', 'testField', array('insertedElement'), $relativeToField, $relativePosition);
697 $this->assertEquals($expectedResultArray, $GLOBALS['TCA']['testTable']['columns']['testField']['config']['items']);
698 }
699
700 /////////////////////////////////////////
701 // Tests concerning loadExtLocalconf
702 /////////////////////////////////////////
703 /**
704 * @test
705 */
706 public function loadExtLocalconfDoesNotReadFromCacheIfCachingIsDenied() {
707 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
708 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
709 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
710 ExtensionManagementUtility::loadExtLocalconf(FALSE);
711 }
712
713 /**
714 * @test
715 */
716 public function loadExtLocalconfRequiresCacheFileIfExistsAndCachingIsAllowed() {
717 $mockCache = $this->getMock(
718 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
719 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
720 array(),
721 '',
722 FALSE
723 );
724 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
725 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
726 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
727 $mockCache->expects($this->once())->method('requireOnce');
728 ExtensionManagementUtility::loadExtLocalconf(TRUE);
729 }
730
731 /////////////////////////////////////////
732 // Tests concerning loadSingleExtLocalconfFiles
733 /////////////////////////////////////////
734 /**
735 * @test
736 * @expectedException \RuntimeException
737 */
738 public function loadSingleExtLocalconfFilesRequiresExtLocalconfFileRegisteredInGlobalTypo3LoadedExt() {
739 $extensionName = uniqid('foo');
740 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
741 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
742 file_put_contents($extLocalconfLocation, "<?php\n\nthrow new RuntimeException('', 1340559079);\n\n?>");
743 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
744 ExtensionManagementUtilityAccessibleProxy::loadSingleExtLocalconfFiles();
745 }
746
747
748 /////////////////////////////////////////
749 // Tests concerning addModule
750 /////////////////////////////////////////
751
752 /**
753 * Data provider for addModule tests
754 * @return array
755 */
756 public function addModulePositionTestsDataProvider() {
757 return array(
758 'can add new main module if none exists' => array(
759 'top',
760 '',
761 'newModule'
762 ),
763 'can add new sub module if no position specified' => array(
764 '',
765 'some,modules',
766 'some,modules,newModule'
767 ),
768 'can add new sub module to top of module' => array(
769 'top',
770 'some,modules',
771 'newModule,some,modules'
772 ),
773 'can add new sub module if bottom of module' => array(
774 'bottom',
775 'some,modules',
776 'some,modules,newModule'
777 ),
778 'can add new sub module before specified sub module' => array(
779 'before:modules',
780 'some,modules',
781 'some,newModule,modules'
782 ),
783 'can add new sub module after specified sub module' => array(
784 'after:some',
785 'some,modules',
786 'some,newModule,modules'
787 ),
788 'can add new sub module at the bottom if specified sub module to add before does not exist' => array(
789 'before:modules',
790 'some,otherModules',
791 'some,otherModules,newModule'
792 ),
793 'can add new sub module at the bottom if specified sub module to add after does not exist' => array(
794 'after:some',
795 'someOther,modules',
796 'someOther,modules,newModule'
797 ),
798 );
799 }
800
801 /**
802 * @test
803 * @dataProvider addModulePositionTestsDataProvider
804 */
805 public function addModuleCanAddModule($position, $existing, $expected) {
806 $mainModule = 'foobar';
807 $subModule = 'newModule';
808 if ($existing) {
809 $GLOBALS['TBE_MODULES'][$mainModule] = $existing;
810 }
811
812 ExtensionManagementUtility::addModule($mainModule, $subModule, $position);
813
814 $this->assertTrue(isset($GLOBALS['TBE_MODULES'][$mainModule]));
815 $this->assertEquals($expected, $GLOBALS['TBE_MODULES'][$mainModule]);
816 }
817
818 /////////////////////////////////////////
819 // Tests concerning createExtLocalconfCacheEntry
820 /////////////////////////////////////////
821 /**
822 * @test
823 */
824 public function createExtLocalconfCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtLocalconf() {
825 $extensionName = uniqid('foo');
826 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
827 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
828 $this->testFilesToDelete[] = $extLocalconfLocation;
829 $uniqueStringInLocalconf = uniqid('foo');
830 file_put_contents($extLocalconfLocation, "<?php\n\n" . $uniqueStringInLocalconf . "\n\n?>");
831 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
832 $mockCache = $this->getMock(
833 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
834 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
835 array(),
836 '',
837 FALSE
838 );
839 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
840 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
841 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInLocalconf), $this->anything());
842 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
843 }
844
845 /**
846 * @test
847 */
848 public function createExtLocalconfCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtLocalconfExists() {
849 $extensionName = uniqid('foo');
850 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
851 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
852 $mockCache = $this->getMock(
853 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
854 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
855 array(),
856 '',
857 FALSE
858 );
859 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
860 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
861 $mockCache->expects($this->once())
862 ->method('set')
863 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
864 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
865 }
866
867 /**
868 * @test
869 */
870 public function createExtLocalconfCacheEntryWritesCacheEntryWithNoTags() {
871 $mockCache = $this->getMock(
872 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
873 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
874 array(),
875 '',
876 FALSE
877 );
878 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
879 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
880 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
881 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
882 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
883 }
884
885 /////////////////////////////////////////
886 // Tests concerning getExtLocalconfCacheIdentifier
887 /////////////////////////////////////////
888 /**
889 * @test
890 */
891 public function getExtLocalconfCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
892 $prefix = 'ext_localconf_';
893 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtLocalconfCacheIdentifier();
894 $this->assertStringStartsWith($prefix, $identifier);
895 $sha1 = str_replace($prefix, '', $identifier);
896 $this->assertEquals(40, strlen($sha1));
897 }
898
899 /////////////////////////////////////////
900 // Tests concerning loadBaseTca
901 /////////////////////////////////////////
902
903 /**
904 * @test
905 */
906 public function loadBaseTcaDoesNotReadFromCacheIfCachingIsDenied() {
907 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
908 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
909 ExtensionManagementUtility::loadBaseTca(FALSE);
910 }
911
912 /**
913 * @test
914 */
915 public function loadBaseTcaRequiresCacheFileIfExistsAndCachingIsAllowed() {
916 $mockCache = $this->getMock(
917 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
918 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
919 array(),
920 '',
921 FALSE
922 );
923 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
924 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
925 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
926 $mockCache->expects($this->once())->method('get');
927 ExtensionManagementUtility::loadBaseTca(TRUE);
928 }
929
930 /**
931 * @test
932 */
933 public function loadBaseTcaCreatesCacheFileWithContentOfAnExtensionsConfigurationTcaPhpFile() {
934 $extensionName = uniqid('test_baseTca_');
935 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
936 $packagePath = $packageManager->getPackage($extensionName)->getPackagePath();
937 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath);
938 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/');
939 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/TCA/');
940 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
941 ExtensionManagementUtility::setPackageManager($packageManager);
942 $uniqueTableName = uniqid('table_name_');
943 $uniqueStringInTableConfiguration = uniqid('table_configuration_');
944 $tableConfiguration = '<?php return array(\'foo\' => \'' . $uniqueStringInTableConfiguration . '\'); ?>';
945 file_put_contents($packagePath . 'Configuration/TCA/' . $uniqueTableName . '.php', $tableConfiguration);
946 $mockCache = $this->getMock(
947 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
948 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
949 array(),
950 '',
951 FALSE
952 );
953 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
954 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
955 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
956 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTableConfiguration), $this->anything());
957 ExtensionManagementUtility::loadBaseTca(TRUE);
958 }
959
960 /**
961 * @test
962 */
963 public function loadBaseTcaWritesCacheEntryWithNoTags() {
964 $mockCache = $this->getMock(
965 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
966 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
967 array(),
968 '',
969 FALSE
970 );
971 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
972 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
973 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
974 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
975 ExtensionManagementUtility::loadBaseTca();
976 }
977
978 /////////////////////////////////////////
979 // Tests concerning getBaseTcaCacheIdentifier
980 /////////////////////////////////////////
981
982 /**
983 * @test
984 */
985 public function getBaseTcaCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
986 $prefix = 'tca_base_';
987 $identifier = ExtensionManagementUtilityAccessibleProxy::getBaseTcaCacheIdentifier();
988 $this->assertStringStartsWith($prefix, $identifier);
989 $sha1 = str_replace($prefix, '', $identifier);
990 $this->assertEquals(40, strlen($sha1));
991 }
992
993 /////////////////////////////////////////
994 // Tests concerning loadExtTables
995 /////////////////////////////////////////
996 /**
997 * @test
998 */
999 public function loadExtTablesDoesNotReadFromCacheIfCachingIsDenied() {
1000 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1001 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
1002 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
1003 ExtensionManagementUtility::loadExtLocalconf(FALSE);
1004 }
1005
1006 /**
1007 * @test
1008 */
1009 public function loadExtTablesRequiresCacheFileIfExistsAndCachingIsAllowed() {
1010 $mockCache = $this->getMock(
1011 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1012 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1013 array(),
1014 '',
1015 FALSE
1016 );
1017 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1018 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1019 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
1020 $mockCache->expects($this->once())->method('requireOnce');
1021 // Reset the internal cache access tracking variable of extMgm
1022 // This method is only in the ProxyClass!
1023 ExtensionManagementUtilityAccessibleProxy::resetExtTablesWasReadFromCacheOnceBoolean();
1024 ExtensionManagementUtility::loadExtTables(TRUE);
1025 }
1026
1027 /////////////////////////////////////////
1028 // Tests concerning createExtTablesCacheEntry
1029 /////////////////////////////////////////
1030 /**
1031 * @test
1032 */
1033 public function createExtTablesCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtTables() {
1034 $extensionName = uniqid('foo');
1035 $extTablesLocation = PATH_site . 'typo3temp/' . uniqid('test_ext_tables') . '.php';
1036 $this->testFilesToDelete[] = $extTablesLocation;
1037 $uniqueStringInTables = uniqid('foo');
1038 file_put_contents($extTablesLocation, "<?php\n\n$uniqueStringInTables\n\n?>");
1039 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1040 $extensionName => array(
1041 'ext_tables.php' => $extTablesLocation
1042 )
1043 );
1044 $mockCache = $this->getMock(
1045 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1046 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1047 array(),
1048 '',
1049 FALSE
1050 );
1051 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1052 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1053 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTables), $this->anything());
1054 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1055 }
1056
1057 /**
1058 * @test
1059 */
1060 public function createExtTablesCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtTablesExists() {
1061 $extensionName = uniqid('foo');
1062 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1063 $extensionName => array(),
1064 );
1065 $mockCache = $this->getMock(
1066 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1067 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1068 array(),
1069 '',
1070 FALSE
1071 );
1072 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1073 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1074 $mockCache->expects($this->once())
1075 ->method('set')
1076 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
1077 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1078 }
1079
1080 /**
1081 * @test
1082 */
1083 public function createExtTablesCacheEntryWritesCacheEntryWithNoTags() {
1084 $mockCache = $this->getMock(
1085 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1086 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1087 array(),
1088 '',
1089 FALSE
1090 );
1091 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1092 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1093 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1094 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
1095 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1096 }
1097
1098 /////////////////////////////////////////
1099 // Tests concerning getExtTablesCacheIdentifier
1100 /////////////////////////////////////////
1101 /**
1102 * @test
1103 */
1104 public function getExtTablesCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
1105 $prefix = 'ext_tables_';
1106 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtTablesCacheIdentifier();
1107 $this->assertStringStartsWith($prefix, $identifier);
1108 $sha1 = str_replace($prefix, '', $identifier);
1109 $this->assertEquals(40, strlen($sha1));
1110 }
1111
1112 /////////////////////////////////////////
1113 // Tests concerning removeCacheFiles
1114 /////////////////////////////////////////
1115 /**
1116 * @test
1117 */
1118 public function removeCacheFilesFlushesSystemCaches() {
1119 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('flushCachesInGroup'));
1120 $GLOBALS['typo3CacheManager']->expects($this->once())->method('flushCachesInGroup')->with('system');
1121 ExtensionManagementUtility::removeCacheFiles();
1122 }
1123
1124 /////////////////////////////////////////
1125 // Tests concerning loadNewTcaColumnsConfigFiles
1126 /////////////////////////////////////////
1127
1128 /**
1129 * @test
1130 * @expectedException \RuntimeException
1131 */
1132 public function loadNewTcaColumnsConfigFilesIncludesDefinedDynamicConfigFileIfNoColumnsExist() {
1133 $GLOBALS['TCA'] = array(
1134 'test' => array(
1135 'ctrl' => array(
1136 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1137 ),
1138 ),
1139 );
1140 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1141 }
1142
1143 /**
1144 * @test
1145 */
1146 public function loadNewTcaColumnsConfigFilesDoesNotIncludeFileIfColumnsExist() {
1147 $GLOBALS['TCA'] = array(
1148 'test' => array(
1149 'ctrl' => array(
1150 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1151 ),
1152 'columns' => array(
1153 'foo' => 'bar',
1154 ),
1155 ),
1156 );
1157 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1158 }
1159
1160 /////////////////////////////////////////
1161 // Tests concerning getExtensionVersion
1162 /////////////////////////////////////////
1163 /**
1164 * Data provider for negative getExtensionVersion() tests.
1165 *
1166 * @return array
1167 */
1168 public function getExtensionVersionFaultyDataProvider() {
1169 return array(
1170 array(''),
1171 array(0),
1172 array(new \stdClass()),
1173 array(TRUE)
1174 );
1175 }
1176
1177 /**
1178 * @test
1179 * @expectedException \InvalidArgumentException
1180 * @dataProvider getExtensionVersionFaultyDataProvider
1181 */
1182 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
1183 ExtensionManagementUtility::getExtensionVersion($key);
1184 }
1185
1186 /**
1187 * @test
1188 */
1189 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
1190 ExtensionManagementUtility::clearExtensionKeyMap();
1191 $uniqueSuffix = uniqid('test');
1192 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1193 $this->assertEquals('', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1194 }
1195
1196 /**
1197 * @test
1198 */
1199 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
1200 ExtensionManagementUtility::clearExtensionKeyMap();
1201 $className = uniqid('ExtensionManagementUtility');
1202 eval(
1203 'namespace ' . __NAMESPACE__ .';' .
1204 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
1205 ' public static function isLoaded() {' .
1206 ' return TRUE;' .
1207 ' }' .
1208 '}'
1209 );
1210 $className = __NAMESPACE__ . '\\' . $className;
1211 ExtensionManagementUtility::clearExtensionKeyMap();
1212 $uniqueSuffix = uniqid('test');
1213 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1214 $packageMetaData = $this->getMock('TYPO3\\Flow\\Package\\MetaData', array('getVersion'), array($extensionKey));
1215 $packageMetaData->expects($this->any())->method('getVersion')->will($this->returnValue('1.2.3'));
1216 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey, array('getPackagePath', 'getPackageKey', 'getPackageMetaData'));
1217 /** @var \PHPUnit_Framework_MockObject_MockObject $package */
1218 $package = $packageManager->getPackage($extensionKey);
1219 $package->expects($this->any())
1220 ->method('getPackageMetaData')
1221 ->will($this->returnValue($packageMetaData));
1222 ExtensionManagementUtility::setPackageManager($packageManager);
1223 $this->assertEquals('1.2.3', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1224 }
1225
1226 /////////////////////////////////////////
1227 // Tests concerning loadExtension
1228 /////////////////////////////////////////
1229 /**
1230 * @test
1231 * @expectedException \RuntimeException
1232 */
1233 public function loadExtensionThrowsExceptionIfExtensionIsLoaded() {
1234 $extensionKey = uniqid('test');
1235 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey);
1236 ExtensionManagementUtility::setPackageManager($packageManager);
1237 ExtensionManagementUtility::loadExtension($extensionKey);
1238 }
1239
1240 /////////////////////////////////////////
1241 // Tests concerning unloadExtension
1242 /////////////////////////////////////////
1243 /**
1244 * @test
1245 * @expectedException \RuntimeException
1246 */
1247 public function unloadExtensionThrowsExceptionIfExtensionIsNotLoaded() {
1248 $packageName = uniqid('foo');
1249 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('isPackageActive'));
1250 $packageManager->expects($this->once())
1251 ->method('isPackageActive')
1252 ->with($this->equalTo($packageName))
1253 ->will($this->returnValue(FALSE));
1254 ExtensionManagementUtility::setPackageManager($packageManager);
1255 ExtensionManagementUtility::unloadExtension($packageName);
1256 }
1257
1258 /**
1259 * @test
1260 */
1261 public function unloadExtensionCallsPackageManagerToDeactivatePackage() {
1262 $packageName = uniqid('foo');
1263 $packageManager = $this->getMock(
1264 'TYPO3\\CMS\\Core\\Package\\PackageManager',
1265 array('isPackageActive', 'deactivatePackage')
1266 );
1267 $packageManager->expects($this->any())
1268 ->method('isPackageActive')
1269 ->will($this->returnValue(TRUE));
1270 $packageManager->expects($this->once())
1271 ->method('deactivatePackage')
1272 ->with($packageName);
1273 ExtensionManagementUtility::setPackageManager($packageManager);
1274 ExtensionManagementUtility::unloadExtension($packageName);
1275 }
1276
1277 /////////////////////////////////////////
1278 // Tests concerning makeCategorizable
1279 /////////////////////////////////////////
1280 /**
1281 * @test
1282 */
1283 public function isMakeCategorizableAvailableInRegistryWithDefaultField() {
1284 $extensionKey = uniqid('extension');
1285 $tableName = uniqid('table');
1286 $GLOBALS['TCA'][$tableName] = array(
1287 'ctrl' => array(),
1288 'columns' => array()
1289 );
1290 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1291 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1292 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName);
1293 $registryMock->applyTca();
1294 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns']['categories']);
1295 }
1296
1297 /**
1298 * @test
1299 */
1300 public function isMakeCategorizableAvailableInRegistryWithSpecifictField() {
1301 $extensionKey = uniqid('extension');
1302 $tableName = uniqid('table');
1303 $fieldName = uniqid('field');
1304 $GLOBALS['TCA'][$tableName] = array(
1305 'ctrl' => array(),
1306 'columns' => array()
1307 );
1308 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1309 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1310 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName, $fieldName);
1311 $registryMock->applyTca();
1312 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns'][$fieldName]);
1313 }
1314
1315 }