123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 |
- <?php
- /**
- * @author Lukas Reschke <lukas@owncloud.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Roeland Jago Douma <rullzer@owncloud.com>
- * @author Vincent Petry <pvince81@owncloud.com>
- *
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- * @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\Files\Controller;
- use OC\Files\FileInfo;
- use OCP\AppFramework\Http;
- use OCP\Files\NotFoundException;
- use OCP\Files\StorageNotAvailableException;
- use Test\TestCase;
- use OCP\IRequest;
- use OCA\Files\Service\TagService;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\IPreview;
- use OCP\Image;
- /**
- * Class ApiController
- *
- * @package OCA\Files\Controller
- */
- class ApiControllerTest extends TestCase {
- /** @var string */
- private $appName = 'files';
- /** @var IRequest */
- private $request;
- /** @var TagService */
- private $tagService;
- /** @var IPreview */
- private $preview;
- /** @var ApiController */
- private $apiController;
- public function setUp() {
- $this->request = $this->getMockBuilder('\OCP\IRequest')
- ->disableOriginalConstructor()
- ->getMock();
- $this->tagService = $this->getMockBuilder('\OCA\Files\Service\TagService')
- ->disableOriginalConstructor()
- ->getMock();
- $this->preview = $this->getMockBuilder('\OCP\IPreview')
- ->disableOriginalConstructor()
- ->getMock();
- $this->apiController = new ApiController(
- $this->appName,
- $this->request,
- $this->tagService,
- $this->preview
- );
- }
- public function testGetFilesByTagEmpty() {
- $tagName = 'MyTagName';
- $this->tagService->expects($this->once())
- ->method('getFilesByTag')
- ->with($this->equalTo([$tagName]))
- ->will($this->returnValue([]));
- $expected = new DataResponse(['files' => []]);
- $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName]));
- }
- public function testGetFilesByTagSingle() {
- $tagName = 'MyTagName';
- $fileInfo = new FileInfo(
- '/root.txt',
- $this->getMockBuilder('\OC\Files\Storage\Storage')
- ->disableOriginalConstructor()
- ->getMock(),
- '/var/www/root.txt',
- [
- 'mtime' => 55,
- 'mimetype' => 'application/pdf',
- 'permissions' => 31,
- 'size' => 1234,
- 'etag' => 'MyEtag',
- ],
- $this->getMockBuilder('\OCP\Files\Mount\IMountPoint')
- ->disableOriginalConstructor()
- ->getMock()
- );
- $this->tagService->expects($this->once())
- ->method('getFilesByTag')
- ->with($this->equalTo([$tagName]))
- ->will($this->returnValue([$fileInfo]));
- $expected = new DataResponse([
- 'files' => [
- [
- 'id' => null,
- 'parentId' => null,
- 'mtime' => 55000,
- 'name' => 'root.txt',
- 'permissions' => 31,
- 'mimetype' => 'application/pdf',
- 'size' => 1234,
- 'type' => 'file',
- 'etag' => 'MyEtag',
- 'path' => '/',
- 'tags' => [
- [
- 'MyTagName'
- ]
- ],
- ],
- ],
- ]);
- $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName]));
- }
- public function testGetFilesByTagMultiple() {
- $tagName = 'MyTagName';
- $fileInfo1 = new FileInfo(
- '/root.txt',
- $this->getMockBuilder('\OC\Files\Storage\Storage')
- ->disableOriginalConstructor()
- ->getMock(),
- '/var/www/root.txt',
- [
- 'mtime' => 55,
- 'mimetype' => 'application/pdf',
- 'permissions' => 31,
- 'size' => 1234,
- 'etag' => 'MyEtag',
- ],
- $this->getMockBuilder('\OCP\Files\Mount\IMountPoint')
- ->disableOriginalConstructor()
- ->getMock()
- );
- $fileInfo2 = new FileInfo(
- '/root.txt',
- $this->getMockBuilder('\OC\Files\Storage\Storage')
- ->disableOriginalConstructor()
- ->getMock(),
- '/var/www/some/sub.txt',
- [
- 'mtime' => 999,
- 'mimetype' => 'application/binary',
- 'permissions' => 31,
- 'size' => 9876,
- 'etag' => 'SubEtag',
- ],
- $this->getMockBuilder('\OCP\Files\Mount\IMountPoint')
- ->disableOriginalConstructor()
- ->getMock()
- );
- $this->tagService->expects($this->once())
- ->method('getFilesByTag')
- ->with($this->equalTo([$tagName]))
- ->will($this->returnValue([$fileInfo1, $fileInfo2]));
- $expected = new DataResponse([
- 'files' => [
- [
- 'id' => null,
- 'parentId' => null,
- 'mtime' => 55000,
- 'name' => 'root.txt',
- 'permissions' => 31,
- 'mimetype' => 'application/pdf',
- 'size' => 1234,
- 'type' => 'file',
- 'etag' => 'MyEtag',
- 'path' => '/',
- 'tags' => [
- [
- 'MyTagName'
- ]
- ],
- ],
- [
- 'id' => null,
- 'parentId' => null,
- 'mtime' => 999000,
- 'name' => 'root.txt',
- 'permissions' => 31,
- 'mimetype' => 'application/binary',
- 'size' => 9876,
- 'type' => 'file',
- 'etag' => 'SubEtag',
- 'path' => '/',
- 'tags' => [
- [
- 'MyTagName'
- ]
- ],
- ]
- ],
- ]);
- $this->assertEquals($expected, $this->apiController->getFilesByTag([$tagName]));
- }
- public function testUpdateFileTagsEmpty() {
- $expected = new DataResponse([]);
- $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt'));
- }
- public function testUpdateFileTagsWorking() {
- $this->tagService->expects($this->once())
- ->method('updateFileTags')
- ->with('/path.txt', ['Tag1', 'Tag2']);
- $expected = new DataResponse([
- 'tags' => [
- 'Tag1',
- 'Tag2'
- ],
- ]);
- $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
- }
- public function testUpdateFileTagsNotFoundException() {
- $this->tagService->expects($this->once())
- ->method('updateFileTags')
- ->with('/path.txt', ['Tag1', 'Tag2'])
- ->will($this->throwException(new NotFoundException('My error message')));
- $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
- $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
- }
- public function testUpdateFileTagsStorageNotAvailableException() {
- $this->tagService->expects($this->once())
- ->method('updateFileTags')
- ->with('/path.txt', ['Tag1', 'Tag2'])
- ->will($this->throwException(new StorageNotAvailableException('My error message')));
- $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE);
- $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
- }
- public function testUpdateFileTagsStorageGenericException() {
- $this->tagService->expects($this->once())
- ->method('updateFileTags')
- ->with('/path.txt', ['Tag1', 'Tag2'])
- ->will($this->throwException(new \Exception('My error message')));
- $expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
- $this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
- }
- public function testGetThumbnailInvalidSize() {
- $expected = new DataResponse(['message' => 'Requested size must be numeric and a positive value.'], Http::STATUS_BAD_REQUEST);
- $this->assertEquals($expected, $this->apiController->getThumbnail(0, 0, ''));
- }
- public function testGetThumbnailInvaidImage() {
- $this->preview->expects($this->once())
- ->method('createPreview')
- ->with('files/unknown.jpg', 10, 10, true)
- ->willReturn(new Image);
- $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
- $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
- }
- public function testGetThumbnail() {
- $this->preview->expects($this->once())
- ->method('createPreview')
- ->with('files/known.jpg', 10, 10, true)
- ->willReturn(new Image(\OC::$SERVERROOT.'/tests/data/testimage.jpg'));
- $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
- $this->assertEquals(Http::STATUS_OK, $ret->getStatus());
- }
- }
|