4958a1d2f58c7eb1490091d2ed49eaf992567018
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Utility / IconUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Backend\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2010-2011 Fabien Udriot <fabien.udriot@ecodev.ch>
8 * (c) 2010-2011 Oliver Klee <typo3-coding@oliverklee.de>
9 * All rights reserved
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * Testcase for TYPO3\CMS\Backend\Utility\IconUtility
30 *
31 * @author Fabien Udriot <fabien.udriot@ecodev.ch>
32 * @author Oliver Klee <typo3-coding@oliverklee.de>
33 * @package TYPO3
34 * @subpackage t3lib
35 */
36 class IconUtilityTest extends \tx_phpunit_testcase {
37
38 /**
39 * Enable backup of global and system variables
40 *
41 * @var boolean
42 */
43 protected $backupGlobals = TRUE;
44
45 /**
46 * Exclude TYPO3_DB from backup/restore of $GLOBALS
47 * because resource types cannot be handled during serializing
48 *
49 * @var array
50 */
51 protected $backupGlobalsBlacklist = array('TYPO3_DB', 'TBE_STYLES');
52
53 /**
54 * @var array
55 */
56 protected $mockRecord;
57
58 /**
59 * Set up test case
60 *
61 * @return void
62 */
63 public function setUp() {
64 if (!\TYPO3\CMS\Backend\Sprite\SpriteManager::isInitialized()) {
65 \TYPO3\CMS\Backend\Sprite\SpriteManager::initialize();
66 }
67 // Simulate a tt_content record
68 $this->mockRecord = array();
69 $this->mockRecord['header'] = 'dummy content header';
70 $this->mockRecord['uid'] = '1';
71 $this->mockRecord['pid'] = '1';
72 $this->mockRecord['image'] = '';
73 $this->mockRecord['hidden'] = '0';
74 $this->mockRecord['starttime'] = '0';
75 $this->mockRecord['endtime'] = '0';
76 $this->mockRecord['fe_group'] = '';
77 $this->mockRecord['CType'] = 'text';
78 $this->mockRecord['t3ver_id'] = '0';
79 $this->mockRecord['t3ver_state'] = '0';
80 $this->mockRecord['t3ver_wsid'] = '0';
81 $this->mockRecord['sys_TYPO3\\CMS\\Lang\\LanguageService_uid'] = '0';
82 $this->mockRecord['l18n_parent'] = '0';
83 $this->mockRecord['subheader'] = '';
84 $this->mockRecord['bodytext'] = '';
85 }
86
87 public function tearDown() {
88 unset($this->mockRecord);
89 }
90
91 //////////////////////////////////////////
92 // Tests concerning imagemake
93 //////////////////////////////////////////
94 /**
95 * @test
96 */
97 public function imagemakeFixesPermissionsOnNewFiles() {
98 if (TYPO3_OS == 'WIN') {
99 $this->markTestSkipped('imagemakeFixesPermissionsOnNewFiles() test not available on Windows.');
100 }
101 $testFinder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('Tx_Phpunit_Service_TestFinder');
102 $fixtureGifFile = $testFinder->getAbsoluteCoreTestsPath() . 'Unit/t3lib/fixtures/clear.gif';
103 // Create image ressource, determine target filename, fake target permission, run method and clean up
104 $fixtureGifRessource = imagecreatefromgif($fixtureGifFile);
105 $targetFilename = ((PATH_site . 'typo3temp/') . uniqid('test_')) . '.gif';
106 $GLOBALS['TYPO3_CONF_VARS']['BE']['fileCreateMask'] = '0777';
107 \TYPO3\CMS\Backend\Utility\IconUtility::imagemake($fixtureGifRessource, $targetFilename);
108 clearstatcache();
109 $resultFilePermissions = substr(decoct(fileperms($targetFilename)), 2);
110 \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($targetFilename);
111 $this->assertEquals($resultFilePermissions, '0777');
112 }
113
114 //////////////////////////////////////////
115 // Tests concerning getSpriteIconClasses
116 //////////////////////////////////////////
117 /**
118 * Tests whether an empty string returns 't3-icon'
119 *
120 * @test
121 */
122 public function getSpriteIconClassesWithEmptyStringReturnsT3Icon() {
123 $this->assertEquals('t3-icon', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses(''));
124 }
125
126 /**
127 * Tests whether one part returns 't3-icon'
128 *
129 * @test
130 */
131 public function getSpriteIconClassesWithOnePartReturnsT3Icon() {
132 $this->assertEquals('t3-icon', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions'));
133 }
134
135 /**
136 * Tests the return of two parts
137 *
138 * @test
139 */
140 public function getSpriteIconClassesWithTwoPartsReturnsT3IconAndCombinedParts() {
141 $result = explode(' ', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions-juggle'));
142 sort($result);
143 $this->assertEquals(array('t3-icon', 't3-icon-actions', 't3-icon-actions-juggle', 't3-icon-juggle'), $result);
144 }
145
146 /**
147 * Tests the return of tree parts
148 *
149 * @test
150 */
151 public function getSpriteIconClassesWithThreePartsReturnsT3IconAndCombinedParts() {
152 $result = explode(' ', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions-juggle-speed'));
153 sort($result);
154 $this->assertEquals(array('t3-icon', 't3-icon-actions', 't3-icon-actions-juggle', 't3-icon-juggle-speed'), $result);
155 }
156
157 /**
158 * Tests the return of four parts
159 *
160 * @test
161 */
162 public function getSpriteIconClassesWithFourPartsReturnsT3IconAndCombinedParts() {
163 $result = explode(' ', \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconClasses('actions-juggle-speed-game'));
164 sort($result);
165 $this->assertEquals(array('t3-icon', 't3-icon-actions', 't3-icon-actions-juggle', 't3-icon-juggle-speed-game'), $result);
166 }
167
168 //////////////////////////////////////////
169 // Tests concerning getSpriteIcon
170 //////////////////////////////////////////
171 /**
172 * Tests whether an empty string returns a span with the missing sprite
173 *
174 * @test
175 */
176 public function getSpriteIconWithEmptyStringReturnsSpanWithIconMissingSprite() {
177 $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(''));
178 }
179
180 /**
181 * Tests whether an non existing icons returns a span with the missing sprite
182 *
183 * @test
184 */
185 public function getSpriteIconWithMissingIconReturnsSpanWithIconMissingSprite() {
186 $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'));
187 }
188
189 /**
190 * Tests whether an existing icon returns a span with the correct sprite
191 *
192 * @test
193 */
194 public function getSpriteIconWithExistingIconReturnsSpanWithIconSprite() {
195 $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'));
196 }
197
198 /**
199 * Tests the returns of an existing icon + an other attribute like title="foo"
200 *
201 * @test
202 */
203 public function getSpriteIconWithExistingIconAndAttributeReturnsSpanWithIconSpriteAndAttribute() {
204 $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')));
205 }
206
207 /**
208 * Tests the returns of an existing icon + a class attribute
209 *
210 * @test
211 */
212 public function getSpriteIconWithExistingIconAndClassAttributeReturnsSpanWithIconSpriteAndClassAttribute() {
213 $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')));
214 }
215
216 /**
217 * Tests the returns of an existing icon + a class attribute
218 *
219 * @test
220 */
221 public function getSpriteIconWithExistingIconAndInnerHTMLReturnsSpanWithIconSpriteAndInnerHTML() {
222 $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')));
223 }
224
225 /**
226 * Tests the returns of an existing icon + an overlay
227 *
228 * @test
229 */
230 public function getSpriteIconWithExistingIconAndOverlayReturnsSpanWithIconSpriteAndOverlay() {
231 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array(), array('status-overlay-hidden' => array()));
232 $overlay = '<span class="t3-icon t3-icon-status t3-icon-status-overlay t3-icon-overlay-hidden t3-icon-overlay">&nbsp;</span>';
233 $this->assertEquals(('<span class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new">' . $overlay) . '</span>', $result);
234 }
235
236 /**
237 * Tests the returns of an existing icon + an overlay
238 *
239 * @test
240 */
241 public function getSpriteIconWithExistingIconAndOverlayAndAttributesReturnsSpanWithIconSpriteAndOverlayAndAttributes() {
242 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIcon('actions-document-new', array('html' => 'foo1'), array('status-overlay-hidden' => array('class' => 'foo2')));
243 $overlay = '<span class="t3-icon t3-icon-status t3-icon-status-overlay t3-icon-overlay-hidden foo2 t3-icon-overlay">foo1</span>';
244 $this->assertEquals(('<span class="t3-icon t3-icon-actions t3-icon-actions-document t3-icon-document-new">' . $overlay) . '</span>', $result);
245 }
246
247 //////////////////////////////////////////
248 // Tests concerning getSpriteIconForRecord
249 //////////////////////////////////////////
250 /**
251 * Tests the returns of NULL table + empty array
252 *
253 * @test
254 */
255 public function getSpriteIconForRecordWithNullTableReturnsMissingIcon() {
256 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('', array());
257 $this->assertEquals('<span class="t3-icon t3-icon-status t3-icon-status-status t3-icon-status-icon-missing">&nbsp;</span>', $result);
258 }
259
260 /**
261 * Tests the returns of tt_content + empty record
262 *
263 * @test
264 */
265 public function getSpriteIconForRecordWithEmptyRecordReturnsNormalSprite() {
266 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', array());
267 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text">&nbsp;</span>', $result);
268 }
269
270 /**
271 * Tests the returns of tt_content + mock record
272 *
273 * @test
274 */
275 public function getSpriteIconForRecordWithMockRecordReturnsNormalSprite() {
276 $mockRecord = $this->mockRecord;
277 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
278 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text">&nbsp;</span>', $result);
279 }
280
281 /**
282 * Tests the returns of tt_content + mock record + options
283 *
284 * @test
285 */
286 public function getSpriteIconForRecordWithMockRecordAndOptionsReturnsNormalSprite() {
287 $mockRecord = $this->mockRecord;
288 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord, array('class' => 'foo', 'title' => 'bar'));
289 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text foo" title="bar">&nbsp;</span>', $result);
290 }
291
292 /**
293 * Tests the returns of tt_content + mock record of type 'list' (aka plugin)
294 *
295 * @test
296 */
297 public function getSpriteIconForRecordWithMockRecordOfTypePluginReturnsPluginSprite() {
298 $mockRecord = $this->mockRecord;
299 $mockRecord['CType'] = 'list';
300 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
301 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-plugin">&nbsp;</span>', $result);
302 }
303
304 /**
305 * Tests the returns of tt_content + mock record with hidden flag
306 *
307 * @test
308 */
309 public function getSpriteIconForRecordWithMockRecordWithHiddenFlagReturnsNormalSpriteAndOverlay() {
310 $mockRecord = $this->mockRecord;
311 $mockRecord['hidden'] = '1';
312 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
313 $overlay = '<span class="t3-icon t3-icon-status t3-icon-status-overlay t3-icon-overlay-hidden t3-icon-overlay">&nbsp;</span>';
314 $this->assertEquals(('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-x t3-icon-x-content-text">' . $overlay) . '</span>', $result);
315 }
316
317 //////////////////////////////////////////
318 // Tests concerning getSpriteIconForFile
319 //////////////////////////////////////////
320 /**
321 * Tests the returns of no file
322 *
323 * @test
324 */
325 public function getSpriteIconForFileWithNoFileTypeReturnsOtherSprite() {
326 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('');
327 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-other t3-icon-other-other">&nbsp;</span>', $result);
328 }
329
330 /**
331 * Tests the returns of unknown file
332 *
333 * @test
334 */
335 public function getSpriteIconForFileWithNoUnknowFileTypeReturnsOtherSprite() {
336 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('foo');
337 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-other t3-icon-other-other">&nbsp;</span>', $result);
338 }
339
340 /**
341 * Tests the returns of file pdf
342 *
343 * @test
344 */
345 public function getSpriteIconForFileWithPdfReturnsPdfSprite() {
346 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('pdf');
347 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-pdf t3-icon-pdf">&nbsp;</span>', $result);
348 }
349
350 /**
351 * Tests the returns of file png
352 *
353 * @test
354 */
355 public function getSpriteIconForFileWithPngReturnsPngSprite() {
356 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('png');
357 $this->assertEquals('<span class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-media t3-icon-media-image">&nbsp;</span>', $result);
358 }
359
360 /**
361 * Tests the returns of file png + option
362 *
363 * @test
364 */
365 public function getSpriteIconForFileWithPngAndOptionsReturnsPngSpriteAndOptions() {
366 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('png', array('title' => 'bar'));
367 $this->assertEquals('<span title="bar" class="t3-icon t3-icon-mimetypes t3-icon-mimetypes-media t3-icon-media-image">&nbsp;</span>', $result);
368 }
369
370 /**
371 * Tests whether a overrideIconOverlay hook is called.
372 *
373 * @test
374 */
375 public function isOverrideIconOverlayHookCalled() {
376 $classReference = uniqid('user_overrideIconOverlayHook');
377 $hookMock = $this->getMock($classReference, array('overrideIconOverlay'), array());
378 $hookMock->expects($this->once())->method('overrideIconOverlay');
379 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'][$classReference] = $classReference;
380 $GLOBALS['T3_VAR']['getUserObj'][$classReference] = $hookMock;
381 \TYPO3\CMS\Backend\Utility\IconUtility::mapRecordOverlayToSpriteIconName('tt_content', array());
382 }
383
384 /**
385 * Tests whether a faulty overrideIconOverlay hook (the hook object cannot be found) is not called.
386 *
387 * @test
388 */
389 public function isFaultyOverrideIconOverlayHookNotCalled() {
390 $classReference = uniqid('user_overrideIconOverlayHook');
391 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'][$classReference] = $classReference;
392 $GLOBALS['T3_VAR']['getUserObj'][$classReference] = new \stdClass();
393 \TYPO3\CMS\Backend\Utility\IconUtility::mapRecordOverlayToSpriteIconName('tt_content', array());
394 }
395
396 }
397
398 ?>