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