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