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