[TASK] Render Resources using SVG in IconFactory
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Imaging / IconFactoryTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Imaging;
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
17 use Prophecy\Argument;
18 use TYPO3\CMS\Core\Imaging\Icon;
19 use TYPO3\CMS\Core\Imaging\IconFactory;
20 use TYPO3\CMS\Core\Imaging\IconProvider\FontawesomeIconProvider;
21 use TYPO3\CMS\Core\Resource\File;
22 use TYPO3\CMS\Core\Resource\Folder;
23 use TYPO3\CMS\Core\Resource\FolderInterface;
24 use TYPO3\CMS\Core\Resource\InaccessibleFolder;
25 use TYPO3\CMS\Core\Resource\ResourceStorage;
26 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
27
28 /**
29 * TestCase for \TYPO3\CMS\Core\Imaging\IconFactory
30 */
31 class IconFactoryTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
32
33 /**
34 * @var \TYPO3\CMS\Core\Imaging\IconFactory
35 */
36 protected $subject = NULL;
37
38 /**
39 * @var string
40 */
41 protected $notRegisteredIconIdentifier = 'my-super-unregistered-identifier';
42
43 /**
44 * @var string
45 */
46 protected $registeredIconIdentifier = 'actions-document-close';
47
48 /**
49 * @var string
50 */
51 protected $registeredSpinningIconIdentifier = 'spinning-icon';
52
53 /**
54 * @var \TYPO3\CMS\Core\Imaging\IconRegistry
55 */
56 protected $iconRegistryMock;
57
58 /**
59 * Set up
60 *
61 * @return void
62 */
63 protected function setUp() {
64 $this->iconRegistryMock = $this->prophesize(\TYPO3\CMS\Core\Imaging\IconRegistry::class);
65 $this->subject = new IconFactory($this->iconRegistryMock->reveal());
66
67 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(TRUE);
68 $this->iconRegistryMock->isDeprecated(Argument::any())->willReturn(FALSE);
69 $this->iconRegistryMock->getIconConfigurationByIdentifier(Argument::any())->willReturn([
70 'provider' => FontawesomeIconProvider::class,
71 'options' => array(
72 'name' => 'times',
73 'additionalClasses' => 'fa-fw'
74 )
75 ]);
76 }
77
78 /**
79 * DataProvider for icon sizes
80 *
81 * @return array
82 */
83 public function differentSizesDataProvider() {
84 return [
85 ['size ' . Icon::SIZE_SMALL => ['input' => Icon::SIZE_SMALL, 'expected' => Icon::SIZE_SMALL]],
86 ['size ' . Icon::SIZE_DEFAULT => ['input' => Icon::SIZE_DEFAULT, 'expected' => Icon::SIZE_DEFAULT]],
87 ['size ' . Icon::SIZE_LARGE => ['input' => Icon::SIZE_LARGE, 'expected' => Icon::SIZE_LARGE]]
88 ];
89 }
90
91 /**
92 * @test
93 */
94 public function getIconReturnsIconWithCorrectMarkupWrapperIfRegisteredIconIdentifierIsUsed() {
95 $this->assertContains('<span class="icon-markup">',
96 $this->subject->getIcon($this->registeredIconIdentifier)->render());
97 }
98
99 /**
100 * @test
101 */
102 public function getIconByIdentifierReturnsIconWithCorrectMarkupIfRegisteredIconIdentifierIsUsed() {
103 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-actions-document-close">',
104 $this->subject->getIcon($this->registeredIconIdentifier)->render());
105 }
106
107 /**
108 * @test
109 * @dataProvider differentSizesDataProvider
110 */
111 public function getIconByIdentifierAndSizeReturnsIconWithCorrectMarkupIfRegisteredIconIdentifierIsUsed($size) {
112 $this->assertContains('<span class="icon icon-size-' . $size['expected'] . ' icon-state-default icon-actions-document-close">',
113 $this->subject->getIcon($this->registeredIconIdentifier, $size['input'])->render());
114 }
115
116 /**
117 * @test
118 * @dataProvider differentSizesDataProvider
119 */
120 public function getIconByIdentifierAndSizeAndWithOverlayReturnsIconWithCorrectOverlayMarkupIfRegisteredIconIdentifierIsUsed($size) {
121 $this->assertContains('<span class="icon-overlay icon-overlay-readonly">',
122 $this->subject->getIcon($this->registeredIconIdentifier, $size['input'], 'overlay-readonly')->render());
123 }
124
125 /**
126 * @test
127 */
128 public function getIconReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed() {
129 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(FALSE);
130 $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
131 $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
132 'provider' => FontawesomeIconProvider::class,
133 'options' => array(
134 'name' => 'times-circle',
135 'additionalClasses' => 'fa-fw'
136 )
137 ]);
138 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-default-not-found">',
139 $this->subject->getIcon($this->notRegisteredIconIdentifier)->render());
140 }
141
142 /**
143 * @test
144 * @dataProvider differentSizesDataProvider
145 */
146 public function getIconByIdentifierAndSizeReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size) {
147 $this->iconRegistryMock->isRegistered(Argument::any())->willReturn(FALSE);
148 $this->iconRegistryMock->getDefaultIconIdentifier(Argument::any())->willReturn('default-not-found');
149 $this->iconRegistryMock->getIconConfigurationByIdentifier('default-not-found')->willReturn([
150 'provider' => FontawesomeIconProvider::class,
151 'options' => array(
152 'name' => 'times-circle',
153 'additionalClasses' => 'fa-fw'
154 )
155 ]);
156 $this->assertContains('<span class="icon icon-size-' . $size['expected'] . ' icon-state-default icon-default-not-found">',
157 $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'])->render());
158 }
159
160 /**
161 * @test
162 */
163 public function getIconReturnsCorrectMarkupIfIconIsRegisteredAsSpinningIcon() {
164 $this->iconRegistryMock->getIconConfigurationByIdentifier($this->registeredSpinningIconIdentifier)->willReturn([
165 'provider' => FontawesomeIconProvider::class,
166 'options' => array(
167 'name' => 'times-circle',
168 'additionalClasses' => 'fa-fw',
169 'spinning' => TRUE
170 )
171 ]);
172 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-' . $this->registeredSpinningIconIdentifier . ' icon-spin">',
173 $this->subject->getIcon($this->registeredSpinningIconIdentifier)->render());
174 }
175
176 /**
177 * @test
178 * @dataProvider differentSizesDataProvider
179 * @param string $size
180 */
181 public function getIconByIdentifierAndSizeAndOverlayReturnsNotFoundIconWithCorrectMarkupIfUnregisteredIdentifierIsUsed($size) {
182 $this->assertContains('<span class="icon-overlay icon-overlay-readonly">',
183 $this->subject->getIcon($this->notRegisteredIconIdentifier, $size['input'], 'overlay-readonly')->render());
184 }
185
186 /**
187 * @test
188 */
189 public function getIconThrowsExceptionIfInvalidSizeIsGiven() {
190 $this->setExpectedException('InvalidArgumentException');
191 $this->subject->getIcon($this->registeredIconIdentifier, 'foo')->render();
192 }
193
194 /**
195 * @test
196 * @param $deprecationSettings
197 * @param $expected
198 * @dataProvider getIconReturnsReplacementIconWhenDeprecatedDataProvider
199 */
200 public function getIconReturnsReplacementIconWhenDeprecated($deprecationSettings, $expected) {
201 $this->iconRegistryMock->isDeprecated($this->registeredIconIdentifier)->willReturn(TRUE);
202 $this->iconRegistryMock->getDeprecationSettings($this->registeredIconIdentifier)->willReturn($deprecationSettings);
203
204 $this->assertContains(
205 $expected,
206 $this->subject->getIcon($this->registeredIconIdentifier, Icon::SIZE_SMALL)->render()
207 );
208 }
209
210 /**
211 * Data provider for getIconReturnsReplacementIconWhenDeprecated
212 *
213 * @return array
214 */
215 public function getIconReturnsReplacementIconWhenDeprecatedDataProvider() {
216 return array(
217 'Deprecated icon returns replacement' => [
218 [
219 'message' => '%s is deprecated since TYPO3 CMS 7, this icon will be removed in TYPO3 CMS 8',
220 'replacement' => 'alternative-icon-identifier' // must be registered
221 ],
222 '<span class="icon icon-size-small icon-state-default icon-alternative-icon-identifier">'
223 ],
224 'Deprecated icon returns default icon' => [
225 [
226 'message' => '%s is deprecated since TYPO3 CMS 7, this icon will be removed in TYPO3 CMS 8'
227 ],
228 '<span class="icon icon-size-small icon-state-default icon-actions-document-close">'
229 ],
230 );
231 }
232
233 //
234 // Tests for getIconForFileExtension
235 //
236
237 /**
238 * Tests the return of an icon for a file without extension
239 *
240 * @test
241 */
242 public function getIconForFileWithNoFileTypeReturnsDefaultFileIcon() {
243 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-mimetypes-other-other">',
244 $this->subject->getIconForFileExtension('')->render());
245 }
246
247 /**
248 * Tests the return of an icon for an unknown file type
249 *
250 * @test
251 */
252 public function getIconForFileWithUnknownFileTypeReturnsDefaultFileIcon() {
253 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-mimetypes-other-other">',
254 $this->subject->getIconForFileExtension('foo')->render());
255 }
256
257 /**
258 * Tests the return of an icon for a file with extension pdf
259 *
260 * @test
261 */
262 public function getIconForFileWithFileTypePdfReturnsPdfSprite() {
263 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-mimetypes-pdf">',
264 $this->subject->getIconForFileExtension('pdf')->render());
265 }
266
267 /**
268 * Tests the return of an icon for a file with extension png
269 *
270 * @test
271 */
272 public function getIconForFileWithFileTypePngReturnsPngSprite() {
273 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-mimetypes-media-image">',
274 $this->subject->getIconForFileExtension('png')->render());
275 }
276
277 //
278 // Tests for getIconForResource
279 //
280
281 /**
282 * @test
283 */
284 public function getIconForResourceReturnsCorrectMarkupForFileResources() {
285 $resourceProphecy = $this->prophesize(File::class);
286 $resourceProphecy->isMissing()->willReturn(FALSE);
287 $resourceProphecy->getExtension()->willReturn('pdf');
288
289 $this->assertContains('<span class="icon icon-size-default icon-state-default icon-mimetypes-pdf">',
290 $this->subject->getIconForResource($resourceProphecy->reveal())->render());
291 }
292
293 }