2016-08-25 17:26:28 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Julius Härtl <jus@bitgrid.net>
|
|
|
|
*
|
2016-08-30 12:51:48 +03:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2016-08-25 17:26:28 +03: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 12:51:48 +03:00
|
|
|
|
2016-09-18 21:15:06 +03:00
|
|
|
use OC\Files\SimpleFS\SimpleFile;
|
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;
|
2016-08-25 17:26:28 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2016-11-05 22:11:49 +03:00
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
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-30 12:51:48 +03:00
|
|
|
use OCA\Theming\Util;
|
|
|
|
use OCA\Theming\Controller\IconController;
|
2016-09-18 21:15:06 +03:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2016-08-30 12:51:48 +03:00
|
|
|
|
2016-08-25 17:26:28 +03:00
|
|
|
|
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 Util */
|
|
|
|
private $util;
|
|
|
|
/** @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;
|
2016-09-18 21:15:06 +03:00
|
|
|
/** @var ImageManager */
|
|
|
|
private $imageManager;
|
2016-08-25 17:26:28 +03:00
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
$this->request = $this->getMockBuilder('OCP\IRequest')->getMock();
|
2017-04-20 14:10:34 +03:00
|
|
|
$this->themingDefaults = $this->getMockBuilder('OCA\Theming\ThemingDefaults')
|
2016-08-25 17:26:28 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-08-29 12:53:44 +03:00
|
|
|
$this->util = $this->getMockBuilder('\OCA\Theming\Util')->disableOriginalConstructor()
|
|
|
|
->setMethods(['getAppImage', 'getAppIcon', 'elementColor'])->getMock();
|
2016-08-25 17:26:28 +03:00
|
|
|
$this->timeFactory = $this->getMockBuilder('OCP\AppFramework\Utility\ITimeFactory')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->config = $this->getMockBuilder('OCP\IConfig')->getMock();
|
2016-08-30 10:03:42 +03:00
|
|
|
$this->iconBuilder = $this->getMockBuilder('OCA\Theming\IconBuilder')
|
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->imageManager = $this->getMockBuilder('OCA\Theming\ImageManager')->disableOriginalConstructor()->getMock();
|
2016-08-25 17:26:28 +03: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 21:15:06 +03:00
|
|
|
$this->config,
|
|
|
|
$this->iconBuilder,
|
|
|
|
$this->imageManager
|
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);
|
2016-10-14 15:57:58 +03:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2016-08-30 10:03:42 +03:00
|
|
|
$expected->addHeader('Pragma', 'cache');
|
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.');
|
|
|
|
}
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->themingDefaults->expects($this->any())
|
2016-08-30 10:03:42 +03:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(true);
|
2016-09-18 21:15:06 +03:00
|
|
|
|
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');
|
|
|
|
$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-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-10-14 15:57:58 +03:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2016-08-30 10:03:42 +03:00
|
|
|
$expected->addHeader('Pragma', 'cache');
|
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() {
|
|
|
|
$this->themingDefaults->expects($this->any())
|
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(false);
|
2016-11-04 20:55:00 +03:00
|
|
|
$expected = new Http\Response();
|
|
|
|
$expected->setStatus(Http::STATUS_NOT_FOUND);
|
2016-10-14 15:57:58 +03:00
|
|
|
$expected->cacheFor(0);
|
|
|
|
$expected->setLastModified(new \DateTime('now', new \DateTimeZone('GMT')));
|
2016-11-05 22:11:49 +03:00
|
|
|
$this->assertInstanceOf(NotFoundResponse::class, $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.');
|
|
|
|
}
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->themingDefaults->expects($this->any())
|
2016-08-30 10:03:42 +03:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(true);
|
2016-09-18 21:15:06 +03:00
|
|
|
|
2016-08-30 10:03:42 +03:00
|
|
|
$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-10-14 15:57:58 +03:00
|
|
|
$expires = new \DateTime();
|
|
|
|
$expires->setTimestamp($this->timeFactory->getTime());
|
|
|
|
$expires->add(new \DateInterval('PT24H'));
|
|
|
|
$expected->addHeader('Expires', $expires->format(\DateTime::RFC2822));
|
2016-08-30 10:03:42 +03:00
|
|
|
$expected->addHeader('Pragma', 'cache');
|
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() {
|
2016-09-18 21:15:06 +03:00
|
|
|
$this->themingDefaults->expects($this->any())
|
2016-08-30 10:03:42 +03:00
|
|
|
->method('shouldReplaceIcons')
|
|
|
|
->willReturn(false);
|
2016-11-05 22:11:49 +03:00
|
|
|
$this->assertInstanceOf(NotFoundResponse::class, $this->iconController->getTouchIcon());
|
2016-08-30 10:03:42 +03:00
|
|
|
}
|
2016-08-25 17:26:28 +03:00
|
|
|
|
|
|
|
}
|