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