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