2016-06-21 13:25:44 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 14:49:16 +00:00
|
|
|
*
|
2017-11-06 19:15:27 +00:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2016-07-21 14:49:16 +00:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2016-06-21 13:25:44 +00:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\unit\CardDAV;
|
|
|
|
|
|
|
|
|
2017-05-05 13:29:43 +00:00
|
|
|
use OCA\DAV\CardDAV\AddressBook;
|
2016-06-21 13:25:44 +00:00
|
|
|
use OCA\DAV\CardDAV\ImageExportPlugin;
|
2017-05-05 13:29:43 +00:00
|
|
|
use OCA\DAV\CardDAV\PhotoCache;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2016-06-21 13:25:44 +00:00
|
|
|
use Sabre\CardDAV\Card;
|
2017-05-05 13:29:43 +00:00
|
|
|
use Sabre\DAV\Node;
|
2016-06-21 13:25:44 +00:00
|
|
|
use Sabre\DAV\Server;
|
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ImageExportPluginTest extends TestCase {
|
|
|
|
|
2017-05-05 13:29:43 +00:00
|
|
|
/** @var ResponseInterface|\PHPUnit_Framework_MockObject_MockObject */
|
2016-06-21 13:25:44 +00:00
|
|
|
private $response;
|
2017-05-05 13:29:43 +00:00
|
|
|
/** @var RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
|
2016-06-21 13:25:44 +00:00
|
|
|
private $request;
|
2017-05-05 13:29:43 +00:00
|
|
|
/** @var ImageExportPlugin|\PHPUnit_Framework_MockObject_MockObject */
|
2016-06-21 13:25:44 +00:00
|
|
|
private $plugin;
|
|
|
|
/** @var Server */
|
|
|
|
private $server;
|
2017-05-05 13:29:43 +00:00
|
|
|
/** @var Tree|\PHPUnit_Framework_MockObject_MockObject */
|
2016-06-21 13:25:44 +00:00
|
|
|
private $tree;
|
2017-05-05 13:29:43 +00:00
|
|
|
/** @var PhotoCache|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $cache;
|
2016-06-21 13:25:44 +00:00
|
|
|
|
|
|
|
function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2017-05-05 13:29:43 +00:00
|
|
|
$this->request = $this->createMock(RequestInterface::class);
|
|
|
|
$this->response = $this->createMock(ResponseInterface::class);
|
|
|
|
$this->server = $this->createMock(Server::class);
|
|
|
|
$this->tree = $this->createMock(Tree::class);
|
2016-06-21 13:25:44 +00:00
|
|
|
$this->server->tree = $this->tree;
|
2017-05-05 13:29:43 +00:00
|
|
|
$this->cache = $this->createMock(PhotoCache::class);
|
2016-06-21 13:25:44 +00:00
|
|
|
|
2017-05-05 13:29:43 +00:00
|
|
|
$this->plugin = $this->getMockBuilder(ImageExportPlugin::class)
|
2016-07-15 07:52:46 +00:00
|
|
|
->setMethods(['getPhoto'])
|
2017-05-05 13:29:43 +00:00
|
|
|
->setConstructorArgs([$this->cache])
|
2016-07-15 07:52:46 +00:00
|
|
|
->getMock();
|
2016-06-21 13:25:44 +00:00
|
|
|
$this->plugin->initialize($this->server);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providesQueryParams
|
|
|
|
* @param $param
|
|
|
|
*/
|
|
|
|
public function testQueryParams($param) {
|
|
|
|
$this->request->expects($this->once())->method('getQueryParameters')->willReturn($param);
|
|
|
|
$result = $this->plugin->httpGet($this->request, $this->response);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesQueryParams() {
|
|
|
|
return [
|
|
|
|
[[]],
|
|
|
|
[['1']],
|
|
|
|
[['foo' => 'bar']],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:29:43 +00:00
|
|
|
public function testNoCard() {
|
|
|
|
$this->request->method('getQueryParameters')
|
|
|
|
->willReturn([
|
|
|
|
'photo'
|
|
|
|
]);
|
|
|
|
$this->request->method('getPath')
|
|
|
|
->willReturn('user/book/card');
|
|
|
|
|
|
|
|
$node = $this->createMock(Node::class);
|
|
|
|
$this->tree->method('getNodeForPath')
|
|
|
|
->with('user/book/card')
|
|
|
|
->willReturn($node);
|
|
|
|
|
2016-06-21 13:25:44 +00:00
|
|
|
$result = $this->plugin->httpGet($this->request, $this->response);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2017-05-05 13:29:43 +00:00
|
|
|
public function dataTestCard() {
|
|
|
|
return [
|
|
|
|
[null, false],
|
|
|
|
[null, true],
|
|
|
|
[32, false],
|
|
|
|
[32, true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-21 13:25:44 +00:00
|
|
|
/**
|
2017-05-05 13:29:43 +00:00
|
|
|
* @dataProvider dataTestCard
|
|
|
|
*
|
|
|
|
* @param $size
|
|
|
|
* @param bool $photo
|
2016-06-21 13:25:44 +00:00
|
|
|
*/
|
2017-05-05 13:29:43 +00:00
|
|
|
public function testCard($size, $photo) {
|
|
|
|
$query = ['photo' => null];
|
|
|
|
if ($size !== null) {
|
|
|
|
$query['size'] = $size;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->request->method('getQueryParameters')
|
|
|
|
->willReturn($query);
|
|
|
|
$this->request->method('getPath')
|
|
|
|
->willReturn('user/book/card');
|
2016-06-21 13:25:44 +00:00
|
|
|
|
2017-05-05 13:29:43 +00:00
|
|
|
$card = $this->createMock(Card::class);
|
2017-05-02 10:35:42 +00:00
|
|
|
$card->method('getETag')
|
|
|
|
->willReturn('"myEtag"');
|
2017-05-05 13:29:43 +00:00
|
|
|
$card->method('getName')
|
|
|
|
->willReturn('card');
|
|
|
|
$book = $this->createMock(AddressBook::class);
|
|
|
|
$book->method('getResourceId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$this->tree->method('getNodeForPath')
|
|
|
|
->willReturnCallback(function($path) use ($card, $book) {
|
|
|
|
if ($path === 'user/book/card') {
|
|
|
|
return $card;
|
|
|
|
} else if ($path === 'user/book') {
|
|
|
|
return $book;
|
|
|
|
}
|
|
|
|
$this->fail();
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->response->expects($this->at(0))
|
|
|
|
->method('setHeader')
|
|
|
|
->with('Cache-Control', 'private, max-age=3600, must-revalidate');
|
|
|
|
$this->response->expects($this->at(1))
|
|
|
|
->method('setHeader')
|
|
|
|
->with('Etag', '"myEtag"');
|
|
|
|
$this->response->expects($this->at(2))
|
|
|
|
->method('setHeader')
|
|
|
|
->with('Pragma', 'public');
|
|
|
|
|
|
|
|
$size = $size === null ? -1 : $size;
|
|
|
|
|
|
|
|
if ($photo) {
|
|
|
|
$file = $this->createMock(ISimpleFile::class);
|
|
|
|
$file->method('getMimeType')
|
|
|
|
->willReturn('imgtype');
|
|
|
|
$file->method('getContent')
|
|
|
|
->willReturn('imgdata');
|
|
|
|
|
|
|
|
$this->cache->method('get')
|
|
|
|
->with(1, 'card', $size, $card)
|
|
|
|
->willReturn($file);
|
|
|
|
|
|
|
|
$this->response->expects($this->at(3))
|
2017-05-02 10:35:42 +00:00
|
|
|
->method('setHeader')
|
2017-05-05 13:29:43 +00:00
|
|
|
->with('Content-Type', 'imgtype');
|
|
|
|
$this->response->expects($this->at(4))
|
2017-05-02 10:35:42 +00:00
|
|
|
->method('setHeader')
|
2016-09-09 10:32:56 +00:00
|
|
|
->with('Content-Disposition', 'attachment');
|
2017-05-05 13:29:43 +00:00
|
|
|
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('setStatus')
|
|
|
|
->with(200);
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('setBody')
|
|
|
|
->with('imgdata');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$this->cache->method('get')
|
|
|
|
->with(1, 'card', $size, $card)
|
|
|
|
->willThrowException(new NotFoundException());
|
|
|
|
$this->response->expects($this->once())
|
|
|
|
->method('setStatus')
|
|
|
|
->with(404);
|
2016-06-21 13:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->plugin->httpGet($this->request, $this->response);
|
2017-05-05 13:29:43 +00:00
|
|
|
$this->assertFalse($result);
|
2016-06-21 13:25:44 +00:00
|
|
|
}
|
|
|
|
}
|