typo3v4core /tests/t3lib/t3lib_extmgmTest.php

Language PHP Lines 466
MD5 Hash c99fd902bf77b1bcbc8c2afa2539fbfe
Repository https://github.com/foxsoft/typo3v4core.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
/***************************************************************
*  Copyright notice
*
*  (c) 2009-2010 Oliver Hader <oliver@typo3.org>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/

/**
 * Testcase for class t3lib_extMgm
 *
 * @author Oliver Hader <oliver@typo3.org>
 * @author Oliver Klee <typo3-coding@oliverklee.de>
 *
 * @package TYPO3
 * @subpackage t3lib
 */
class t3lib_extmgmTest extends tx_phpunit_testcase {
	/**
	 * backup of defined GLOBALS
	 *
	 * @var array
	 */
	protected $globals = array();

	public function setUp() {
		$this->globals = array(
			'TYPO3_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
			'TCA' => serialize($GLOBALS['TCA']),
		);
	}

	public function tearDown() {
		t3lib_extMgm::clearExtensionKeyMap();

		foreach ($this->globals as $key => $value) {
			$GLOBALS[$key] = unserialize($value);
		}
	}


	//////////////////////
	// Utility functions
	//////////////////////

	/**
	 * Generates a basic TCA for a given table.
	 *
	 * @param string $table name of the table, must not be empty
	 * @return array generated TCA for the given table, will not be empty
	 */
	private function generateTCAForTable($table) {
		$tca = array();
		$tca[$table] = array();
		$tca[$table]['columns'] = array(
			'fieldA' => array(),
			'fieldC' => array(),
		);
		$tca[$table]['types'] = array(
			'typeA' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
			'typeB' => array('showitem' => 'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD'),
			'typeC' => array('showitem' => 'fieldC;;paletteD'),
		);
		$tca[$table]['palettes'] = array(
			'paletteA' => array('showitem' => 'fieldX, fieldY'),
			'paletteB' => array('showitem' => 'fieldX, fieldY'),
			'paletteC' => array('showitem' => 'fieldX, fieldY'),
			'paletteD' => array('showitem' => 'fieldX, fieldY'),
		);

		return $tca;
	}


	/////////////////////////////////////////////
	// Tests concerning getExtensionKeyByPrefix
	/////////////////////////////////////////////

	/**
	 * @test
	 * @see t3lib_extMgm::getExtensionKeyByPrefix
	 */
	public function getExtensionKeyByPrefixForLoadedExtensionWithUnderscoresReturnsExtensionKey() {
		t3lib_extMgm::clearExtensionKeyMap();

		$uniqueSuffix = uniqid('test');
		$extensionKey = 'tt_news' . $uniqueSuffix;
		$extensionPrefix = 'tx_ttnews' . $uniqueSuffix;

		$GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();

		$this->assertEquals(
			$extensionKey,
			t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
		);
	}

	/**
	 * @test
	 * @see t3lib_extMgm::getExtensionKeyByPrefix
	 */
	public function getExtensionKeyByPrefixForLoadedExtensionWithoutUnderscoresReturnsExtensionKey() {
		t3lib_extMgm::clearExtensionKeyMap();

		$uniqueSuffix = uniqid('test');
		$extensionKey = 'kickstarter' . $uniqueSuffix;
		$extensionPrefix = 'tx_kickstarter' . $uniqueSuffix;

		$GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array();

		$this->assertEquals(
			$extensionKey,
			t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
		);
	}

	/**
	 * @test
	 * @see t3lib_extMgm::getExtensionKeyByPrefix
	 */
	public function getExtensionKeyByPrefixForNotLoadedExtensionReturnsFalse(){
		t3lib_extMgm::clearExtensionKeyMap();

		$uniqueSuffix = uniqid('test');
		$extensionKey = 'unloadedextension' . $uniqueSuffix;
		$extensionPrefix = 'tx_unloadedextension' . $uniqueSuffix;

		$this->assertFalse(
			t3lib_extMgm::getExtensionKeyByPrefix($extensionPrefix)
		);
	}


	//////////////////////////////////////
	// Tests concerning addToAllTCAtypes
	//////////////////////////////////////

	/**
	 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
	 * @test
	 * @see t3lib_extMgm::addToAllTCAtypes()
	 */
	public function canAddFieldsToAllTCATypesBeforeExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'before:fieldD');

			// Checking typeA:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeA']['showitem']
		);
			// Checking typeB:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeB']['showitem']
		);
	}

	/**
	 * Tests whether fields can be add to all TCA types and duplicate fields are considered.
	 * @test
	 * @see t3lib_extMgm::addToAllTCAtypes()
	 */
	public function canAddFieldsToAllTCATypesAfterExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', '', 'after:fieldC');

			// Checking typeA:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeA']['showitem']
		);
			// Checking typeB:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeB']['showitem']
		);
	}

	/**
	 * Tests whether fields can be add to a TCA type before existing ones
	 * @test
	 * @see t3lib_extMgm::addToAllTCAtypes()
	 */
	public function canAddFieldsToTCATypeBeforeExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'before:fieldD');

			// Checking typeA:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeA']['showitem']
		);
			// Checking typeB:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeB']['showitem']
		);
	}

	/**
	 * Tests whether fields can be add to a TCA type after existing ones
	 * @test
	 * @see t3lib_extMgm::addToAllTCAtypes()
	 */
	public function canAddFieldsToTCATypeAfterExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addToAllTCAtypes($table, 'newA, newA, newB, fieldA', 'typeA', 'after:fieldC');

			// Checking typeA:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, newA, newB, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeA']['showitem']
		);
			// Checking typeB:
		$this->assertEquals(
			'fieldA, fieldB, fieldC;labelC;paletteC;specialC, fieldD',
			$GLOBALS['TCA'][$table]['types']['typeB']['showitem']
		);
	}


	///////////////////////////////////////////////////
	// Tests concerning addFieldsToAllPalettesOfField
	///////////////////////////////////////////////////

	/**
	 * Tests whether fields can be added to a palette before existing elements.
	 * @test
	 * @see t3lib_extMgm::addFieldsToPalette()
	 */
	public function canAddFieldsToPaletteBeforeExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'before:fieldY');

		$this->assertEquals(
			'fieldX, newA, newB, fieldY',
			$GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
		);
	}

	/**
	 * Tests whether fields can be added to a palette after existing elements.
	 * @test
	 * @see t3lib_extMgm::addFieldsToPalette()
	 */
	public function canAddFieldsToPaletteAfterExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:fieldX');

		$this->assertEquals(
			'fieldX, newA, newB, fieldY',
			$GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
		);
	}

	/**
	 * Tests whether fields can be added to a palette after a not existing elements.
	 * @test
	 * @see t3lib_extMgm::addFieldsToPalette()
	 */
	public function canAddFieldsToPaletteAfterNotExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addFieldsToPalette($table, 'paletteA', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));

		$this->assertEquals(
			'fieldX, fieldY, newA, newB',
			$GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
		);
	}

	/**
	 * Tests whether fields can be added to all palettes of a regular field before existing ones.
	 * @test
	 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
	 */
	public function canAddFieldsToAllPalettesOfFieldBeforeExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'before:fieldY');

		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
		);
		$this->assertEquals(
			'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
		);
		$this->assertEquals(
			'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
		);
	}

	/**
	 * Tests whether fields can be added to all palettes of a regular field after existing ones.
	 * @test
	 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
	 */
	public function canAddFieldsToAllPalettesOfFieldAfterExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:fieldX');

		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
		);
		$this->assertEquals(
			'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
		);
		$this->assertEquals(
			'fieldX, newA, newB, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
		);
	}

	/**
	 * Tests whether fields can be added to all palettes of a regular field after a not existing field.
	 * @test
	 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
	 */
	public function canAddFieldsToAllPalettesOfFieldAfterNotExistingOnes() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldC', 'newA, newA, newB, fieldX', 'after:' . uniqid('notExisting'));

		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY, newA, newB', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
		);
	}

	/**
	 * Tests whether fields are added to a new palette that did not exist before.
	 * @test
	 * @see t3lib_extMgm::addFieldsToAllPalettesOfField()
	 */
	public function canAddFieldsToAllPalettesOfFieldWithoutPaletteExistingBefore() {
		$table = uniqid('tx_coretest_table');
		$GLOBALS['TCA'] = $this->generateTCAForTable($table);

		t3lib_extMgm::addFieldsToAllPalettesOfField($table, 'fieldA', 'newA, newA, newB, fieldX');

		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteA']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteB']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteC']['showitem']
		);
		$this->assertEquals(
			'fieldX, fieldY', $GLOBALS['TCA'][$table]['palettes']['paletteD']['showitem']
		);
		$this->assertEquals(
			'newA, newB, fieldX', $GLOBALS['TCA'][$table]['palettes']['generatedFor-fieldA']['showitem']
		);
	}


	/////////////////////////////////////////
	// Tests concerning getExtensionVersion
	/////////////////////////////////////////

	/**
	 * Data provider for negative getExtensionVersion() tests.
	 *
	 * @return array
	 */
	public function getExtensionVersionFaultyDataProvider() {
		return array(
			array(''),
			array(0),
			array(new stdClass()),
			array(TRUE),
		);
	}

	/**
	 * @test
	 * @expectedException InvalidArgumentException
	 * @dataProvider getExtensionVersionFaultyDataProvider
	 */
	public function getExtensionVersionForFaultyExtensionKeyThrowsException($key) {
		t3lib_extMgm::getExtensionVersion($key);
	}

	/**
	 * @test
	 */
	public function getExtensionVersionForNotLoadedExtensionReturnsEmptyString() {
		t3lib_extMgm::clearExtensionKeyMap();

		$uniqueSuffix = uniqid('test');
		$extensionKey = 'unloadedextension' . $uniqueSuffix;

		$this->assertEquals(
			'',
			t3lib_extMgm::getExtensionVersion($extensionKey)
		);
	}

	/**
	 * @test
	 */
	public function getExtensionVersionForLoadedExtensionReturnsExtensionVersion() {
		t3lib_extMgm::clearExtensionKeyMap();

		$uniqueSuffix = uniqid('test');
		$extensionKey = 'unloadedextension' . $uniqueSuffix;

		$GLOBALS['TYPO3_LOADED_EXT'][$extensionKey] = array(
			'siteRelPath' => 'typo3_src/tests/t3lib/fixtures/',
		);
		$this->assertEquals(
			'1.2.3',
			t3lib_extMgm::getExtensionVersion($extensionKey)
		);
	}
}
?>
Back to Top