Skip to content

Commit e54963c

Browse files
miaulalalasusnux
authored andcommitted
test(theming): update tests for ImageManager and ThemingController fixes
- ImageManagerTest: inject IAppConfig mock, switch cachebuster assertions from IConfig::getAppValue to IAppConfig::getAppValueInt, add testGetImageSvgToSvg and testGetImageSvgToPng, update mockGetImage to reflect the corrected getImage() logic - ThemingControllerTest: update getImage and getManifest tests to use IAppConfig::getAppValueString for MIME type and cachebuster lookups, add testGetLogoOriginalFile for the extensionless-file MIME path AI-Assisted-By: Claude Sonnet 4.6 <noreply@anthropic.com> Signed-off-by: Anna Larch <anna@nextcloud.com>
1 parent 8a0080c commit e54963c

2 files changed

Lines changed: 127 additions & 52 deletions

File tree

apps/theming/tests/Controller/ThemingControllerTest.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,29 @@ public function testGetLogo(): void {
669669
}
670670

671671

672+
public function testGetLogoOriginalFile(): void {
673+
$file = $this->createMock(ISimpleFile::class);
674+
$file->method('getName')->willReturn('logo');
675+
$file->method('getMTime')->willReturn(42);
676+
$this->imageManager->expects($this->once())
677+
->method('getImage')
678+
->willReturn($file);
679+
$this->appConfig
680+
->expects($this->once())
681+
->method('getAppValueString')
682+
->with('logoMime', '')
683+
->willReturn('image/png');
684+
685+
@$expected = new FileDisplayResponse($file);
686+
$expected->cacheFor(3600);
687+
$expected->addHeader('Content-Type', 'image/png');
688+
$expected->addHeader('Content-Disposition', 'attachment; filename="logo"');
689+
$csp = new ContentSecurityPolicy();
690+
$csp->allowInlineStyle();
691+
$expected->setContentSecurityPolicy($csp);
692+
@$this->assertEquals($expected, $this->themingController->getImage('logo', false));
693+
}
694+
672695
public function testGetLoginBackgroundNotExistent(): void {
673696
$this->imageManager->method('getImage')
674697
->with($this->equalTo('background'))
@@ -711,10 +734,10 @@ public static function dataGetManifest(): array {
711734

712735
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dataGetManifest')]
713736
public function testGetManifest(bool $standalone): void {
714-
$this->config
737+
$this->appConfig
715738
->expects($this->once())
716-
->method('getAppValue')
717-
->with('theming', 'cachebuster', '0')
739+
->method('getAppValueString')
740+
->with('cachebuster', '0')
718741
->willReturn('0');
719742
$this->themingDefaults
720743
->expects($this->any())

apps/theming/tests/ImageManagerTest.php

Lines changed: 101 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use OCA\Theming\ImageManager;
1111
use OCA\Theming\Service\BackgroundService;
12+
use OCP\AppFramework\Services\IAppConfig;
1213
use OCP\Files\IAppData;
1314
use OCP\Files\NotFoundException;
1415
use 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

Comments
 (0)