2b3976b42d073a3b9d834211e9377837db7c8b7b
[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 // Tests concerning addModule
749 /////////////////////////////////////////
750
751 /**
752 * @test
753 */
754 public function newSubmoduleCanBeAddedToTopOfModule() {
755 $mainModule = 'foobar';
756 $subModule = 'newModule';
757 $GLOBALS['TBE_MODULES'][$mainModule] = 'some,modules';
758
759 ExtensionManagementUtility::addModule($mainModule, $subModule, 'top');
760
761 $this->assertEquals('newModule,some,modules', $GLOBALS['TBE_MODULES'][$mainModule]);
762 }
763
764 /**
765 * @test
766 */
767 public function newSubmoduleCanBeAddedToBottomOfModule() {
768 $mainModule = 'foobar';
769 $subModule = 'newModule';
770 $GLOBALS['TBE_MODULES'][$mainModule] = 'some,modules';
771
772 ExtensionManagementUtility::addModule($mainModule, $subModule, 'bottom');
773
774 $this->assertEquals('some,modules,newModule', $GLOBALS['TBE_MODULES'][$mainModule]);
775 }
776
777 /**
778 * @test
779 */
780 public function newSubmoduleCanBeAddedAfterSpecifiedSubmodule() {
781 $mainModule = 'foobar';
782 $subModule = 'newModule';
783 $GLOBALS['TBE_MODULES'][$mainModule] = 'some,modules';
784
785 ExtensionManagementUtility::addModule($mainModule, $subModule, 'after:some');
786
787 $this->assertEquals('some,newModule,modules', $GLOBALS['TBE_MODULES'][$mainModule]);
788 }
789
790 /**
791 * @test
792 */
793 public function newSubmoduleCanBeAddedBeforeSpecifiedSubmodule() {
794 $mainModule = 'foobar';
795 $subModule = 'newModule';
796 $GLOBALS['TBE_MODULES'][$mainModule] = 'some,modules';
797
798 ExtensionManagementUtility::addModule($mainModule, $subModule, 'before:modules');
799
800 $this->assertEquals('some,newModule,modules', $GLOBALS['TBE_MODULES'][$mainModule]);
801 }
802
803 /////////////////////////////////////////
804 // Tests concerning createExtLocalconfCacheEntry
805 /////////////////////////////////////////
806 /**
807 * @test
808 */
809 public function createExtLocalconfCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtLocalconf() {
810 $extensionName = uniqid('foo');
811 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
812 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
813 $this->testFilesToDelete[] = $extLocalconfLocation;
814 $uniqueStringInLocalconf = uniqid('foo');
815 file_put_contents($extLocalconfLocation, "<?php\n\n" . $uniqueStringInLocalconf . "\n\n?>");
816 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
817 $mockCache = $this->getMock(
818 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
819 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
820 array(),
821 '',
822 FALSE
823 );
824 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
825 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
826 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInLocalconf), $this->anything());
827 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
828 }
829
830 /**
831 * @test
832 */
833 public function createExtLocalconfCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtLocalconfExists() {
834 $extensionName = uniqid('foo');
835 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
836 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
837 $mockCache = $this->getMock(
838 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
839 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
840 array(),
841 '',
842 FALSE
843 );
844 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
845 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
846 $mockCache->expects($this->once())
847 ->method('set')
848 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
849 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
850 }
851
852 /**
853 * @test
854 */
855 public function createExtLocalconfCacheEntryWritesCacheEntryWithNoTags() {
856 $mockCache = $this->getMock(
857 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
858 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
859 array(),
860 '',
861 FALSE
862 );
863 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
864 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
865 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
866 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
867 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
868 }
869
870 /////////////////////////////////////////
871 // Tests concerning getExtLocalconfCacheIdentifier
872 /////////////////////////////////////////
873 /**
874 * @test
875 */
876 public function getExtLocalconfCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
877 $prefix = 'ext_localconf_';
878 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtLocalconfCacheIdentifier();
879 $this->assertStringStartsWith($prefix, $identifier);
880 $sha1 = str_replace($prefix, '', $identifier);
881 $this->assertEquals(40, strlen($sha1));
882 }
883
884 /////////////////////////////////////////
885 // Tests concerning loadBaseTca
886 /////////////////////////////////////////
887
888 /**
889 * @test
890 */
891 public function loadBaseTcaDoesNotReadFromCacheIfCachingIsDenied() {
892 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
893 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
894 ExtensionManagementUtility::loadBaseTca(FALSE);
895 }
896
897 /**
898 * @test
899 */
900 public function loadBaseTcaRequiresCacheFileIfExistsAndCachingIsAllowed() {
901 $mockCache = $this->getMock(
902 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
903 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
904 array(),
905 '',
906 FALSE
907 );
908 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
909 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
910 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
911 $mockCache->expects($this->once())->method('get');
912 ExtensionManagementUtility::loadBaseTca(TRUE);
913 }
914
915 /**
916 * @test
917 */
918 public function loadBaseTcaCreatesCacheFileWithContentOfAnExtensionsConfigurationTcaPhpFile() {
919 $extensionName = uniqid('test_baseTca_');
920 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
921 $packagePath = $packageManager->getPackage($extensionName)->getPackagePath();
922 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath);
923 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/');
924 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/TCA/');
925 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
926 ExtensionManagementUtility::setPackageManager($packageManager);
927 $uniqueTableName = uniqid('table_name_');
928 $uniqueStringInTableConfiguration = uniqid('table_configuration_');
929 $tableConfiguration = '<?php return array(\'foo\' => \'' . $uniqueStringInTableConfiguration . '\'); ?>';
930 file_put_contents($packagePath . 'Configuration/TCA/' . $uniqueTableName . '.php', $tableConfiguration);
931 $mockCache = $this->getMock(
932 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
933 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
934 array(),
935 '',
936 FALSE
937 );
938 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
939 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
940 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
941 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTableConfiguration), $this->anything());
942 ExtensionManagementUtility::loadBaseTca(TRUE);
943 }
944
945 /**
946 * @test
947 */
948 public function loadBaseTcaWritesCacheEntryWithNoTags() {
949 $mockCache = $this->getMock(
950 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
951 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
952 array(),
953 '',
954 FALSE
955 );
956 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
957 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
958 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
959 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
960 ExtensionManagementUtility::loadBaseTca();
961 }
962
963 /////////////////////////////////////////
964 // Tests concerning getBaseTcaCacheIdentifier
965 /////////////////////////////////////////
966
967 /**
968 * @test
969 */
970 public function getBaseTcaCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
971 $prefix = 'tca_base_';
972 $identifier = ExtensionManagementUtilityAccessibleProxy::getBaseTcaCacheIdentifier();
973 $this->assertStringStartsWith($prefix, $identifier);
974 $sha1 = str_replace($prefix, '', $identifier);
975 $this->assertEquals(40, strlen($sha1));
976 }
977
978 /////////////////////////////////////////
979 // Tests concerning loadExtTables
980 /////////////////////////////////////////
981 /**
982 * @test
983 */
984 public function loadExtTablesDoesNotReadFromCacheIfCachingIsDenied() {
985 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
986 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
987 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
988 ExtensionManagementUtility::loadExtLocalconf(FALSE);
989 }
990
991 /**
992 * @test
993 */
994 public function loadExtTablesRequiresCacheFileIfExistsAndCachingIsAllowed() {
995 $mockCache = $this->getMock(
996 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
997 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
998 array(),
999 '',
1000 FALSE
1001 );
1002 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1003 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1004 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
1005 $mockCache->expects($this->once())->method('requireOnce');
1006 // Reset the internal cache access tracking variable of extMgm
1007 // This method is only in the ProxyClass!
1008 ExtensionManagementUtilityAccessibleProxy::resetExtTablesWasReadFromCacheOnceBoolean();
1009 ExtensionManagementUtility::loadExtTables(TRUE);
1010 }
1011
1012 /////////////////////////////////////////
1013 // Tests concerning createExtTablesCacheEntry
1014 /////////////////////////////////////////
1015 /**
1016 * @test
1017 */
1018 public function createExtTablesCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtTables() {
1019 $extensionName = uniqid('foo');
1020 $extTablesLocation = PATH_site . 'typo3temp/' . uniqid('test_ext_tables') . '.php';
1021 $this->testFilesToDelete[] = $extTablesLocation;
1022 $uniqueStringInTables = uniqid('foo');
1023 file_put_contents($extTablesLocation, "<?php\n\n$uniqueStringInTables\n\n?>");
1024 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1025 $extensionName => array(
1026 'ext_tables.php' => $extTablesLocation
1027 )
1028 );
1029 $mockCache = $this->getMock(
1030 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1031 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1032 array(),
1033 '',
1034 FALSE
1035 );
1036 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1037 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1038 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTables), $this->anything());
1039 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1040 }
1041
1042 /**
1043 * @test
1044 */
1045 public function createExtTablesCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtTablesExists() {
1046 $extensionName = uniqid('foo');
1047 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1048 $extensionName => array(),
1049 );
1050 $mockCache = $this->getMock(
1051 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1052 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1053 array(),
1054 '',
1055 FALSE
1056 );
1057 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1058 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1059 $mockCache->expects($this->once())
1060 ->method('set')
1061 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
1062 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1063 }
1064
1065 /**
1066 * @test
1067 */
1068 public function createExtTablesCacheEntryWritesCacheEntryWithNoTags() {
1069 $mockCache = $this->getMock(
1070 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1071 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1072 array(),
1073 '',
1074 FALSE
1075 );
1076 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1077 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1078 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1079 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
1080 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1081 }
1082
1083 /////////////////////////////////////////
1084 // Tests concerning getExtTablesCacheIdentifier
1085 /////////////////////////////////////////
1086 /**
1087 * @test
1088 */
1089 public function getExtTablesCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
1090 $prefix = 'ext_tables_';
1091 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtTablesCacheIdentifier();
1092 $this->assertStringStartsWith($prefix, $identifier);
1093 $sha1 = str_replace($prefix, '', $identifier);
1094 $this->assertEquals(40, strlen($sha1));
1095 }
1096
1097 /////////////////////////////////////////
1098 // Tests concerning removeCacheFiles
1099 /////////////////////////////////////////
1100 /**
1101 * @test
1102 */
1103 public function removeCacheFilesFlushesSystemCaches() {
1104 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('flushCachesInGroup'));
1105 $GLOBALS['typo3CacheManager']->expects($this->once())->method('flushCachesInGroup')->with('system');
1106 ExtensionManagementUtility::removeCacheFiles();
1107 }
1108
1109 /////////////////////////////////////////
1110 // Tests concerning loadNewTcaColumnsConfigFiles
1111 /////////////////////////////////////////
1112
1113 /**
1114 * @test
1115 * @expectedException \RuntimeException
1116 */
1117 public function loadNewTcaColumnsConfigFilesIncludesDefinedDynamicConfigFileIfNoColumnsExist() {
1118 $GLOBALS['TCA'] = array(
1119 'test' => array(
1120 'ctrl' => array(
1121 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1122 ),
1123 ),
1124 );
1125 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1126 }
1127
1128 /**
1129 * @test
1130 */
1131 public function loadNewTcaColumnsConfigFilesDoesNotIncludeFileIfColumnsExist() {
1132 $GLOBALS['TCA'] = array(
1133 'test' => array(
1134 'ctrl' => array(
1135 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1136 ),
1137 'columns' => array(
1138 'foo' => 'bar',
1139 ),
1140 ),
1141 );
1142 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1143 }
1144
1145 /////////////////////////////////////////
1146 // Tests concerning getExtensionVersion
1147 /////////////////////////////////////////
1148 /**
1149 * Data provider for negative getExtensionVersion() tests.
1150 *
1151 * @return array
1152 */
1153 public function getExtensionVersionFaultyDataProvider() {
1154 return array(
1155 array(''),
1156 array(0),
1157 array(new \stdClass()),
1158 array(TRUE)
1159 );
1160 }
1161
1162 /**
1163 * @test
1164 * @expectedException \InvalidArgumentException
1165 * @dataProvider getExtensionVersionFaultyDataProvider
1166 */
1167 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
1168 ExtensionManagementUtility::getExtensionVersion($key);
1169 }
1170
1171 /**
1172 * @test
1173 */
1174 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
1175 ExtensionManagementUtility::clearExtensionKeyMap();
1176 $uniqueSuffix = uniqid('test');
1177 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1178 $this->assertEquals('', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1179 }
1180
1181 /**
1182 * @test
1183 */
1184 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
1185 ExtensionManagementUtility::clearExtensionKeyMap();
1186 $className = uniqid('ExtensionManagementUtility');
1187 eval(
1188 'namespace ' . __NAMESPACE__ .';' .
1189 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
1190 ' public static function isLoaded() {' .
1191 ' return TRUE;' .
1192 ' }' .
1193 '}'
1194 );
1195 $className = __NAMESPACE__ . '\\' . $className;
1196 ExtensionManagementUtility::clearExtensionKeyMap();
1197 $uniqueSuffix = uniqid('test');
1198 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1199 $packageMetaData = $this->getMock('TYPO3\\Flow\\Package\\MetaData', array('getVersion'), array($extensionKey));
1200 $packageMetaData->expects($this->any())->method('getVersion')->will($this->returnValue('1.2.3'));
1201 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey, array('getPackagePath', 'getPackageKey', 'getPackageMetaData'));
1202 /** @var \PHPUnit_Framework_MockObject_MockObject $package */
1203 $package = $packageManager->getPackage($extensionKey);
1204 $package->expects($this->any())
1205 ->method('getPackageMetaData')
1206 ->will($this->returnValue($packageMetaData));
1207 ExtensionManagementUtility::setPackageManager($packageManager);
1208 $this->assertEquals('1.2.3', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1209 }
1210
1211 /////////////////////////////////////////
1212 // Tests concerning loadExtension
1213 /////////////////////////////////////////
1214 /**
1215 * @test
1216 * @expectedException \RuntimeException
1217 */
1218 public function loadExtensionThrowsExceptionIfExtensionIsLoaded() {
1219 $extensionKey = uniqid('test');
1220 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey);
1221 ExtensionManagementUtility::setPackageManager($packageManager);
1222 ExtensionManagementUtility::loadExtension($extensionKey);
1223 }
1224
1225 /////////////////////////////////////////
1226 // Tests concerning unloadExtension
1227 /////////////////////////////////////////
1228 /**
1229 * @test
1230 * @expectedException \RuntimeException
1231 */
1232 public function unloadExtensionThrowsExceptionIfExtensionIsNotLoaded() {
1233 $packageName = uniqid('foo');
1234 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('isPackageActive'));
1235 $packageManager->expects($this->once())
1236 ->method('isPackageActive')
1237 ->with($this->equalTo($packageName))
1238 ->will($this->returnValue(FALSE));
1239 ExtensionManagementUtility::setPackageManager($packageManager);
1240 ExtensionManagementUtility::unloadExtension($packageName);
1241 }
1242
1243 /**
1244 * @test
1245 */
1246 public function unloadExtensionCallsPackageManagerToDeactivatePackage() {
1247 $packageName = uniqid('foo');
1248 $packageManager = $this->getMock(
1249 'TYPO3\\CMS\\Core\\Package\\PackageManager',
1250 array('isPackageActive', 'deactivatePackage')
1251 );
1252 $packageManager->expects($this->any())
1253 ->method('isPackageActive')
1254 ->will($this->returnValue(TRUE));
1255 $packageManager->expects($this->once())
1256 ->method('deactivatePackage')
1257 ->with($packageName);
1258 ExtensionManagementUtility::setPackageManager($packageManager);
1259 ExtensionManagementUtility::unloadExtension($packageName);
1260 }
1261
1262 /////////////////////////////////////////
1263 // Tests concerning makeCategorizable
1264 /////////////////////////////////////////
1265 /**
1266 * @test
1267 */
1268 public function isMakeCategorizableAvailableInRegistryWithDefaultField() {
1269 $extensionKey = uniqid('extension');
1270 $tableName = uniqid('table');
1271 $GLOBALS['TCA'][$tableName] = array(
1272 'ctrl' => array(),
1273 'columns' => array()
1274 );
1275 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1276 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1277 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName);
1278 $registryMock->applyTca();
1279 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns']['categories']);
1280 }
1281
1282 /**
1283 * @test
1284 */
1285 public function isMakeCategorizableAvailableInRegistryWithSpecifictField() {
1286 $extensionKey = uniqid('extension');
1287 $tableName = uniqid('table');
1288 $fieldName = uniqid('field');
1289 $GLOBALS['TCA'][$tableName] = array(
1290 'ctrl' => array(),
1291 'columns' => array()
1292 );
1293 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1294 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1295 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName, $fieldName);
1296 $registryMock->applyTca();
1297 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns'][$fieldName]);
1298 }
1299
1300 }