Skip to content

Commit 2a2ed3e

Browse files
committed
fixup! fixup! feat: make preview conversion timeout and max file size configurable
1 parent 1f269e6 commit 2a2ed3e

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

tests/lib/AppConfigTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,40 @@ public function testGetAppValueArrayWithNoneValue() {
6363

6464
$this->assertSame([], $result);
6565
}
66+
67+
public function testGetPreviewConversionTimeoutReturnsDefault(): void {
68+
$this->config->expects($this->once())
69+
->method('getAppValue')
70+
->with('richdocuments', 'preview_conversion_timeout', 5)
71+
->willReturn(5);
72+
73+
$this->assertSame(5, $this->appConfig->getPreviewConversionTimeout());
74+
}
75+
76+
public function testGetPreviewConversionTimeoutReturnsConfiguredValue(): void {
77+
$this->config->expects($this->once())
78+
->method('getAppValue')
79+
->with('richdocuments', 'preview_conversion_timeout', 5)
80+
->willReturn(30);
81+
82+
$this->assertSame(30, $this->appConfig->getPreviewConversionTimeout());
83+
}
84+
85+
public function testGetPreviewConversionMaxFileSizeReturnsDefault(): void {
86+
$this->config->expects($this->once())
87+
->method('getAppValue')
88+
->with('richdocuments', 'preview_conversion_max_filesize', 104857600)
89+
->willReturn(104857600);
90+
91+
$this->assertSame(104857600, $this->appConfig->getPreviewConversionMaxFileSize());
92+
}
93+
94+
public function testGetPreviewConversionMaxFileSizeReturnsConfiguredValue(): void {
95+
$this->config->expects($this->once())
96+
->method('getAppValue')
97+
->with('richdocuments', 'preview_conversion_max_filesize', 104857600)
98+
->willReturn(52428800);
99+
100+
$this->assertSame(52428800, $this->appConfig->getPreviewConversionMaxFileSize());
101+
}
66102
}

tests/lib/Preview/OfficeTest.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
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

Comments
 (0)