2016-06-21 16:25:44 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2017-11-06 22:15:27 +03:00
|
|
|
* @author Georg Ehrke <oc.list@georgehrke.com>
|
2020-09-07 15:37:44 +03:00
|
|
|
* @author Jacob Neplokh <me@jacobneplokh.com>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2016-06-21 16:25:44 +03: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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-06-21 16:25:44 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\DAV\Tests\unit\CardDAV;
|
|
|
|
|
2017-05-05 16:29:43 +03:00
|
|
|
use OCA\DAV\CardDAV\AddressBook;
|
2016-06-21 16:25:44 +03:00
|
|
|
use OCA\DAV\CardDAV\ImageExportPlugin;
|
2017-05-05 16:29:43 +03:00
|
|
|
use OCA\DAV\CardDAV\PhotoCache;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2016-06-21 16:25:44 +03:00
|
|
|
use Sabre\CardDAV\Card;
|
2017-05-05 16:29:43 +03:00
|
|
|
use Sabre\DAV\Node;
|
2016-06-21 16:25:44 +03:00
|
|
|
use Sabre\DAV\Server;
|
|
|
|
use Sabre\DAV\Tree;
|
|
|
|
use Sabre\HTTP\RequestInterface;
|
|
|
|
use Sabre\HTTP\ResponseInterface;
|
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class ImageExportPluginTest extends TestCase {
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ResponseInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2016-06-21 16:25:44 +03:00
|
|
|
private $response;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var RequestInterface|\PHPUnit\Framework\MockObject\MockObject */
|
2016-06-21 16:25:44 +03:00
|
|
|
private $request;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ImageExportPlugin|\PHPUnit\Framework\MockObject\MockObject */
|
2016-06-21 16:25:44 +03:00
|
|
|
private $plugin;
|
|
|
|
/** @var Server */
|
|
|
|
private $server;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Tree|\PHPUnit\Framework\MockObject\MockObject */
|
2016-06-21 16:25:44 +03:00
|
|
|
private $tree;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var PhotoCache|\PHPUnit\Framework\MockObject\MockObject */
|
2017-05-05 16:29:43 +03:00
|
|
|
private $cache;
|
2016-06-21 16:25:44 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-06-21 16:25:44 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-05-05 16:29:43 +03: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 16:25:44 +03:00
|
|
|
$this->server->tree = $this->tree;
|
2017-05-05 16:29:43 +03:00
|
|
|
$this->cache = $this->createMock(PhotoCache::class);
|
2016-06-21 16:25:44 +03:00
|
|
|
|
2017-05-05 16:29:43 +03:00
|
|
|
$this->plugin = $this->getMockBuilder(ImageExportPlugin::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['getPhoto'])
|
2017-05-05 16:29:43 +03:00
|
|
|
->setConstructorArgs([$this->cache])
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2016-06-21 16:25:44 +03: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 16:29:43 +03: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 16:25:44 +03:00
|
|
|
$result = $this->plugin->httpGet($this->request, $this->response);
|
|
|
|
$this->assertTrue($result);
|
|
|
|
}
|
|
|
|
|
2017-05-05 16:29:43 +03:00
|
|
|
public function dataTestCard() {
|
|
|
|
return [
|
|
|
|
[null, false],
|
|
|
|
[null, true],
|
|
|
|
[32, false],
|
|
|
|
[32, true],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-06-21 16:25:44 +03:00
|
|
|
/**
|
2017-05-05 16:29:43 +03:00
|
|
|
* @dataProvider dataTestCard
|
|
|
|
*
|
|
|
|
* @param $size
|
|
|
|
* @param bool $photo
|
2016-06-21 16:25:44 +03:00
|
|
|
*/
|
2017-05-05 16:29:43 +03: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 16:25:44 +03:00
|
|
|
|
2017-05-05 16:29:43 +03:00
|
|
|
$card = $this->createMock(Card::class);
|
2017-05-02 13:35:42 +03:00
|
|
|
$card->method('getETag')
|
|
|
|
->willReturn('"myEtag"');
|
2017-05-05 16:29:43 +03:00
|
|
|
$card->method('getName')
|
|
|
|
->willReturn('card');
|
|
|
|
$book = $this->createMock(AddressBook::class);
|
|
|
|
$book->method('getResourceId')
|
|
|
|
->willReturn(1);
|
|
|
|
|
|
|
|
$this->tree->method('getNodeForPath')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($path) use ($card, $book) {
|
2017-05-05 16:29:43 +03:00
|
|
|
if ($path === 'user/book/card') {
|
|
|
|
return $card;
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif ($path === 'user/book') {
|
2017-05-05 16:29:43 +03:00
|
|
|
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')
|
2020-09-05 21:44:01 +03:00
|
|
|
->willReturn('image/jpeg');
|
2017-05-05 16:29:43 +03:00
|
|
|
$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 13:35:42 +03:00
|
|
|
->method('setHeader')
|
2020-09-05 21:44:01 +03:00
|
|
|
->with('Content-Type', 'image/jpeg');
|
2017-05-05 16:29:43 +03:00
|
|
|
$this->response->expects($this->at(4))
|
2017-05-02 13:35:42 +03:00
|
|
|
->method('setHeader')
|
2020-09-05 21:44:01 +03:00
|
|
|
->with('Content-Disposition', 'attachment; filename=card.jpg');
|
2017-05-05 16:29:43 +03: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 16:25:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->plugin->httpGet($this->request, $this->response);
|
2017-05-05 16:29:43 +03:00
|
|
|
$this->assertFalse($result);
|
2016-06-21 16:25:44 +03:00
|
|
|
}
|
|
|
|
}
|