99
1010use OCA \Theming \ImageManager ;
1111use OCA \Theming \Service \BackgroundService ;
12+ use OCP \AppFramework \Services \IAppConfig ;
1213use OCP \Files \IAppData ;
1314use OCP \Files \NotFoundException ;
1415use OCP \Files \SimpleFS \ISimpleFile ;
@@ -29,6 +30,7 @@ class ImageManagerTest extends TestCase {
2930 private LoggerInterface &MockObject $ logger ;
3031 private ITempManager &MockObject $ tempManager ;
3132 private ISimpleFolder &MockObject $ rootFolder ;
33+ private IAppConfig &MockObject $ appConfig ;
3234 protected ImageManager $ imageManager ;
3335
3436 protected function setUp (): void {
@@ -41,6 +43,7 @@ protected function setUp(): void {
4143 $ this ->tempManager = $ this ->createMock (ITempManager::class);
4244 $ this ->rootFolder = $ this ->createMock (ISimpleFolder::class);
4345 $ backgroundService = $ this ->createMock (BackgroundService::class);
46+ $ this ->appConfig = $ this ->createMock (IAppConfig::class);
4447 $ this ->imageManager = new ImageManager (
4548 $ this ->config ,
4649 $ this ->appData ,
@@ -49,6 +52,7 @@ protected function setUp(): void {
4952 $ this ->logger ,
5053 $ this ->tempManager ,
5154 $ backgroundService ,
55+ $ this ->appConfig ,
5256 );
5357 $ this ->appData
5458 ->expects ($ this ->any ())
@@ -79,26 +83,14 @@ public function mockGetImage($key, $file) {
7983 ->with ('logo ' )
8084 ->willThrowException (new NotFoundException ());
8185 } else {
82- $ file ->expects ($ this ->once ())
83- ->method ('getContent ' )
84- ->willReturn (file_get_contents (__DIR__ . '/../../../tests/data/testimage.png ' ));
85- $ folder ->expects ($ this ->exactly (2 ))
86+ $ folder ->expects ($ this ->once ())
8687 ->method ('fileExists ' )
87- ->willReturnMap ([
88- ['logo ' , true ],
89- ['logo.png ' , false ],
90- ]);
88+ ->with ('logo ' )
89+ ->willReturn (true );
9190 $ folder ->expects ($ this ->once ())
9291 ->method ('getFile ' )
9392 ->with ('logo ' )
9493 ->willReturn ($ file );
95- $ newFile = $ this ->createMock (ISimpleFile::class);
96- $ folder ->expects ($ this ->once ())
97- ->method ('newFile ' )
98- ->with ('logo.png ' )
99- ->willReturn ($ newFile );
100- $ newFile ->expects ($ this ->once ())
101- ->method ('putContent ' );
10294 $ this ->rootFolder ->expects ($ this ->once ())
10395 ->method ('getFolder ' )
10496 ->with ('images ' )
@@ -108,25 +100,29 @@ public function mockGetImage($key, $file) {
108100
109101 public function testGetImageUrl (): void {
110102 $ this ->checkImagick ();
111- $ this ->config ->expects ($ this ->exactly (2 ))
103+ $ this ->appConfig ->expects ($ this ->once ())
104+ ->method ('getAppValueInt ' )
105+ ->with ('cachebuster ' )
106+ ->willReturn (0 );
107+ $ this ->config ->expects ($ this ->once ())
112108 ->method ('getAppValue ' )
113- ->willReturnMap ([
114- ['theming ' , 'cachebuster ' , '0 ' , '0 ' ],
115- ['theming ' , 'logoMime ' , '' , '0 ' ],
116- ]);
109+ ->with ('theming ' , 'logoMime ' , '' )
110+ ->willReturn ('image/png ' );
117111 $ this ->urlGenerator ->expects ($ this ->once ())
118112 ->method ('linkToRoute ' )
119113 ->willReturn ('url-to-image ' );
120114 $ this ->assertEquals ('url-to-image?v=0 ' , $ this ->imageManager ->getImageUrl ('logo ' , false ));
121115 }
122116
123117 public function testGetImageUrlDefault (): void {
124- $ this ->config ->expects ($ this ->exactly (2 ))
118+ $ this ->appConfig ->expects ($ this ->once ())
119+ ->method ('getAppValueInt ' )
120+ ->with ('cachebuster ' )
121+ ->willReturn (0 );
122+ $ this ->config ->expects ($ this ->once ())
125123 ->method ('getAppValue ' )
126- ->willReturnMap ([
127- ['theming ' , 'cachebuster ' , '0 ' , '0 ' ],
128- ['theming ' , 'logoMime ' , '' , '' ],
129- ]);
124+ ->with ('theming ' , 'logoMime ' , '' )
125+ ->willReturn ('' );
130126 $ this ->urlGenerator ->expects ($ this ->once ())
131127 ->method ('imagePath ' )
132128 ->with ('core ' , 'logo/logo.png ' )
@@ -136,28 +132,84 @@ public function testGetImageUrlDefault(): void {
136132
137133 public function testGetImageUrlAbsolute (): void {
138134 $ this ->checkImagick ();
139- $ this ->config ->expects ($ this ->exactly (2 ))
135+ $ this ->appConfig ->expects ($ this ->once ())
136+ ->method ('getAppValueInt ' )
137+ ->with ('cachebuster ' )
138+ ->willReturn (0 );
139+ $ this ->config ->expects ($ this ->once ())
140140 ->method ('getAppValue ' )
141- ->willReturnMap ([
142- ['theming ' , 'cachebuster ' , '0 ' , '0 ' ],
143- ['theming ' , 'logoMime ' , '' , '' ],
144- ]);
141+ ->with ('theming ' , 'logoMime ' , '' )
142+ ->willReturn ('' );
145143 $ this ->urlGenerator ->expects ($ this ->any ())
146144 ->method ('getAbsoluteUrl ' )
147145 ->willReturn ('url-to-image-absolute?v=0 ' );
148146 $ this ->assertEquals ('url-to-image-absolute?v=0 ' , $ this ->imageManager ->getImageUrlAbsolute ('logo ' , false ));
149147 }
150148
151149 public function testGetImage (): void {
152- $ this ->checkImagick ();
153150 $ this ->config ->expects ($ this ->once ())
154- ->method ('getAppValue ' )->with ('theming ' , 'logoMime ' , false )
155- ->willReturn ('png ' );
151+ ->method ('getAppValue ' )->with ('theming ' , 'logoMime ' , '' )
152+ ->willReturn ('image/ png ' );
156153 $ file = $ this ->createMock (ISimpleFile::class);
157154 $ this ->mockGetImage ('logo ' , $ file );
158155 $ this ->assertEquals ($ file , $ this ->imageManager ->getImage ('logo ' , false ));
159156 }
160157
158+ public function testGetImageSvgToSvg (): void {
159+ $ this ->config ->expects ($ this ->once ())
160+ ->method ('getAppValue ' )->with ('theming ' , 'logoMime ' , '' )
161+ ->willReturn ('image/svg+xml ' );
162+ $ folder = $ this ->createMock (ISimpleFolder::class);
163+ $ file = $ this ->createMock (ISimpleFile::class);
164+ $ folder ->expects ($ this ->once ())
165+ ->method ('fileExists ' )
166+ ->with ('logo ' )
167+ ->willReturn (true );
168+ $ folder ->expects ($ this ->once ())
169+ ->method ('getFile ' )
170+ ->with ('logo ' )
171+ ->willReturn ($ file );
172+ $ this ->rootFolder ->expects ($ this ->once ())
173+ ->method ('getFolder ' )
174+ ->with ('images ' )
175+ ->willReturn ($ folder );
176+ $ this ->assertEquals ($ file , $ this ->imageManager ->getImage ('logo ' , true ));
177+ }
178+
179+ public function testGetImageSvgToPng (): void {
180+ $ this ->checkImagick ();
181+ $ this ->config ->expects ($ this ->once ())
182+ ->method ('getAppValue ' )->with ('theming ' , 'logoMime ' , '' )
183+ ->willReturn ('image/svg+xml ' );
184+ $ folder = $ this ->createMock (ISimpleFolder::class);
185+ $ svgFile = $ this ->createMock (ISimpleFile::class);
186+ $ pngFile = $ this ->createMock (ISimpleFile::class);
187+ $ svgFile ->expects ($ this ->once ())
188+ ->method ('getContent ' )
189+ ->willReturn (file_get_contents (__DIR__ . '/../../../core/img/logo/logo.svg ' ));
190+ $ folder ->expects ($ this ->exactly (2 ))
191+ ->method ('fileExists ' )
192+ ->willReturnMap ([
193+ ['logo ' , true ],
194+ ['logo.png ' , false ],
195+ ]);
196+ $ folder ->expects ($ this ->once ())
197+ ->method ('getFile ' )
198+ ->with ('logo ' )
199+ ->willReturn ($ svgFile );
200+ $ folder ->expects ($ this ->once ())
201+ ->method ('newFile ' )
202+ ->with ('logo.png ' )
203+ ->willReturn ($ pngFile );
204+ $ pngFile ->expects ($ this ->once ())
205+ ->method ('putContent ' );
206+ $ this ->rootFolder ->expects ($ this ->once ())
207+ ->method ('getFolder ' )
208+ ->with ('images ' )
209+ ->willReturn ($ folder );
210+ $ this ->assertEquals ($ pngFile , $ this ->imageManager ->getImage ('logo ' , false ));
211+ }
212+
161213
162214 public function testGetImageUnset (): void {
163215 $ this ->expectException (NotFoundException::class);
@@ -170,10 +222,10 @@ public function testGetImageUnset(): void {
170222
171223 public function testGetCacheFolder (): void {
172224 $ folder = $ this ->createMock (ISimpleFolder::class);
173- $ this ->config ->expects ($ this ->once ())
174- ->method ('getAppValue ' )
175- ->with ('theming ' , ' cachebuster ' , ' 0 ' )
176- ->willReturn (' 0 ' );
225+ $ this ->appConfig ->expects ($ this ->once ())
226+ ->method ('getAppValueInt ' )
227+ ->with ('cachebuster ' )
228+ ->willReturn (0 );
177229 $ this ->rootFolder ->expects ($ this ->once ())
178230 ->method ('getFolder ' )
179231 ->with ('0 ' )
@@ -182,10 +234,10 @@ public function testGetCacheFolder(): void {
182234 }
183235 public function testGetCacheFolderCreate (): void {
184236 $ folder = $ this ->createMock (ISimpleFolder::class);
185- $ this ->config ->expects ($ this ->exactly (2 ))
186- ->method ('getAppValue ' )
187- ->with ('theming ' , ' cachebuster ' , ' 0 ' )
188- ->willReturn (' 0 ' );
237+ $ this ->appConfig ->expects ($ this ->exactly (2 ))
238+ ->method ('getAppValueInt ' )
239+ ->with ('cachebuster ' )
240+ ->willReturn (0 );
189241 $ this ->rootFolder ->expects ($ this ->exactly (2 ))
190242 ->method ('getFolder ' )
191243 ->with ('0 ' )
@@ -261,10 +313,10 @@ public function testSetCachedImageCreate(): void {
261313
262314 private function setupCacheFolder () {
263315 $ folder = $ this ->createMock (ISimpleFolder::class);
264- $ this ->config ->expects ($ this ->once ())
265- ->method ('getAppValue ' )
266- ->with ('theming ' , ' cachebuster ' , ' 0 ' )
267- ->willReturn (' 0 ' );
316+ $ this ->appConfig ->expects ($ this ->once ())
317+ ->method ('getAppValueInt ' )
318+ ->with ('cachebuster ' )
319+ ->willReturn (0 );
268320 $ this ->rootFolder ->expects ($ this ->once ())
269321 ->method ('getFolder ' )
270322 ->with ('0 ' )
@@ -286,10 +338,10 @@ public function testCleanup(): void {
286338 $ folders [0 ]->expects ($ this ->once ())->method ('delete ' );
287339 $ folders [1 ]->expects ($ this ->once ())->method ('delete ' );
288340 $ folders [2 ]->expects ($ this ->never ())->method ('delete ' );
289- $ this ->config ->expects ($ this ->once ())
290- ->method ('getAppValue ' )
291- ->with ('theming ' , ' cachebuster ' , ' 0 ' )
292- ->willReturn (' 2 ' );
341+ $ this ->appConfig ->expects ($ this ->once ())
342+ ->method ('getAppValueInt ' )
343+ ->with ('cachebuster ' )
344+ ->willReturn (2 );
293345 $ this ->rootFolder ->expects ($ this ->once ())
294346 ->method ('getDirectoryListing ' )
295347 ->willReturn ($ folders );
0 commit comments