[BUGFIX] Title field disappears after using 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 * Data provider for executePositionedStringInsertionTrimsCorrectCharacters
566 * @return array
567 */
568 public function executePositionedStringInsertionTrimsCorrectCharactersDataProvider() {
569 return array(
570 'normal characters' => array(
571 'tr0',
572 'tr0',
573 ),
574 'newlines' => array(
575 "test\n",
576 'test',
577 ),
578 'newlines with carriage return' => array(
579 "test\r\n",
580 'test',
581 ),
582 'tabs' => array(
583 "test\t",
584 'test',
585 ),
586 'commas' => array(
587 "test,",
588 'test',
589 ),
590 'multiple commas with trailing spaces' => array(
591 "test,,\t, \r\n",
592 'test',
593 ),
594 );
595 }
596
597 /**
598 * @test
599 * @dataProvider executePositionedStringInsertionTrimsCorrectCharactersDataProvider
600 */
601 public function executePositionedStringInsertionTrimsCorrectCharacters($string, $expectedResult) {
602 $extensionManagementUtility = $this->getAccessibleMock('\\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility', array('dummy'));
603 $string = $extensionManagementUtility->_call('executePositionedStringInsertion', $string, '');
604 $this->assertEquals($expectedResult, $string);
605 }
606
607 /////////////////////////////////////////
608 // Tests concerning addTcaSelectItem
609 /////////////////////////////////////////
610 /**
611 * @test
612 * @expectedException \InvalidArgumentException
613 */
614 public function addTcaSelectItemThrowsExceptionIfTableIsNotOfTypeString() {
615 ExtensionManagementUtility::addTcaSelectItem(array(), 'foo', array());
616 }
617
618 /**
619 * @test
620 * @expectedException \InvalidArgumentException
621 */
622 public function addTcaSelectItemThrowsExceptionIfFieldIsNotOfTypeString() {
623 ExtensionManagementUtility::addTcaSelectItem('foo', array(), array());
624 }
625
626 /**
627 * @test
628 * @expectedException \InvalidArgumentException
629 */
630 public function addTcaSelectItemThrowsExceptionIfRelativeToFieldIsNotOfTypeString() {
631 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), array());
632 }
633
634 /**
635 * @test
636 * @expectedException \InvalidArgumentException
637 */
638 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOfTypeString() {
639 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', array());
640 }
641
642 /**
643 * @test
644 * @expectedException \InvalidArgumentException
645 */
646 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOneOfValidKeywords() {
647 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', 'not allowed keyword');
648 }
649
650 /**
651 * @test
652 * @expectedException \RuntimeException
653 */
654 public function addTcaSelectItemThrowsExceptionIfFieldIsNotFoundInTca() {
655 $GLOBALS['TCA'] = array();
656 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array());
657 }
658
659 /**
660 * Data provider for addTcaSelectItemInsertsItemAtSpecifiedPosition
661 */
662 public function addTcaSelectItemDataProvider() {
663 // Every array splits into:
664 // - relativeToField
665 // - relativePosition
666 // - expectedResultArray
667 return array(
668 'add at end of array' => array(
669 '',
670 '',
671 array(
672 0 => array('firstElement'),
673 1 => array('matchMe'),
674 2 => array('thirdElement'),
675 3 => array('insertedElement')
676 )
677 ),
678 'replace element' => array(
679 'matchMe',
680 'replace',
681 array(
682 0 => array('firstElement'),
683 1 => array('insertedElement'),
684 2 => array('thirdElement')
685 )
686 ),
687 'add element after' => array(
688 'matchMe',
689 'after',
690 array(
691 0 => array('firstElement'),
692 1 => array('matchMe'),
693 2 => array('insertedElement'),
694 3 => array('thirdElement')
695 )
696 ),
697 'add element before' => array(
698 'matchMe',
699 'before',
700 array(
701 0 => array('firstElement'),
702 1 => array('insertedElement'),
703 2 => array('matchMe'),
704 3 => array('thirdElement')
705 )
706 ),
707 'add at end if relative position was not found' => array(
708 'notExistingItem',
709 'after',
710 array(
711 0 => array('firstElement'),
712 1 => array('matchMe'),
713 2 => array('thirdElement'),
714 3 => array('insertedElement')
715 )
716 )
717 );
718 }
719
720 /**
721 * @test
722 * @dataProvider addTcaSelectItemDataProvider
723 */
724 public function addTcaSelectItemInsertsItemAtSpecifiedPosition($relativeToField, $relativePosition, $expectedResultArray) {
725 $GLOBALS['TCA'] = array(
726 'testTable' => array(
727 'columns' => array(
728 'testField' => array(
729 'config' => array(
730 'items' => array(
731 '0' => array('firstElement'),
732 '1' => array('matchMe'),
733 2 => array('thirdElement')
734 )
735 )
736 )
737 )
738 )
739 );
740 ExtensionManagementUtility::addTcaSelectItem('testTable', 'testField', array('insertedElement'), $relativeToField, $relativePosition);
741 $this->assertEquals($expectedResultArray, $GLOBALS['TCA']['testTable']['columns']['testField']['config']['items']);
742 }
743
744 /////////////////////////////////////////
745 // Tests concerning loadExtLocalconf
746 /////////////////////////////////////////
747 /**
748 * @test
749 */
750 public function loadExtLocalconfDoesNotReadFromCacheIfCachingIsDenied() {
751 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
752 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
753 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
754 ExtensionManagementUtility::loadExtLocalconf(FALSE);
755 }
756
757 /**
758 * @test
759 */
760 public function loadExtLocalconfRequiresCacheFileIfExistsAndCachingIsAllowed() {
761 $mockCache = $this->getMock(
762 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
763 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
764 array(),
765 '',
766 FALSE
767 );
768 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
769 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
770 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
771 $mockCache->expects($this->once())->method('requireOnce');
772 ExtensionManagementUtility::loadExtLocalconf(TRUE);
773 }
774
775 /////////////////////////////////////////
776 // Tests concerning loadSingleExtLocalconfFiles
777 /////////////////////////////////////////
778 /**
779 * @test
780 * @expectedException \RuntimeException
781 */
782 public function loadSingleExtLocalconfFilesRequiresExtLocalconfFileRegisteredInGlobalTypo3LoadedExt() {
783 $extensionName = uniqid('foo');
784 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
785 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
786 file_put_contents($extLocalconfLocation, "<?php\n\nthrow new RuntimeException('', 1340559079);\n\n?>");
787 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
788 ExtensionManagementUtilityAccessibleProxy::loadSingleExtLocalconfFiles();
789 }
790
791
792 /////////////////////////////////////////
793 // Tests concerning addModule
794 /////////////////////////////////////////
795
796 /**
797 * Data provider for addModule tests
798 * @return array
799 */
800 public function addModulePositionTestsDataProvider() {
801 return array(
802 'can add new main module if none exists' => array(
803 'top',
804 '',
805 'newModule'
806 ),
807 'can add new sub module if no position specified' => array(
808 '',
809 'some,modules',
810 'some,modules,newModule'
811 ),
812 'can add new sub module to top of module' => array(
813 'top',
814 'some,modules',
815 'newModule,some,modules'
816 ),
817 'can add new sub module if bottom of module' => array(
818 'bottom',
819 'some,modules',
820 'some,modules,newModule'
821 ),
822 'can add new sub module before specified sub module' => array(
823 'before:modules',
824 'some,modules',
825 'some,newModule,modules'
826 ),
827 'can add new sub module after specified sub module' => array(
828 'after:some',
829 'some,modules',
830 'some,newModule,modules'
831 ),
832 'can add new sub module at the bottom if specified sub module to add before does not exist' => array(
833 'before:modules',
834 'some,otherModules',
835 'some,otherModules,newModule'
836 ),
837 'can add new sub module at the bottom if specified sub module to add after does not exist' => array(
838 'after:some',
839 'someOther,modules',
840 'someOther,modules,newModule'
841 ),
842 );
843 }
844
845 /**
846 * @test
847 * @dataProvider addModulePositionTestsDataProvider
848 */
849 public function addModuleCanAddModule($position, $existing, $expected) {
850 $mainModule = 'foobar';
851 $subModule = 'newModule';
852 if ($existing) {
853 $GLOBALS['TBE_MODULES'][$mainModule] = $existing;
854 }
855
856 ExtensionManagementUtility::addModule($mainModule, $subModule, $position);
857
858 $this->assertTrue(isset($GLOBALS['TBE_MODULES'][$mainModule]));
859 $this->assertEquals($expected, $GLOBALS['TBE_MODULES'][$mainModule]);
860 }
861
862 /////////////////////////////////////////
863 // Tests concerning createExtLocalconfCacheEntry
864 /////////////////////////////////////////
865 /**
866 * @test
867 */
868 public function createExtLocalconfCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtLocalconf() {
869 $extensionName = uniqid('foo');
870 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
871 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
872 $this->testFilesToDelete[] = $extLocalconfLocation;
873 $uniqueStringInLocalconf = uniqid('foo');
874 file_put_contents($extLocalconfLocation, "<?php\n\n" . $uniqueStringInLocalconf . "\n\n?>");
875 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
876 $mockCache = $this->getMock(
877 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
878 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
879 array(),
880 '',
881 FALSE
882 );
883 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
884 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
885 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInLocalconf), $this->anything());
886 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
887 }
888
889 /**
890 * @test
891 */
892 public function createExtLocalconfCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtLocalconfExists() {
893 $extensionName = uniqid('foo');
894 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
895 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
896 $mockCache = $this->getMock(
897 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
898 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
899 array(),
900 '',
901 FALSE
902 );
903 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
904 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
905 $mockCache->expects($this->once())
906 ->method('set')
907 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
908 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
909 }
910
911 /**
912 * @test
913 */
914 public function createExtLocalconfCacheEntryWritesCacheEntryWithNoTags() {
915 $mockCache = $this->getMock(
916 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
917 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
918 array(),
919 '',
920 FALSE
921 );
922 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
923 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
924 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
925 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
926 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
927 }
928
929 /////////////////////////////////////////
930 // Tests concerning getExtLocalconfCacheIdentifier
931 /////////////////////////////////////////
932 /**
933 * @test
934 */
935 public function getExtLocalconfCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
936 $prefix = 'ext_localconf_';
937 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtLocalconfCacheIdentifier();
938 $this->assertStringStartsWith($prefix, $identifier);
939 $sha1 = str_replace($prefix, '', $identifier);
940 $this->assertEquals(40, strlen($sha1));
941 }
942
943 /////////////////////////////////////////
944 // Tests concerning loadBaseTca
945 /////////////////////////////////////////
946
947 /**
948 * @test
949 */
950 public function loadBaseTcaDoesNotReadFromCacheIfCachingIsDenied() {
951 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
952 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
953 ExtensionManagementUtility::loadBaseTca(FALSE);
954 }
955
956 /**
957 * @test
958 */
959 public function loadBaseTcaRequiresCacheFileIfExistsAndCachingIsAllowed() {
960 $mockCache = $this->getMock(
961 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
962 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
963 array(),
964 '',
965 FALSE
966 );
967 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
968 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
969 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
970 $mockCache->expects($this->once())->method('get');
971 ExtensionManagementUtility::loadBaseTca(TRUE);
972 }
973
974 /**
975 * @test
976 */
977 public function loadBaseTcaCreatesCacheFileWithContentOfAnExtensionsConfigurationTcaPhpFile() {
978 $extensionName = uniqid('test_baseTca_');
979 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
980 $packagePath = $packageManager->getPackage($extensionName)->getPackagePath();
981 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath);
982 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/');
983 \TYPO3\CMS\Core\Utility\GeneralUtility::mkdir($packagePath . 'Configuration/TCA/');
984 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($packageManager);
985 ExtensionManagementUtility::setPackageManager($packageManager);
986 $uniqueTableName = uniqid('table_name_');
987 $uniqueStringInTableConfiguration = uniqid('table_configuration_');
988 $tableConfiguration = '<?php return array(\'foo\' => \'' . $uniqueStringInTableConfiguration . '\'); ?>';
989 file_put_contents($packagePath . 'Configuration/TCA/' . $uniqueTableName . '.php', $tableConfiguration);
990 $mockCache = $this->getMock(
991 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
992 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
993 array(),
994 '',
995 FALSE
996 );
997 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
998 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
999 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
1000 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTableConfiguration), $this->anything());
1001 ExtensionManagementUtility::loadBaseTca(TRUE);
1002 }
1003
1004 /**
1005 * @test
1006 */
1007 public function loadBaseTcaWritesCacheEntryWithNoTags() {
1008 $mockCache = $this->getMock(
1009 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1010 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1011 array(),
1012 '',
1013 FALSE
1014 );
1015 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1016 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1017 $mockCache->expects($this->once())->method('has')->will($this->returnValue(FALSE));
1018 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1019 ExtensionManagementUtility::loadBaseTca();
1020 }
1021
1022 /////////////////////////////////////////
1023 // Tests concerning getBaseTcaCacheIdentifier
1024 /////////////////////////////////////////
1025
1026 /**
1027 * @test
1028 */
1029 public function getBaseTcaCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
1030 $prefix = 'tca_base_';
1031 $identifier = ExtensionManagementUtilityAccessibleProxy::getBaseTcaCacheIdentifier();
1032 $this->assertStringStartsWith($prefix, $identifier);
1033 $sha1 = str_replace($prefix, '', $identifier);
1034 $this->assertEquals(40, strlen($sha1));
1035 }
1036
1037 /////////////////////////////////////////
1038 // Tests concerning loadExtTables
1039 /////////////////////////////////////////
1040 /**
1041 * @test
1042 */
1043 public function loadExtTablesDoesNotReadFromCacheIfCachingIsDenied() {
1044 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1045 $GLOBALS['typo3CacheManager']->expects($this->never())->method('getCache');
1046 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
1047 ExtensionManagementUtility::loadExtLocalconf(FALSE);
1048 }
1049
1050 /**
1051 * @test
1052 */
1053 public function loadExtTablesRequiresCacheFileIfExistsAndCachingIsAllowed() {
1054 $mockCache = $this->getMock(
1055 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1056 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1057 array(),
1058 '',
1059 FALSE
1060 );
1061 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1062 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1063 $mockCache->expects($this->any())->method('has')->will($this->returnValue(TRUE));
1064 $mockCache->expects($this->once())->method('requireOnce');
1065 // Reset the internal cache access tracking variable of extMgm
1066 // This method is only in the ProxyClass!
1067 ExtensionManagementUtilityAccessibleProxy::resetExtTablesWasReadFromCacheOnceBoolean();
1068 ExtensionManagementUtility::loadExtTables(TRUE);
1069 }
1070
1071 /////////////////////////////////////////
1072 // Tests concerning createExtTablesCacheEntry
1073 /////////////////////////////////////////
1074 /**
1075 * @test
1076 */
1077 public function createExtTablesCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtTables() {
1078 $extensionName = uniqid('foo');
1079 $extTablesLocation = PATH_site . 'typo3temp/' . uniqid('test_ext_tables') . '.php';
1080 $this->testFilesToDelete[] = $extTablesLocation;
1081 $uniqueStringInTables = uniqid('foo');
1082 file_put_contents($extTablesLocation, "<?php\n\n$uniqueStringInTables\n\n?>");
1083 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1084 $extensionName => array(
1085 'ext_tables.php' => $extTablesLocation
1086 )
1087 );
1088 $mockCache = $this->getMock(
1089 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1090 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1091 array(),
1092 '',
1093 FALSE
1094 );
1095 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1096 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1097 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTables), $this->anything());
1098 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1099 }
1100
1101 /**
1102 * @test
1103 */
1104 public function createExtTablesCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtTablesExists() {
1105 $extensionName = uniqid('foo');
1106 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1107 $extensionName => array(),
1108 );
1109 $mockCache = $this->getMock(
1110 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1111 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1112 array(),
1113 '',
1114 FALSE
1115 );
1116 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1117 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1118 $mockCache->expects($this->once())
1119 ->method('set')
1120 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
1121 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1122 }
1123
1124 /**
1125 * @test
1126 */
1127 public function createExtTablesCacheEntryWritesCacheEntryWithNoTags() {
1128 $mockCache = $this->getMock(
1129 'TYPO3\\CMS\\Core\\Cache\\Frontend\\AbstractFrontend',
1130 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1131 array(),
1132 '',
1133 FALSE
1134 );
1135 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('getCache'));
1136 $GLOBALS['typo3CacheManager']->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1137 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1138 $GLOBALS['TYPO3_LOADED_EXT'] = new \TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage(uniqid()));
1139 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1140 }
1141
1142 /////////////////////////////////////////
1143 // Tests concerning getExtTablesCacheIdentifier
1144 /////////////////////////////////////////
1145 /**
1146 * @test
1147 */
1148 public function getExtTablesCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix() {
1149 $prefix = 'ext_tables_';
1150 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtTablesCacheIdentifier();
1151 $this->assertStringStartsWith($prefix, $identifier);
1152 $sha1 = str_replace($prefix, '', $identifier);
1153 $this->assertEquals(40, strlen($sha1));
1154 }
1155
1156 /////////////////////////////////////////
1157 // Tests concerning removeCacheFiles
1158 /////////////////////////////////////////
1159 /**
1160 * @test
1161 */
1162 public function removeCacheFilesFlushesSystemCaches() {
1163 $GLOBALS['typo3CacheManager'] = $this->getMock('TYPO3\\CMS\\Core\\Cache\\CacheManager', array('flushCachesInGroup'));
1164 $GLOBALS['typo3CacheManager']->expects($this->once())->method('flushCachesInGroup')->with('system');
1165 ExtensionManagementUtility::removeCacheFiles();
1166 }
1167
1168 /////////////////////////////////////////
1169 // Tests concerning loadNewTcaColumnsConfigFiles
1170 /////////////////////////////////////////
1171
1172 /**
1173 * @test
1174 * @expectedException \RuntimeException
1175 */
1176 public function loadNewTcaColumnsConfigFilesIncludesDefinedDynamicConfigFileIfNoColumnsExist() {
1177 $GLOBALS['TCA'] = array(
1178 'test' => array(
1179 'ctrl' => array(
1180 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1181 ),
1182 ),
1183 );
1184 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1185 }
1186
1187 /**
1188 * @test
1189 */
1190 public function loadNewTcaColumnsConfigFilesDoesNotIncludeFileIfColumnsExist() {
1191 $GLOBALS['TCA'] = array(
1192 'test' => array(
1193 'ctrl' => array(
1194 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1195 ),
1196 'columns' => array(
1197 'foo' => 'bar',
1198 ),
1199 ),
1200 );
1201 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1202 }
1203
1204 /////////////////////////////////////////
1205 // Tests concerning getExtensionVersion
1206 /////////////////////////////////////////
1207 /**
1208 * Data provider for negative getExtensionVersion() tests.
1209 *
1210 * @return array
1211 */
1212 public function getExtensionVersionFaultyDataProvider() {
1213 return array(
1214 array(''),
1215 array(0),
1216 array(new \stdClass()),
1217 array(TRUE)
1218 );
1219 }
1220
1221 /**
1222 * @test
1223 * @expectedException \InvalidArgumentException
1224 * @dataProvider getExtensionVersionFaultyDataProvider
1225 */
1226 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
1227 ExtensionManagementUtility::getExtensionVersion($key);
1228 }
1229
1230 /**
1231 * @test
1232 */
1233 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
1234 ExtensionManagementUtility::clearExtensionKeyMap();
1235 $uniqueSuffix = uniqid('test');
1236 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1237 $this->assertEquals('', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1238 }
1239
1240 /**
1241 * @test
1242 */
1243 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
1244 ExtensionManagementUtility::clearExtensionKeyMap();
1245 $className = uniqid('ExtensionManagementUtility');
1246 eval(
1247 'namespace ' . __NAMESPACE__ .';' .
1248 'class ' . $className . ' extends \\TYPO3\\CMS\\Core\\Utility\\ExtensionManagementUtility {' .
1249 ' public static function isLoaded() {' .
1250 ' return TRUE;' .
1251 ' }' .
1252 '}'
1253 );
1254 $className = __NAMESPACE__ . '\\' . $className;
1255 ExtensionManagementUtility::clearExtensionKeyMap();
1256 $uniqueSuffix = uniqid('test');
1257 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1258 $packageMetaData = $this->getMock('TYPO3\\Flow\\Package\\MetaData', array('getVersion'), array($extensionKey));
1259 $packageMetaData->expects($this->any())->method('getVersion')->will($this->returnValue('1.2.3'));
1260 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey, array('getPackagePath', 'getPackageKey', 'getPackageMetaData'));
1261 /** @var \PHPUnit_Framework_MockObject_MockObject $package */
1262 $package = $packageManager->getPackage($extensionKey);
1263 $package->expects($this->any())
1264 ->method('getPackageMetaData')
1265 ->will($this->returnValue($packageMetaData));
1266 ExtensionManagementUtility::setPackageManager($packageManager);
1267 $this->assertEquals('1.2.3', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1268 }
1269
1270 /////////////////////////////////////////
1271 // Tests concerning loadExtension
1272 /////////////////////////////////////////
1273 /**
1274 * @test
1275 * @expectedException \RuntimeException
1276 */
1277 public function loadExtensionThrowsExceptionIfExtensionIsLoaded() {
1278 $extensionKey = uniqid('test');
1279 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey);
1280 ExtensionManagementUtility::setPackageManager($packageManager);
1281 ExtensionManagementUtility::loadExtension($extensionKey);
1282 }
1283
1284 /////////////////////////////////////////
1285 // Tests concerning unloadExtension
1286 /////////////////////////////////////////
1287 /**
1288 * @test
1289 * @expectedException \RuntimeException
1290 */
1291 public function unloadExtensionThrowsExceptionIfExtensionIsNotLoaded() {
1292 $packageName = uniqid('foo');
1293 $packageManager = $this->getMock('TYPO3\\CMS\\Core\\Package\\PackageManager', array('isPackageActive'));
1294 $packageManager->expects($this->once())
1295 ->method('isPackageActive')
1296 ->with($this->equalTo($packageName))
1297 ->will($this->returnValue(FALSE));
1298 ExtensionManagementUtility::setPackageManager($packageManager);
1299 ExtensionManagementUtility::unloadExtension($packageName);
1300 }
1301
1302 /**
1303 * @test
1304 */
1305 public function unloadExtensionCallsPackageManagerToDeactivatePackage() {
1306 $packageName = uniqid('foo');
1307 $packageManager = $this->getMock(
1308 'TYPO3\\CMS\\Core\\Package\\PackageManager',
1309 array('isPackageActive', 'deactivatePackage')
1310 );
1311 $packageManager->expects($this->any())
1312 ->method('isPackageActive')
1313 ->will($this->returnValue(TRUE));
1314 $packageManager->expects($this->once())
1315 ->method('deactivatePackage')
1316 ->with($packageName);
1317 ExtensionManagementUtility::setPackageManager($packageManager);
1318 ExtensionManagementUtility::unloadExtension($packageName);
1319 }
1320
1321 /////////////////////////////////////////
1322 // Tests concerning makeCategorizable
1323 /////////////////////////////////////////
1324 /**
1325 * @test
1326 */
1327 public function isMakeCategorizableAvailableInRegistryWithDefaultField() {
1328 $extensionKey = uniqid('extension');
1329 $tableName = uniqid('table');
1330 $GLOBALS['TCA'][$tableName] = array(
1331 'ctrl' => array(),
1332 'columns' => array()
1333 );
1334 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1335 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1336 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName);
1337 $registryMock->applyTca();
1338 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns']['categories']);
1339 }
1340
1341 /**
1342 * @test
1343 */
1344 public function isMakeCategorizableAvailableInRegistryWithSpecifictField() {
1345 $extensionKey = uniqid('extension');
1346 $tableName = uniqid('table');
1347 $fieldName = uniqid('field');
1348 $GLOBALS['TCA'][$tableName] = array(
1349 'ctrl' => array(),
1350 'columns' => array()
1351 );
1352 $registryMock = $this->getMock('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', array('dummy'));
1353 \TYPO3\CMS\Core\Utility\GeneralUtility::setSingletonInstance('TYPO3\\CMS\\Core\\Category\\CategoryRegistry', $registryMock);
1354 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName, $fieldName);
1355 $registryMock->applyTca();
1356 $this->assertNotEmpty($GLOBALS['TCA'][$tableName]['columns'][$fieldName]);
1357 }
1358
1359 }