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