TYPO3 CMS  TYPO3_8-7
IconFactoryTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
22 
26 class IconFactoryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
27 {
31  protected $subject = null;
32 
36  protected $notRegisteredIconIdentifier = 'my-super-unregistered-identifier';
37 
41  protected $registeredIconIdentifier = 'actions-close';
42 
46  protected $registeredSpinningIconIdentifier = 'spinning-icon';
47 
51  protected $iconRegistryMock;
52 
56  protected $mockRecord = [
57  'header' => 'dummy content header',
58  'uid' => '1',
59  'pid' => '1',
60  'image' => '',
61  'hidden' => '0',
62  'starttime' => '0',
63  'endtime' => '0',
64  'fe_group' => '',
65  'CType' => 'text',
66  't3ver_id' => '0',
67  't3ver_state' => '0',
68  't3ver_wsid' => '0',
69  'sys_language_uid' => '0',
70  'l18n_parent' => '0',
71  'subheader' => '',
72  'bodytext' => '',
73  ];
74 
78  protected function setUp()
79  {
80  $this->iconRegistryMock = $this->prophesize(\TYPO3\CMS\Core\Imaging\IconRegistry::class);
81  $this->subject = new IconFactory($this->iconRegistryMock->reveal());
82 
83  $this->iconRegistryMock->isRegistered('tcarecords--default')->willReturn(false);
84  $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(true);
85  $this->iconRegistryMock->isDeprecated(Argument::any())->willReturn(false);
86  $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
87  $this->iconRegistryMock->getIconIdentifierForMimeType('application/pdf')->willReturn('mimetypes-pdf');
88  $this->iconRegistryMock->getIconIdentifierForMimeType('image/*')->willReturn('mimetypes-media-image');
89  $this->iconRegistryMock->getIconIdentifierForMimeType(Argument::any())->willReturn(null);
90  $this->iconRegistryMock->getIconIdentifierForFileExtension(Argument::any())->willReturn('mimetypes-other-other');
91  $this->iconRegistryMock->getIconIdentifierForFileExtension('foo')->willReturn('mimetypes-other-other');
92  $this->iconRegistryMock->getIconIdentifierForFileExtension('pdf')->willReturn('mimetypes-pdf');
93  $this->iconRegistryMock->getIconIdentifierForFileExtension('png')->willReturn('mimetypes-media-image');
94  $this->iconRegistryMock->getIconConfigurationByIdentifier(Argument::any())->willReturn([
95  'provider' => FontawesomeIconProvider::class,
96  'options' => [
97  'name' => 'times',
98  'additionalClasses' => 'fa-fw'
99  ]
100  ]);
101  }
102 
108  public function differentSizesDataProvider()
109  {
110  return [
111  ['size ' . Icon::SIZE_SMALL => ['input' => Icon::SIZE_SMALL, 'expected' => Icon::SIZE_SMALL]],
112  ['size ' . Icon::SIZE_DEFAULT => ['input' => Icon::SIZE_DEFAULT, 'expected' => Icon::SIZE_DEFAULT]],
113  ['size ' . Icon::SIZE_LARGE => ['input' => Icon::SIZE_LARGE, 'expected' => Icon::SIZE_LARGE]]
114  ];
115  }
116 
121  {
122  $this->assertContains(
123  '<span class="icon-markup">',
124  $this->subject->getIcon($this->registeredIconIdentifier)->render()
125  );
126  }
127 
132  {
133  $this->assertContains(
134  '<span class="t3js-icon icon icon-size-default icon-state-default icon-actions-close" data-identifier="actions-close">',
135  $this->subject->getIcon($this->registeredIconIdentifier)->render()
136  );
137  }
138 
144  {
145  $this->assertContains(
146  '<span class="t3js-icon icon icon-size-' . $size['expected'] . ' icon-state-default icon-actions-close" data-identifier="actions-close">',
147  $this->subject->getIcon($this->registeredIconIdentifier, $size['input'])->render()
148  );
149  }
150 
156  {
157  $this->assertContains(
158  '<span class="icon-overlay icon-overlay-readonly">',
159  $this->subject->getIcon($this->registeredIconIdentifier, $size['input'], 'overlay-readonly')->render()
160  );
161  }
162 
167  {
168  $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(false);
169  $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
170  $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
171  'provider' => FontawesomeIconProvider::class,
172  'options' => [
173  'name' => 'times-circle',
174  'additionalClasses' => 'fa-fw'
175  ]
176  ]);
177  $this->assertContains(
178  '<span class="t3js-icon icon icon-size-default icon-state-default icon-default-not-found" data-identifier="default-not-found">',
179  $this->subject->getIcon($this->notRegisteredIconIdentifier)->render()
180  );
181  }
182 
188  {
189  $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(false);
190  $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
191  $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
192  'provider' => FontawesomeIconProvider::class,
193  'options' => [
194  'name' => 'times-circle',
195  'additionalClasses' => 'fa-fw'
196  ]
197  ]);
198  $this->assertContains(
199  '<span class="t3js-icon icon icon-size-' . $size['expected'] . ' icon-state-default icon-default-not-found" data-identifier="default-not-found">',
200  $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'])->render()
201  );
202  }
203 
208  {
209  $this->iconRegistryMock->getIconConfigurationByIdentifier($this->registeredSpinningIconIdentifier)->willReturn([
210  'provider' => FontawesomeIconProvider::class,
211  'options' => [
212  'name' => 'times-circle',
213  'additionalClasses' => 'fa-fw',
214  'spinning' => true
215  ]
216  ]);
217  $this->assertContains(
218  '<span class="t3js-icon icon icon-size-default icon-state-default icon-' . $this->registeredSpinningIconIdentifier . ' icon-spin" data-identifier="spinning-icon">',
219  $this->subject->getIcon($this->registeredSpinningIconIdentifier)->render()
220  );
221  }
222 
229  {
230  $this->assertContains(
231  '<span class="icon-overlay icon-overlay-readonly">',
232  $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'], 'overlay-readonly')->render()
233  );
234  }
235 
240  {
241  $this->expectException(\InvalidArgumentException::class);
242  $this->subject->getIcon($this->registeredIconIdentifier, 'foo')->render();
243  }
244 
245  //
246  // Tests for getIconForFileExtension
247  //
248 
255  {
256  $this->assertContains(
257  '<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">',
258  $this->subject->getIconForFileExtension('')->render()
259  );
260  }
261 
268  {
269  $this->assertContains(
270  '<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">',
271  $this->subject->getIconForFileExtension('foo')->render()
272  );
273  }
274 
281  {
282  $this->assertContains(
283  '<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">',
284  $this->subject->getIconForFileExtension('pdf')->render()
285  );
286  }
287 
294  {
295  $this->assertContains(
296  '<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-media-image" data-identifier="mimetypes-media-image">',
297  $this->subject->getIconForFileExtension('png')->render()
298  );
299  }
300 
305  {
306  $resourceProphecy = $this->prophesize(File::class);
307  $resourceProphecy->isMissing()->willReturn(false);
308  $resourceProphecy->getExtension()->willReturn('pdf');
309  $resourceProphecy->getMimeType()->willReturn('');
310 
311  $this->assertContains(
312  '<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">',
313  $this->subject->getIconForResource($resourceProphecy->reveal())->render()
314  );
315  }
316 
318  // Tests concerning getIconForResource
320 
326  {
327  $fileObject = $this->getTestSubjectFileObject('');
328  $result = $this->subject->getIconForResource($fileObject)->render();
329  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">', $result);
330  }
331 
338  {
339  $fileObject = $this->getTestSubjectFileObject('foo');
340  $result = $this->subject->getIconForResource($fileObject)->render();
341  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">', $result);
342  }
343 
350  {
351  $fileObject = $this->getTestSubjectFileObject('pdf');
352  $result = $this->subject->getIconForResource($fileObject)->render();
353  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">', $result);
354  }
355 
362  {
363  $fileObject = $this->getTestSubjectFileObject('pdf', 'application/pdf');
364  $result = $this->subject->getIconForResource($fileObject)->render();
365  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">', $result);
366  }
367 
374  {
375  $fileObject = $this->getTestSubjectFileObject('custom', 'image/my-custom-extension');
376  $result = $this->subject->getIconForResource($fileObject)->render();
377  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-media-image" data-identifier="mimetypes-media-image">', $result);
378  }
379 
386  {
387  $fileObject = $this->getTestSubjectFileObject('png', 'image/png');
388  $result = $this->subject->getIconForResource($fileObject)->render();
389  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-media-image" data-identifier="mimetypes-media-image">', $result);
390  }
391 
398  {
399  $folderObject = $this->getTestSubjectFolderObject('/test');
400  $result = $this->subject->getIconForResource($folderObject)->render();
401  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-apps-filetree-folder-default" data-identifier="apps-filetree-folder-default">', $result);
402  }
403 
410  {
411  $folderObject = $this->getTestSubjectFolderObject('/test');
412  $result = $this->subject->getIconForResource($folderObject, Icon::SIZE_DEFAULT, null, ['folder-open' => true])->render();
413  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-apps-filetree-folder-opened" data-identifier="apps-filetree-folder-opened">', $result);
414  }
415 
422  {
423  $folderObject = $this->getTestSubjectFolderObject('/');
424  $result = $this->subject->getIconForResource($folderObject)->render();
425  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-apps-filetree-root" data-identifier="apps-filetree-root">', $result);
426  }
427 
434  {
435  $folderObject = $this->getTestSubjectFolderObject('/mount');
436  $result = $this->subject->getIconForResource($folderObject, Icon::SIZE_DEFAULT, null, ['mount-root' => true])->render();
437  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-apps-filetree-mount" data-identifier="apps-filetree-mount">', $result);
438  }
439 
440  //
441  // Test for getIconForRecord
442  //
443 
450  {
451  $this->assertContains(
452  '<span class="t3js-icon icon icon-size-default icon-state-default icon-default-not-found" data-identifier="default-not-found">',
453  $this->subject->getIconForRecord('', [])->render()
454  );
455  }
456 
463  {
464  $GLOBALS['TCA'] = [
465  'tt_content' => [
466  'ctrl' => [
467  'typeicon_column' => 'CType',
468  'typeicon_classes' => [
469  'default' => 'mimetypes-x-content-text',
470  ],
471  ],
472  ],
473  ];
474  $result = $this->subject->getIconForRecord('tt_content', [])->render();
475  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-x-content-text" data-identifier="mimetypes-x-content-text">', $result);
476  }
477 
484  {
485  $GLOBALS['TCA'] = [
486  'tt_content' => [
487  'ctrl' => [
488  'typeicon_column' => 'CType',
489  'typeicon_classes' => [
490  'text' => 'mimetypes-x-content-text',
491  ],
492  ],
493  ],
494  ];
495  $result = $this->subject->getIconForRecord('tt_content', $this->mockRecord)->render();
496  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-x-content-text" data-identifier="mimetypes-x-content-text">', $result);
497  }
498 
505  {
506  $GLOBALS['TCA'] = [
507  'tt_content' => [
508  'ctrl' => [
509  'typeicon_column' => 'CType',
510  'typeicon_classes' => [
511  'list' => 'mimetypes-x-content-plugin',
512  ],
513  ],
514  ],
515  ];
517  $mockRecord['CType'] = 'list';
518  $result = $this->subject->getIconForRecord('tt_content', $mockRecord)->render();
519  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-x-content-plugin" data-identifier="mimetypes-x-content-plugin">', $result);
520  }
521 
528  {
529  $GLOBALS['TCA'] = [
530  'tt_content' => [
531  'ctrl' => [
532  'enablecolumns' => [
533  'disabled' => 'hidden',
534  ],
535  'typeicon_column' => 'CType',
536  'typeicon_classes' => [
537  'text' => 'mimetypes-x-content-text',
538  ],
539  ],
540  ],
541  ];
543  $mockRecord['hidden'] = '1';
544  $result = $this->subject->getIconForRecord('tt_content', $mockRecord)->render();
545  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-x-content-text" data-identifier="mimetypes-x-content-text">', $result);
546  $this->assertContains('<span class="icon-overlay icon-overlay-hidden">', $result);
547  }
548 
556  protected function getTestSubjectFileObject($extension, $mimeType = '')
557  {
558  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
559  $mockedFile = $this->getMockBuilder(\TYPO3\CMS\Core\Resource\File::class)
560  ->setConstructorArgs([[], $mockedStorage])
561  ->getMock();
562  $mockedFile->expects($this->atMost(1))->method('getExtension')->will($this->returnValue($extension));
563  $mockedFile->expects($this->atLeastOnce())->method('getMimeType')->will($this->returnValue($mimeType));
564  return $mockedFile;
565  }
566 
573  protected function getTestSubjectFolderObject($identifier)
574  {
575  $mockedStorage = $this->createMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class);
576  $mockedStorage->expects($this->any())->method('getRootLevelFolder')->will($this->returnValue(
577  new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, '/', '/')
578  ));
579  $mockedStorage->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(true));
580  $mockedStorage->expects($this->any())->method('isBrowsable')->will($this->returnValue(true));
581  return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $identifier, $identifier);
582  }
583 }
getIconByIdentifierAndSizeAndOverlayReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size)
getIconByIdentifierAndSizeReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size)
getIconByIdentifierAndSizeAndWithOverlayReturnsIconWithCorrectOverlayMarkupIfRegisteredIconIdentifierIsUsed($size)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']