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