2015-02-17 02:47:29 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2020-12-16 16:54:15 +03:00
|
|
|
* @author Vincent Petry <vincent@nextcloud.com>
|
2015-03-26 13:44:34 +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/>
|
2015-03-26 13:44:34 +03:00
|
|
|
*
|
2015-02-17 02:47:29 +03:00
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2015-02-17 02:47:29 +03:00
|
|
|
namespace OCA\Files\Controller;
|
|
|
|
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCA\Files\Service\TagService;
|
2015-02-17 02:47:29 +03:00
|
|
|
use OCP\AppFramework\Http;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2016-10-17 22:53:23 +03:00
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\Folder;
|
2015-02-17 02:47:29 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-10-17 22:53:23 +03:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2015-02-17 02:47:29 +03:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2016-09-02 11:40:48 +03:00
|
|
|
use OCP\IConfig;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\IPreview;
|
|
|
|
use OCP\IRequest;
|
2016-09-02 11:40:48 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
2016-10-17 22:53:23 +03:00
|
|
|
use OCP\Share\IManager;
|
2015-02-17 02:47:29 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ApiController
|
|
|
|
*
|
|
|
|
* @package OCA\Files\Controller
|
|
|
|
*/
|
|
|
|
class ApiControllerTest extends TestCase {
|
|
|
|
/** @var string */
|
|
|
|
private $appName = 'files';
|
2016-04-12 12:51:50 +03:00
|
|
|
/** @var \OCP\IUser */
|
|
|
|
private $user;
|
2015-02-17 02:47:29 +03:00
|
|
|
/** @var IRequest */
|
|
|
|
private $request;
|
|
|
|
/** @var TagService */
|
|
|
|
private $tagService;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
|
2015-03-16 14:42:40 +03:00
|
|
|
private $preview;
|
2015-02-17 02:47:29 +03:00
|
|
|
/** @var ApiController */
|
|
|
|
private $apiController;
|
2016-03-16 13:03:26 +03:00
|
|
|
/** @var \OCP\Share\IManager */
|
|
|
|
private $shareManager;
|
2016-04-12 12:51:50 +03:00
|
|
|
/** @var \OCP\IConfig */
|
|
|
|
private $config;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Folder|\PHPUnit\Framework\MockObject\MockObject */
|
2016-07-11 13:58:43 +03:00
|
|
|
private $userFolder;
|
2015-02-17 02:47:29 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-10-17 22:53:23 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->request = $this->getMockBuilder(IRequest::class)
|
2015-02-17 02:47:29 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-02 11:40:48 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2016-04-12 12:51:50 +03:00
|
|
|
$this->user->expects($this->any())
|
2016-03-16 13:03:26 +03:00
|
|
|
->method('getUID')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('user1');
|
2016-09-02 11:40:48 +03:00
|
|
|
$userSession = $this->createMock(IUserSession::class);
|
2016-03-16 13:03:26 +03:00
|
|
|
$userSession->expects($this->any())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($this->user);
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->tagService = $this->getMockBuilder(TagService::class)
|
2015-02-17 02:47:29 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->shareManager = $this->getMockBuilder(IManager::class)
|
2016-03-16 13:03:26 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->preview = $this->getMockBuilder(IPreview::class)
|
2015-03-16 14:42:40 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-02 11:40:48 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->userFolder = $this->getMockBuilder(Folder::class)
|
2016-07-11 13:58:43 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-02-17 02:47:29 +03:00
|
|
|
|
|
|
|
$this->apiController = new ApiController(
|
|
|
|
$this->appName,
|
|
|
|
$this->request,
|
2016-03-16 13:03:26 +03:00
|
|
|
$userSession,
|
2015-03-16 14:42:40 +03:00
|
|
|
$this->tagService,
|
2016-03-16 13:03:26 +03:00
|
|
|
$this->preview,
|
2016-04-12 12:51:50 +03:00
|
|
|
$this->shareManager,
|
2016-07-11 13:58:43 +03:00
|
|
|
$this->config,
|
|
|
|
$this->userFolder
|
2015-02-17 02:47:29 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
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']));
|
|
|
|
}
|
2015-03-16 14:42:40 +03:00
|
|
|
|
|
|
|
public function testGetThumbnailInvalidSize() {
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->userFolder->method('get')
|
|
|
|
->with($this->equalTo(''))
|
|
|
|
->willThrowException(new NotFoundException());
|
2015-03-16 14:42:40 +03:00
|
|
|
$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() {
|
2016-10-17 22:53:23 +03:00
|
|
|
$file = $this->createMock(File::class);
|
|
|
|
$this->userFolder->method('get')
|
|
|
|
->with($this->equalTo('unknown.jpg'))
|
|
|
|
->willReturn($file);
|
2015-03-16 14:42:40 +03:00
|
|
|
$this->preview->expects($this->once())
|
2016-10-17 22:53:23 +03:00
|
|
|
->method('getPreview')
|
|
|
|
->with($file, 10, 10, true)
|
|
|
|
->willThrowException(new NotFoundException());
|
2015-03-16 14:42:40 +03:00
|
|
|
$expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
|
|
|
|
$this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetThumbnail() {
|
2016-10-17 22:53:23 +03:00
|
|
|
$file = $this->createMock(File::class);
|
|
|
|
$this->userFolder->method('get')
|
|
|
|
->with($this->equalTo('known.jpg'))
|
|
|
|
->willReturn($file);
|
|
|
|
$preview = $this->createMock(ISimpleFile::class);
|
2015-03-16 14:42:40 +03:00
|
|
|
$this->preview->expects($this->once())
|
2016-10-17 22:53:23 +03:00
|
|
|
->method('getPreview')
|
|
|
|
->with($this->equalTo($file), 10, 10, true)
|
|
|
|
->willReturn($preview);
|
2015-03-16 14:42:40 +03:00
|
|
|
|
|
|
|
$ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_OK, $ret->getStatus());
|
2016-10-17 22:53:23 +03:00
|
|
|
$this->assertInstanceOf(Http\FileDisplayResponse::class, $ret);
|
2015-03-16 14:42:40 +03:00
|
|
|
}
|
2016-04-12 12:51:50 +03:00
|
|
|
|
|
|
|
public function testUpdateFileSorting() {
|
|
|
|
$mode = 'mtime';
|
|
|
|
$direction = 'desc';
|
|
|
|
|
|
|
|
$this->config->expects($this->at(0))
|
|
|
|
->method('setUserValue')
|
|
|
|
->with($this->user->getUID(), 'files', 'file_sorting', $mode);
|
|
|
|
$this->config->expects($this->at(1))
|
|
|
|
->method('setUserValue')
|
|
|
|
->with($this->user->getUID(), 'files', 'file_sorting_direction', $direction);
|
|
|
|
|
|
|
|
$expected = new HTTP\Response();
|
|
|
|
$actual = $this->apiController->updateFileSorting($mode, $direction);
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function invalidSortingModeData() {
|
|
|
|
return [
|
|
|
|
['color', 'asc'],
|
|
|
|
['name', 'size'],
|
|
|
|
['foo', 'bar']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider invalidSortingModeData
|
|
|
|
*/
|
|
|
|
public function testUpdateInvalidFileSorting($mode, $direction) {
|
|
|
|
$this->config->expects($this->never())
|
|
|
|
->method('setUserValue');
|
|
|
|
|
|
|
|
$expected = new Http\Response(null);
|
|
|
|
$expected->setStatus(Http::STATUS_UNPROCESSABLE_ENTITY);
|
|
|
|
|
|
|
|
$result = $this->apiController->updateFileSorting($mode, $direction);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $result);
|
|
|
|
}
|
|
|
|
|
2016-04-12 18:10:09 +03:00
|
|
|
public function testShowHiddenFiles() {
|
|
|
|
$show = false;
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
->method('setUserValue')
|
|
|
|
->with($this->user->getUID(), 'files', 'show_hidden', $show);
|
|
|
|
|
|
|
|
$expected = new Http\Response();
|
|
|
|
$actual = $this->apiController->showHiddenFiles($show);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
}
|
2015-02-17 02:47:29 +03:00
|
|
|
}
|