2016-08-25 17:26:28 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
|
|
|
* @author Julius Haertl <jus@bitgrid.net>
|
2016-08-30 12:51:48 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-08-25 17:26:28 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
2017-11-06 17:56:42 +03: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 17:26:28 +03:00
|
|
|
*
|
2017-11-06 17:56:42 +03: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 17:26:28 +03:00
|
|
|
*
|
2017-11-06 17:56:42 +03: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 17:26:28 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-08-25 17:26:28 +03:00
|
|
|
namespace OCA\Theming\Tests\Controller;
|
|
|
|
|
2016-08-30 12:51:48 +03:00
|
|
|
|
2016-09-18 21:15:06 +03:00
|
|
|
use OC\Files\SimpleFS\SimpleFile;
|
2017-05-18 11:45:42 +03:00
|
|
|
use OC\IntegrityCheck\Helpers\FileAccessHelper;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Theming\Controller\IconController;
|
2017-04-20 14:10:34 +03:00
|
|
|
use OCA\Theming\IconBuilder;
|
2016-09-18 21:15:06 +03:00
|
|
|
use OCA\Theming\ImageManager;
|
2017-04-20 14:10:34 +03:00
|
|
|
use OCA\Theming\ThemingDefaults;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Theming\Util;
|
2016-08-25 17:26:28 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2017-05-15 00:48:51 +03:00
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-10-17 17:31:07 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-09-18 21:15:06 +03:00
|
|
|
use OCP\IConfig;
|
2016-08-25 17:26:28 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
2016-08-29 12:53:44 +03:00
|
|
|
class IconControllerTest extends TestCase {
|
2016-08-25 17:26:28 +03:00
|
|
|
/** @var IRequest|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $request;
|
2017-04-20 14:10:34 +03:00
|
|
|
/** @var ThemingDefaults|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-25 17:26:28 +03:00
|
|
|
private $themingDefaults;
|
|
|
|
/** @var \OCP\AppFramework\Utility\ITimeFactory */
|
|
|
|
private $timeFactory;
|
2016-11-04 20:55:00 +03:00
|
|
|
/** @var IconController|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-25 17:26:28 +03:00
|
|
|
private $iconController;
|
2016-09-18 21:15:06 +03:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $config;
|
2017-04-20 14:10:34 +03:00
|
|
|
/** @var IconBuilder|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-30 10:03:42 +03:00
|
|
|
private $iconBuilder;
|
2017-05-18 11:45:42 +03:00
|
|
|
/** @var FileAccessHelper|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $fileAccessHelper;
|
2016-09-18 21:15:06 +03:00
|
|
|
/** @var ImageManager */
|
|
|
|
private $imageManager;
|
2016-08-25 17:26:28 +03:00
|
|
|
|
|
|
|
public function setUp() {
|
2018-03-28 15:56:31 +03:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->themingDefaults = $this->createMock(ThemingDefaults::class);
|
|
|
|
$this->iconBuilder = $this->createMock(IconBuilder::class);
|
|
|
|
$this->imageManager = $this->createMock(ImageManager::class);
|
2017-05-18 11:45:42 +03:00
|
|
|
$this->fileAccessHelper = $this->createMock(FileAccessHelper::class);
|
2018-06-13 23:54:08 +03:00
|
|
|
|
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2016-08-25 17:26:28 +03:00
|
|
|
$this->timeFactory->expects($this->any())
|
|
|
|
->method('getTime')
|
|
|
|
->willReturn(123);
|
|
|
|
|
2018-06-13 23:54:08 +03:00
|
|
|
$this->overwriteService(ITimeFactory::class, $this->timeFactory);
|
|
|
|
|
2016-08-25 17:26:28 +03:00
|
|
|
$this->iconController = new IconController(
|
|
|
|
'theming',
|
|
|
|
$this->request,
|
|
|
|
$this->themingDefaults,
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->iconBuilder,
|
2017-05-18 11:45:42 +03:00
|
|
|
$this->imageManager,
|
|
|
|
$this->fileAccessHelper
|
2016-08-25 17:26:28 +03:00
|
|
|
);
|
|
|
|
|
2016-08-30 10:03:42 +03:00
|
|
|
parent::setUp();
|
2016-08-25 17:26:28 +03:00
|
|
|
}
|
|
|
|
|
2016-09-18 21:15:06 +03: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 17:26:28 +03:00
|
|
|
public function testGetThemedIcon() {
|
2016-09-18 21:15:06 +03: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 17:26:28 +03:00
|
|
|
$expected->cacheFor(86400);
|
2017-04-20 14:10:34 +03:00
|
|
|
$this->assertEquals($expected, $this->iconController->getThemedIcon('core', 'filetypes/folder.svg'));
|
2016-08-25 17:26:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFaviconDefault() {
|
2016-09-18 21:15:06 +03:00
|
|
|
if (!extension_loaded('imagick')) {
|
2016-08-30 10:03:42 +03: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 15:56:31 +03:00
|
|
|
$file = $this->iconFileMock('filename', 'filecontent');
|
|
|
|
$this->imageManager->expects($this->once())
|
2018-09-02 15:17:17 +03:00
|
|
|
->method('getImage', false)
|
2018-03-28 15:56:31 +03:00
|
|
|
->with('favicon')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2018-06-05 17:59:05 +03:00
|
|
|
$this->imageManager->expects($this->any())
|
2016-08-30 10:03:42 +03:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(true);
|
2018-03-28 15:56:31 +03:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getCachedImage')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2016-08-30 10:03:42 +03:00
|
|
|
$this->iconBuilder->expects($this->once())
|
|
|
|
->method('getFavicon')
|
2016-08-29 12:53:44 +03:00
|
|
|
->with('core')
|
2016-09-18 21:15:06 +03:00
|
|
|
->willReturn('filecontent');
|
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('setCachedImage')
|
|
|
|
->willReturn($file);
|
2016-08-30 10:03:42 +03:00
|
|
|
|
2016-09-18 21:15:06 +03:00
|
|
|
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
2016-08-25 17:26:28 +03:00
|
|
|
$expected->cacheFor(86400);
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->assertEquals($expected, $this->iconController->getFavicon());
|
2016-08-25 17:26:28 +03:00
|
|
|
}
|
2016-09-18 21:15:06 +03:00
|
|
|
|
|
|
|
public function testGetFaviconFail() {
|
2018-03-28 15:56:31 +03:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getImage')
|
2018-09-02 15:17:17 +03:00
|
|
|
->with('favicon', false)
|
2018-03-28 15:56:31 +03:00
|
|
|
->will($this->throwException(new NotFoundException()));
|
2018-06-05 17:59:05 +03:00
|
|
|
$this->imageManager->expects($this->any())
|
2016-09-18 21:15:06 +03:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(false);
|
2017-05-15 00:48:51 +03:00
|
|
|
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon.png';
|
2017-05-18 11:45:42 +03:00
|
|
|
$this->fileAccessHelper->expects($this->once())
|
|
|
|
->method('file_get_contents')
|
|
|
|
->with($fallbackLogo)
|
|
|
|
->willReturn(file_get_contents($fallbackLogo));
|
2017-05-17 11:10:40 +03:00
|
|
|
$expected = new DataDisplayResponse(file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/x-icon']);
|
2017-05-15 00:48:51 +03:00
|
|
|
$expected->cacheFor(86400);
|
|
|
|
$this->assertEquals($expected, $this->iconController->getFavicon());
|
2016-09-18 21:15:06 +03:00
|
|
|
}
|
|
|
|
|
2016-08-25 17:26:28 +03:00
|
|
|
public function testGetTouchIconDefault() {
|
2016-09-18 21:15:06 +03:00
|
|
|
if (!extension_loaded('imagick')) {
|
2016-08-30 10:03:42 +03: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-06-05 17:59:05 +03:00
|
|
|
|
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getImage')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
|
|
|
$this->imageManager->expects($this->any())
|
2016-08-30 10:03:42 +03:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(true);
|
|
|
|
$this->iconBuilder->expects($this->once())
|
|
|
|
->method('getTouchIcon')
|
2016-08-29 12:53:44 +03:00
|
|
|
->with('core')
|
2016-09-18 21:15:06 +03:00
|
|
|
->willReturn('filecontent');
|
|
|
|
$file = $this->iconFileMock('filename', 'filecontent');
|
2016-10-17 17:31:07 +03:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getCachedImage')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('setCachedImage')
|
|
|
|
->willReturn($file);
|
2016-08-25 17:26:28 +03:00
|
|
|
|
2016-09-18 21:15:06 +03:00
|
|
|
$expected = new FileDisplayResponse($file, Http::STATUS_OK, ['Content-Type' => 'image/png']);
|
2016-08-25 17:26:28 +03:00
|
|
|
$expected->cacheFor(86400);
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->assertEquals($expected, $this->iconController->getTouchIcon());
|
2016-08-25 17:26:28 +03:00
|
|
|
}
|
|
|
|
|
2016-08-30 10:03:42 +03:00
|
|
|
public function testGetTouchIconFail() {
|
2018-03-28 15:56:31 +03:00
|
|
|
$this->imageManager->expects($this->once())
|
|
|
|
->method('getImage')
|
|
|
|
->with('favicon')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
2018-06-05 17:59:05 +03:00
|
|
|
$this->imageManager->expects($this->any())
|
2016-08-30 10:03:42 +03:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(false);
|
2017-05-15 00:48:51 +03:00
|
|
|
$fallbackLogo = \OC::$SERVERROOT . '/core/img/favicon-touch.png';
|
2017-05-18 11:45:42 +03:00
|
|
|
$this->fileAccessHelper->expects($this->once())
|
|
|
|
->method('file_get_contents')
|
|
|
|
->with($fallbackLogo)
|
|
|
|
->willReturn(file_get_contents($fallbackLogo));
|
2017-05-17 11:10:40 +03:00
|
|
|
$expected = new DataDisplayResponse(file_get_contents($fallbackLogo), Http::STATUS_OK, ['Content-Type' => 'image/png']);
|
2017-05-15 00:48:51 +03:00
|
|
|
$expected->cacheFor(86400);
|
|
|
|
$this->assertEquals($expected, $this->iconController->getTouchIcon());
|
2016-08-30 10:03:42 +03:00
|
|
|
}
|
2016-08-25 17:26:28 +03:00
|
|
|
|
|
|
|
}
|