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