TYPO3 CMS  TYPO3_7-6
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 
27 {
31  protected $subject = null;
32 
36  protected $notRegisteredIconIdentifier = 'my-super-unregistered-identifier';
37 
41  protected $registeredIconIdentifier = 'actions-document-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 
80  protected function setUp()
81  {
82  $this->iconRegistryMock = $this->prophesize(\TYPO3\CMS\Core\Imaging\IconRegistry::class);
83  $this->subject = new IconFactory($this->iconRegistryMock->reveal());
84 
85  $this->iconRegistryMock->isRegistered('tcarecords--default')->willReturn(false);
86  $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(true);
87  $this->iconRegistryMock->isDeprecated(Argument::any())->willReturn(false);
88  $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
89  $this->iconRegistryMock->getIconIdentifierForMimeType('application/pdf')->willReturn('mimetypes-pdf');
90  $this->iconRegistryMock->getIconIdentifierForMimeType('image/*')->willReturn('mimetypes-media-image');
91  $this->iconRegistryMock->getIconIdentifierForMimeType(Argument::any())->willReturn(null);
92  $this->iconRegistryMock->getIconIdentifierForFileExtension(Argument::any())->willReturn('mimetypes-other-other');
93  $this->iconRegistryMock->getIconIdentifierForFileExtension('foo')->willReturn('mimetypes-other-other');
94  $this->iconRegistryMock->getIconIdentifierForFileExtension('pdf')->willReturn('mimetypes-pdf');
95  $this->iconRegistryMock->getIconIdentifierForFileExtension('png')->willReturn('mimetypes-media-image');
96  $this->iconRegistryMock->getIconConfigurationByIdentifier(Argument::any())->willReturn([
97  'provider' => FontawesomeIconProvider::class,
98  'options' => [
99  'name' => 'times',
100  'additionalClasses' => 'fa-fw'
101  ]
102  ]);
103  }
104 
110  public function differentSizesDataProvider()
111  {
112  return [
113  ['size ' . Icon::SIZE_SMALL => ['input' => Icon::SIZE_SMALL, 'expected' => Icon::SIZE_SMALL]],
114  ['size ' . Icon::SIZE_DEFAULT => ['input' => Icon::SIZE_DEFAULT, 'expected' => Icon::SIZE_DEFAULT]],
115  ['size ' . Icon::SIZE_LARGE => ['input' => Icon::SIZE_LARGE, 'expected' => Icon::SIZE_LARGE]]
116  ];
117  }
118 
123  {
124  $this->assertContains('<span class="icon-markup">',
125  $this->subject->getIcon($this->registeredIconIdentifier)->render());
126  }
127 
132  {
133  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-actions-document-close" data-identifier="actions-document-close">',
134  $this->subject->getIcon($this->registeredIconIdentifier)->render());
135  }
136 
142  {
143  $this->assertContains('<span class="t3js-icon icon icon-size-' . $size['expected'] . ' icon-state-default icon-actions-document-close" data-identifier="actions-document-close">',
144  $this->subject->getIcon($this->registeredIconIdentifier, $size['input'])->render());
145  }
146 
152  {
153  $this->assertContains('<span class="icon-overlay icon-overlay-readonly">',
154  $this->subject->getIcon($this->registeredIconIdentifier, $size['input'], 'overlay-readonly')->render());
155  }
156 
161  {
162  $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(false);
163  $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
164  $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
165  'provider' => FontawesomeIconProvider::class,
166  'options' => [
167  'name' => 'times-circle',
168  'additionalClasses' => 'fa-fw'
169  ]
170  ]);
171  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-default-not-found" data-identifier="default-not-found">',
172  $this->subject->getIcon($this->notRegisteredIconIdentifier)->render());
173  }
174 
180  {
181  $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(false);
182  $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
183  $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
184  'provider' => FontawesomeIconProvider::class,
185  'options' => [
186  'name' => 'times-circle',
187  'additionalClasses' => 'fa-fw'
188  ]
189  ]);
190  $this->assertContains('<span class="t3js-icon icon icon-size-' . $size['expected'] . ' icon-state-default icon-default-not-found" data-identifier="default-not-found">',
191  $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'])->render());
192  }
193 
198  {
199  $this->iconRegistryMock->getIconConfigurationByIdentifier($this->registeredSpinningIconIdentifier)->willReturn([
200  'provider' => FontawesomeIconProvider::class,
201  'options' => [
202  'name' => 'times-circle',
203  'additionalClasses' => 'fa-fw',
204  'spinning' => true
205  ]
206  ]);
207  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-' . $this->registeredSpinningIconIdentifier . ' icon-spin" data-identifier="spinning-icon">',
208  $this->subject->getIcon($this->registeredSpinningIconIdentifier)->render());
209  }
210 
217  {
218  $this->assertContains('<span class="icon-overlay icon-overlay-readonly">',
219  $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'], 'overlay-readonly')->render());
220  }
221 
226  {
227  $this->setExpectedException('InvalidArgumentException');
228  $this->subject->getIcon($this->registeredIconIdentifier, 'foo')->render();
229  }
230 
237  public function getIconReturnsReplacementIconWhenDeprecated($deprecationSettings, $expected)
238  {
239  $this->iconRegistryMock->isDeprecated($this->registeredIconIdentifier)->willReturn(true);
240  $this->iconRegistryMock->getDeprecationSettings($this->registeredIconIdentifier)->willReturn($deprecationSettings);
241 
242  $this->assertContains(
243  $expected,
244  $this->subject->getIcon($this->registeredIconIdentifier, Icon::SIZE_SMALL)->render()
245  );
246  }
247 
254  {
255  return [
256  'Deprecated icon returns replacement' => [
257  [
258  'message' => '%s is deprecated since TYPO3 CMS 7, this icon will be removed in TYPO3 CMS 8',
259  'replacement' => 'alternative-icon-identifier' // must be registered
260  ],
261  '<span class="t3js-icon icon icon-size-small icon-state-default icon-alternative-icon-identifier" data-identifier="alternative-icon-identifier">'
262  ],
263  'Deprecated icon returns default icon' => [
264  [
265  'message' => '%s is deprecated since TYPO3 CMS 7, this icon will be removed in TYPO3 CMS 8'
266  ],
267  '<span class="t3js-icon icon icon-size-small icon-state-default icon-actions-document-close" data-identifier="actions-document-close">'
268  ],
269  ];
270  }
271 
272  //
273  // Tests for getIconForFileExtension
274  //
275 
282  {
283  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">',
284  $this->subject->getIconForFileExtension('')->render());
285  }
286 
293  {
294  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">',
295  $this->subject->getIconForFileExtension('foo')->render());
296  }
297 
304  {
305  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">',
306  $this->subject->getIconForFileExtension('pdf')->render());
307  }
308 
315  {
316  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-media-image" data-identifier="mimetypes-media-image">',
317  $this->subject->getIconForFileExtension('png')->render());
318  }
319 
324  {
325  $resourceProphecy = $this->prophesize(File::class);
326  $resourceProphecy->isMissing()->willReturn(false);
327  $resourceProphecy->getExtension()->willReturn('pdf');
328  $resourceProphecy->getMimeType()->willReturn('');
329 
330  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">',
331  $this->subject->getIconForResource($resourceProphecy->reveal())->render());
332  }
333 
335  // Tests concerning getIconForResource
337 
343  {
344  $fileObject = $this->getTestSubjectFileObject('');
345  $result = $this->subject->getIconForResource($fileObject)->render();
346  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">', $result);
347  }
348 
355  {
356  $fileObject = $this->getTestSubjectFileObject('foo');
357  $result = $this->subject->getIconForResource($fileObject)->render();
358  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-other-other" data-identifier="mimetypes-other-other">', $result);
359  }
360 
367  {
368  $fileObject = $this->getTestSubjectFileObject('pdf');
369  $result = $this->subject->getIconForResource($fileObject)->render();
370  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">', $result);
371  }
372 
379  {
380  $fileObject = $this->getTestSubjectFileObject('pdf', 'application/pdf');
381  $result = $this->subject->getIconForResource($fileObject)->render();
382  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-pdf" data-identifier="mimetypes-pdf">', $result);
383  }
384 
391  {
392  $fileObject = $this->getTestSubjectFileObject('custom', 'image/my-custom-extension');
393  $result = $this->subject->getIconForResource($fileObject)->render();
394  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-media-image" data-identifier="mimetypes-media-image">', $result);
395  }
396 
403  {
404  $fileObject = $this->getTestSubjectFileObject('png', 'image/png');
405  $result = $this->subject->getIconForResource($fileObject)->render();
406  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-mimetypes-media-image" data-identifier="mimetypes-media-image">', $result);
407  }
408 
415  {
416  $folderObject = $this->getTestSubjectFolderObject('/test');
417  $result = $this->subject->getIconForResource($folderObject)->render();
418  $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);
419  }
420 
427  {
428  $folderObject = $this->getTestSubjectFolderObject('/test');
429  $result = $this->subject->getIconForResource($folderObject, Icon::SIZE_DEFAULT, null, ['folder-open' => true])->render();
430  $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);
431  }
432 
439  {
440  $folderObject = $this->getTestSubjectFolderObject('/');
441  $result = $this->subject->getIconForResource($folderObject)->render();
442  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-apps-filetree-root" data-identifier="apps-filetree-root">', $result);
443  }
444 
451  {
452  $folderObject = $this->getTestSubjectFolderObject('/mount');
453  $result = $this->subject->getIconForResource($folderObject, Icon::SIZE_DEFAULT, null, ['mount-root' => true])->render();
454  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-apps-filetree-mount" data-identifier="apps-filetree-mount">', $result);
455  }
456 
457  //
458  // Test for getIconForRecord
459  //
460 
467  {
468  $this->assertContains('<span class="t3js-icon icon icon-size-default icon-state-default icon-default-not-found" data-identifier="default-not-found">',
469  $this->subject->getIconForRecord('', [])->render());
470  }
471 
478  {
479  $GLOBALS['TCA'] = [
480  'tt_content' => [
481  'ctrl' => [
482  'typeicon_column' => 'CType',
483  'typeicon_classes' => [
484  'default' => 'mimetypes-x-content-text',
485  ],
486  ],
487  ],
488  ];
489  $result = $this->subject->getIconForRecord('tt_content', [])->render();
490  $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);
491  }
492 
499  {
500  $GLOBALS['TCA'] = [
501  'tt_content' => [
502  'ctrl' => [
503  'typeicon_column' => 'CType',
504  'typeicon_classes' => [
505  'text' => 'mimetypes-x-content-text',
506  ],
507  ],
508  ],
509  ];
510  $result = $this->subject->getIconForRecord('tt_content', $this->mockRecord)->render();
511  $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);
512  }
513 
520  {
521  $GLOBALS['TCA'] = [
522  'tt_content' => [
523  'ctrl' => [
524  'typeicon_column' => 'CType',
525  'typeicon_classes' => [
526  'list' => 'mimetypes-x-content-plugin',
527  ],
528  ],
529  ],
530  ];
532  $mockRecord['CType'] = 'list';
533  $result = $this->subject->getIconForRecord('tt_content', $mockRecord)->render();
534  $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);
535  }
536 
543  {
544  $GLOBALS['TCA'] = [
545  'tt_content' => [
546  'ctrl' => [
547  'enablecolumns' => [
548  'disabled' => 'hidden',
549  ],
550  'typeicon_column' => 'CType',
551  'typeicon_classes' => [
552  'text' => 'mimetypes-x-content-text',
553  ],
554  ],
555  ],
556  ];
558  $mockRecord['hidden'] = '1';
559  $result = $this->subject->getIconForRecord('tt_content', $mockRecord)->render();
560  $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);
561  $this->assertContains('<span class="icon-overlay icon-overlay-hidden">', $result);
562  }
563 
571  protected function getTestSubjectFileObject($extension, $mimeType = '')
572  {
573  $mockedStorage = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
574  $mockedFile = $this->getMock(\TYPO3\CMS\Core\Resource\File::class, [], [[], $mockedStorage]);
575  $mockedFile->expects($this->atMost(1))->method('getExtension')->will($this->returnValue($extension));
576  $mockedFile->expects($this->atLeastOnce())->method('getMimeType')->will($this->returnValue($mimeType));
577  return $mockedFile;
578  }
579 
586  protected function getTestSubjectFolderObject($identifier)
587  {
588  $mockedStorage = $this->getMock(\TYPO3\CMS\Core\Resource\ResourceStorage::class, [], [], '', false);
589  $mockedStorage->expects($this->any())->method('getRootLevelFolder')->will($this->returnValue(
590  new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, '/', '/')
591  ));
592  $mockedStorage->expects($this->any())->method('checkFolderActionPermission')->will($this->returnValue(true));
593  $mockedStorage->expects($this->any())->method('isBrowsable')->will($this->returnValue(true));
594  return new \TYPO3\CMS\Core\Resource\Folder($mockedStorage, $identifier, $identifier);
595  }
596 }
getIconByIdentifierAndSizeAndOverlayReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size)
getIconByIdentifierAndSizeReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size)
getIconByIdentifierAndSizeAndWithOverlayReturnsIconWithCorrectOverlayMarkupIfRegisteredIconIdentifierIsUsed($size)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']
getIconReturnsReplacementIconWhenDeprecated($deprecationSettings, $expected)