123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
- * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
- * SPDX-License-Identifier: AGPL-3.0-only
- */
- namespace OCA\Files\Controller;
- use OCA\Files\Service\TagService;
- use OCA\Files\Service\UserConfig;
- use OCA\Files\Service\ViewConfig;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\Files\File;
- use OCP\Files\Folder;
- use OCP\Files\NotFoundException;
- use OCP\Files\SimpleFS\ISimpleFile;
- use OCP\Files\StorageNotAvailableException;
- use OCP\IConfig;
- use OCP\IPreview;
- use OCP\IRequest;
- use OCP\IUser;
- use OCP\IUserSession;
- use OCP\Share\IManager;
- use Test\TestCase;
- /**
- * Class ApiController
- *
- * @package OCA\Files\Controller
- */
- class ApiControllerTest extends TestCase {
- /** @var string */
- private $appName = 'files';
- /** @var \OCP\IUser */
- private $user;
- /** @var IRequest */
- private $request;
- /** @var TagService */
- private $tagService;
- /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
- private $preview;
- /** @var ApiController */
- private $apiController;
- /** @var \OCP\Share\IManager */
- private $shareManager;
- /** @var \OCP\IConfig */
- private $config;
- /** @var Folder|\PHPUnit\Framework\MockObject\MockObject */
- private $userFolder;
- /** @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $userConfig;
- /** @var ViewConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $viewConfig;
- protected function setUp(): void {
- parent::setUp();
- $this->request = $this->getMockBuilder(IRequest::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->user = $this->createMock(IUser::class);
- $this->user->expects($this->any())
- ->method('getUID')
- ->willReturn('user1');
- $userSession = $this->createMock(IUserSession::class);
- $userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($this->user);
- $this->tagService = $this->getMockBuilder(TagService::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->shareManager = $this->getMockBuilder(IManager::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->preview = $this->getMockBuilder(IPreview::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->config = $this->createMock(IConfig::class);
- $this->userFolder = $this->getMockBuilder(Folder::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->userConfig = $this->createMock(UserConfig::class);
- $this->viewConfig = $this->createMock(ViewConfig::class);
- $this->apiController = new ApiController(
- $this->appName,
- $this->request,
- $userSession,
- $this->tagService,
- $this->preview,
- $this->shareManager,
- $this->config,
- $this->userFolder,
- $this->userConfig,
- $this->viewConfig
- );
- }
- 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() {
- $this->userFolder->method('get')
- ->with($this->equalTo(''))
- ->willThrowException(new NotFoundException());
- $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 testGetThumbnailInvalidImage() {
- $file = $this->createMock(File::class);
- $this->userFolder->method('get')
- ->with($this->equalTo('unknown.jpg'))
- ->willReturn($file);
- $this->preview->expects($this->once())
- ->method('getPreview')
- ->with($file, 10, 10, true)
- ->willThrowException(new NotFoundException());
- $expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
- $this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
- }
- public function testGetThumbnail() {
- $file = $this->createMock(File::class);
- $this->userFolder->method('get')
- ->with($this->equalTo('known.jpg'))
- ->willReturn($file);
- $preview = $this->createMock(ISimpleFile::class);
- $preview->method('getName')->willReturn('my name');
- $preview->method('getMTime')->willReturn(42);
- $this->preview->expects($this->once())
- ->method('getPreview')
- ->with($this->equalTo($file), 10, 10, true)
- ->willReturn($preview);
- $ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
- $this->assertEquals(Http::STATUS_OK, $ret->getStatus());
- $this->assertInstanceOf(Http\FileDisplayResponse::class, $ret);
- }
- public function testShowHiddenFiles() {
- $show = false;
- $this->config->expects($this->once())
- ->method('setUserValue')
- ->with($this->user->getUID(), 'files', 'show_hidden', '0');
- $expected = new Http\Response();
- $actual = $this->apiController->showHiddenFiles($show);
- $this->assertEquals($expected, $actual);
- }
- public function testCropImagePreviews() {
- $crop = true;
- $this->config->expects($this->once())
- ->method('setUserValue')
- ->with($this->user->getUID(), 'files', 'crop_image_previews', '1');
- $expected = new Http\Response();
- $actual = $this->apiController->cropImagePreviews($crop);
- $this->assertEquals($expected, $actual);
- }
- }
|