[TASK] Move fixture files from tests/
[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 $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 clearstatcache();
108 $resultFilePermissions = substr(decoct(fileperms($targetFilename)), 2);
109 \TYPO3\CMS\Core\Utility\GeneralUtility::unlink_tempfile($targetFilename);
110 $this->assertEquals($resultFilePermissions, '0777');
111 }
112
113 //////////////////////////////////////////
114 // Tests concerning getSpriteIconClasses
115 //////////////////////////////////////////
116 /**
117 * Tests whether an empty string returns 't3-icon'
118 *
119 * @test
120 */
121 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 * @test
129 */
130 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 * @test
138 */
139 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 * @test
149 */
150 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 * @test
160 */
161 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 // Tests concerning getSpriteIcon
169 //////////////////////////////////////////
170 /**
171 * Tests whether an empty string returns a span with the missing sprite
172 *
173 * @test
174 */
175 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 * @test
183 */
184 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 * @test
192 */
193 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 * @test
201 */
202 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 * @test
210 */
211 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 * @test
219 */
220 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 * @test
228 */
229 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 * @test
239 */
240 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 // Tests concerning getSpriteIconForRecord
248 //////////////////////////////////////////
249 /**
250 * Tests the returns of NULL table + empty array
251 *
252 * @test
253 */
254 public function getSpriteIconForRecordWithNullTableReturnsMissingIcon() {
255 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('', array());
256 $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 * @test
263 */
264 public function getSpriteIconForRecordWithEmptyRecordReturnsNormalSprite() {
265 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', array());
266 $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 * @test
273 */
274 public function getSpriteIconForRecordWithMockRecordReturnsNormalSprite() {
275 $mockRecord = $this->mockRecord;
276 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
277 $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 * @test
284 */
285 public function getSpriteIconForRecordWithMockRecordAndOptionsReturnsNormalSprite() {
286 $mockRecord = $this->mockRecord;
287 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord, array('class' => 'foo', 'title' => 'bar'));
288 $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 * @test
295 */
296 public function getSpriteIconForRecordWithMockRecordOfTypePluginReturnsPluginSprite() {
297 $mockRecord = $this->mockRecord;
298 $mockRecord['CType'] = 'list';
299 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForRecord('tt_content', $mockRecord);
300 $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 * @test
307 */
308 public function getSpriteIconForRecordWithMockRecordWithHiddenFlagReturnsNormalSpriteAndOverlay() {
309 $mockRecord = $this->mockRecord;
310 $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 // Tests concerning getSpriteIconForFile
318 //////////////////////////////////////////
319 /**
320 * Tests the returns of no file
321 *
322 * @test
323 */
324 public function getSpriteIconForFileWithNoFileTypeReturnsOtherSprite() {
325 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('');
326 $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 * @test
333 */
334 public function getSpriteIconForFileWithNoUnknowFileTypeReturnsOtherSprite() {
335 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('foo');
336 $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 * @test
343 */
344 public function getSpriteIconForFileWithPdfReturnsPdfSprite() {
345 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('pdf');
346 $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 * @test
353 */
354 public function getSpriteIconForFileWithPngReturnsPngSprite() {
355 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('png');
356 $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 * @test
363 */
364 public function getSpriteIconForFileWithPngAndOptionsReturnsPngSpriteAndOptions() {
365 $result = \TYPO3\CMS\Backend\Utility\IconUtility::getSpriteIconForFile('png', array('title' => 'bar'));
366 $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 * @test
373 */
374 public function isOverrideIconOverlayHookCalled() {
375 $classReference = uniqid('user_overrideIconOverlayHook');
376 $hookMock = $this->getMock($classReference, array('overrideIconOverlay'), array());
377 $hookMock->expects($this->once())->method('overrideIconOverlay');
378 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_iconworks.php']['overrideIconOverlay'][$classReference] = $classReference;
379 $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 * @test
387 */
388 public function isFaultyOverrideIconOverlayHookNotCalled() {
389 $classReference = uniqid('user_overrideIconOverlayHook');
390 $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 ?>