IconUtilityTest.php 15.1 KB
Newer Older
1
<?php
2
3
namespace TYPO3\CMS\Backend\Tests\Unit\Utility;

4
/***************************************************************
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *  Copyright notice
 *
 *  (c) 2010-2011 Fabien Udriot <fabien.udriot@ecodev.ch>
 *  (c) 2010-2011 Oliver Klee <typo3-coding@oliverklee.de>
 *  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!
 ***************************************************************/
27

28
/**
29
 * Testcase for TYPO3\CMS\Backend\Utility\IconUtility
30
31
32
33
34
35
 *
 * @author Fabien Udriot <fabien.udriot@ecodev.ch>
 * @author Oliver Klee <typo3-coding@oliverklee.de>
 * @package TYPO3
 * @subpackage t3lib
 */
36
class IconUtilityTest extends \tx_phpunit_testcase {
37
38
39
40
41
42
43

	/**
	 * Enable backup of global and system variables
	 *
	 * @var boolean
	 */
	protected $backupGlobals = TRUE;
44

45
46
47
48
49
50
	/**
	 * Exclude TYPO3_DB from backup/restore of $GLOBALS
	 * because resource types cannot be handled during serializing
	 *
	 * @var array
	 */
51
	protected $backupGlobalsBlacklist = array('TYPO3_DB', 'TBE_STYLES');
52

53
54
55
56
57
	/**
	 * @var array
	 */
	protected $mockRecord;

58
59
60
61
62
	/**
	 * Set up test case
	 *
	 * @return void
	 */
63
	public function setUp() {
64
65
		if (!\TYPO3\CMS\Backend\Sprite\SpriteManager::isInitialized()) {
			\TYPO3\CMS\Backend\Sprite\SpriteManager::initialize();
66
		}
67
		// Simulate a tt_content record
68
69
70
71
72
73
74
75
76
77
78
79
80
		$this->mockRecord = array();
		$this->mockRecord['header'] = 'dummy content header';
		$this->mockRecord['uid'] = '1';
		$this->mockRecord['pid'] = '1';
		$this->mockRecord['image'] = '';
		$this->mockRecord['hidden'] = '0';
		$this->mockRecord['starttime'] = '0';
		$this->mockRecord['endtime'] = '0';
		$this->mockRecord['fe_group'] = '';
		$this->mockRecord['CType'] = 'text';
		$this->mockRecord['t3ver_id'] = '0';
		$this->mockRecord['t3ver_state'] = '0';
		$this->mockRecord['t3ver_wsid'] = '0';
81
		$this->mockRecord['sys_TYPO3\\CMS\\Lang\\LanguageService_uid'] = '0';
82
83
84
85
86
87
88
89
90
		$this->mockRecord['l18n_parent'] = '0';
		$this->mockRecord['subheader'] = '';
		$this->mockRecord['bodytext'] = '';
	}

	public function tearDown() {
		unset($this->mockRecord);
	}

91
92
93
94
95
96
97
98
99
100
	//////////////////////////////////////////
	// Tests concerning imagemake
	//////////////////////////////////////////
	/**
	 * @test
	 */
	public function imagemakeFixesPermissionsOnNewFiles() {
		if (TYPO3_OS == 'WIN') {
			$this->markTestSkipped('imagemakeFixesPermissionsOnNewFiles() test not available on Windows.');
		}
101
		$fixtureGifFile = __DIR__ . '/Fixtures/clear.gif';
102
		// Create image ressource, determine target filename, fake target permission, run method and clean up
103
		$fixtureGifRessource = imagecreatefromgif($fixtureGifFile);
104
		$targetFilename = ((PATH_site . 'typo3temp/') . uniqid('test_')) . '.gif';
105
		$GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
106
		\TYPO3\CMS\Backend\Utility\IconUtility::imagemake($fixtureGifRessource, $targetFilename);
107
108
		clearstatcache();
		$resultFilePermissions = substr(decoct(fileperms($targetFilename)), 2);
109
		\TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($targetFilename);
110
111
112
		$this->assertEquals($resultFilePermissions, '0777');
	}

113
114
115
116
117
	//////////////////////////////////////////
	// Tests concerning getSpriteIconClasses
	//////////////////////////////////////////
	/**
	 * Tests whether an empty string returns 't3-icon'
118
	 *
119
120
121
	 * @test
	 */
	public function getSpriteIconClassesWithEmptyStringReturnsT3Icon() {
122
		$this->assertEquals('t3-icon', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses(''));
123
124
125
126
	}

	/**
	 * Tests whether one part returns 't3-icon'
127
	 *
128
129
130
	 * @test
	 */
	public function getSpriteIconClassesWithOnePartReturnsT3Icon() {
131
		$this->assertEquals('t3-icon', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions'));
132
133
134
135
	}

	/**
	 * Tests the return of two parts
136
	 *
137
138
139
	 * @test
	 */
	public function getSpriteIconClassesWithTwoPartsReturnsT3IconAndCombinedParts() {
140
		$result = explode(' ', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions-juggle'));
141
		sort($result);
142
		$this->assertEquals(array('t3-icon', 't3-icon-actions', 't3-icon-actions-juggle', 't3-icon-juggle'), $result);
143
144
145
146
	}

	/**
	 * Tests the return of tree parts
147
	 *
148
149
150
	 * @test
	 */
	public function getSpriteIconClassesWithThreePartsReturnsT3IconAndCombinedParts() {
151
		$result = explode(' ', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions-juggle-speed'));
152
		sort($result);
153
		$this->assertEquals(array('t3-icon', 't3-icon-actions', 't3-icon-actions-juggle', 't3-icon-juggle-speed'), $result);
154
155
156
157
	}

	/**
	 * Tests the return of four parts
158
	 *
159
160
161
	 * @test
	 */
	public function getSpriteIconClassesWithFourPartsReturnsT3IconAndCombinedParts() {
162
		$result = explode(' ', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions-juggle-speed-game'));
163
		sort($result);
164
		$this->assertEquals(array('t3-icon', 't3-icon-actions', 't3-icon-actions-juggle', 't3-icon-juggle-speed-game'), $result);
165
166
167
168
169
170
171
	}

	//////////////////////////////////////////
	// Tests concerning getSpriteIcon
	//////////////////////////////////////////
	/**
	 * Tests whether an empty string returns a span with the missing sprite
172
	 *
173
174
175
	 * @test
	 */
	public function getSpriteIconWithEmptyStringReturnsSpanWithIconMissingSprite() {
176
		$this->assertEquals('<span class="t3-icon t3-icon-status t3-icon-status-status t3-icon-status-icon-missing">&nbsp;</span>', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon(''));
177
178
179
180
	}

	/**
	 * Tests whether an non existing icons returns a span with the missing sprite
181
	 *
182
183
184
	 * @test
	 */
	public function getSpriteIconWithMissingIconReturnsSpanWithIconMissingSprite() {
185
		$this->assertEquals('<span class="t3-icon t3-icon-status t3-icon-status-status t3-icon-status-icon-missing">&nbsp;</span>', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-juggle-speed'));
186
187
188
189
	}

	/**
	 * Tests whether an existing icon returns a span with the correct sprite
190
	 *
191
192
193
	 * @test
	 */
	public function getSpriteIconWithExistingIconReturnsSpanWithIconSprite() {
194
		$this->assertEquals('<span class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new">&nbsp;</span>', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new'));
195
196
197
198
	}

	/**
	 * Tests the returns of an existing icon + an other attribute like title="foo"
199
	 *
200
201
202
	 * @test
	 */
	public function getSpriteIconWithExistingIconAndAttributeReturnsSpanWithIconSpriteAndAttribute() {
203
		$this->assertEquals('<span title="foo" class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new">&nbsp;</span>', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array('title' => 'foo')));
204
205
206
207
	}

	/**
	 * Tests the returns of an existing icon + a class attribute
208
	 *
209
210
211
	 * @test
	 */
	public function getSpriteIconWithExistingIconAndClassAttributeReturnsSpanWithIconSpriteAndClassAttribute() {
212
		$this->assertEquals('<span class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new foo">&nbsp;</span>', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array('class' => 'foo')));
213
214
215
216
	}

	/**
	 * Tests the returns of an existing icon + a class attribute
217
	 *
218
219
220
	 * @test
	 */
	public function getSpriteIconWithExistingIconAndInnerHTMLReturnsSpanWithIconSpriteAndInnerHTML() {
221
		$this->assertEquals('<span class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new">foo</span>', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array('html' => 'foo')));
222
223
224
225
	}

	/**
	 * Tests the returns of an existing icon + an overlay
226
	 *
227
228
229
	 * @test
	 */
	public function getSpriteIconWithExistingIconAndOverlayReturnsSpanWithIconSpriteAndOverlay() {
230
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array(), array('status-overlay-hidden' => array()));
231
		$overlay = '<span class="t3-icon t3-icon-status t3-icon-status-overlay t3-icon-overlay-hidden t3-icon-overlay">&nbsp;</span>';
232
		$this->assertEquals(('<span class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new">' . $overlay) . '</span>', $result);
233
234
235
236
	}

	/**
	 * Tests the returns of an existing icon + an overlay
237
	 *
238
239
240
	 * @test
	 */
	public function getSpriteIconWithExistingIconAndOverlayAndAttributesReturnsSpanWithIconSpriteAndOverlayAndAttributes() {
241
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array('html' => 'foo1'), array('status-overlay-hidden' => array('class' => 'foo2')));
242
		$overlay = '<span class="t3-icon t3-icon-status t3-icon-status-overlay t3-icon-overlay-hidden foo2 t3-icon-overlay">foo1</span>';
243
		$this->assertEquals(('<span class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new">' . $overlay) . '</span>', $result);
244
245
246
247
248
249
	}

	//////////////////////////////////////////
	// Tests concerning getSpriteIconForRecord
	//////////////////////////////////////////
	/**
250
	 * Tests the returns of NULL table + empty array
251
	 *
252
253
254
	 * @test
	 */
	public function getSpriteIconForRecordWithNullTableReturnsMissingIcon() {
255
256
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('', array());
		$this->assertEquals('<span class="t3-icon t3-icon-status t3-icon-status-status t3-icon-status-icon-missing">&nbsp;</span>', $result);
257
258
259
260
	}

	/**
	 * Tests the returns of tt_content + empty record
261
	 *
262
263
264
	 * @test
	 */
	public function getSpriteIconForRecordWithEmptyRecordReturnsNormalSprite() {
265
266
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', array());
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text">&nbsp;</span>', $result);
267
268
269
270
	}

	/**
	 * Tests the returns of tt_content + mock record
271
	 *
272
273
274
275
	 * @test
	 */
	public function getSpriteIconForRecordWithMockRecordReturnsNormalSprite() {
		$mockRecord = $this->mockRecord;
276
277
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text">&nbsp;</span>', $result);
278
279
280
281
	}

	/**
	 * Tests the returns of tt_content + mock record + options
282
	 *
283
284
285
286
	 * @test
	 */
	public function getSpriteIconForRecordWithMockRecordAndOptionsReturnsNormalSprite() {
		$mockRecord = $this->mockRecord;
287
288
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord, array('class' => 'foo', 'title' => 'bar'));
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text foo" title="bar">&nbsp;</span>', $result);
289
290
291
292
	}

	/**
	 * Tests the returns of tt_content + mock record of type 'list' (aka plugin)
293
	 *
294
295
296
297
298
	 * @test
	 */
	public function getSpriteIconForRecordWithMockRecordOfTypePluginReturnsPluginSprite() {
		$mockRecord = $this->mockRecord;
		$mockRecord['CType'] = 'list';
299
300
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-plugin">&nbsp;</span>', $result);
301
302
303
304
	}

	/**
	 * Tests the returns of tt_content + mock record with hidden flag
305
	 *
306
307
308
309
310
	 * @test
	 */
	public function getSpriteIconForRecordWithMockRecordWithHiddenFlagReturnsNormalSpriteAndOverlay() {
		$mockRecord = $this->mockRecord;
		$mockRecord['hidden'] = '1';
311
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
312
		$overlay = '<span class="t3-icon t3-icon-status t3-icon-status-overlay t3-icon-overlay-hidden t3-icon-overlay">&nbsp;</span>';
313
		$this->assertEquals(('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text">' . $overlay) . '</span>', $result);
314
315
316
317
318
319
320
	}

	//////////////////////////////////////////
	// Tests concerning getSpriteIconForFile
	//////////////////////////////////////////
	/**
	 * Tests the returns of no file
321
	 *
322
323
324
	 * @test
	 */
	public function getSpriteIconForFileWithNoFileTypeReturnsOtherSprite() {
325
326
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('');
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-other t3-icon-other-other">&nbsp;</span>', $result);
327
328
329
330
	}

	/**
	 * Tests the returns of unknown file
331
	 *
332
333
334
	 * @test
	 */
	public function getSpriteIconForFileWithNoUnknowFileTypeReturnsOtherSprite() {
335
336
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('foo');
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-other t3-icon-other-other">&nbsp;</span>', $result);
337
338
339
340
	}

	/**
	 * Tests the returns of file pdf
341
	 *
342
343
344
	 * @test
	 */
	public function getSpriteIconForFileWithPdfReturnsPdfSprite() {
345
346
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('pdf');
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-pdf t3-icon-pdf">&nbsp;</span>', $result);
347
348
349
350
	}

	/**
	 * Tests the returns of file png
351
	 *
352
353
354
	 * @test
	 */
	public function getSpriteIconForFileWithPngReturnsPngSprite() {
355
356
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('png');
		$this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-media t3-icon-media-image">&nbsp;</span>', $result);
357
358
359
360
	}

	/**
	 * Tests the returns of file png + option
361
	 *
362
363
364
	 * @test
	 */
	public function getSpriteIconForFileWithPngAndOptionsReturnsPngSpriteAndOptions() {
365
366
		$result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('png', array('title' => 'bar'));
		$this->assertEquals('<span title="bar" class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-media t3-icon-media-image">&nbsp;</span>', $result);
367
	}
368
369
370

	/**
	 * Tests whether a overrideIconOverlay hook is called.
371
	 *
372
373
374
375
376
377
378
379
	 * @test
	 */
	public function isOverrideIconOverlayHookCalled() {
		$classReference = uniqid('user_overrideIconOverlayHook');
		$hookMock = $this->getMock($classReference, array('overrideIconOverlay'), array());
		$hookMock->expects($this->once())->method('overrideIconOverlay');
		$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'][$classReference] = $classReference;
		$GLOBALS['T3_VAR']['getUserObj'][$classReference] = $hookMock;
380
		\TYPO3\CMS\Backend\Utility\IconUtility::mapRecordOverlayToSpriteIconName('tt_content', array());
381
382
383
384
	}

	/**
	 * Tests whether a faulty overrideIconOverlay hook (the hook object cannot be found) is not called.
385
	 *
386
387
388
389
390
	 * @test
	 */
	public function isFaultyOverrideIconOverlayHookNotCalled() {
		$classReference = uniqid('user_overrideIconOverlayHook');
		$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'][$classReference] = $classReference;
391
		$GLOBALS['T3_VAR']['getUserObj'][$classReference] = new \stdClass();
392
		\TYPO3\CMS\Backend\Utility\IconUtility::mapRecordOverlayToSpriteIconName('tt_content', array());
393
	}
394

395
}
396

397
?>