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