169ce32baaf80b7ea8d360c07c80448c0e387a2f
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Imaging / IconRegistryTest.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 TYPO3\CMS\Core\Imaging\IconProvider\FontawesomeIconProvider;
18 use TYPO3\CMS\Core\Imaging\IconProviderInterface;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20
21 /**
22 * TestCase for \TYPO3\CMS\Core\Imaging\IconRegistry
23 */
24 class IconRegistryTest extends \TYPO3\TestingFramework\Core\Unit\UnitTestCase
25 {
26 /**
27 * @var \TYPO3\CMS\Core\Imaging\IconRegistry
28 */
29 protected $subject = null;
30
31 /**
32 * @var string
33 */
34 protected $notRegisteredIconIdentifier = 'my-super-unregistered-identifier';
35
36 /**
37 * Set up
38 */
39 protected function setUp()
40 {
41 $this->subject = new \TYPO3\CMS\Core\Imaging\IconRegistry();
42 }
43
44 /**
45 * @test
46 */
47 public function getDefaultIconIdentifierReturnsTheCorrectDefaultIconIdentifierString()
48 {
49 $result = $this->subject->getDefaultIconIdentifier();
50 $this->assertEquals($result, 'default-not-found');
51 }
52
53 /**
54 * @test
55 */
56 public function isRegisteredReturnsTrueForRegisteredIcon()
57 {
58 $result = $this->subject->isRegistered($this->subject->getDefaultIconIdentifier());
59 $this->assertEquals($result, true);
60 }
61
62 /**
63 * @test
64 */
65 public function isRegisteredReturnsFalseForNotRegisteredIcon()
66 {
67 $result = $this->subject->isRegistered($this->notRegisteredIconIdentifier);
68 $this->assertEquals($result, false);
69 }
70
71 /**
72 * @test
73 */
74 public function registerIconAddNewIconToRegistry()
75 {
76 $unregisterdIcon = 'foo-bar-unregistered';
77 $this->assertFalse($this->subject->isRegistered($unregisterdIcon));
78 $this->subject->registerIcon($unregisterdIcon, FontawesomeIconProvider::class, [
79 'name' => 'pencil',
80 'additionalClasses' => 'fa-fw'
81 ]);
82 $this->assertTrue($this->subject->isRegistered($unregisterdIcon));
83 }
84
85 /**
86 * @test
87 */
88 public function registerIconThrowsInvalidArgumentExceptionWithInvalidIconProvider()
89 {
90 $this->expectException(\InvalidArgumentException::class);
91 $this->expectExceptionCode(1437425803);
92
93 $this->subject->registerIcon($this->notRegisteredIconIdentifier, GeneralUtility::class);
94 }
95
96 /**
97 * @test
98 */
99 public function getIconConfigurationByIdentifierThrowsExceptionWithUnregisteredIconIdentifier()
100 {
101 $this->expectException(\TYPO3\CMS\Core\Exception::class);
102 $this->expectExceptionCode(1437425804);
103
104 $this->subject->getIconConfigurationByIdentifier($this->notRegisteredIconIdentifier);
105 }
106
107 /**
108 * @test
109 */
110 public function getIconConfigurationByIdentifierReturnsCorrectConfiguration()
111 {
112 $result = $this->subject->getIconConfigurationByIdentifier('default-not-found');
113 // result must contain at least provider and options array
114 $this->assertArrayHasKey('provider', $result);
115 $this->assertArrayHasKey('options', $result);
116 // the provider must implement the IconProviderInterface
117 $this->assertTrue(in_array(IconProviderInterface::class, class_implements($result['provider'])));
118 }
119
120 /**
121 * @test
122 */
123 public function getAllRegisteredIconIdentifiersReturnsAnArrayWithIconIdentiefiers()
124 {
125 $this->assertInternalType('array', $this->subject->getAllRegisteredIconIdentifiers());
126 }
127
128 /**
129 * @test
130 */
131 public function getAllRegisteredIconIdentifiersReturnsArrayWithAllRegisteredIconIdentifiers()
132 {
133 $result = $this->subject->getAllRegisteredIconIdentifiers();
134 $this->assertInternalType('array', $result);
135 $this->assertContains('default-not-found', $result);
136 }
137
138 /**
139 * @test
140 */
141 public function getIconIdentifierForFileExtensionReturnsDefaultIconIdentifierForEmptyFileExtension()
142 {
143 $result = $this->subject->getIconIdentifierForFileExtension('');
144 $this->assertEquals('mimetypes-other-other', $result);
145 }
146
147 /**
148 * @test
149 */
150 public function getIconIdentifierForFileExtensionReturnsDefaultIconIdentifierForUnknownFileExtension()
151 {
152 $result = $this->subject->getIconIdentifierForFileExtension('xyz');
153 $this->assertEquals('mimetypes-other-other', $result);
154 }
155
156 /**
157 * @test
158 */
159 public function getIconIdentifierForFileExtensionReturnsImageIconIdentifierForImageFileExtension()
160 {
161 $result = $this->subject->getIconIdentifierForFileExtension('jpg');
162 $this->assertEquals('mimetypes-media-image', $result);
163 }
164
165 /**
166 * @test
167 */
168 public function registerFileExtensionRegisterAnIcon()
169 {
170 $this->subject->registerFileExtension('abc', 'xyz');
171 $result = $this->subject->getIconIdentifierForFileExtension('abc');
172 $this->assertEquals('xyz', $result);
173 }
174
175 /**
176 * @test
177 */
178 public function registerFileExtensionOverwriteAnExistingIcon()
179 {
180 $this->subject->registerFileExtension('jpg', 'xyz');
181 $result = $this->subject->getIconIdentifierForFileExtension('jpg');
182 $this->assertEquals('xyz', $result);
183 }
184
185 /**
186 * @test
187 */
188 public function registerMimeTypeIconRegisterAnIcon()
189 {
190 $this->subject->registerMimeTypeIcon('foo/bar', 'mimetype-foo-bar');
191 $result = $this->subject->getIconIdentifierForMimeType('foo/bar');
192 $this->assertEquals('mimetype-foo-bar', $result);
193 }
194
195 /**
196 * @test
197 */
198 public function registerMimeTypeIconOverwriteAnExistingIcon()
199 {
200 $this->subject->registerMimeTypeIcon('video/*', 'mimetype-foo-bar');
201 $result = $this->subject->getIconIdentifierForMimeType('video/*');
202 $this->assertEquals('mimetype-foo-bar', $result);
203 }
204
205 /**
206 * @test
207 */
208 public function getIconIdentifierForMimeTypeWithUnknowMimeTypeReturnNull()
209 {
210 $result = $this->subject->getIconIdentifierForMimeType('bar/foo');
211 $this->assertEquals(null, $result);
212 }
213 }