|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 6 | + */ |
| 7 | + |
| 8 | +namespace Tests\Richdocuments\Preview; |
| 9 | + |
| 10 | +use OCA\Richdocuments\AppConfig; |
| 11 | +use OCA\Richdocuments\Capabilities; |
| 12 | +use OCA\Richdocuments\Preview\Office; |
| 13 | +use OCA\Richdocuments\Service\RemoteService; |
| 14 | +use OCP\Files\File; |
| 15 | +use PHPUnit\Framework\MockObject\MockObject; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Psr\Log\LoggerInterface; |
| 18 | + |
| 19 | +class OfficeTest extends TestCase { |
| 20 | + private RemoteService&MockObject $remoteService; |
| 21 | + private LoggerInterface&MockObject $logger; |
| 22 | + private AppConfig&MockObject $appConfig; |
| 23 | + private Capabilities&MockObject $capabilities; |
| 24 | + private Office $provider; |
| 25 | + |
| 26 | + protected function setUp(): void { |
| 27 | + parent::setUp(); |
| 28 | + |
| 29 | + $this->remoteService = $this->createMock(RemoteService::class); |
| 30 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 31 | + $this->appConfig = $this->createMock(AppConfig::class); |
| 32 | + $this->capabilities = $this->createMock(Capabilities::class); |
| 33 | + $this->capabilities->method('getCapabilities')->willReturn(['richdocuments' => []]); |
| 34 | + |
| 35 | + $this->provider = new class($this->remoteService, $this->logger, $this->appConfig, $this->capabilities) extends Office { |
| 36 | + #[\Override] |
| 37 | + public function getMimeType(): string { |
| 38 | + return '/application\\/test/'; |
| 39 | + } |
| 40 | + }; |
| 41 | + } |
| 42 | + |
| 43 | + public function testGetThumbnailSkipsConversionWhenFileIsTooLarge(): void { |
| 44 | + $file = $this->createMock(File::class); |
| 45 | + $file->expects($this->once())->method('getSize')->willReturn(101 * 1024 * 1024); |
| 46 | + |
| 47 | + $this->appConfig->expects($this->once()) |
| 48 | + ->method('getPreviewConversionMaxFileSize') |
| 49 | + ->willReturn(100 * 1024 * 1024); |
| 50 | + $this->remoteService->expects($this->never())->method('convertFileTo'); |
| 51 | + |
| 52 | + $result = $this->provider->getThumbnail($file, 64, 64); |
| 53 | + |
| 54 | + $this->assertNull($result); |
| 55 | + } |
| 56 | + |
| 57 | + public function testGetThumbnailReturnsNullForEmptyFile(): void { |
| 58 | + $file = $this->createMock(File::class); |
| 59 | + $file->expects($this->once())->method('getSize')->willReturn(0); |
| 60 | + |
| 61 | + $this->appConfig->expects($this->never())->method('getPreviewConversionMaxFileSize'); |
| 62 | + $this->remoteService->expects($this->never())->method('convertFileTo'); |
| 63 | + |
| 64 | + $result = $this->provider->getThumbnail($file, 64, 64); |
| 65 | + |
| 66 | + $this->assertNull($result); |
| 67 | + } |
| 68 | + |
| 69 | + public function testGetThumbnailAttemptsConversionWhenFileSizeIsExactlyAtLimit(): void { |
| 70 | + $file = $this->createMock(File::class); |
| 71 | + $file->expects($this->once())->method('getSize')->willReturn(100 * 1024 * 1024); |
| 72 | + |
| 73 | + $this->appConfig->expects($this->once()) |
| 74 | + ->method('getPreviewConversionMaxFileSize') |
| 75 | + ->willReturn(100 * 1024 * 1024); |
| 76 | + // Conversion is attempted; throw to keep the test simple (image loading is not unit-tested here) |
| 77 | + $this->remoteService->expects($this->once()) |
| 78 | + ->method('convertFileTo') |
| 79 | + ->with($file, 'png') |
| 80 | + ->willThrowException(new \Exception('conversion failed')); |
| 81 | + |
| 82 | + $result = $this->provider->getThumbnail($file, 64, 64); |
| 83 | + |
| 84 | + $this->assertNull($result); |
| 85 | + } |
| 86 | + |
| 87 | + public function testGetThumbnailUsesConfiguredConversionTimeout(): void { |
| 88 | + $file = $this->createMock(File::class); |
| 89 | + $file->expects($this->once())->method('getSize')->willReturn(1024); |
| 90 | + |
| 91 | + $this->appConfig->expects($this->once()) |
| 92 | + ->method('getPreviewConversionMaxFileSize') |
| 93 | + ->willReturn(100 * 1024 * 1024); |
| 94 | + $this->remoteService->expects($this->once()) |
| 95 | + ->method('convertFileTo') |
| 96 | + ->with($file, 'png') |
| 97 | + ->willThrowException(new \Exception('conversion failed')); |
| 98 | + |
| 99 | + $result = $this->provider->getThumbnail($file, 64, 64); |
| 100 | + |
| 101 | + $this->assertNull($result); |
| 102 | + } |
| 103 | +} |
0 commit comments