Fixed bug #16847: Use Tx_Phpunit_Service_TestFinder for finding the path of the Core...
[Packages/TYPO3.CMS.git] / tests / t3lib / t3lib_extmgmTest.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009-2010 Oliver Hader <oliver@typo3.org>
6 * All rights reserved
7 *
8 * This script is part of the TYPO3 project. The TYPO3 project is
9 * free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * The GNU General Public License can be found at
15 * http://www.gnu.org/copyleft/gpl.html.
16 *
17 * This script is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * This copyright notice MUST APPEAR in all copies of the script!
23 ***************************************************************/
24
25 /**
26 * Testcase for class t3lib_extMgm
27 *
28 * @author Oliver Hader <oliver@typo3.org>
29 * @author Oliver Klee <typo3-coding@oliverklee.de>
30 *
31 * @package TYPO3
32 * @subpackage t3lib
33 */
34 class t3lib_extmgmTest extends tx_phpunit_testcase {
35 /**
36 * backup of defined GLOBALS
37 *
38 * @var array
39 */
40 protected $globals = array();
41
42 public function setUp() {
43 $this->globals = array(
44 'TYPO3_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
45 'TCA' => serialize($GLOBALS['TCA']),
46 );
47 }
48
49 public function tearDown() {
50 t3lib_extMgm::clearExtensionKeyMap();
51
52 foreach ($this->globals as $key => $value) {
53 $GLOBALS[$key] = unserialize($value);
54 }
55 }
56
57
58 //////////////////////
59 // Utility functions
60 //////////////////////
61
62 /**
63 * Generates a basic TCA for a given table.
64 *
65 * @param string $table name of the table, must not be empty
66 * @return array generated TCA for the given table, will not be empty
67 */
68 private function generateTCAForTable($table) {
69 $tca = array();
70 $tca[$table] = array();
71 $tca[$table]['columns'] = array(
72 'fieldA' => array(),
73 'fieldC' => array(),
74 );
75 $tca[$table]['types'] = array(
76 'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
77 'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
78 'typeC' => array('showitem' => 'fieldC;;paletteD'),
79 );
80 $tca[$table]['palettes'] = array(
81 'paletteA' => array('showitem' => 'fieldX, fieldY'),
82 'paletteB' => array('showitem' => 'fieldX, fieldY'),
83 'paletteC' => array('showitem' => 'fieldX, fieldY'),
84 'paletteD' => array('showitem' => 'fieldX, fieldY'),
85 );
86
87 return $tca;
88 }
89
90 /**
91 * Returns the fixtures path for this testcase relative to PATH_site.
92 *
93 * @return string the fixtures path for this testcase, will not be empty
94 */
95 private function determineFixturesPath() {
96 return t3lib_div::makeInstance('Tx_Phpunit_Service_TestFinder')
97 ->getRelativeCoreTestsPath() . 't3lib/fixtures/';
98 }
99
100
101 /////////////////////////////////////////////
102 // Tests concerning getExtensionKeyByPrefix
103 /////////////////////////////////////////////
104
105 /**
106 * @test
107 * @see t3lib_extMgm::getExtensionKeyByPrefix
108 */
109 public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
110 t3lib_extMgm::clearExtensionKeyMap();
111
112 $uniqueSuffix = uniqid('test');
113 $extensionKey = 'tt_news' . $uniqueSuffix;
114 $extensionPrefix = 'tx_ttnews' . $uniqueSuffix;
115
116 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
117
118 $this->assertEquals(
119 $extensionKey,
120 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
121 );
122 }
123
124 /**
125 * @test
126 * @see t3lib_extMgm::getExtensionKeyByPrefix
127 */
128 public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
129 t3lib_extMgm::clearExtensionKeyMap();
130
131 $uniqueSuffix = uniqid('test');
132 $extensionKey = 'kickstarter' . $uniqueSuffix;
133 $extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;
134
135 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();
136
137 $this->assertEquals(
138 $extensionKey,
139 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
140 );
141 }
142
143 /**
144 * @test
145 * @see t3lib_extMgm::getExtensionKeyByPrefix
146 */
147 public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse(){
148 t3lib_extMgm::clearExtensionKeyMap();
149
150 $uniqueSuffix = uniqid('test');
151 $extensionKey = 'unloadedextension' . $uniqueSuffix;
152 $extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;
153
154 $this->assertFalse(
155 t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
156 );
157 }
158
159
160 //////////////////////////////////////
161 // Tests concerning addToAllTCAtypes
162 //////////////////////////////////////
163
164 /**
165 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
166 * @test
167 * @see t3lib_extMgm::addToAllTCAtypes()
168 */
169 public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
170 $table = uniqid('tx_coretest_table');
171 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
172
173 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');
174
175 // Checking typeA:
176 $this->assertEquals(
177 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
178 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
179 );
180 // Checking typeB:
181 $this->assertEquals(
182 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
183 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
184 );
185 }
186
187 /**
188 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
189 * @test
190 * @see t3lib_extMgm::addToAllTCAtypes()
191 */
192 public function canAddFieldsToAllTCATypesAfterExistingOnes() {
193 $table = uniqid('tx_coretest_table');
194 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
195
196 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');
197
198 // Checking typeA:
199 $this->assertEquals(
200 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
201 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
202 );
203 // Checking typeB:
204 $this->assertEquals(
205 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
206 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
207 );
208 }
209
210 /**
211 * Tests whether fields can be add to a TCA type before existing ones
212 * @test
213 * @see t3lib_extMgm::addToAllTCAtypes()
214 */
215 public function canAddFieldsToTCATypeBeforeExistingOnes() {
216 $table = uniqid('tx_coretest_table');
217 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
218
219 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');
220
221 // Checking typeA:
222 $this->assertEquals(
223 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
224 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
225 );
226 // Checking typeB:
227 $this->assertEquals(
228 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
229 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
230 );
231 }
232
233 /**
234 * Tests whether fields can be add to a TCA type after existing ones
235 * @test
236 * @see t3lib_extMgm::addToAllTCAtypes()
237 */
238 public function canAddFieldsToTCATypeAfterExistingOnes() {
239 $table = uniqid('tx_coretest_table');
240 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
241
242 t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');
243
244 // Checking typeA:
245 $this->assertEquals(
246 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
247 $GLOBALS['TCA'][$table]['types']['typeA']['showitem']
248 );
249 // Checking typeB:
250 $this->assertEquals(
251 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
252 $GLOBALS['TCA'][$table]['types']['typeB']['showitem']
253 );
254 }
255
256
257 ///////////////////////////////////////////////////
258 // Tests concerning addFieldsToAllPalettesOfField
259 ///////////////////////////////////////////////////
260
261 /**
262 * Tests whether fields can be added to a palette before existing elements.
263 * @test
264 * @see t3lib_extMgm::addFieldsToPalette()
265 */
266 public function canAddFieldsToPaletteBeforeExistingOnes() {
267 $table = uniqid('tx_coretest_table');
268 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
269
270 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');
271
272 $this->assertEquals(
273 'fieldX, newA, newB, fieldY',
274 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
275 );
276 }
277
278 /**
279 * Tests whether fields can be added to a palette after existing elements.
280 * @test
281 * @see t3lib_extMgm::addFieldsToPalette()
282 */
283 public function canAddFieldsToPaletteAfterExistingOnes() {
284 $table = uniqid('tx_coretest_table');
285 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
286
287 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');
288
289 $this->assertEquals(
290 'fieldX, newA, newB, fieldY',
291 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
292 );
293 }
294
295 /**
296 * Tests whether fields can be added to a palette after a not existing elements.
297 * @test
298 * @see t3lib_extMgm::addFieldsToPalette()
299 */
300 public function canAddFieldsToPaletteAfterNotExistingOnes() {
301 $table = uniqid('tx_coretest_table');
302 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
303
304 t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
305
306 $this->assertEquals(
307 'fieldX, fieldY, newA, newB',
308 $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
309 );
310 }
311
312 /**
313 * Tests whether fields can be added to all palettes of a regular field before existing ones.
314 * @test
315 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
316 */
317 public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
318 $table = uniqid('tx_coretest_table');
319 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
320
321 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');
322
323 $this->assertEquals(
324 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
325 );
326 $this->assertEquals(
327 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
328 );
329 $this->assertEquals(
330 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
331 );
332 $this->assertEquals(
333 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
334 );
335 }
336
337 /**
338 * Tests whether fields can be added to all palettes of a regular field after existing ones.
339 * @test
340 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
341 */
342 public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
343 $table = uniqid('tx_coretest_table');
344 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
345
346 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');
347
348 $this->assertEquals(
349 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
350 );
351 $this->assertEquals(
352 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
353 );
354 $this->assertEquals(
355 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
356 );
357 $this->assertEquals(
358 'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
359 );
360 }
361
362 /**
363 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
364 * @test
365 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
366 */
367 public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
368 $table = uniqid('tx_coretest_table');
369 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
370
371 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));
372
373 $this->assertEquals(
374 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
375 );
376 $this->assertEquals(
377 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
378 );
379 $this->assertEquals(
380 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
381 );
382 $this->assertEquals(
383 'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
384 );
385 }
386
387 /**
388 * Tests whether fields are added to a new palette that did not exist before.
389 * @test
390 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
391 */
392 public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
393 $table = uniqid('tx_coretest_table');
394 $GLOBALS['TCA'] = $this->generateTCAForTable($table);
395
396 t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');
397
398 $this->assertEquals(
399 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
400 );
401 $this->assertEquals(
402 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
403 );
404 $this->assertEquals(
405 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
406 );
407 $this->assertEquals(
408 'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
409 );
410 $this->assertEquals(
411 'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
412 );
413 }
414
415
416 /////////////////////////////////////////
417 // Tests concerning getExtensionVersion
418 /////////////////////////////////////////
419
420 /**
421 * Data provider for negative getExtensionVersion() tests.
422 *
423 * @return array
424 */
425 public function getExtensionVersionFaultyDataProvider() {
426 return array(
427 array(''),
428 array(0),
429 array(new stdClass()),
430 array(TRUE),
431 );
432 }
433
434 /**
435 * @test
436 * @expectedException InvalidArgumentException
437 * @dataProvider getExtensionVersionFaultyDataProvider
438 */
439 public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
440 t3lib_extMgm::getExtensionVersion($key);
441 }
442
443 /**
444 * @test
445 */
446 public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
447 t3lib_extMgm::clearExtensionKeyMap();
448
449 $uniqueSuffix = uniqid('test');
450 $extensionKey = 'unloadedextension' . $uniqueSuffix;
451
452 $this->assertEquals(
453 '',
454 t3lib_extMgm::getExtensionVersion($extensionKey)
455 );
456 }
457
458 /**
459 * @test
460 */
461 public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
462 t3lib_extMgm::clearExtensionKeyMap();
463
464 $uniqueSuffix = uniqid('test');
465 $extensionKey = 'unloadedextension' . $uniqueSuffix;
466
467 $GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
468 'siteRelPath' => $this->determineFixturesPath(),
469 );
470 $this->assertEquals(
471 '1.2.3',
472 t3lib_extMgm::getExtensionVersion($extensionKey)
473 );
474 }
475 }
476 ?>