2015-02-16 18:47:29 -05:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
declare(strict_types=1);
|
2015-02-16 18:47:29 -05:00
|
|
|
/**
|
2024-05-28 10:42:42 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-02-16 18:47:29 -05:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files\Controller;
|
|
|
|
|
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCA\Files\Service\TagService;
|
2022-12-28 13:08:54 -05:00
|
|
|
use OCA\Files\Service\UserConfig;
|
2023-04-14 06:40:08 -04:00
|
|
|
use OCA\Files\Service\ViewConfig;
|
2015-02-16 18:47:29 -05:00
|
|
|
use OCP\AppFramework\Http;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
|
|
|
|
use OCP\AppFramework\Http\Response;
|
2016-10-17 15:53:23 -04:00
|
|
|
use OCP\Files\File;
|
|
|
|
|
use OCP\Files\Folder;
|
2024-07-30 21:48:52 -04:00
|
|
|
use OCP\Files\IRootFolder;
|
2015-02-16 18:47:29 -05:00
|
|
|
use OCP\Files\NotFoundException;
|
2016-10-17 15:53:23 -04:00
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
2025-01-25 13:46:19 -05:00
|
|
|
use OCP\Files\Storage\ISharedStorage;
|
|
|
|
|
use OCP\Files\Storage\IStorage;
|
2015-02-16 18:47:29 -05:00
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2016-09-02 04:40:48 -04:00
|
|
|
use OCP\IConfig;
|
2024-07-30 21:48:52 -04:00
|
|
|
use OCP\IL10N;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IPreview;
|
|
|
|
|
use OCP\IRequest;
|
2016-09-02 04:40:48 -04:00
|
|
|
use OCP\IUser;
|
|
|
|
|
use OCP\IUserSession;
|
2016-10-17 15:53:23 -04:00
|
|
|
use OCP\Share\IManager;
|
2025-01-25 13:46:19 -05:00
|
|
|
use OCP\Share\IShare;
|
2025-05-29 06:19:28 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2024-07-30 21:48:52 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-02-16 18:47:29 -05:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class ApiController
|
|
|
|
|
*
|
|
|
|
|
* @package OCA\Files\Controller
|
|
|
|
|
*/
|
|
|
|
|
class ApiControllerTest extends TestCase {
|
2025-05-29 06:19:28 -04:00
|
|
|
private string $appName = 'files';
|
|
|
|
|
private IUser $user;
|
|
|
|
|
private IRequest $request;
|
2026-01-07 08:21:48 -05:00
|
|
|
private TagService&MockObject $tagService;
|
2025-05-29 06:19:28 -04:00
|
|
|
private IPreview&MockObject $preview;
|
|
|
|
|
private ApiController $apiController;
|
|
|
|
|
private IManager $shareManager;
|
2026-01-07 08:21:48 -05:00
|
|
|
private IConfig&MockObject $config;
|
2025-05-29 06:19:28 -04:00
|
|
|
private Folder&MockObject $userFolder;
|
|
|
|
|
private UserConfig&MockObject $userConfig;
|
|
|
|
|
private ViewConfig&MockObject $viewConfig;
|
|
|
|
|
private IL10N&MockObject $l10n;
|
|
|
|
|
private IRootFolder&MockObject $rootFolder;
|
|
|
|
|
private LoggerInterface&MockObject $logger;
|
2015-02-16 18:47:29 -05:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2016-10-17 15:53:23 -04:00
|
|
|
parent::setUp();
|
|
|
|
|
|
2025-05-29 06:19:28 -04:00
|
|
|
$this->request = $this->createMock(IRequest::class);
|
2016-09-02 04:40:48 -04:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2016-04-12 05:51:50 -04:00
|
|
|
$this->user->expects($this->any())
|
2016-03-16 06:03:26 -04:00
|
|
|
->method('getUID')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('user1');
|
2016-09-02 04:40:48 -04:00
|
|
|
$userSession = $this->createMock(IUserSession::class);
|
2016-03-16 06:03:26 -04:00
|
|
|
$userSession->expects($this->any())
|
|
|
|
|
->method('getUser')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($this->user);
|
2025-05-29 06:19:28 -04:00
|
|
|
$this->tagService = $this->createMock(TagService::class);
|
|
|
|
|
$this->shareManager = $this->createMock(IManager::class);
|
|
|
|
|
$this->preview = $this->createMock(IPreview::class);
|
2016-09-02 04:40:48 -04:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2025-05-29 06:19:28 -04:00
|
|
|
$this->userFolder = $this->createMock(Folder::class);
|
2022-12-28 13:08:54 -05:00
|
|
|
$this->userConfig = $this->createMock(UserConfig::class);
|
2023-04-14 06:40:08 -04:00
|
|
|
$this->viewConfig = $this->createMock(ViewConfig::class);
|
2024-07-30 21:48:52 -04:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
|
$this->rootFolder = $this->createMock(IRootFolder::class);
|
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
2015-02-16 18:47:29 -05:00
|
|
|
|
|
|
|
|
$this->apiController = new ApiController(
|
|
|
|
|
$this->appName,
|
|
|
|
|
$this->request,
|
2016-03-16 06:03:26 -04:00
|
|
|
$userSession,
|
2015-03-16 07:42:40 -04:00
|
|
|
$this->tagService,
|
2016-03-16 06:03:26 -04:00
|
|
|
$this->preview,
|
2016-04-12 05:51:50 -04:00
|
|
|
$this->shareManager,
|
2016-07-11 06:58:43 -04:00
|
|
|
$this->config,
|
2022-12-28 13:08:54 -05:00
|
|
|
$this->userFolder,
|
2023-04-14 06:40:08 -04:00
|
|
|
$this->userConfig,
|
2024-07-30 21:48:52 -04:00
|
|
|
$this->viewConfig,
|
|
|
|
|
$this->l10n,
|
|
|
|
|
$this->rootFolder,
|
|
|
|
|
$this->logger,
|
2015-02-16 18:47:29 -05:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUpdateFileTagsEmpty(): void {
|
|
|
|
|
$expected = new DataResponse([]);
|
|
|
|
|
$this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUpdateFileTagsWorking(): void {
|
|
|
|
|
$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(): void {
|
|
|
|
|
$this->tagService->expects($this->once())
|
|
|
|
|
->method('updateFileTags')
|
|
|
|
|
->with('/path.txt', ['Tag1', 'Tag2'])
|
2025-06-30 10:56:59 -04:00
|
|
|
->willThrowException(new NotFoundException('My error message'));
|
2015-02-16 18:47:29 -05:00
|
|
|
|
|
|
|
|
$expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
|
|
|
|
|
$this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUpdateFileTagsStorageNotAvailableException(): void {
|
|
|
|
|
$this->tagService->expects($this->once())
|
|
|
|
|
->method('updateFileTags')
|
|
|
|
|
->with('/path.txt', ['Tag1', 'Tag2'])
|
2025-06-30 10:56:59 -04:00
|
|
|
->willThrowException(new StorageNotAvailableException('My error message'));
|
2015-02-16 18:47:29 -05:00
|
|
|
|
|
|
|
|
$expected = new DataResponse(['message' => 'My error message'], Http::STATUS_SERVICE_UNAVAILABLE);
|
|
|
|
|
$this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testUpdateFileTagsStorageGenericException(): void {
|
|
|
|
|
$this->tagService->expects($this->once())
|
|
|
|
|
->method('updateFileTags')
|
|
|
|
|
->with('/path.txt', ['Tag1', 'Tag2'])
|
2025-06-30 10:56:59 -04:00
|
|
|
->willThrowException(new \Exception('My error message'));
|
2015-02-16 18:47:29 -05:00
|
|
|
|
|
|
|
|
$expected = new DataResponse(['message' => 'My error message'], Http::STATUS_NOT_FOUND);
|
|
|
|
|
$this->assertEquals($expected, $this->apiController->updateFileTags('/path.txt', ['Tag1', 'Tag2']));
|
|
|
|
|
}
|
2015-03-16 07:42:40 -04:00
|
|
|
|
|
|
|
|
public function testGetThumbnailInvalidSize(): void {
|
2016-10-17 15:53:23 -04:00
|
|
|
$this->userFolder->method('get')
|
|
|
|
|
->with($this->equalTo(''))
|
|
|
|
|
->willThrowException(new NotFoundException());
|
2015-03-16 07:42:40 -04: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, ''));
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-25 10:43:09 -04:00
|
|
|
public function testGetThumbnailInvalidImage(): void {
|
2025-01-25 13:46:19 -05:00
|
|
|
$storage = $this->createMock(IStorage::class);
|
|
|
|
|
$storage->method('instanceOfStorage')->with(ISharedStorage::class)->willReturn(false);
|
|
|
|
|
|
2016-10-17 15:53:23 -04:00
|
|
|
$file = $this->createMock(File::class);
|
2024-06-21 05:37:47 -04:00
|
|
|
$file->method('getId')->willReturn(123);
|
2025-01-25 13:46:19 -05:00
|
|
|
$file->method('getStorage')->willReturn($storage);
|
2016-10-17 15:53:23 -04:00
|
|
|
$this->userFolder->method('get')
|
|
|
|
|
->with($this->equalTo('unknown.jpg'))
|
|
|
|
|
->willReturn($file);
|
2015-03-16 07:42:40 -04:00
|
|
|
$this->preview->expects($this->once())
|
2016-10-17 15:53:23 -04:00
|
|
|
->method('getPreview')
|
|
|
|
|
->with($file, 10, 10, true)
|
|
|
|
|
->willThrowException(new NotFoundException());
|
2015-03-16 07:42:40 -04:00
|
|
|
$expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
|
|
|
|
|
$this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-21 05:37:47 -04:00
|
|
|
public function testGetThumbnailInvalidPartFile(): void {
|
|
|
|
|
$file = $this->createMock(File::class);
|
|
|
|
|
$file->method('getId')->willReturn(0);
|
|
|
|
|
$this->userFolder->method('get')
|
|
|
|
|
->with($this->equalTo('unknown.jpg'))
|
|
|
|
|
->willReturn($file);
|
|
|
|
|
$expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
|
|
|
|
|
$this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
|
|
|
|
|
}
|
|
|
|
|
|
2025-01-25 13:46:19 -05:00
|
|
|
public function testGetThumbnailSharedNoDownload(): void {
|
|
|
|
|
$share = $this->createMock(IShare::class);
|
|
|
|
|
$share->expects(self::once())
|
2025-06-25 02:49:14 -04:00
|
|
|
->method('canSeeContent')
|
|
|
|
|
->willReturn(false);
|
2025-01-25 13:46:19 -05:00
|
|
|
|
|
|
|
|
$storage = $this->createMock(ISharedStorage::class);
|
|
|
|
|
$storage->expects(self::once())
|
|
|
|
|
->method('instanceOfStorage')
|
|
|
|
|
->with(ISharedStorage::class)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
$storage->expects(self::once())
|
|
|
|
|
->method('getShare')
|
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
|
|
$file = $this->createMock(File::class);
|
|
|
|
|
$file->method('getId')->willReturn(123);
|
|
|
|
|
$file->method('getStorage')->willReturn($storage);
|
|
|
|
|
|
|
|
|
|
$this->userFolder->method('get')
|
|
|
|
|
->with('unknown.jpg')
|
|
|
|
|
->willReturn($file);
|
|
|
|
|
|
|
|
|
|
$this->preview->expects($this->never())
|
|
|
|
|
->method('getPreview');
|
|
|
|
|
|
|
|
|
|
$expected = new DataResponse(['message' => 'File not found.'], Http::STATUS_NOT_FOUND);
|
|
|
|
|
$this->assertEquals($expected, $this->apiController->getThumbnail(10, 10, 'unknown.jpg'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testGetThumbnailShared(): void {
|
|
|
|
|
$share = $this->createMock(IShare::class);
|
|
|
|
|
$share->expects(self::once())
|
2025-06-25 02:49:14 -04:00
|
|
|
->method('canSeeContent')
|
|
|
|
|
->willReturn(true);
|
2025-01-25 13:46:19 -05:00
|
|
|
|
|
|
|
|
$storage = $this->createMock(ISharedStorage::class);
|
|
|
|
|
$storage->expects(self::once())
|
|
|
|
|
->method('instanceOfStorage')
|
|
|
|
|
->with(ISharedStorage::class)
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
$storage->expects(self::once())
|
|
|
|
|
->method('getShare')
|
|
|
|
|
->willReturn($share);
|
|
|
|
|
|
|
|
|
|
$file = $this->createMock(File::class);
|
|
|
|
|
$file->method('getId')->willReturn(123);
|
|
|
|
|
$file->method('getStorage')->willReturn($storage);
|
|
|
|
|
|
|
|
|
|
$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(FileDisplayResponse::class, $ret);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-16 07:42:40 -04:00
|
|
|
public function testGetThumbnail(): void {
|
2025-01-25 13:46:19 -05:00
|
|
|
$storage = $this->createMock(IStorage::class);
|
|
|
|
|
$storage->method('instanceOfStorage')->with(ISharedStorage::class)->willReturn(false);
|
|
|
|
|
|
2016-10-17 15:53:23 -04:00
|
|
|
$file = $this->createMock(File::class);
|
2024-06-21 05:37:47 -04:00
|
|
|
$file->method('getId')->willReturn(123);
|
2025-01-25 13:46:19 -05:00
|
|
|
$file->method('getStorage')->willReturn($storage);
|
|
|
|
|
|
2016-10-17 15:53:23 -04:00
|
|
|
$this->userFolder->method('get')
|
|
|
|
|
->with($this->equalTo('known.jpg'))
|
|
|
|
|
->willReturn($file);
|
|
|
|
|
$preview = $this->createMock(ISimpleFile::class);
|
2021-10-25 10:43:09 -04:00
|
|
|
$preview->method('getName')->willReturn('my name');
|
|
|
|
|
$preview->method('getMTime')->willReturn(42);
|
2015-03-16 07:42:40 -04:00
|
|
|
$this->preview->expects($this->once())
|
2016-10-17 15:53:23 -04:00
|
|
|
->method('getPreview')
|
|
|
|
|
->with($this->equalTo($file), 10, 10, true)
|
|
|
|
|
->willReturn($preview);
|
2015-03-16 07:42:40 -04:00
|
|
|
|
|
|
|
|
$ret = $this->apiController->getThumbnail(10, 10, 'known.jpg');
|
|
|
|
|
|
|
|
|
|
$this->assertEquals(Http::STATUS_OK, $ret->getStatus());
|
2024-10-10 06:40:31 -04:00
|
|
|
$this->assertInstanceOf(FileDisplayResponse::class, $ret);
|
2015-03-16 07:42:40 -04:00
|
|
|
}
|
2016-04-12 05:51:50 -04:00
|
|
|
|
2016-04-12 11:10:09 -04:00
|
|
|
public function testShowHiddenFiles(): void {
|
|
|
|
|
$show = false;
|
|
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
|
->method('setUserValue')
|
2021-05-28 18:02:02 -04:00
|
|
|
->with($this->user->getUID(), 'files', 'show_hidden', '0');
|
2016-04-12 11:10:09 -04:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$expected = new Response();
|
2016-04-12 11:10:09 -04:00
|
|
|
$actual = $this->apiController->showHiddenFiles($show);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
|
}
|
2021-01-10 03:14:49 -05:00
|
|
|
|
|
|
|
|
public function testCropImagePreviews(): void {
|
|
|
|
|
$crop = true;
|
|
|
|
|
|
|
|
|
|
$this->config->expects($this->once())
|
|
|
|
|
->method('setUserValue')
|
2021-05-28 02:22:10 -04:00
|
|
|
->with($this->user->getUID(), 'files', 'crop_image_previews', '1');
|
2021-01-10 03:14:49 -05:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$expected = new Response();
|
2021-01-10 03:14:49 -05:00
|
|
|
$actual = $this->apiController->cropImagePreviews($crop);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
|
|
|
}
|
2015-02-16 18:47:29 -05:00
|
|
|
}
|