Browse Source

Adjust ImageManager tests for png generation

Signed-off-by: Julius Härtl <jus@bitgrid.net>
tags/v14.0.0beta1
Julius Härtl 6 years ago
parent
commit
9b919245f6
No account linked to committer's email address
2 changed files with 51 additions and 13 deletions
  1. 3
    6
      apps/theming/lib/ImageManager.php
  2. 48
    7
      apps/theming/tests/ImageManagerTest.php

+ 3
- 6
apps/theming/lib/ImageManager.php View File

use OCP\Files\NotPermittedException; use OCP\Files\NotPermittedException;
use OCP\IURLGenerator; use OCP\IURLGenerator;


/**
* @property IURLGenerator urlGenerator
*/
class ImageManager { class ImageManager {


/** @var IConfig */ /** @var IConfig */
* @param IConfig $config * @param IConfig $config
* @param IAppData $appData * @param IAppData $appData
* @param IURLGenerator $urlGenerator * @param IURLGenerator $urlGenerator
* @param ThemingDefaults $themingDefaults
* @param ICacheFactory $cacheFactory
*/ */
public function __construct(IConfig $config, public function __construct(IConfig $config,
IAppData $appData, IAppData $appData,
*/ */
public function getImage(string $key, bool $useSvg = false): ISimpleFile { public function getImage(string $key, bool $useSvg = false): ISimpleFile {
$logo = $this->config->getAppValue('theming', $key . 'Mime', false); $logo = $this->config->getAppValue('theming', $key . 'Mime', false);
if ($logo === false) {
$folder = $this->appData->getFolder('images');
if ($logo === false || !$folder->fileExists($key)) {
throw new NotFoundException(); throw new NotFoundException();
} }
$folder = $this->appData->getFolder('images');
if (!$useSvg && $this->shouldReplaceIcons()) { if (!$useSvg && $this->shouldReplaceIcons()) {
if (!$folder->fileExists($key . '.png')) { if (!$folder->fileExists($key . '.png')) {
try { try {

+ 48
- 7
apps/theming/tests/ImageManagerTest.php View File

use OCA\Theming\ImageManager; use OCA\Theming\ImageManager;
use OCA\Theming\ThemingDefaults; use OCA\Theming\ThemingDefaults;
use OCP\Files\SimpleFS\ISimpleFile; use OCP\Files\SimpleFS\ISimpleFile;
use OCP\ICacheFactory;
use OCP\IConfig; use OCP\IConfig;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use Test\TestCase; use Test\TestCase;
protected $imageManager; protected $imageManager;
/** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */ /** @var IURLGenerator|\PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator; private $urlGenerator;
/** @var ICacheFactory|\PHPUnit_Framework_MockObject_MockObject */
private $cacheFactory;


protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->appData = $this->createMock(IAppData::class); $this->appData = $this->createMock(IAppData::class);
$this->urlGenerator = $this->createMock(IURLGenerator::class); $this->urlGenerator = $this->createMock(IURLGenerator::class);
$this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->imageManager = new ImageManager( $this->imageManager = new ImageManager(
$this->config, $this->config,
$this->appData, $this->appData,
$this->urlGenerator
$this->urlGenerator,
$this->cacheFactory
); );
} }


private function checkImagick() {
if(!extension_loaded('imagick')) {
$this->markTestSkipped('Imagemagick is required for dynamic icon generation.');
}
$checkImagick = new \Imagick();
if (count($checkImagick->queryFormats('SVG')) < 1) {
$this->markTestSkipped('No SVG provider present.');
}
if (count($checkImagick->queryFormats('PNG')) < 1) {
$this->markTestSkipped('No PNG provider present.');
}
}

public function mockGetImage($key, $file) { public function mockGetImage($key, $file) {
/** @var \PHPUnit_Framework_MockObject_MockObject $folder */ /** @var \PHPUnit_Framework_MockObject_MockObject $folder */
$folder = $this->createMock(ISimpleFolder::class); $folder = $this->createMock(ISimpleFolder::class);
->with('logo') ->with('logo')
->willThrowException(new NotFoundException()); ->willThrowException(new NotFoundException());
} else { } else {
$folder->expects($this->once())
$file->expects($this->once())
->method('getContent')
->willReturn(file_get_contents(__DIR__ . '/../../../tests/data/testimage.png'));
$folder->expects($this->at(0))
->method('fileExists')
->with('logo')
->willReturn(true);
$folder->expects($this->at(1))
->method('fileExists')
->with('logo.png')
->willReturn(false);
$folder->expects($this->at(2))
->method('getFile') ->method('getFile')
->with('logo') ->with('logo')
->willReturn($file); ->willReturn($file);
$newFile = $this->createMock(ISimpleFile::class);
$folder->expects($this->at(3))
->method('newFile')
->with('logo.png')
->willReturn($newFile);
$newFile->expects($this->once())
->method('putContent');
$this->appData->expects($this->once()) $this->appData->expects($this->once())
->method('getFolder') ->method('getFolder')
->with('images') ->with('images')
} }


public function testGetImageUrl() { public function testGetImageUrl() {
$this->checkImagick();
$file = $this->createMock(ISimpleFile::class); $file = $this->createMock(ISimpleFile::class);
$this->config->expects($this->exactly(2)) $this->config->expects($this->exactly(2))
->method('getAppValue') ->method('getAppValue')
->withConsecutive( ->withConsecutive(
['theming', 'cachebuster', '0'], ['theming', 'cachebuster', '0'],
['theming', 'logoMime', false]
['theming', 'logoMime', '']
) )
->willReturn(0); ->willReturn(0);
$this->mockGetImage('logo', $file); $this->mockGetImage('logo', $file);
} }


public function testGetImageUrlAbsolute() { public function testGetImageUrlAbsolute() {
$this->checkImagick();
$file = $this->createMock(ISimpleFile::class); $file = $this->createMock(ISimpleFile::class);
$this->config->expects($this->exactly(2)) $this->config->expects($this->exactly(2))
->method('getAppValue') ->method('getAppValue')
->withConsecutive( ->withConsecutive(
['theming', 'cachebuster', '0'], ['theming', 'cachebuster', '0'],
['theming', 'logoMime', false]
['theming', 'logoMime', '']
) )
->willReturn(0); ->willReturn(0);
$this->mockGetImage('logo', $file); $this->mockGetImage('logo', $file);
$this->urlGenerator->expects($this->at(0)) $this->urlGenerator->expects($this->at(0))
->method('linkToRoute')
->willReturn('url-to-image');
->method('getBaseUrl')
->willReturn('baseurl');
$this->urlGenerator->expects($this->at(1)) $this->urlGenerator->expects($this->at(1))
->method('getAbsoluteUrl') ->method('getAbsoluteUrl')
->with('url-to-image?v=0')
->willReturn('url-to-image-absolute?v=0');
$this->urlGenerator->expects($this->at(2))
->method('getAbsoluteUrl')
->willReturn('url-to-image-absolute?v=0'); ->willReturn('url-to-image-absolute?v=0');
$this->assertEquals('url-to-image-absolute?v=0', $this->imageManager->getImageUrlAbsolute('logo')); $this->assertEquals('url-to-image-absolute?v=0', $this->imageManager->getImageUrlAbsolute('logo'));


} }


public function testGetImage() { public function testGetImage() {
$this->checkImagick();
$this->config->expects($this->once()) $this->config->expects($this->once())
->method('getAppValue')->with('theming', 'logoMime', false) ->method('getAppValue')->with('theming', 'logoMime', false)
->willReturn('png'); ->willReturn('png');

Loading…
Cancel
Save