2016-08-25 14:26:28 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
2016-08-30 09:51:48 +00:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-08-25 14:26:28 +00:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
use OCA\Theming\ImageManager;
|
2016-08-25 14:26:28 +00:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataDisplayResponse;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
2016-10-17 14:31:07 +00:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-09-18 18:15:06 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
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;
|
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;
|
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $themingDefaults;
|
|
|
|
/** @var Util */
|
|
|
|
private $util;
|
|
|
|
/** @var \OCP\AppFramework\Utility\ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
/** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $iconController;
|
2016-09-18 18:15:06 +00:00
|
|
|
/** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $config;
|
2016-08-25 14:26:28 +00:00
|
|
|
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
|
2016-08-30 07:03:42 +00:00
|
|
|
private $iconBuilder;
|
2016-09-18 18:15:06 +00:00
|
|
|
/** @var ImageManager */
|
|
|
|
private $imageManager;
|
2016-08-25 14:26:28 +00:00
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
$this->request = $this->getMockBuilder('OCP\IRequest')->getMock();
|
|
|
|
$this->themingDefaults = $this->getMockBuilder('OCA\Theming\ThemingDefaults')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-08-29 09:53:44 +00:00
|
|
|
$this->util = $this->getMockBuilder('\OCA\Theming\Util')->disableOriginalConstructor()
|
|
|
|
->setMethods(['getAppImage', 'getAppIcon', 'elementColor'])->getMock();
|
2016-08-25 14:26:28 +00:00
|
|
|
$this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->config = $this->getMockBuilder('OCP\IConfig')->getMock();
|
2016-08-30 07:03:42 +00:00
|
|
|
$this->iconBuilder = $this->getMockBuilder('OCA\Theming\IconBuilder')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->imageManager = $this->getMockBuilder('OCA\Theming\ImageManager')->disableOriginalConstructor()->getMock();
|
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->util,
|
|
|
|
$this->timeFactory,
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->config,
|
|
|
|
$this->iconBuilder,
|
|
|
|
$this->imageManager
|
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);
|
2016-10-14 12:57:58 +00:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2016-08-30 07:03:42 +00:00
|
|
|
$expected->addHeader('Pragma', 'cache');
|
2016-09-18 18:15:06 +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.');
|
|
|
|
}
|
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('getFavicon')
|
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-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-10-14 12:57:58 +00:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2016-08-30 07:03:42 +00:00
|
|
|
$expected->addHeader('Pragma', 'cache');
|
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() {
|
|
|
|
$this->themingDefaults->expects($this->any())
|
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(false);
|
|
|
|
$expected = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND);
|
2016-10-14 12:57:58 +00:00
|
|
|
$expected->cacheFor(0);
|
|
|
|
$expected->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->assertEquals($expected, $this->iconController->getFavicon());
|
|
|
|
}
|
|
|
|
|
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-10-14 12:57:58 +00:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2016-08-30 07:03:42 +00:00
|
|
|
$expected->addHeader('Pragma', 'cache');
|
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() {
|
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);
|
|
|
|
$expected = new DataDisplayResponse(null, Http::STATUS_NOT_FOUND);
|
2016-10-14 12:57:58 +00:00
|
|
|
$expected->cacheFor(0);
|
|
|
|
$expected->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
|
2016-09-18 18:15:06 +00:00
|
|
|
$this->assertEquals($expected, $this->iconController->getTouchIcon());
|
2016-08-30 07:03:42 +00:00
|
|
|
}
|
2016-08-25 14:26:28 +00:00
|
|
|
|
|
|
|
}
|