mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
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>
This commit is contained in:
parent
8a0080cbbb
commit
e54963cdf6
2 changed files with 127 additions and 52 deletions
|
|
@ -669,6 +669,29 @@ class ThemingControllerTest extends TestCase {
|
|||
}
|
||||
|
||||
|
||||
public function testGetLogoOriginalFile(): void {
|
||||
$file = $this->createMock(ISimpleFile::class);
|
||||
$file->method('getName')->willReturn('logo');
|
||||
$file->method('getMTime')->willReturn(42);
|
||||
$this->imageManager->expects($this->once())
|
||||
->method('getImage')
|
||||
->willReturn($file);
|
||||
$this->appConfig
|
||||
->expects($this->once())
|
||||
->method('getAppValueString')
|
||||
->with('logoMime', '')
|
||||
->willReturn('image/png');
|
||||
|
||||
@$expected = new FileDisplayResponse($file);
|
||||
$expected->cacheFor(3600);
|
||||
$expected->addHeader('Content-Type', 'image/png');
|
||||
$expected->addHeader('Content-Disposition', 'attachment; filename="logo"');
|
||||
$csp = new ContentSecurityPolicy();
|
||||
$csp->allowInlineStyle();
|
||||
$expected->setContentSecurityPolicy($csp);
|
||||
@$this->assertEquals($expected, $this->themingController->getImage('logo', false));
|
||||
}
|
||||
|
||||
public function testGetLoginBackgroundNotExistent(): void {
|
||||
$this->imageManager->method('getImage')
|
||||
->with($this->equalTo('background'))
|
||||
|
|
@ -711,10 +734,10 @@ class ThemingControllerTest extends TestCase {
|
|||
|
||||
#[\PHPUnit\Framework\Attributes\DataProvider(methodName: 'dataGetManifest')]
|
||||
public function testGetManifest(bool $standalone): void {
|
||||
$this->config
|
||||
$this->appConfig
|
||||
->expects($this->once())
|
||||
->method('getAppValue')
|
||||
->with('theming', 'cachebuster', '0')
|
||||
->method('getAppValueString')
|
||||
->with('cachebuster', '0')
|
||||
->willReturn('0');
|
||||
$this->themingDefaults
|
||||
->expects($this->any())
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@ namespace OCA\Theming\Tests;
|
|||
|
||||
use OCA\Theming\ImageManager;
|
||||
use OCA\Theming\Service\BackgroundService;
|
||||
use OCP\AppFramework\Services\IAppConfig;
|
||||
use OCP\Files\IAppData;
|
||||
use OCP\Files\NotFoundException;
|
||||
use OCP\Files\SimpleFS\ISimpleFile;
|
||||
|
|
@ -29,6 +30,7 @@ class ImageManagerTest extends TestCase {
|
|||
private LoggerInterface&MockObject $logger;
|
||||
private ITempManager&MockObject $tempManager;
|
||||
private ISimpleFolder&MockObject $rootFolder;
|
||||
private IAppConfig&MockObject $appConfig;
|
||||
protected ImageManager $imageManager;
|
||||
|
||||
protected function setUp(): void {
|
||||
|
|
@ -41,6 +43,7 @@ class ImageManagerTest extends TestCase {
|
|||
$this->tempManager = $this->createMock(ITempManager::class);
|
||||
$this->rootFolder = $this->createMock(ISimpleFolder::class);
|
||||
$backgroundService = $this->createMock(BackgroundService::class);
|
||||
$this->appConfig = $this->createMock(IAppConfig::class);
|
||||
$this->imageManager = new ImageManager(
|
||||
$this->config,
|
||||
$this->appData,
|
||||
|
|
@ -49,6 +52,7 @@ class ImageManagerTest extends TestCase {
|
|||
$this->logger,
|
||||
$this->tempManager,
|
||||
$backgroundService,
|
||||
$this->appConfig,
|
||||
);
|
||||
$this->appData
|
||||
->expects($this->any())
|
||||
|
|
@ -79,26 +83,14 @@ class ImageManagerTest extends TestCase {
|
|||
->with('logo')
|
||||
->willThrowException(new NotFoundException());
|
||||
} else {
|
||||
$file->expects($this->once())
|
||||
->method('getContent')
|
||||
->willReturn(file_get_contents(__DIR__ . '/../../../tests/data/testimage.png'));
|
||||
$folder->expects($this->exactly(2))
|
||||
$folder->expects($this->once())
|
||||
->method('fileExists')
|
||||
->willReturnMap([
|
||||
['logo', true],
|
||||
['logo.png', false],
|
||||
]);
|
||||
->with('logo')
|
||||
->willReturn(true);
|
||||
$folder->expects($this->once())
|
||||
->method('getFile')
|
||||
->with('logo')
|
||||
->willReturn($file);
|
||||
$newFile = $this->createMock(ISimpleFile::class);
|
||||
$folder->expects($this->once())
|
||||
->method('newFile')
|
||||
->with('logo.png')
|
||||
->willReturn($newFile);
|
||||
$newFile->expects($this->once())
|
||||
->method('putContent');
|
||||
$this->rootFolder->expects($this->once())
|
||||
->method('getFolder')
|
||||
->with('images')
|
||||
|
|
@ -108,12 +100,14 @@ class ImageManagerTest extends TestCase {
|
|||
|
||||
public function testGetImageUrl(): void {
|
||||
$this->checkImagick();
|
||||
$this->config->expects($this->exactly(2))
|
||||
$this->appConfig->expects($this->once())
|
||||
->method('getAppValueInt')
|
||||
->with('cachebuster')
|
||||
->willReturn(0);
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')
|
||||
->willReturnMap([
|
||||
['theming', 'cachebuster', '0', '0'],
|
||||
['theming', 'logoMime', '', '0'],
|
||||
]);
|
||||
->with('theming', 'logoMime', '')
|
||||
->willReturn('image/png');
|
||||
$this->urlGenerator->expects($this->once())
|
||||
->method('linkToRoute')
|
||||
->willReturn('url-to-image');
|
||||
|
|
@ -121,12 +115,14 @@ class ImageManagerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testGetImageUrlDefault(): void {
|
||||
$this->config->expects($this->exactly(2))
|
||||
$this->appConfig->expects($this->once())
|
||||
->method('getAppValueInt')
|
||||
->with('cachebuster')
|
||||
->willReturn(0);
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')
|
||||
->willReturnMap([
|
||||
['theming', 'cachebuster', '0', '0'],
|
||||
['theming', 'logoMime', '', ''],
|
||||
]);
|
||||
->with('theming', 'logoMime', '')
|
||||
->willReturn('');
|
||||
$this->urlGenerator->expects($this->once())
|
||||
->method('imagePath')
|
||||
->with('core', 'logo/logo.png')
|
||||
|
|
@ -136,12 +132,14 @@ class ImageManagerTest extends TestCase {
|
|||
|
||||
public function testGetImageUrlAbsolute(): void {
|
||||
$this->checkImagick();
|
||||
$this->config->expects($this->exactly(2))
|
||||
$this->appConfig->expects($this->once())
|
||||
->method('getAppValueInt')
|
||||
->with('cachebuster')
|
||||
->willReturn(0);
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')
|
||||
->willReturnMap([
|
||||
['theming', 'cachebuster', '0', '0'],
|
||||
['theming', 'logoMime', '', ''],
|
||||
]);
|
||||
->with('theming', 'logoMime', '')
|
||||
->willReturn('');
|
||||
$this->urlGenerator->expects($this->any())
|
||||
->method('getAbsoluteUrl')
|
||||
->willReturn('url-to-image-absolute?v=0');
|
||||
|
|
@ -149,15 +147,69 @@ class ImageManagerTest extends TestCase {
|
|||
}
|
||||
|
||||
public function testGetImage(): void {
|
||||
$this->checkImagick();
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')->with('theming', 'logoMime', false)
|
||||
->willReturn('png');
|
||||
->method('getAppValue')->with('theming', 'logoMime', '')
|
||||
->willReturn('image/png');
|
||||
$file = $this->createMock(ISimpleFile::class);
|
||||
$this->mockGetImage('logo', $file);
|
||||
$this->assertEquals($file, $this->imageManager->getImage('logo', false));
|
||||
}
|
||||
|
||||
public function testGetImageSvgToSvg(): void {
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')->with('theming', 'logoMime', '')
|
||||
->willReturn('image/svg+xml');
|
||||
$folder = $this->createMock(ISimpleFolder::class);
|
||||
$file = $this->createMock(ISimpleFile::class);
|
||||
$folder->expects($this->once())
|
||||
->method('fileExists')
|
||||
->with('logo')
|
||||
->willReturn(true);
|
||||
$folder->expects($this->once())
|
||||
->method('getFile')
|
||||
->with('logo')
|
||||
->willReturn($file);
|
||||
$this->rootFolder->expects($this->once())
|
||||
->method('getFolder')
|
||||
->with('images')
|
||||
->willReturn($folder);
|
||||
$this->assertEquals($file, $this->imageManager->getImage('logo', true));
|
||||
}
|
||||
|
||||
public function testGetImageSvgToPng(): void {
|
||||
$this->checkImagick();
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')->with('theming', 'logoMime', '')
|
||||
->willReturn('image/svg+xml');
|
||||
$folder = $this->createMock(ISimpleFolder::class);
|
||||
$svgFile = $this->createMock(ISimpleFile::class);
|
||||
$pngFile = $this->createMock(ISimpleFile::class);
|
||||
$svgFile->expects($this->once())
|
||||
->method('getContent')
|
||||
->willReturn(file_get_contents(__DIR__ . '/../../../core/img/logo/logo.svg'));
|
||||
$folder->expects($this->exactly(2))
|
||||
->method('fileExists')
|
||||
->willReturnMap([
|
||||
['logo', true],
|
||||
['logo.png', false],
|
||||
]);
|
||||
$folder->expects($this->once())
|
||||
->method('getFile')
|
||||
->with('logo')
|
||||
->willReturn($svgFile);
|
||||
$folder->expects($this->once())
|
||||
->method('newFile')
|
||||
->with('logo.png')
|
||||
->willReturn($pngFile);
|
||||
$pngFile->expects($this->once())
|
||||
->method('putContent');
|
||||
$this->rootFolder->expects($this->once())
|
||||
->method('getFolder')
|
||||
->with('images')
|
||||
->willReturn($folder);
|
||||
$this->assertEquals($pngFile, $this->imageManager->getImage('logo', false));
|
||||
}
|
||||
|
||||
|
||||
public function testGetImageUnset(): void {
|
||||
$this->expectException(NotFoundException::class);
|
||||
|
|
@ -170,10 +222,10 @@ class ImageManagerTest extends TestCase {
|
|||
|
||||
public function testGetCacheFolder(): void {
|
||||
$folder = $this->createMock(ISimpleFolder::class);
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')
|
||||
->with('theming', 'cachebuster', '0')
|
||||
->willReturn('0');
|
||||
$this->appConfig->expects($this->once())
|
||||
->method('getAppValueInt')
|
||||
->with('cachebuster')
|
||||
->willReturn(0);
|
||||
$this->rootFolder->expects($this->once())
|
||||
->method('getFolder')
|
||||
->with('0')
|
||||
|
|
@ -182,10 +234,10 @@ class ImageManagerTest extends TestCase {
|
|||
}
|
||||
public function testGetCacheFolderCreate(): void {
|
||||
$folder = $this->createMock(ISimpleFolder::class);
|
||||
$this->config->expects($this->exactly(2))
|
||||
->method('getAppValue')
|
||||
->with('theming', 'cachebuster', '0')
|
||||
->willReturn('0');
|
||||
$this->appConfig->expects($this->exactly(2))
|
||||
->method('getAppValueInt')
|
||||
->with('cachebuster')
|
||||
->willReturn(0);
|
||||
$this->rootFolder->expects($this->exactly(2))
|
||||
->method('getFolder')
|
||||
->with('0')
|
||||
|
|
@ -261,10 +313,10 @@ class ImageManagerTest extends TestCase {
|
|||
|
||||
private function setupCacheFolder() {
|
||||
$folder = $this->createMock(ISimpleFolder::class);
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')
|
||||
->with('theming', 'cachebuster', '0')
|
||||
->willReturn('0');
|
||||
$this->appConfig->expects($this->once())
|
||||
->method('getAppValueInt')
|
||||
->with('cachebuster')
|
||||
->willReturn(0);
|
||||
$this->rootFolder->expects($this->once())
|
||||
->method('getFolder')
|
||||
->with('0')
|
||||
|
|
@ -286,10 +338,10 @@ class ImageManagerTest extends TestCase {
|
|||
$folders[0]->expects($this->once())->method('delete');
|
||||
$folders[1]->expects($this->once())->method('delete');
|
||||
$folders[2]->expects($this->never())->method('delete');
|
||||
$this->config->expects($this->once())
|
||||
->method('getAppValue')
|
||||
->with('theming', 'cachebuster', '0')
|
||||
->willReturn('2');
|
||||
$this->appConfig->expects($this->once())
|
||||
->method('getAppValueInt')
|
||||
->with('cachebuster')
|
||||
->willReturn(2);
|
||||
$this->rootFolder->expects($this->once())
|
||||
->method('getDirectoryListing')
|
||||
->willReturn($folders);
|
||||
|
|
|
|||
Loading…
Reference in a new issue