1111use OCP \AppFramework \Http ;
1212use OCP \AppFramework \Http \DataResponse ;
1313use OCP \AppFramework \Http \FileDisplayResponse ;
14+ use OCP \AppFramework \Http \RedirectResponse ;
1415use OCP \AppFramework \Utility \ITimeFactory ;
1516use OCP \Constants ;
1617use OCP \Files \File ;
@@ -33,6 +34,7 @@ class PublicPreviewControllerTest extends TestCase {
3334 private IManager &MockObject $ shareManager ;
3435 private ITimeFactory &MockObject $ timeFactory ;
3536 private IRequest &MockObject $ request ;
37+ private IMimeIconProvider &MockObject $ mimeIconProvider ;
3638
3739 private PublicPreviewController $ controller ;
3840
@@ -43,6 +45,7 @@ protected function setUp(): void {
4345 $ this ->shareManager = $ this ->createMock (IManager::class);
4446 $ this ->timeFactory = $ this ->createMock (ITimeFactory::class);
4547 $ this ->request = $ this ->createMock (IRequest::class);
48+ $ this ->mimeIconProvider = $ this ->createMock (IMimeIconProvider::class);
4649
4750 $ this ->timeFactory ->method ('getTime ' )
4851 ->willReturn (1337 );
@@ -55,7 +58,7 @@ protected function setUp(): void {
5558 $ this ->shareManager ,
5659 $ this ->createMock (ISession::class),
5760 $ this ->previewManager ,
58- $ this ->createMock (IMimeIconProvider::class) ,
61+ $ this ->mimeIconProvider ,
5962 );
6063 }
6164
@@ -154,7 +157,7 @@ public function testShareNoDownloadButPreviewHeader() {
154157 $ preview ->method ('getMimeType ' )
155158 ->willReturn ('myMime ' );
156159
157- $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true );
160+ $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true , false );
158161 $ expected = new FileDisplayResponse ($ preview , Http::STATUS_OK , ['Content-Type ' => 'myMime ' ]);
159162 $ expected ->cacheFor (15 * 60 );
160163 $ this ->assertEquals ($ expected , $ res );
@@ -190,7 +193,7 @@ public function testShareWithAttributes() {
190193 $ preview ->method ('getMimeType ' )
191194 ->willReturn ('myMime ' );
192195
193- $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true );
196+ $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true , false );
194197 $ expected = new FileDisplayResponse ($ preview , Http::STATUS_OK , ['Content-Type ' => 'myMime ' ]);
195198 $ expected ->cacheFor (3600 * 24 );
196199 $ this ->assertEquals ($ expected , $ res );
@@ -222,7 +225,7 @@ public function testPreviewFile() {
222225 $ preview ->method ('getMimeType ' )
223226 ->willReturn ('myMime ' );
224227
225- $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true );
228+ $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true , false );
226229 $ expected = new FileDisplayResponse ($ preview , Http::STATUS_OK , ['Content-Type ' => 'myMime ' ]);
227230 $ expected ->cacheFor (3600 * 24 );
228231 $ this ->assertEquals ($ expected , $ res );
@@ -248,11 +251,123 @@ public function testPreviewFolderInvalidFile(): void {
248251 ->with ($ this ->equalTo ('file ' ))
249252 ->willThrowException (new NotFoundException ());
250253
251- $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true );
254+ $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true , false );
252255 $ expected = new DataResponse ([], Http::STATUS_NOT_FOUND );
253256 $ this ->assertEquals ($ expected , $ res );
254257 }
255258
259+ public function testPreviewFolderEmptyFileReturnsBadRequest (): void {
260+ $ share = $ this ->createMock (IShare::class);
261+ $ this ->shareManager ->method ('getShareByToken ' )
262+ ->with ($ this ->equalTo ('token ' ))
263+ ->willReturn ($ share );
264+
265+ $ share ->method ('getPermissions ' )
266+ ->willReturn (Constants::PERMISSION_READ );
267+
268+ $ folder = $ this ->createMock (Folder::class);
269+ $ share ->method ('getNode ' )
270+ ->willReturn ($ folder );
271+
272+ $ share ->method ('canSeeContent ' )
273+ ->willReturn (true );
274+
275+ $ res = $ this ->controller ->getPreview ('token ' , '' , 10 , 10 , false , false );
276+ $ expected = new DataResponse ([], Http::STATUS_BAD_REQUEST );
277+ $ this ->assertEquals ($ expected , $ res );
278+ }
279+
280+ public function testPreviewFolderSubfolderReturnsBadRequest (): void {
281+ $ share = $ this ->createMock (IShare::class);
282+ $ this ->shareManager ->method ('getShareByToken ' )
283+ ->with ($ this ->equalTo ('token ' ))
284+ ->willReturn ($ share );
285+
286+ $ share ->method ('getPermissions ' )
287+ ->willReturn (Constants::PERMISSION_READ );
288+
289+ $ folder = $ this ->createMock (Folder::class);
290+ $ share ->method ('getNode ' )
291+ ->willReturn ($ folder );
292+
293+ $ share ->method ('canSeeContent ' )
294+ ->willReturn (true );
295+
296+ $ subfolder = $ this ->createMock (Folder::class);
297+ $ folder ->method ('get ' )
298+ ->with ($ this ->equalTo ('nested ' ))
299+ ->willReturn ($ subfolder );
300+
301+ $ res = $ this ->controller ->getPreview ('token ' , 'nested ' , 10 , 10 , false , false );
302+ $ expected = new DataResponse ([], Http::STATUS_BAD_REQUEST );
303+ $ this ->assertEquals ($ expected , $ res );
304+ }
305+
306+ public function testPreviewFolderInvalidFileWithMimeFallbackReturnsNotFound (): void {
307+ $ share = $ this ->createMock (IShare::class);
308+ $ this ->shareManager ->method ('getShareByToken ' )
309+ ->with ($ this ->equalTo ('token ' ))
310+ ->willReturn ($ share );
311+
312+ $ share ->method ('getPermissions ' )
313+ ->willReturn (Constants::PERMISSION_READ );
314+
315+ $ folder = $ this ->createMock (Folder::class);
316+ $ share ->method ('getNode ' )
317+ ->willReturn ($ folder );
318+
319+ $ share ->method ('canSeeContent ' )
320+ ->willReturn (true );
321+
322+ $ folder ->method ('get ' )
323+ ->with ($ this ->equalTo ('file ' ))
324+ ->willThrowException (new NotFoundException ());
325+
326+ $ this ->mimeIconProvider ->expects ($ this ->never ())
327+ ->method ('getMimeIconUrl ' );
328+
329+ $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , false , true );
330+ $ expected = new DataResponse ([], Http::STATUS_NOT_FOUND );
331+ $ this ->assertEquals ($ expected , $ res );
332+ }
333+
334+ public function testPreviewFolderValidFileMimeFallbackRedirectsWhenPreviewMissing (): void {
335+ $ share = $ this ->createMock (IShare::class);
336+ $ this ->shareManager ->method ('getShareByToken ' )
337+ ->with ($ this ->equalTo ('token ' ))
338+ ->willReturn ($ share );
339+
340+ $ share ->method ('getPermissions ' )
341+ ->willReturn (Constants::PERMISSION_READ );
342+
343+ $ folder = $ this ->createMock (Folder::class);
344+ $ share ->method ('getNode ' )
345+ ->willReturn ($ folder );
346+
347+ $ share ->method ('canSeeContent ' )
348+ ->willReturn (true );
349+
350+ $ file = $ this ->createMock (File::class);
351+ $ folder ->method ('get ' )
352+ ->with ($ this ->equalTo ('file ' ))
353+ ->willReturn ($ file );
354+
355+ $ file ->method ('getMimeType ' )
356+ ->willReturn ('text/plain ' );
357+
358+ $ this ->previewManager ->method ('getPreview ' )
359+ ->with ($ this ->equalTo ($ file ), 10 , 10 , true )
360+ ->willThrowException (new NotFoundException ());
361+
362+ $ this ->mimeIconProvider ->method ('getMimeIconUrl ' )
363+ ->with ('text/plain ' )
364+ ->willReturn ('/icon-url ' );
365+
366+ $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , false , true );
367+ $ expected = new RedirectResponse ('/icon-url ' );
368+ $ this ->assertEquals ($ expected , $ res );
369+ }
370+
256371 public function testPreviewFolderValidFile (): void {
257372 $ share = $ this ->createMock (IShare::class);
258373 $ this ->shareManager ->method ('getShareByToken ' )
@@ -284,7 +399,7 @@ public function testPreviewFolderValidFile(): void {
284399 $ preview ->method ('getMimeType ' )
285400 ->willReturn ('myMime ' );
286401
287- $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true );
402+ $ res = $ this ->controller ->getPreview ('token ' , 'file ' , 10 , 10 , true , false );
288403 $ expected = new FileDisplayResponse ($ preview , Http::STATUS_OK , ['Content-Type ' => 'myMime ' ]);
289404 $ expected ->cacheFor (3600 * 24 );
290405 $ this ->assertEquals ($ expected , $ res );
0 commit comments