2016-08-25 14:26:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Julius Haertl <jus@bitgrid.net>
|
2016-08-30 09:51:48 +00:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-08-25 14:26:28 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
2016-08-25 14:26:28 +00:00
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
2016-08-25 14:26:28 +00:00
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-08-25 14:26:28 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
namespace OCA\Theming\Tests\Controller;
|
|
|
|
|
2016-08-30 09:51:48 +00:00
|
|
|
|
2016-09-18 18:15:06 +00:00
|
|
|
use OC\Files\SimpleFS\SimpleFile;
|
2017-05-18 08:45:42 +00:00
|
|
|
use OC\IntegrityCheck\Helpers\FileAccessHelper;
|
2017-04-20 11:10:34 +00:00
|
|
|
use OCA\Theming\IconBuilder;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCA\Theming\ImageManager;
|
2017-04-20 11:10:34 +00:00
|
|
|
use OCA\Theming\ThemingDefaults;
|
2016-08-25 14:26:28 +00:00
|
|
|
use OCP\AppFramework\Http;
|
2017-05-14 21:48:51 +00:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2016-10-17 14:31:07 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCP\IConfig;
|
2016-08-25 14:26:28 +00:00
|
|
|
use OCP\IRequest;
|
|
|
|
use Test\TestCase;
|
2016-08-30 09:51:48 +00:00
|
|
|
use OCA\Theming\Util;
|
|
|
|
use OCA\Theming\Controller\IconController;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2018-03-28 12:56:31 +00:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-08-30 09:51:48 +00:00
|
|
|
|
2016-08-25 14:26:28 +00:00
|
|
|
|
2016-08-29 09:53:44 +00:00
|
|
|
class IconControllerTest extends TestCase {
|
2016-08-25 14:26:28 +00:00
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $request;
|
2017-04-20 11:10:34 +00:00
|
|
|
/** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-25 14:26:28 +00:00
|
|
|
private $themingDefaults;
|
|
|
|
/** @var \OCP\AppFramework\Utility\ITimeFactory */
|
|
|
|
private $timeFactory;
|
2016-11-04 17:55:00 +00:00
|
|
|
/** @var IconController|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-25 14:26:28 +00:00
|
|
|
private $iconController;
|
2016-09-18 18:15:06 +00:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $config;
|
2017-04-20 11:10:34 +00:00
|
|
|
/** @var IconBuilder|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-30 07:03:42 +00:00
|
|
|
private $iconBuilder;
|
2017-05-18 08:45:42 +00:00
|
|
|
/** @var FileAccessHelper|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $fileAccessHelper;
|
2016-09-18 18:15:06 +00:00
|
|
|
/** @var ImageManager */
|
|
|
|
private $imageManager;
|
2016-08-25 14:26:28 +00:00
|
|
|
|
|
|
|
public function setUp() {
|
2018-03-28 12:56:31 +00:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
|
|
|
$this->iconBuilder = $this->createMock(IconBuilder::class);
|
|
|
|
$this->imageManager = $this->createMock(ImageManager::class);
|
2017-05-18 08:45:42 +00:00
|
|
|
$this->fileAccessHelper = $this->createMock(FileAccessHelper::class);
|
2016-08-25 14:26:28 +00:00
|
|
|
$this->timeFactory->expects($this->any())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(123);
|
|
|
|
|
|
|
|
$this->iconController = new IconController(
|
|
|
|
'theming',
|
|
|
|
$this->request,
|
|
|
|
$this->themingDefaults,
|
|
|
|
$this->timeFactory,
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->iconBuilder,
|
2017-05-18 08:45:42 +00:00
|
|
|
$this->imageManager,
|
|
|
|
$this->fileAccessHelper
|
2016-08-25 14:26:28 +00:00
|
|
|
);
|
|
|
|
|
2016-08-30 07:03:42 +00:00
|
|
|
parent::setUp();
|
2016-08-25 14:26:28 +00:00
|
|
|
}
|
|
|
|
|
2016-09-18 18:15:06 +00:00
|
|
|
private function iconFileMock($filename, $data) {
|
|
|
|
$icon = $this->getMockBuilder('OCP\Files\File')->getMock();
|
|
|
|
$icon->expects($this->any())->method('getContent')->willReturn($data);
|
|
|
|
$icon->expects($this->any())->method('getMimeType')->willReturn('image type');
|
|
|
|
$icon->expects($this->any())->method('getEtag')->willReturn('my etag');
|
|
|
|
$icon->method('getName')->willReturn($filename);
|
|
|
|
return new SimpleFile($icon);
|
|
|
|
}
|
|
|
|
|
2016-08-25 14:26:28 +00:00
|
|
|
public function testGetThemedIcon() {
|
2016-09-18 18:15:06 +00:00
|
|
|
$file = $this->iconFileMock('icon-core-filetypes_folder.svg', 'filecontent');
|
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getCachedImage')
|
|
|
|
->with('icon-core-filetypes_folder.svg')
|
|
|
|
->willReturn($file);
|
|
|
|
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/svg+xml']);
|
2016-08-25 14:26:28 +00:00
|
|
|
$expected->cacheFor(86400);
|
2017-04-20 11:10:34 +00:00
|
|
|
$this->assertEquals($expected, $this->iconController->getThemedIcon('core', 'filetypes/folder.svg'));
|
2016-08-25 14:26:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFaviconDefault() {
|
2016-09-18 18:15:06 +00:00
|
|
|
if (!extension_loaded('imagick')) {
|
2016-08-30 07:03:42 +00:00
|
|
|
$this->markTestSkipped('Imagemagick is required for dynamic icon generation.');
|
|
|
|
}
|
|
|
|
$checkImagick = new \Imagick();
|
|
|
|
if (count($checkImagick->queryFormats('SVG')) < 1) {
|
|
|
|
$this->markTestSkipped('No SVG provider present.');
|
|
|
|
}
|
2018-03-28 12:56:31 +00:00
|
|
|
$file = $this->iconFileMock('filename', 'filecontent');
|
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getImage')
|
|
|
|
->with('favicon')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->themingDefaults->expects($this->any())
|
2016-08-30 07:03:42 +00:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(true);
|
2018-03-28 12:56:31 +00:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getCachedImage')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2016-08-30 07:03:42 +00:00
|
|
|
$this->iconBuilder->expects($this->once())
|
|
|
|
->method('getFavicon')
|
2016-08-29 09:53:44 +00:00
|
|
|
->with('core')
|
2016-09-18 18:15:06 +00:00
|
|
|
->willReturn('filecontent');
|
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('setCachedImage')
|
|
|
|
->willReturn($file);
|
2016-08-30 07:03:42 +00:00
|
|
|
|
2016-09-18 18:15:06 +00:00
|
|
|
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
2016-08-25 14:26:28 +00:00
|
|
|
$expected->cacheFor(86400);
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->assertEquals($expected, $this->iconController->getFavicon());
|
2016-08-25 14:26:28 +00:00
|
|
|
}
|
2016-09-18 18:15:06 +00:00
|
|
|
|
|
|
|
public function testGetFaviconFail() {
|
2018-03-28 12:56:31 +00:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getImage')
|
|
|
|
->with('favicon')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->themingDefaults->expects($this->any())
|
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(false);
|
2017-05-14 21:48:51 +00:00
|
|
|
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
|
2017-05-18 08:45:42 +00:00
|
|
|
$this->fileAccessHelper->expects($this->once())
|
|
|
|
->method('file_get_contents')
|
|
|
|
->with($fallbackLogo)
|
|
|
|
->willReturn(file_get_contents($fallbackLogo));
|
2017-05-17 08:10:40 +00:00
|
|
|
$expected = new DataDisplayResponse(file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
2017-05-14 21:48:51 +00:00
|
|
|
$expected->cacheFor(86400);
|
|
|
|
$this->assertEquals($expected, $this->iconController->getFavicon());
|
2016-09-18 18:15:06 +00:00
|
|
|
}
|
|
|
|
|
2016-08-25 14:26:28 +00:00
|
|
|
public function testGetTouchIconDefault() {
|
2016-09-18 18:15:06 +00:00
|
|
|
if (!extension_loaded('imagick')) {
|
2016-08-30 07:03:42 +00:00
|
|
|
$this->markTestSkipped('Imagemagick is required for dynamic icon generation.');
|
|
|
|
}
|
|
|
|
$checkImagick = new \Imagick();
|
|
|
|
if (count($checkImagick->queryFormats('SVG')) < 1) {
|
|
|
|
$this->markTestSkipped('No SVG provider present.');
|
|
|
|
}
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->themingDefaults->expects($this->any())
|
2016-08-30 07:03:42 +00:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(true);
|
2016-09-18 18:15:06 +00:00
|
|
|
|
2016-08-30 07:03:42 +00:00
|
|
|
$this->iconBuilder->expects($this->once())
|
|
|
|
->method('getTouchIcon')
|
2016-08-29 09:53:44 +00:00
|
|
|
->with('core')
|
2016-09-18 18:15:06 +00:00
|
|
|
->willReturn('filecontent');
|
|
|
|
$file = $this->iconFileMock('filename', 'filecontent');
|
2016-10-17 14:31:07 +00:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getCachedImage')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('setCachedImage')
|
|
|
|
->willReturn($file);
|
2016-08-25 14:26:28 +00:00
|
|
|
|
2016-09-18 18:15:06 +00:00
|
|
|
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/png']);
|
2016-08-25 14:26:28 +00:00
|
|
|
$expected->cacheFor(86400);
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->assertEquals($expected, $this->iconController->getTouchIcon());
|
2016-08-25 14:26:28 +00:00
|
|
|
}
|
|
|
|
|
2016-08-30 07:03:42 +00:00
|
|
|
public function testGetTouchIconFail() {
|
2018-03-28 12:56:31 +00:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getImage')
|
|
|
|
->with('favicon')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->themingDefaults->expects($this->any())
|
2016-08-30 07:03:42 +00:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(false);
|
2017-05-14 21:48:51 +00:00
|
|
|
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
|
2017-05-18 08:45:42 +00:00
|
|
|
$this->fileAccessHelper->expects($this->once())
|
|
|
|
->method('file_get_contents')
|
|
|
|
->with($fallbackLogo)
|
|
|
|
->willReturn(file_get_contents($fallbackLogo));
|
2017-05-17 08:10:40 +00:00
|
|
|
$expected = new DataDisplayResponse(file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
|
2017-05-14 21:48:51 +00:00
|
|
|
$expected->cacheFor(86400);
|
|
|
|
$this->assertEquals($expected, $this->iconController->getTouchIcon());
|
2016-08-30 07:03:42 +00:00
|
|
|
}
|
2016-08-25 14:26:28 +00:00
|
|
|
|
|
|
|
}
|