[BUGFIX] Field is not added to TCA if part of fieldname exists
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / ExtensionManagementUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Cache\CacheManager;
18 use TYPO3\CMS\Core\Cache\Frontend\AbstractFrontend;
19 use TYPO3\CMS\Core\Category\CategoryRegistry;
20 use TYPO3\CMS\Core\Compatibility\LoadedExtensionsArray;
21 use TYPO3\CMS\Core\Package\MetaData;
22 use TYPO3\CMS\Core\Package\Package;
23 use TYPO3\CMS\Core\Package\PackageManager;
24 use TYPO3\CMS\Core\Tests\Unit\Utility\AccessibleProxies\ExtensionManagementUtilityAccessibleProxy;
25 use TYPO3\CMS\Core\Tests\UnitTestCase;
26 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
27 use TYPO3\CMS\Core\Utility\GeneralUtility;
28
29 /**
30 * Test case
31 */
32 class ExtensionManagementUtilityTest extends UnitTestCase
33 {
34 /**
35 * @var array A backup of registered singleton instances
36 */
37 protected $singletonInstances = array();
38
39 /**
40 * @var \TYPO3\CMS\Core\Package\PackageManager
41 */
42 protected $backUpPackageManager;
43
44 protected function setUp()
45 {
46 $this->singletonInstances = GeneralUtility::getSingletonInstances();
47 $this->backUpPackageManager = ExtensionManagementUtilityAccessibleProxy::getPackageManager();
48 $this->singletonInstances = GeneralUtility::getSingletonInstances();
49 }
50
51 protected function tearDown()
52 {
53 ExtensionManagementUtility::clearExtensionKeyMap();
54 ExtensionManagementUtilityAccessibleProxy::setPackageManager($this->backUpPackageManager);
55 ExtensionManagementUtilityAccessibleProxy::setCacheManager(null);
56 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($this->backUpPackageManager);
57 GeneralUtility::resetSingletonInstances($this->singletonInstances);
58 parent::tearDown();
59 }
60
61 /**
62 * @param string $packageKey
63 * @param array $packageMethods
64 * @return PackageManager|\PHPUnit_Framework_MockObject_MockObject
65 */
66 protected function createMockPackageManagerWithMockPackage($packageKey, $packageMethods = array('getPackagePath', 'getPackageKey'))
67 {
68 $packagePath = PATH_site . 'typo3temp/' . $packageKey . '/';
69 GeneralUtility::mkdir_deep($packagePath);
70 $this->testFilesToDelete[] = $packagePath;
71 $package = $this->getMockBuilder(Package::class)
72 ->disableOriginalConstructor()
73 ->setMethods($packageMethods)
74 ->getMock();
75 $packageManager = $this->getMock(
76 PackageManager::class,
77 array('isPackageActive', 'getPackage', 'getActivePackages')
78 );
79 $package->expects($this->any())
80 ->method('getPackagePath')
81 ->will($this->returnValue($packagePath));
82 $package->expects($this->any())
83 ->method('getPackageKey')
84 ->will($this->returnValue($packageKey));
85 $packageManager->expects($this->any())
86 ->method('isPackageActive')
87 ->will($this->returnValueMap(array(
88 array(null, false),
89 array($packageKey, true)
90 )));
91 $packageManager->expects($this->any())
92 ->method('getPackage')
93 ->with($this->equalTo($packageKey))
94 ->will($this->returnValue($package));
95 $packageManager->expects($this->any())
96 ->method('getActivePackages')
97 ->will($this->returnValue(array($packageKey => $package)));
98 return $packageManager;
99 }
100
101 ///////////////////////////////
102 // Tests concerning isLoaded
103 ///////////////////////////////
104 /**
105 * @test
106 */
107 public function isLoadedReturnsFalseIfExtensionIsNotLoadedAndExitIsDisabled()
108 {
109 $this->assertFalse(ExtensionManagementUtility::isLoaded($this->getUniqueId('foobar'), false));
110 }
111
112 /**
113 * @test
114 * @expectedException \BadFunctionCallException
115 */
116 public function isLoadedThrowsExceptionIfExtensionIsNotLoaded()
117 {
118 $this->assertFalse(ExtensionManagementUtility::isLoaded($this->getUniqueId('foobar'), true));
119 }
120
121 ///////////////////////////////
122 // Tests concerning extPath
123 ///////////////////////////////
124 /**
125 * @test
126 * @expectedException \BadFunctionCallException
127 */
128 public function extPathThrowsExceptionIfExtensionIsNotLoaded()
129 {
130 $packageName = $this->getUniqueId('foo');
131 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
132 $packageManager = $this->getMock(PackageManager::class, array('isPackageActive'));
133 $packageManager->expects($this->once())
134 ->method('isPackageActive')
135 ->with($this->equalTo($packageName))
136 ->will($this->returnValue(false));
137 ExtensionManagementUtility::setPackageManager($packageManager);
138 ExtensionManagementUtility::extPath($packageName);
139 }
140
141 /**
142 * @test
143 */
144 public function extPathAppendsScriptNameToPath()
145 {
146 $package = $this->getMockBuilder(Package::class)
147 ->disableOriginalConstructor()
148 ->setMethods(array('getPackagePath'))
149 ->getMock();
150 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
151 $packageManager = $this->getMock(PackageManager::class, array('isPackageActive', 'getPackage'));
152 $package->expects($this->once())
153 ->method('getPackagePath')
154 ->will($this->returnValue(PATH_site . 'foo/'));
155 $packageManager->expects($this->once())
156 ->method('isPackageActive')
157 ->with($this->equalTo('foo'))
158 ->will($this->returnValue(true));
159 $packageManager->expects($this->once())
160 ->method('getPackage')
161 ->with('foo')
162 ->will($this->returnValue($package));
163 ExtensionManagementUtility::setPackageManager($packageManager);
164 $this->assertSame(PATH_site . 'foo/bar.txt', ExtensionManagementUtility::extPath('foo', 'bar.txt'));
165 }
166
167 //////////////////////
168 // Utility functions
169 //////////////////////
170 /**
171 * Generates a basic TCA for a given table.
172 *
173 * @param string $table name of the table, must not be empty
174 * @return array generated TCA for the given table, will not be empty
175 */
176 private function generateTCAForTable($table)
177 {
178 $tca = array();
179 $tca[$table] = array();
180 $tca[$table]['columns'] = array(
181 'fieldA' => array(),
182 'fieldC' => array()
183 );
184 $tca[$table]['types'] = array(
185 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1'),
186 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1'),
187 'typeC' => array('showitem' => 'fieldC;;paletteD')
188 );
189 $tca[$table]['palettes'] = array(
190 'paletteA' => array('showitem' => 'fieldX, fieldX1, fieldY'),
191 'paletteB' => array('showitem' => 'fieldX, fieldX1, fieldY'),
192 'paletteC' => array('showitem' => 'fieldX, fieldX1, fieldY'),
193 'paletteD' => array('showitem' => 'fieldX, fieldX1, fieldY')
194 );
195 return $tca;
196 }
197
198 /**
199 * Data provider for getClassNamePrefixForExtensionKey.
200 *
201 * @return array
202 */
203 public function extensionKeyDataProvider()
204 {
205 return array(
206 'Without underscores' => array(
207 'testkey',
208 'tx_testkey'
209 ),
210 'With underscores' => array(
211 'this_is_a_test_extension',
212 'tx_thisisatestextension'
213 ),
214 'With user prefix and without underscores' => array(
215 'user_testkey',
216 'user_testkey'
217 ),
218 'With user prefix and with underscores' => array(
219 'user_test_key',
220 'user_testkey'
221 ),
222 );
223 }
224
225 /**
226 * @test
227 * @param string $extensionName
228 * @param string $expectedPrefix
229 * @dataProvider extensionKeyDataProvider
230 */
231 public function getClassNamePrefixForExtensionKey($extensionName, $expectedPrefix)
232 {
233 $this->assertSame($expectedPrefix, ExtensionManagementUtility::getCN($extensionName));
234 }
235
236 /////////////////////////////////////////////
237 // Tests concerning getExtensionKeyByPrefix
238 /////////////////////////////////////////////
239 /**
240 * @test
241 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
242 */
243 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey()
244 {
245 ExtensionManagementUtility::clearExtensionKeyMap();
246 $uniqueSuffix = $this->getUniqueId('test');
247 $extensionKey = 'tt_news' . $uniqueSuffix;
248 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
249 $package = $this->getMockBuilder(Package::class)
250 ->disableOriginalConstructor()
251 ->setMethods(array('getPackageKey'))
252 ->getMock();
253 $package->expects($this->exactly(2))
254 ->method('getPackageKey')
255 ->will($this->returnValue($extensionKey));
256 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
257 $packageManager = $this->getMock(PackageManager::class, array('getActivePackages'));
258 $packageManager->expects($this->once())
259 ->method('getActivePackages')
260 ->will($this->returnValue(array($extensionKey => $package)));
261 ExtensionManagementUtility::setPackageManager($packageManager);
262 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
263 }
264
265 /**
266 * @test
267 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
268 */
269 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey()
270 {
271 ExtensionManagementUtility::clearExtensionKeyMap();
272 $uniqueSuffix = $this->getUniqueId('test');
273 $extensionKey = 'kickstarter' . $uniqueSuffix;
274 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
275 $package = $this->getMockBuilder(Package::class)
276 ->disableOriginalConstructor()
277 ->setMethods(array('getPackageKey'))
278 ->getMock();
279 $package->expects($this->exactly(2))
280 ->method('getPackageKey')
281 ->will($this->returnValue($extensionKey));
282 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
283 $packageManager = $this->getMock(PackageManager::class, array('getActivePackages'));
284 $packageManager->expects($this->once())
285 ->method('getActivePackages')
286 ->will($this->returnValue(array($extensionKey => $package)));
287 ExtensionManagementUtility::setPackageManager($packageManager);
288 $this->assertEquals($extensionKey, ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
289 }
290
291 /**
292 * @test
293 * @see ExtensionManagementUtility::getExtensionKeyByPrefix
294 */
295 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse()
296 {
297 ExtensionManagementUtility::clearExtensionKeyMap();
298 $uniqueSuffix = $this->getUniqueId('test');
299 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
300 $this->assertFalse(ExtensionManagementUtility::getExtensionKeyByPrefix($extensionPrefix));
301 }
302
303 //////////////////////////////////////
304 // Tests concerning addToAllTCAtypes
305 //////////////////////////////////////
306 /**
307 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
308 *
309 * @test
310 * @see ExtensionManagementUtility::addToAllTCAtypes()
311 */
312 public function canAddFieldsToAllTCATypesBeforeExistingOnes()
313 {
314 $table = $this->getUniqueId('tx_coretest_table');
315 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
316 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
317 // Checking typeA:
318 $this->assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, newA, newB, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
319 // Checking typeB:
320 $this->assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, newA, newB, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
321 }
322
323 /**
324 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
325 *
326 * @test
327 * @see ExtensionManagementUtility::addToAllTCAtypes()
328 */
329 public function canAddFieldsToAllTCATypesAfterExistingOnes()
330 {
331 $table = $this->getUniqueId('tx_coretest_table');
332 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
333 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
334 // Checking typeA:
335 $this->assertEquals('fieldA, fieldB, fieldC;labelC, newA, newB, --palette--;;paletteC, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
336 // Checking typeB:
337 $this->assertEquals('fieldA, fieldB, fieldC;labelC, newA, newB, --palette--;;paletteC, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
338 }
339
340 /**
341 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
342 *
343 * @test
344 * @see ExtensionManagementUtility::addToAllTCAtypes()
345 */
346 public function canAddFieldsToAllTCATypesRespectsPalettes()
347 {
348 $table = $this->getUniqueId('tx_coretest_table');
349 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
350 $GLOBALS['TCA'][$table]['types']['typeD'] = ['showitem' => 'fieldY, --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:pages.palettes.standard;standard, fieldZ'];
351 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:--palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:pages.palettes.standard;standard');
352 // Checking typeD:
353 $this->assertEquals('fieldY, --palette--;LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:pages.palettes.standard;standard, newA, newB, fieldA, fieldZ', $GLOBALS['TCA'][$table]['types']['typeD']['showitem']);
354 }
355
356 /**
357 * Tests whether fields can be add to all TCA types and fields in pallets are respected.
358 *
359 * @test
360 * @see ExtensionManagementUtility::addToAllTCAtypes()
361 */
362 public function canAddFieldsToAllTCATypesRespectsPositionFieldInPalette()
363 {
364 $table = $this->getUniqueId('tx_coretest_table');
365 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
366 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldX1');
367 // Checking typeA:
368 $this->assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, newA, newB, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
369 }
370
371 /**
372 * Tests whether fields can be add to a TCA type before existing ones
373 *
374 * @test
375 * @see ExtensionManagementUtility::addToAllTCAtypes()
376 */
377 public function canAddFieldsToTCATypeBeforeExistingOnes()
378 {
379 $table = $this->getUniqueId('tx_coretest_table');
380 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
381 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
382 // Checking typeA:
383 $this->assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, newA, newB, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
384 // Checking typeB:
385 $this->assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
386 }
387
388 /**
389 * Tests whether fields can be add to a TCA type after existing ones
390 *
391 * @test
392 * @see ExtensionManagementUtility::addToAllTCAtypes()
393 */
394 public function canAddFieldsToTCATypeAfterExistingOnes()
395 {
396 $table = $this->getUniqueId('tx_coretest_table');
397 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
398 ExtensionManagementUtility::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
399 // Checking typeA:
400 $this->assertEquals('fieldA, fieldB, fieldC;labelC, newA, newB, --palette--;;paletteC, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
401 // Checking typeB:
402 $this->assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1', $GLOBALS['TCA'][$table]['types']['typeB']['showitem']);
403 }
404
405 /**
406 * @test
407 */
408 public function canAddFieldWithPartOfAlreadyExistingFieldname()
409 {
410 $table = $this->getUniqueId('tx_coretest_table');
411 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
412 ExtensionManagementUtility::addToAllTCAtypes($table, 'field', 'typeA', 'after:fieldD1');
413
414 $this->assertEquals('fieldA, fieldB, fieldC;labelC, --palette--;;paletteC, fieldC1, fieldD, fieldD1, field', $GLOBALS['TCA'][$table]['types']['typeA']['showitem']);
415 }
416
417 /**
418 * Test wheter replacing other TCA fields works as promissed
419 *
420 * @test
421 * @see ExtensionManagementUtility::addFieldsToAllPalettesOfField()
422 */
423 public function canAddFieldsToTCATypeAndReplaceExistingOnes()
424 {
425 $table = $this->getUniqueId('tx_coretest_table');
426 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
427 $typesBefore = $GLOBALS['TCA'][$table]['types'];
428 ExtensionManagementUtility::addToAllTCAtypes($table, 'fieldZ', '', 'replace:fieldX');
429 $this->assertEquals($typesBefore, $GLOBALS['TCA'][$table]['types'], 'It\'s wrong that the "types" array changes here - the replaced field is only on palettes');
430 // unchanged because the palette is not used
431 $this->assertEquals('fieldX, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
432 $this->assertEquals('fieldX, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
433 // changed
434 $this->assertEquals('fieldZ, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
435 $this->assertEquals('fieldZ, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
436 }
437
438 /**
439 * @test
440 */
441 public function addToAllTCAtypesReplacesExistingOnes()
442 {
443 $table = $this->getUniqueId('tx_coretest_table');
444 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
445 $typesBefore = $GLOBALS['TCA'][$table]['types'];
446 ExtensionManagementUtility::addToAllTCAtypes($table, 'fieldX, --palette--;;foo', '', 'replace:fieldX');
447 $this->assertEquals($typesBefore, $GLOBALS['TCA'][$table]['types'], 'It\'s wrong that the "types" array changes here - the replaced field is only on palettes');
448 // unchanged because the palette is not used
449 $this->assertEquals('fieldX, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
450 $this->assertEquals('fieldX, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']);
451 // changed
452 $this->assertEquals('fieldX, --palette--;;foo, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']);
453 $this->assertEquals('fieldX, --palette--;;foo, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']);
454 }
455
456 /**
457 * Tests whether fields can be added to a palette before existing elements.
458 *
459 * @test
460 * @see ExtensionManagementUtility::addFieldsToPalette()
461 */
462 public function canAddFieldsToPaletteBeforeExistingOnes()
463 {
464 $table = $this->getUniqueId('tx_coretest_table');
465 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
466 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
467 $this->assertEquals('fieldX, fieldX1, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
468 }
469
470 /**
471 * Tests whether fields can be added to a palette after existing elements.
472 *
473 * @test
474 * @see ExtensionManagementUtility::addFieldsToPalette()
475 */
476 public function canAddFieldsToPaletteAfterExistingOnes()
477 {
478 $table = $this->getUniqueId('tx_coretest_table');
479 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
480 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
481 $this->assertEquals('fieldX, newA, newB, fieldX1, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
482 }
483
484 /**
485 * Tests whether fields can be added to a palette after a not existing elements.
486 *
487 * @test
488 * @see ExtensionManagementUtility::addFieldsToPalette()
489 */
490 public function canAddFieldsToPaletteAfterNotExistingOnes()
491 {
492 $table = $this->getUniqueId('tx_coretest_table');
493 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
494 ExtensionManagementUtility::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . $this->getUniqueId('notExisting'));
495 $this->assertEquals('fieldX, fieldX1, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']);
496 }
497
498 /**
499 * @return array
500 */
501 public function removeDuplicatesForInsertionRemovesDuplicatesDataProvider()
502 {
503 return array(
504 'Simple' => array(
505 'field_b, field_d, field_c',
506 'field_a, field_b, field_c',
507 'field_d'
508 ),
509 'with linebreaks' => array(
510 'field_b, --linebreak--, field_d, --linebreak--, field_c',
511 'field_a, field_b, field_c',
512 '--linebreak--, field_d, --linebreak--'
513 ),
514 'with linebreaks in list and insertion list' => array(
515 'field_b, --linebreak--, field_d, --linebreak--, field_c',
516 'field_a, field_b, --linebreak--, field_c',
517 '--linebreak--, field_d, --linebreak--'
518 ),
519 );
520 }
521
522 /**
523 * @test
524 * @dataProvider removeDuplicatesForInsertionRemovesDuplicatesDataProvider
525 * @param $insertionList
526 * @param $list
527 * @param $expected
528 */
529 public function removeDuplicatesForInsertionRemovesDuplicates($insertionList, $list, $expected)
530 {
531 $result = ExtensionManagementUtilityAccessibleProxy::removeDuplicatesForInsertion($insertionList, $list);
532 $this->assertSame($expected, $result);
533 }
534
535 ///////////////////////////////////////////////////
536 // Tests concerning addFieldsToAllPalettesOfField
537 ///////////////////////////////////////////////////
538
539 /**
540 * @test
541 */
542 public function addFieldsToAllPalettesOfFieldDoesNotAddAnythingIfFieldIsNotRegisteredInColumns()
543 {
544 $GLOBALS['TCA'] = array(
545 'aTable' => array(
546 'types' => array(
547 'typeA' => array(
548 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
549 ),
550 ),
551 'palettes' => array(
552 'paletteA' => array(
553 'showitem' => 'fieldX, fieldY',
554 ),
555 ),
556 ),
557 );
558 $expected = $GLOBALS['TCA'];
559 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
560 'aTable',
561 'fieldA',
562 'newA'
563 );
564 $this->assertEquals($expected, $GLOBALS['TCA']);
565 }
566
567 /**
568 * @test
569 */
570 public function addFieldsToAllPalettesOfFieldAddsFieldsToPaletteAndSuppressesDuplicates()
571 {
572 $GLOBALS['TCA'] = array(
573 'aTable' => array(
574 'columns' => array(
575 'fieldA' => array(),
576 ),
577 'types' => array(
578 'typeA' => array(
579 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
580 ),
581 ),
582 'palettes' => array(
583 'paletteA' => array(
584 'showitem' => 'fieldX, fieldY',
585 ),
586 ),
587 ),
588 );
589 $expected = array(
590 'aTable' => array(
591 'columns' => array(
592 'fieldA' => array(),
593 ),
594 'types' => array(
595 'typeA' => array(
596 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
597 ),
598 ),
599 'palettes' => array(
600 'paletteA' => array(
601 'showitem' => 'fieldX, fieldY, dupeA',
602 ),
603 ),
604 ),
605 );
606 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
607 'aTable',
608 'fieldA',
609 'dupeA, dupeA' // Duplicate
610 );
611 $this->assertEquals($expected, $GLOBALS['TCA']);
612 }
613
614 /**
615 * @test
616 */
617 public function addFieldsToAllPalettesOfFieldDoesNotAddAFieldThatIsPartOfPaletteAlready()
618 {
619 $GLOBALS['TCA'] = array(
620 'aTable' => array(
621 'columns' => array(
622 'fieldA' => array(),
623 ),
624 'types' => array(
625 'typeA' => array(
626 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
627 ),
628 ),
629 'palettes' => array(
630 'paletteA' => array(
631 'showitem' => 'existingA',
632 ),
633 ),
634 ),
635 );
636 $expected = array(
637 'aTable' => array(
638 'columns' => array(
639 'fieldA' => array(),
640 ),
641 'types' => array(
642 'typeA' => array(
643 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
644 ),
645 ),
646 'palettes' => array(
647 'paletteA' => array(
648 'showitem' => 'existingA',
649 ),
650 ),
651 ),
652 );
653 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
654 'aTable',
655 'fieldA',
656 'existingA'
657 );
658 $this->assertEquals($expected, $GLOBALS['TCA']);
659 }
660
661 /**
662 * @test
663 */
664 public function addFieldsToAllPalettesOfFieldAddsFieldsToMultiplePalettes()
665 {
666 $GLOBALS['TCA'] = array(
667 'aTable' => array(
668 'columns' => array(
669 'fieldA' => array(),
670 ),
671 'types' => array(
672 'typeA' => array(
673 'showitem' => 'fieldA, --palette--;;palette1',
674 ),
675 'typeB' => array(
676 'showitem' => 'fieldA;aLabel, --palette--;;palette2',
677 ),
678 ),
679 'palettes' => array(
680 'palette1' => array(
681 'showitem' => 'fieldX',
682 ),
683 'palette2' => array(
684 'showitem' => 'fieldY',
685 ),
686 ),
687 ),
688 );
689 $expected = array(
690 'aTable' => array(
691 'columns' => array(
692 'fieldA' => array(),
693 ),
694 'types' => array(
695 'typeA' => array(
696 'showitem' => 'fieldA, --palette--;;palette1',
697 ),
698 'typeB' => array(
699 'showitem' => 'fieldA;aLabel, --palette--;;palette2',
700 ),
701 ),
702 'palettes' => array(
703 'palette1' => array(
704 'showitem' => 'fieldX, newA',
705 ),
706 'palette2' => array(
707 'showitem' => 'fieldY, newA',
708 ),
709 ),
710 ),
711 );
712 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
713 'aTable',
714 'fieldA',
715 'newA'
716 );
717 $this->assertEquals($expected, $GLOBALS['TCA']);
718 }
719
720 /**
721 * @test
722 */
723 public function addFieldsToAllPalettesOfFieldAddsMultipleFields()
724 {
725 $GLOBALS['TCA'] = array(
726 'aTable' => array(
727 'columns' => array(
728 'fieldA' => array(),
729 ),
730 'types' => array(
731 'typeA' => array(
732 'showitem' => 'fieldA, --palette--;;palette1',
733 ),
734 ),
735 'palettes' => array(
736 'palette1' => array(
737 'showitem' => 'fieldX',
738 ),
739 ),
740 ),
741 );
742 $expected = array(
743 'aTable' => array(
744 'columns' => array(
745 'fieldA' => array(),
746 ),
747 'types' => array(
748 'typeA' => array(
749 'showitem' => 'fieldA, --palette--;;palette1',
750 ),
751 ),
752 'palettes' => array(
753 'palette1' => array(
754 'showitem' => 'fieldX, newA, newB',
755 ),
756 ),
757 ),
758 );
759 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
760 'aTable',
761 'fieldA',
762 'newA, newB'
763 );
764 $this->assertEquals($expected, $GLOBALS['TCA']);
765 }
766
767 /**
768 * @test
769 */
770 public function addFieldsToAllPalettesOfFieldAddsBeforeExistingIfRequested()
771 {
772 $GLOBALS['TCA'] = array(
773 'aTable' => array(
774 'columns' => array(
775 'fieldA' => array(),
776 ),
777 'types' => array(
778 'typeA' => array(
779 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
780 ),
781 ),
782 'palettes' => array(
783 'paletteA' => array(
784 'showitem' => 'existingA, existingB',
785 ),
786 ),
787 ),
788 );
789 $expected = array(
790 'aTable' => array(
791 'columns' => array(
792 'fieldA' => array(),
793 ),
794 'types' => array(
795 'typeA' => array(
796 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
797 ),
798 ),
799 'palettes' => array(
800 'paletteA' => array(
801 'showitem' => 'existingA, newA, existingB',
802 ),
803 ),
804 ),
805 );
806 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
807 'aTable',
808 'fieldA',
809 'newA',
810 'before:existingB'
811 );
812 $this->assertEquals($expected, $GLOBALS['TCA']);
813 }
814
815 /**
816 * @test
817 */
818 public function addFieldsToAllPalettesOfFieldAddsFieldsAtEndIfBeforeRequestedDoesNotExist()
819 {
820 $GLOBALS['TCA'] = array(
821 'aTable' => array(
822 'columns' => array(
823 'fieldA' => array(),
824 ),
825 'types' => array(
826 'typeA' => array(
827 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
828 ),
829 ),
830 'palettes' => array(
831 'paletteA' => array(
832 'showitem' => 'fieldX, fieldY',
833 ),
834 ),
835 ),
836 );
837 $expected = array(
838 'aTable' => array(
839 'columns' => array(
840 'fieldA' => array(),
841 ),
842 'types' => array(
843 'typeA' => array(
844 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
845 ),
846 ),
847 'palettes' => array(
848 'paletteA' => array(
849 'showitem' => 'fieldX, fieldY, newA, newB',
850 ),
851 ),
852 ),
853 );
854 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
855 'aTable',
856 'fieldA',
857 'newA, newB',
858 'before:notExisting'
859 );
860 $this->assertEquals($expected, $GLOBALS['TCA']);
861 }
862
863 /**
864 * @test
865 */
866 public function addFieldsToAllPalettesOfFieldAddsAfterExistingIfRequested()
867 {
868 $GLOBALS['TCA'] = array(
869 'aTable' => array(
870 'columns' => array(
871 'fieldA' => array(),
872 ),
873 'types' => array(
874 'typeA' => array(
875 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
876 ),
877 ),
878 'palettes' => array(
879 'paletteA' => array(
880 'showitem' => 'existingA, existingB',
881 ),
882 ),
883 ),
884 );
885 $expected = array(
886 'aTable' => array(
887 'columns' => array(
888 'fieldA' => array(),
889 ),
890 'types' => array(
891 'typeA' => array(
892 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
893 ),
894 ),
895 'palettes' => array(
896 'paletteA' => array(
897 'showitem' => 'existingA, newA, existingB',
898 ),
899 ),
900 ),
901 );
902 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
903 'aTable',
904 'fieldA',
905 'newA',
906 'after:existingA'
907 );
908 $this->assertEquals($expected, $GLOBALS['TCA']);
909 }
910
911 /**
912 * @test
913 */
914 public function addFieldsToAllPalettesOfFieldAddsFieldsAtEndIfAfterRequestedDoesNotExist()
915 {
916 $GLOBALS['TCA'] = array(
917 'aTable' => array(
918 'columns' => array(
919 'fieldA' => array(),
920 ),
921 'types' => array(
922 'typeA' => array(
923 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
924 ),
925 ),
926 'palettes' => array(
927 'paletteA' => array(
928 'showitem' => 'existingA, existingB',
929 ),
930 ),
931 ),
932 );
933 $expected = array(
934 'aTable' => array(
935 'columns' => array(
936 'fieldA' => array(),
937 ),
938 'types' => array(
939 'typeA' => array(
940 'showitem' => 'fieldA;labelA, --palette--;;paletteA',
941 ),
942 ),
943 'palettes' => array(
944 'paletteA' => array(
945 'showitem' => 'existingA, existingB, newA, newB',
946 ),
947 ),
948 ),
949 );
950 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
951 'aTable',
952 'fieldA',
953 'newA, newB',
954 'after:notExistingA'
955 );
956 $this->assertEquals($expected, $GLOBALS['TCA']);
957 }
958
959 /**
960 * @test
961 */
962 public function addFieldsToAllPalettesOfFieldAddsNewPaletteIfFieldHasNoPaletteYet()
963 {
964 $GLOBALS['TCA'] = array(
965 'aTable' => array(
966 'columns' => array(
967 'fieldA' => array(),
968 ),
969 'types' => array(
970 'typeA' => array(
971 'showitem' => 'fieldA',
972 ),
973 ),
974 ),
975 );
976 $expected = array(
977 'aTable' => array(
978 'columns' => array(
979 'fieldA' => array(),
980 ),
981 'types' => array(
982 'typeA' => array(
983 'showitem' => 'fieldA, --palette--;;generatedFor-fieldA',
984 ),
985 ),
986 'palettes' => array(
987 'generatedFor-fieldA' => array(
988 'showitem' => 'newA',
989 ),
990 ),
991 ),
992 );
993 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
994 'aTable',
995 'fieldA',
996 'newA'
997 );
998 $this->assertEquals($expected, $GLOBALS['TCA']);
999 }
1000
1001 /**
1002 * @test
1003 */
1004 public function addFieldsToAllPalettesOfFieldAddsNewPaletteIfFieldHasNoPaletteYetAndKeepsExistingLabel()
1005 {
1006 $GLOBALS['TCA'] = array(
1007 'aTable' => array(
1008 'columns' => array(
1009 'fieldA' => array(),
1010 ),
1011 'types' => array(
1012 'typeA' => array(
1013 'showitem' => 'fieldA;labelA',
1014 ),
1015 ),
1016 ),
1017 );
1018 $expected = array(
1019 'aTable' => array(
1020 'columns' => array(
1021 'fieldA' => array(),
1022 ),
1023 'types' => array(
1024 'typeA' => array(
1025 'showitem' => 'fieldA;labelA, --palette--;;generatedFor-fieldA',
1026 ),
1027 ),
1028 'palettes' => array(
1029 'generatedFor-fieldA' => array(
1030 'showitem' => 'newA',
1031 ),
1032 ),
1033 ),
1034 );
1035 ExtensionManagementUtility::addFieldsToAllPalettesOfField(
1036 'aTable',
1037 'fieldA',
1038 'newA'
1039 );
1040 $this->assertEquals($expected, $GLOBALS['TCA']);
1041 }
1042
1043 ///////////////////////////////////////////////////
1044 // Tests concerning executePositionedStringInsertion
1045 ///////////////////////////////////////////////////
1046
1047 /**
1048 * Data provider for executePositionedStringInsertionTrimsCorrectCharacters
1049 * @return array
1050 */
1051 public function executePositionedStringInsertionTrimsCorrectCharactersDataProvider()
1052 {
1053 return array(
1054 'normal characters' => array(
1055 'tr0',
1056 'tr0',
1057 ),
1058 'newlines' => array(
1059 "test\n",
1060 'test',
1061 ),
1062 'newlines with carriage return' => array(
1063 "test\r\n",
1064 'test',
1065 ),
1066 'tabs' => array(
1067 "test\t",
1068 'test',
1069 ),
1070 'commas' => array(
1071 'test,',
1072 'test',
1073 ),
1074 'multiple commas with trailing spaces' => array(
1075 "test,,\t, \r\n",
1076 'test',
1077 ),
1078 );
1079 }
1080
1081 /**
1082 * @test
1083 * @dataProvider executePositionedStringInsertionTrimsCorrectCharactersDataProvider
1084 * @param $string
1085 * @param $expectedResult
1086 */
1087 public function executePositionedStringInsertionTrimsCorrectCharacters($string, $expectedResult)
1088 {
1089 $extensionManagementUtility = $this->getAccessibleMock(ExtensionManagementUtility::class, array('dummy'));
1090 $string = $extensionManagementUtility->_call('executePositionedStringInsertion', $string, '');
1091 $this->assertEquals($expectedResult, $string);
1092 }
1093
1094 /////////////////////////////////////////
1095 // Tests concerning addTcaSelectItem
1096 /////////////////////////////////////////
1097 /**
1098 * @test
1099 * @expectedException \InvalidArgumentException
1100 */
1101 public function addTcaSelectItemThrowsExceptionIfTableIsNotOfTypeString()
1102 {
1103 ExtensionManagementUtility::addTcaSelectItem(array(), 'foo', array());
1104 }
1105
1106 /**
1107 * @test
1108 * @expectedException \InvalidArgumentException
1109 */
1110 public function addTcaSelectItemThrowsExceptionIfFieldIsNotOfTypeString()
1111 {
1112 ExtensionManagementUtility::addTcaSelectItem('foo', array(), array());
1113 }
1114
1115 /**
1116 * @test
1117 * @expectedException \InvalidArgumentException
1118 */
1119 public function addTcaSelectItemThrowsExceptionIfRelativeToFieldIsNotOfTypeString()
1120 {
1121 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), array());
1122 }
1123
1124 /**
1125 * @test
1126 * @expectedException \InvalidArgumentException
1127 */
1128 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOfTypeString()
1129 {
1130 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', array());
1131 }
1132
1133 /**
1134 * @test
1135 * @expectedException \InvalidArgumentException
1136 */
1137 public function addTcaSelectItemThrowsExceptionIfRelativePositionIsNotOneOfValidKeywords()
1138 {
1139 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array(), 'foo', 'not allowed keyword');
1140 }
1141
1142 /**
1143 * @test
1144 * @expectedException \RuntimeException
1145 */
1146 public function addTcaSelectItemThrowsExceptionIfFieldIsNotFoundInTca()
1147 {
1148 $GLOBALS['TCA'] = array();
1149 ExtensionManagementUtility::addTcaSelectItem('foo', 'bar', array());
1150 }
1151
1152 /**
1153 * Data provider for addTcaSelectItemInsertsItemAtSpecifiedPosition
1154 */
1155 public function addTcaSelectItemDataProvider()
1156 {
1157 // Every array splits into:
1158 // - relativeToField
1159 // - relativePosition
1160 // - expectedResultArray
1161 return array(
1162 'add at end of array' => array(
1163 '',
1164 '',
1165 array(
1166 0 => array('firstElement'),
1167 1 => array('matchMe'),
1168 2 => array('thirdElement'),
1169 3 => array('insertedElement')
1170 )
1171 ),
1172 'replace element' => array(
1173 'matchMe',
1174 'replace',
1175 array(
1176 0 => array('firstElement'),
1177 1 => array('insertedElement'),
1178 2 => array('thirdElement')
1179 )
1180 ),
1181 'add element after' => array(
1182 'matchMe',
1183 'after',
1184 array(
1185 0 => array('firstElement'),
1186 1 => array('matchMe'),
1187 2 => array('insertedElement'),
1188 3 => array('thirdElement')
1189 )
1190 ),
1191 'add element before' => array(
1192 'matchMe',
1193 'before',
1194 array(
1195 0 => array('firstElement'),
1196 1 => array('insertedElement'),
1197 2 => array('matchMe'),
1198 3 => array('thirdElement')
1199 )
1200 ),
1201 'add at end if relative position was not found' => array(
1202 'notExistingItem',
1203 'after',
1204 array(
1205 0 => array('firstElement'),
1206 1 => array('matchMe'),
1207 2 => array('thirdElement'),
1208 3 => array('insertedElement')
1209 )
1210 )
1211 );
1212 }
1213
1214 /**
1215 * @test
1216 * @dataProvider addTcaSelectItemDataProvider
1217 * @param $relativeToField
1218 * @param $relativePosition
1219 * @param $expectedResultArray
1220 */
1221 public function addTcaSelectItemInsertsItemAtSpecifiedPosition($relativeToField, $relativePosition, $expectedResultArray)
1222 {
1223 $GLOBALS['TCA'] = array(
1224 'testTable' => array(
1225 'columns' => array(
1226 'testField' => array(
1227 'config' => array(
1228 'items' => array(
1229 '0' => array('firstElement'),
1230 '1' => array('matchMe'),
1231 2 => array('thirdElement')
1232 )
1233 )
1234 )
1235 )
1236 )
1237 );
1238 ExtensionManagementUtility::addTcaSelectItem('testTable', 'testField', array('insertedElement'), $relativeToField, $relativePosition);
1239 $this->assertEquals($expectedResultArray, $GLOBALS['TCA']['testTable']['columns']['testField']['config']['items']);
1240 }
1241
1242 /////////////////////////////////////////
1243 // Tests concerning loadExtLocalconf
1244 /////////////////////////////////////////
1245 /**
1246 * @test
1247 */
1248 public function loadExtLocalconfDoesNotReadFromCacheIfCachingIsDenied()
1249 {
1250 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1251 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1252 $mockCacheManager->expects($this->never())->method('getCache');
1253 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1254 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage($this->getUniqueId()));
1255 ExtensionManagementUtility::loadExtLocalconf(false);
1256 }
1257
1258 /**
1259 * @test
1260 */
1261 public function loadExtLocalconfRequiresCacheFileIfExistsAndCachingIsAllowed()
1262 {
1263 $mockCache = $this->getMock(
1264 AbstractFrontend::class,
1265 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1266 array(),
1267 '',
1268 false
1269 );
1270 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1271 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1272 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1273 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1274 $mockCache->expects($this->any())->method('has')->will($this->returnValue(true));
1275 $mockCache->expects($this->once())->method('requireOnce');
1276 ExtensionManagementUtility::loadExtLocalconf(true);
1277 }
1278
1279 /////////////////////////////////////////
1280 // Tests concerning loadSingleExtLocalconfFiles
1281 /////////////////////////////////////////
1282 /**
1283 * @test
1284 * @expectedException \RuntimeException
1285 */
1286 public function loadSingleExtLocalconfFilesRequiresExtLocalconfFileRegisteredInGlobalTypo3LoadedExt()
1287 {
1288 $extensionName = $this->getUniqueId('foo');
1289 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
1290 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
1291 file_put_contents($extLocalconfLocation, "<?php\n\nthrow new RuntimeException('', 1340559079);\n\n?>");
1292 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($packageManager);
1293 ExtensionManagementUtilityAccessibleProxy::loadSingleExtLocalconfFiles();
1294 }
1295
1296 /////////////////////////////////////////
1297 // Tests concerning addModule
1298 /////////////////////////////////////////
1299
1300 /**
1301 * Data provider for addModule tests
1302 * @return array
1303 */
1304 public function addModulePositionTestsDataProvider()
1305 {
1306 return array(
1307 'can add new main module if none exists' => array(
1308 'top',
1309 '',
1310 'newModule'
1311 ),
1312 'can add new sub module if no position specified' => array(
1313 '',
1314 'some,modules',
1315 'some,modules,newModule'
1316 ),
1317 'can add new sub module to top of module' => array(
1318 'top',
1319 'some,modules',
1320 'newModule,some,modules'
1321 ),
1322 'can add new sub module if bottom of module' => array(
1323 'bottom',
1324 'some,modules',
1325 'some,modules,newModule'
1326 ),
1327 'can add new sub module before specified sub module' => array(
1328 'before:modules',
1329 'some,modules',
1330 'some,newModule,modules'
1331 ),
1332 'can add new sub module after specified sub module' => array(
1333 'after:some',
1334 'some,modules',
1335 'some,newModule,modules'
1336 ),
1337 'can add new sub module at the bottom if specified sub module to add before does not exist' => array(
1338 'before:modules',
1339 'some,otherModules',
1340 'some,otherModules,newModule'
1341 ),
1342 'can add new sub module at the bottom if specified sub module to add after does not exist' => array(
1343 'after:some',
1344 'someOther,modules',
1345 'someOther,modules,newModule'
1346 ),
1347 );
1348 }
1349
1350 /**
1351 * @test
1352 * @dataProvider addModulePositionTestsDataProvider
1353 * @param $position
1354 * @param $existing
1355 * @param $expected
1356 */
1357 public function addModuleCanAddModule($position, $existing, $expected)
1358 {
1359 $mainModule = 'foobar';
1360 $subModule = 'newModule';
1361 if ($existing) {
1362 $GLOBALS['TBE_MODULES'][$mainModule] = $existing;
1363 }
1364
1365 ExtensionManagementUtility::addModule($mainModule, $subModule, $position);
1366
1367 $this->assertTrue(isset($GLOBALS['TBE_MODULES'][$mainModule]));
1368 $this->assertEquals($expected, $GLOBALS['TBE_MODULES'][$mainModule]);
1369 }
1370
1371 /////////////////////////////////////////
1372 // Tests concerning createExtLocalconfCacheEntry
1373 /////////////////////////////////////////
1374 /**
1375 * @test
1376 */
1377 public function createExtLocalconfCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtLocalconf()
1378 {
1379 $extensionName = $this->getUniqueId('foo');
1380 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
1381 $extLocalconfLocation = $packageManager->getPackage($extensionName)->getPackagePath() . 'ext_localconf.php';
1382 $uniqueStringInLocalconf = $this->getUniqueId('foo');
1383 file_put_contents($extLocalconfLocation, "<?php\n\n" . $uniqueStringInLocalconf . "\n\n?>");
1384 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($packageManager);
1385 $mockCache = $this->getMock(
1386 AbstractFrontend::class,
1387 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1388 array(),
1389 '',
1390 false
1391 );
1392 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1393 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1394 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1395 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1396 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInLocalconf), $this->anything());
1397 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
1398 }
1399
1400 /**
1401 * @test
1402 */
1403 public function createExtLocalconfCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtLocalconfExists()
1404 {
1405 $extensionName = $this->getUniqueId('foo');
1406 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
1407 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($packageManager);
1408 $mockCache = $this->getMock(
1409 AbstractFrontend::class,
1410 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1411 array(),
1412 '',
1413 false
1414 );
1415 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1416 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1417 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1418 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1419 $mockCache->expects($this->once())
1420 ->method('set')
1421 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
1422 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
1423 }
1424
1425 /**
1426 * @test
1427 */
1428 public function createExtLocalconfCacheEntryWritesCacheEntryWithNoTags()
1429 {
1430 $mockCache = $this->getMock(
1431 AbstractFrontend::class,
1432 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1433 array(),
1434 '',
1435 false
1436 );
1437 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1438 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1439 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1440 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1441 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1442 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage($this->getUniqueId()));
1443 ExtensionManagementUtilityAccessibleProxy::createExtLocalconfCacheEntry();
1444 }
1445
1446 /////////////////////////////////////////
1447 // Tests concerning getExtLocalconfCacheIdentifier
1448 /////////////////////////////////////////
1449 /**
1450 * @test
1451 */
1452 public function getExtLocalconfCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix()
1453 {
1454 $prefix = 'ext_localconf_';
1455 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtLocalconfCacheIdentifier();
1456 $this->assertStringStartsWith($prefix, $identifier);
1457 $sha1 = str_replace($prefix, '', $identifier);
1458 $this->assertEquals(40, strlen($sha1));
1459 }
1460
1461 /////////////////////////////////////////
1462 // Tests concerning loadBaseTca
1463 /////////////////////////////////////////
1464
1465 /**
1466 * @test
1467 */
1468 public function loadBaseTcaDoesNotReadFromCacheIfCachingIsDenied()
1469 {
1470 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1471 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1472 $mockCacheManager->expects($this->never())->method('getCache');
1473 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1474 ExtensionManagementUtilityAccessibleProxy::loadBaseTca(false);
1475 }
1476
1477 /**
1478 * @test
1479 */
1480 public function loadBaseTcaRequiresCacheFileIfExistsAndCachingIsAllowed()
1481 {
1482 $mockCache = $this->getMock(
1483 AbstractFrontend::class,
1484 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1485 array(),
1486 '',
1487 false
1488 );
1489 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1490 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1491 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1492 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1493 $mockCache->expects($this->any())->method('has')->will($this->returnValue(true));
1494 $mockCache->expects($this->once())->method('get')->willReturn('<?php ' . serialize(array('tca' => array(), 'categoryRegistry' => CategoryRegistry::getInstance())) . '?>');
1495 ExtensionManagementUtilityAccessibleProxy::loadBaseTca(true);
1496 }
1497
1498 /**
1499 * @test
1500 */
1501 public function loadBaseTcaCreatesCacheFileWithContentOfAnExtensionsConfigurationTcaPhpFile()
1502 {
1503 $extensionName = $this->getUniqueId('test_baseTca_');
1504 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionName);
1505 $packagePath = $packageManager->getPackage($extensionName)->getPackagePath();
1506 GeneralUtility::mkdir($packagePath);
1507 GeneralUtility::mkdir($packagePath . 'Configuration/');
1508 GeneralUtility::mkdir($packagePath . 'Configuration/TCA/');
1509 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($packageManager);
1510 ExtensionManagementUtility::setPackageManager($packageManager);
1511 $uniqueTableName = $this->getUniqueId('table_name_');
1512 $uniqueStringInTableConfiguration = $this->getUniqueId('table_configuration_');
1513 $tableConfiguration = '<?php return array(\'foo\' => \'' . $uniqueStringInTableConfiguration . '\'); ?>';
1514 file_put_contents($packagePath . 'Configuration/TCA/' . $uniqueTableName . '.php', $tableConfiguration);
1515 $mockCache = $this->getMock(
1516 AbstractFrontend::class,
1517 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1518 array(),
1519 '',
1520 false
1521 );
1522 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1523 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1524 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1525 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1526 $mockCache->expects($this->once())->method('has')->will($this->returnValue(false));
1527 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTableConfiguration), $this->anything());
1528 ExtensionManagementUtility::loadBaseTca(true);
1529 }
1530
1531 /**
1532 * @test
1533 */
1534 public function loadBaseTcaWritesCacheEntryWithNoTags()
1535 {
1536 $mockCache = $this->getMock(
1537 AbstractFrontend::class,
1538 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1539 array(),
1540 '',
1541 false
1542 );
1543 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1544 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1545 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1546 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1547 $mockCache->expects($this->once())->method('has')->will($this->returnValue(false));
1548 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1549 ExtensionManagementUtilityAccessibleProxy::loadBaseTca();
1550 }
1551
1552 /////////////////////////////////////////
1553 // Tests concerning getBaseTcaCacheIdentifier
1554 /////////////////////////////////////////
1555
1556 /**
1557 * @test
1558 */
1559 public function getBaseTcaCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix()
1560 {
1561 $prefix = 'tca_base_';
1562 $identifier = ExtensionManagementUtilityAccessibleProxy::getBaseTcaCacheIdentifier();
1563 $this->assertStringStartsWith($prefix, $identifier);
1564 $sha1 = str_replace($prefix, '', $identifier);
1565 $this->assertEquals(40, strlen($sha1));
1566 }
1567
1568 /////////////////////////////////////////
1569 // Tests concerning loadExtTables
1570 /////////////////////////////////////////
1571 /**
1572 * @test
1573 */
1574 public function loadExtTablesDoesNotReadFromCacheIfCachingIsDenied()
1575 {
1576 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1577 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1578 $mockCacheManager->expects($this->never())->method('getCache');
1579 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1580 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage($this->getUniqueId()));
1581 ExtensionManagementUtility::loadExtLocalconf(false);
1582 }
1583
1584 /**
1585 * @test
1586 */
1587 public function loadExtTablesRequiresCacheFileIfExistsAndCachingIsAllowed()
1588 {
1589 $mockCache = $this->getMock(
1590 AbstractFrontend::class,
1591 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1592 array(),
1593 '',
1594 false
1595 );
1596 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1597 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1598 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1599 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1600 $mockCache->expects($this->any())->method('has')->will($this->returnValue(true));
1601 $mockCache->expects($this->once())->method('requireOnce');
1602 // Reset the internal cache access tracking variable of extMgm
1603 // This method is only in the ProxyClass!
1604 ExtensionManagementUtilityAccessibleProxy::resetExtTablesWasReadFromCacheOnceBoolean();
1605 ExtensionManagementUtility::loadExtTables(true);
1606 }
1607
1608 /////////////////////////////////////////
1609 // Tests concerning createExtTablesCacheEntry
1610 /////////////////////////////////////////
1611 /**
1612 * @test
1613 */
1614 public function createExtTablesCacheEntryWritesCacheEntryWithContentOfLoadedExtensionExtTables()
1615 {
1616 $extensionName = $this->getUniqueId('foo');
1617 $extTablesLocation = PATH_site . 'typo3temp/' . $this->getUniqueId('test_ext_tables') . '.php';
1618 $this->testFilesToDelete[] = $extTablesLocation;
1619 $uniqueStringInTables = $this->getUniqueId('foo');
1620 file_put_contents($extTablesLocation, "<?php\n\n$uniqueStringInTables\n\n?>");
1621 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1622 $extensionName => array(
1623 'ext_tables.php' => $extTablesLocation
1624 )
1625 );
1626 $mockCache = $this->getMock(
1627 AbstractFrontend::class,
1628 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1629 array(),
1630 '',
1631 false
1632 );
1633 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1634 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1635 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1636 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1637 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->stringContains($uniqueStringInTables), $this->anything());
1638 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1639 }
1640
1641 /**
1642 * @test
1643 */
1644 public function createExtTablesCacheEntryWritesCacheEntryWithExtensionContentOnlyIfExtTablesExists()
1645 {
1646 $extensionName = $this->getUniqueId('foo');
1647 $GLOBALS['TYPO3_LOADED_EXT'] = array(
1648 $extensionName => array(),
1649 );
1650 $mockCache = $this->getMock(
1651 AbstractFrontend::class,
1652 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1653 array(),
1654 '',
1655 false
1656 );
1657 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1658 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1659 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1660 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1661 $mockCache->expects($this->once())
1662 ->method('set')
1663 ->with($this->anything(), $this->logicalNot($this->stringContains($extensionName)), $this->anything());
1664 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1665 }
1666
1667 /**
1668 * @test
1669 */
1670 public function createExtTablesCacheEntryWritesCacheEntryWithNoTags()
1671 {
1672 $mockCache = $this->getMock(
1673 AbstractFrontend::class,
1674 array('getIdentifier', 'set', 'get', 'getByTag', 'has', 'remove', 'flush', 'flushByTag', 'requireOnce'),
1675 array(),
1676 '',
1677 false
1678 );
1679 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1680 $mockCacheManager = $this->getMock(CacheManager::class, array('getCache'));
1681 $mockCacheManager->expects($this->any())->method('getCache')->will($this->returnValue($mockCache));
1682 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1683 $mockCache->expects($this->once())->method('set')->with($this->anything(), $this->anything(), $this->equalTo(array()));
1684 $GLOBALS['TYPO3_LOADED_EXT'] = new LoadedExtensionsArray($this->createMockPackageManagerWithMockPackage($this->getUniqueId()));
1685 ExtensionManagementUtilityAccessibleProxy::createExtTablesCacheEntry();
1686 }
1687
1688 /////////////////////////////////////////
1689 // Tests concerning getExtTablesCacheIdentifier
1690 /////////////////////////////////////////
1691 /**
1692 * @test
1693 */
1694 public function getExtTablesCacheIdentifierCreatesSha1WithFourtyCharactersAndPrefix()
1695 {
1696 $prefix = 'ext_tables_';
1697 $identifier = ExtensionManagementUtilityAccessibleProxy::getExtTablesCacheIdentifier();
1698 $this->assertStringStartsWith($prefix, $identifier);
1699 $sha1 = str_replace($prefix, '', $identifier);
1700 $this->assertEquals(40, strlen($sha1));
1701 }
1702
1703 /////////////////////////////////////////
1704 // Tests concerning removeCacheFiles
1705 /////////////////////////////////////////
1706 /**
1707 * @test
1708 */
1709 public function removeCacheFilesFlushesSystemCaches()
1710 {
1711 /** @var CacheManager|\PHPUnit_Framework_MockObject_MockObject $mockCacheManager */
1712 $mockCacheManager = $this->getMock(CacheManager::class, array('flushCachesInGroup'));
1713 $mockCacheManager->expects($this->once())->method('flushCachesInGroup')->with('system');
1714 ExtensionManagementUtilityAccessibleProxy::setCacheManager($mockCacheManager);
1715 ExtensionManagementUtility::removeCacheFiles();
1716 }
1717
1718 /////////////////////////////////////////
1719 // Tests concerning loadNewTcaColumnsConfigFiles
1720 /////////////////////////////////////////
1721
1722 /**
1723 * @test
1724 * @expectedException \RuntimeException
1725 */
1726 public function loadNewTcaColumnsConfigFilesIncludesDefinedDynamicConfigFileIfNoColumnsExist()
1727 {
1728 $GLOBALS['TCA'] = array(
1729 'test' => array(
1730 'ctrl' => array(
1731 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1732 ),
1733 ),
1734 );
1735 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1736 }
1737
1738 /**
1739 * @test
1740 */
1741 public function loadNewTcaColumnsConfigFilesDoesNotIncludeFileIfColumnsExist()
1742 {
1743 $GLOBALS['TCA'] = array(
1744 'test' => array(
1745 'ctrl' => array(
1746 'dynamicConfigFile' => __DIR__ . '/Fixtures/RuntimeException.php'
1747 ),
1748 'columns' => array(
1749 'foo' => 'bar',
1750 ),
1751 ),
1752 );
1753 ExtensionManagementUtility::loadNewTcaColumnsConfigFiles();
1754 }
1755
1756 /////////////////////////////////////////
1757 // Tests concerning getExtensionVersion
1758 /////////////////////////////////////////
1759 /**
1760 * Data provider for negative getExtensionVersion() tests.
1761 *
1762 * @return array
1763 */
1764 public function getExtensionVersionFaultyDataProvider()
1765 {
1766 return array(
1767 array(''),
1768 array(0),
1769 array(new \stdClass()),
1770 array(true)
1771 );
1772 }
1773
1774 /**
1775 * @test
1776 * @expectedException \InvalidArgumentException
1777 * @dataProvider getExtensionVersionFaultyDataProvider
1778 * @param $key
1779 * @throws \TYPO3\CMS\Core\Package\Exception
1780 */
1781 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key)
1782 {
1783 ExtensionManagementUtility::getExtensionVersion($key);
1784 }
1785
1786 /**
1787 * @test
1788 */
1789 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString()
1790 {
1791 ExtensionManagementUtility::clearExtensionKeyMap();
1792 $uniqueSuffix = $this->getUniqueId('test');
1793 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1794 $this->assertEquals('', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1795 }
1796
1797 /**
1798 * @test
1799 */
1800 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion()
1801 {
1802 ExtensionManagementUtility::clearExtensionKeyMap();
1803 $uniqueSuffix = $this->getUniqueId('test');
1804 $extensionKey = 'unloadedextension' . $uniqueSuffix;
1805 $packageMetaData = $this->getMock(MetaData::class, array('getVersion'), array($extensionKey));
1806 $packageMetaData->expects($this->any())->method('getVersion')->will($this->returnValue('1.2.3'));
1807 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey, array('getPackagePath', 'getPackageKey', 'getPackageMetaData'));
1808 /** @var \PHPUnit_Framework_MockObject_MockObject $package */
1809 $package = $packageManager->getPackage($extensionKey);
1810 $package->expects($this->any())
1811 ->method('getPackageMetaData')
1812 ->will($this->returnValue($packageMetaData));
1813 ExtensionManagementUtility::setPackageManager($packageManager);
1814 $this->assertEquals('1.2.3', ExtensionManagementUtility::getExtensionVersion($extensionKey));
1815 }
1816
1817 /////////////////////////////////////////
1818 // Tests concerning loadExtension
1819 /////////////////////////////////////////
1820 /**
1821 * @test
1822 * @expectedException \RuntimeException
1823 */
1824 public function loadExtensionThrowsExceptionIfExtensionIsLoaded()
1825 {
1826 $extensionKey = $this->getUniqueId('test');
1827 $packageManager = $this->createMockPackageManagerWithMockPackage($extensionKey);
1828 ExtensionManagementUtility::setPackageManager($packageManager);
1829 ExtensionManagementUtility::loadExtension($extensionKey);
1830 }
1831
1832 /////////////////////////////////////////
1833 // Tests concerning unloadExtension
1834 /////////////////////////////////////////
1835 /**
1836 * @test
1837 * @expectedException \RuntimeException
1838 */
1839 public function unloadExtensionThrowsExceptionIfExtensionIsNotLoaded()
1840 {
1841 $packageName = $this->getUniqueId('foo');
1842 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
1843 $packageManager = $this->getMock(PackageManager::class, array('isPackageActive'));
1844 $packageManager->expects($this->once())
1845 ->method('isPackageActive')
1846 ->with($this->equalTo($packageName))
1847 ->will($this->returnValue(false));
1848 ExtensionManagementUtility::setPackageManager($packageManager);
1849 ExtensionManagementUtility::unloadExtension($packageName);
1850 }
1851
1852 /**
1853 * @test
1854 */
1855 public function unloadExtensionCallsPackageManagerToDeactivatePackage()
1856 {
1857 $packageName = $this->getUniqueId('foo');
1858 /** @var PackageManager|\PHPUnit_Framework_MockObject_MockObject $packageManager */
1859 $packageManager = $this->getMock(
1860 PackageManager::class,
1861 array('isPackageActive', 'deactivatePackage')
1862 );
1863 $packageManager->expects($this->any())
1864 ->method('isPackageActive')
1865 ->will($this->returnValue(true));
1866 $packageManager->expects($this->once())
1867 ->method('deactivatePackage')
1868 ->with($packageName);
1869 ExtensionManagementUtility::setPackageManager($packageManager);
1870 ExtensionManagementUtility::unloadExtension($packageName);
1871 }
1872
1873 /////////////////////////////////////////
1874 // Tests concerning makeCategorizable
1875 /////////////////////////////////////////
1876 /**
1877 * @test
1878 */
1879 public function doesMakeCategorizableCallsTheCategoryRegistryWithDefaultFieldName()
1880 {
1881 $extensionKey = $this->getUniqueId('extension');
1882 $tableName = $this->getUniqueId('table');
1883
1884 /** @var CategoryRegistry|\PHPUnit_Framework_MockObject_MockObject $registryMock */
1885 $registryMock = $this->getMock(CategoryRegistry::class);
1886 $registryMock->expects($this->once())->method('add')->with($extensionKey, $tableName, 'categories', array());
1887 GeneralUtility::setSingletonInstance(CategoryRegistry::class, $registryMock);
1888 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName);
1889 }
1890
1891 /**
1892 * @test
1893 */
1894 public function doesMakeCategorizableCallsTheCategoryRegistryWithFieldName()
1895 {
1896 $extensionKey = $this->getUniqueId('extension');
1897 $tableName = $this->getUniqueId('table');
1898 $fieldName = $this->getUniqueId('field');
1899
1900 /** @var CategoryRegistry|\PHPUnit_Framework_MockObject_MockObject $registryMock */
1901 $registryMock = $this->getMock(CategoryRegistry::class);
1902 $registryMock->expects($this->once())->method('add')->with($extensionKey, $tableName, $fieldName, array());
1903 GeneralUtility::setSingletonInstance(CategoryRegistry::class, $registryMock);
1904 ExtensionManagementUtility::makeCategorizable($extensionKey, $tableName, $fieldName);
1905 }
1906
1907 ///////////////////////////////
1908 // Tests concerning addPlugin
1909 ///////////////////////////////
1910
1911 /**
1912 * @test
1913 */
1914 public function addPluginSetsTcaCorrectlyForGivenExtKeyAsParameter()
1915 {
1916 $extKey = 'indexed_search';
1917 $GLOBALS['TYPO3_LOADED_EXT'] = array();
1918 $GLOBALS['TYPO3_LOADED_EXT'][$extKey]['ext_icon'] = 'foo.gif';
1919 $expectedTCA = array(
1920 array(
1921 'label',
1922 $extKey,
1923 'EXT:' . $extKey . '/foo.gif'
1924 )
1925 );
1926 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'] = array();
1927 ExtensionManagementUtility::addPlugin(array('label', $extKey), 'list_type', $extKey);
1928 $this->assertEquals($expectedTCA, $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items']);
1929 }
1930
1931 /**
1932 * @test
1933 */
1934 public function addPluginSetsTcaCorrectlyForGivenExtKeyAsGlobal()
1935 {
1936 $extKey = 'indexed_search';
1937 $GLOBALS['TYPO3_LOADED_EXT'] = array();
1938 $GLOBALS['TYPO3_LOADED_EXT'][$extKey]['ext_icon'] = 'foo.gif';
1939 $GLOBALS['_EXTKEY'] = $extKey;
1940 $expectedTCA = array(
1941 array(
1942 'label',
1943 $extKey,
1944 'EXT:' . $extKey . '/foo.gif'
1945 )
1946 );
1947 $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items'] = array();
1948 ExtensionManagementUtility::addPlugin(array('label', $extKey));
1949 $this->assertEquals($expectedTCA, $GLOBALS['TCA']['tt_content']['columns']['list_type']['config']['items']);
1950 }
1951
1952 /**
1953 * @test
1954 * @expectedException \RuntimeException
1955 */
1956 public function addPluginThrowsExceptionForMissingExtkey()
1957 {
1958 ExtensionManagementUtility::addPlugin('test');
1959 }
1960 }