2015-11-16 20:57:41 +01:00
|
|
|
<?php
|
2024-05-28 16:42:42 +02:00
|
|
|
|
2015-11-16 20:57:41 +01:00
|
|
|
/**
|
2024-05-28 16:42:42 +02:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2015-11-16 20:57:41 +01:00
|
|
|
*/
|
|
|
|
namespace OCA\Files\Tests\Controller;
|
|
|
|
|
2024-07-15 16:29:44 +02:00
|
|
|
use OC\Files\FilenameValidator;
|
2025-02-14 09:28:58 +01:00
|
|
|
use OC\Route\Router;
|
|
|
|
use OC\URLGenerator;
|
2015-11-16 20:57:41 +01:00
|
|
|
use OCA\Files\Controller\ViewController;
|
2022-12-28 19:08:54 +01:00
|
|
|
use OCA\Files\Service\UserConfig;
|
2023-04-14 12:40:08 +02:00
|
|
|
use OCA\Files\Service\ViewConfig;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\App\IAppManager;
|
2024-10-10 12:40:31 +02:00
|
|
|
use OCP\AppFramework\Http\ContentSecurityPolicy;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\TemplateResponse;
|
2021-01-12 11:28:04 +01:00
|
|
|
use OCP\AppFramework\Services\IInitialState;
|
2025-02-14 09:28:58 +01:00
|
|
|
use OCP\Diagnostics\IEventLogger;
|
2019-08-02 20:08:54 +02:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2017-10-24 15:26:53 +02:00
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\Folder;
|
2016-08-19 08:15:30 +02:00
|
|
|
use OCP\Files\IRootFolder;
|
2021-01-12 11:28:04 +01:00
|
|
|
use OCP\Files\Template\ITemplateManager;
|
2025-02-14 09:28:58 +01:00
|
|
|
use OCP\ICacheFactory;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\IL10N;
|
2015-11-16 20:57:41 +01:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IURLGenerator;
|
2019-11-22 20:52:10 +01:00
|
|
|
use OCP\IUser;
|
2016-04-12 11:51:50 +02:00
|
|
|
use OCP\IUserSession;
|
2024-08-21 10:31:21 -07:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2025-02-14 09:28:58 +01:00
|
|
|
use Psr\Container\ContainerInterface;
|
|
|
|
use Psr\Log\LoggerInterface;
|
2019-11-22 20:52:10 +01:00
|
|
|
use Test\TestCase;
|
2015-11-16 20:57:41 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ViewControllerTest
|
|
|
|
*
|
2025-02-14 09:28:58 +01:00
|
|
|
* @group RoutingWeirdness
|
|
|
|
*
|
2015-11-16 20:57:41 +01:00
|
|
|
* @package OCA\Files\Tests\Controller
|
|
|
|
*/
|
|
|
|
class ViewControllerTest extends TestCase {
|
2025-02-14 09:28:58 +01:00
|
|
|
private ContainerInterface&MockObject $container;
|
|
|
|
private IAppManager&MockObject $appManager;
|
|
|
|
private ICacheFactory&MockObject $cacheFactory;
|
2024-08-21 10:31:21 -07:00
|
|
|
private IConfig&MockObject $config;
|
|
|
|
private IEventDispatcher $eventDispatcher;
|
2025-02-14 09:28:58 +01:00
|
|
|
private IEventLogger&MockObject $eventLogger;
|
2024-08-21 10:31:21 -07:00
|
|
|
private IInitialState&MockObject $initialState;
|
2025-02-14 09:28:58 +01:00
|
|
|
private IL10N&MockObject $l10n;
|
|
|
|
private IRequest&MockObject $request;
|
|
|
|
private IRootFolder&MockObject $rootFolder;
|
2024-08-21 10:31:21 -07:00
|
|
|
private ITemplateManager&MockObject $templateManager;
|
2025-02-14 09:28:58 +01:00
|
|
|
private IURLGenerator $urlGenerator;
|
|
|
|
private IUser&MockObject $user;
|
|
|
|
private IUserSession&MockObject $userSession;
|
|
|
|
private LoggerInterface&MockObject $logger;
|
2024-08-21 10:31:21 -07:00
|
|
|
private UserConfig&MockObject $userConfig;
|
|
|
|
private ViewConfig&MockObject $viewConfig;
|
2025-02-14 09:28:58 +01:00
|
|
|
private Router $router;
|
2024-08-21 10:31:21 -07:00
|
|
|
|
|
|
|
private ViewController&MockObject $viewController;
|
2015-11-16 20:57:41 +01:00
|
|
|
|
2019-11-27 15:27:18 +01:00
|
|
|
protected function setUp(): void {
|
2015-11-16 20:57:41 +01:00
|
|
|
parent::setUp();
|
2025-02-14 09:28:58 +01:00
|
|
|
$this->appManager = $this->createMock(IAppManager::class);
|
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2019-08-02 20:08:54 +02:00
|
|
|
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
|
2025-02-14 09:28:58 +01:00
|
|
|
$this->initialState = $this->createMock(IInitialState::class);
|
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
|
|
|
$this->request = $this->createMock(IRequest::class);
|
|
|
|
$this->rootFolder = $this->createMock(IRootFolder::class);
|
|
|
|
$this->templateManager = $this->createMock(ITemplateManager::class);
|
|
|
|
$this->userConfig = $this->createMock(UserConfig::class);
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$this->viewConfig = $this->createMock(ViewConfig::class);
|
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$this->user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-05-11 19:41:36 +02:00
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn('testuser1');
|
2016-04-12 11:51:50 +02:00
|
|
|
$this->userSession->expects($this->any())
|
|
|
|
->method('getUser')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn($this->user);
|
2024-07-15 16:29:44 +02:00
|
|
|
|
2025-02-14 09:28:58 +01:00
|
|
|
// Make sure we know the app is enabled
|
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('cleanAppId')
|
|
|
|
->willReturnArgument(0);
|
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('getAppPath')
|
|
|
|
->willReturnCallback(fn (string $appid): string => \OC::$SERVERROOT . '/apps/' . $appid);
|
2025-02-24 16:23:21 +01:00
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('isAppLoaded')
|
|
|
|
->willReturn(true);
|
2025-02-14 09:28:58 +01:00
|
|
|
|
|
|
|
$this->cacheFactory = $this->createMock(ICacheFactory::class);
|
|
|
|
$this->logger = $this->createMock(LoggerInterface::class);
|
|
|
|
$this->eventLogger = $this->createMock(IEventLogger::class);
|
|
|
|
$this->container = $this->createMock(ContainerInterface::class);
|
|
|
|
$this->router = new Router(
|
|
|
|
$this->logger,
|
|
|
|
$this->request,
|
|
|
|
$this->config,
|
|
|
|
$this->eventLogger,
|
|
|
|
$this->container,
|
|
|
|
$this->appManager,
|
|
|
|
);
|
2024-07-15 16:29:44 +02:00
|
|
|
|
2025-02-14 09:28:58 +01:00
|
|
|
// Create a real URLGenerator instance to generate URLs
|
|
|
|
$this->urlGenerator = new URLGenerator(
|
|
|
|
$this->config,
|
|
|
|
$this->userSession,
|
|
|
|
$this->cacheFactory,
|
|
|
|
$this->request,
|
|
|
|
$this->router
|
|
|
|
);
|
|
|
|
|
|
|
|
$filenameValidator = $this->createMock(FilenameValidator::class);
|
2024-07-15 16:29:44 +02:00
|
|
|
$this->viewController = $this->getMockBuilder(ViewController::class)
|
2015-11-16 20:57:41 +01:00
|
|
|
->setConstructorArgs([
|
2020-04-09 09:22:29 +02:00
|
|
|
'files',
|
|
|
|
$this->request,
|
|
|
|
$this->urlGenerator,
|
|
|
|
$this->l10n,
|
|
|
|
$this->config,
|
|
|
|
$this->eventDispatcher,
|
|
|
|
$this->userSession,
|
|
|
|
$this->appManager,
|
|
|
|
$this->rootFolder,
|
2021-01-12 11:28:04 +01:00
|
|
|
$this->initialState,
|
|
|
|
$this->templateManager,
|
2022-12-28 19:08:54 +01:00
|
|
|
$this->userConfig,
|
2023-04-14 12:40:08 +02:00
|
|
|
$this->viewConfig,
|
2024-07-15 16:29:44 +02:00
|
|
|
$filenameValidator,
|
2020-04-09 09:22:29 +02:00
|
|
|
])
|
2024-07-15 16:29:44 +02:00
|
|
|
->onlyMethods([
|
2015-11-16 20:57:41 +01:00
|
|
|
'getStorageInfo',
|
|
|
|
])
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIndexWithRegularBrowser(): void {
|
|
|
|
$this->viewController
|
2023-01-04 19:06:52 +01:00
|
|
|
->expects($this->any())
|
2015-11-16 20:57:41 +01:00
|
|
|
->method('getStorageInfo')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn([
|
2017-06-12 10:32:25 +02:00
|
|
|
'used' => 123,
|
|
|
|
'quota' => 100,
|
|
|
|
'total' => 100,
|
2015-11-16 20:57:41 +01:00
|
|
|
'relative' => 123,
|
|
|
|
'owner' => 'MyName',
|
|
|
|
'ownerDisplayName' => 'MyDisplayName',
|
2020-03-25 22:21:27 +01:00
|
|
|
]);
|
2023-09-22 14:22:04 +02:00
|
|
|
|
2018-07-12 14:30:39 +02:00
|
|
|
$this->config
|
2016-04-12 11:51:50 +02:00
|
|
|
->method('getUserValue')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturnMap([
|
2016-04-12 11:51:50 +02:00
|
|
|
[$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
|
2016-04-12 17:10:09 +02:00
|
|
|
[$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
|
2023-08-14 17:03:56 +02:00
|
|
|
[$this->user->getUID(), 'files', 'files_sorting_configs', '{}', '{}'],
|
2016-04-12 17:10:09 +02:00
|
|
|
[$this->user->getUID(), 'files', 'show_hidden', false, false],
|
2021-01-10 19:14:49 +11:00
|
|
|
[$this->user->getUID(), 'files', 'crop_image_previews', true, true],
|
2018-11-15 20:29:10 +01:00
|
|
|
[$this->user->getUID(), 'files', 'show_grid', true],
|
2020-03-25 22:21:27 +01:00
|
|
|
]);
|
2024-02-09 09:54:52 +01:00
|
|
|
|
2023-08-17 08:55:30 +02:00
|
|
|
$baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
|
|
|
|
|
|
|
|
$this->rootFolder->expects($this->any())
|
|
|
|
->method('getUserFolder')
|
|
|
|
->with('testuser1')
|
|
|
|
->willReturn($baseFolderFiles);
|
2015-11-16 20:57:41 +01:00
|
|
|
|
2018-11-06 15:52:49 +01:00
|
|
|
$this->config
|
2023-01-04 19:06:52 +01:00
|
|
|
->expects($this->any())
|
|
|
|
->method('getAppValue')
|
|
|
|
->willReturnArgument(2);
|
2015-11-16 20:57:41 +01:00
|
|
|
|
2024-10-10 12:40:31 +02:00
|
|
|
$expected = new TemplateResponse(
|
2015-11-16 20:57:41 +01:00
|
|
|
'files',
|
|
|
|
'index',
|
|
|
|
);
|
2024-10-10 12:40:31 +02:00
|
|
|
$policy = new ContentSecurityPolicy();
|
2023-08-17 08:55:30 +02:00
|
|
|
$policy->addAllowedWorkerSrcDomain('\'self\'');
|
2015-12-02 17:30:40 +01:00
|
|
|
$policy->addAllowedFrameDomain('\'self\'');
|
|
|
|
$expected->setContentSecurityPolicy($policy);
|
2018-07-12 14:30:39 +02:00
|
|
|
|
2015-11-16 20:57:41 +01:00
|
|
|
$this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
|
|
|
|
}
|
2016-05-04 10:28:06 +02:00
|
|
|
|
2025-02-14 09:28:58 +01:00
|
|
|
public function dataTestShortRedirect(): array {
|
|
|
|
// openfile is true by default
|
|
|
|
// opendetails is undefined by default
|
|
|
|
// both will be evaluated as truthy
|
|
|
|
return [
|
|
|
|
[null, null, '/index.php/apps/files/files/123456?openfile=true'],
|
|
|
|
['', null, '/index.php/apps/files/files/123456?openfile=true'],
|
|
|
|
[null, '', '/index.php/apps/files/files/123456?openfile=true&opendetails=true'],
|
|
|
|
['', '', '/index.php/apps/files/files/123456?openfile=true&opendetails=true'],
|
|
|
|
['false', '', '/index.php/apps/files/files/123456?openfile=false'],
|
|
|
|
[null, 'false', '/index.php/apps/files/files/123456?openfile=true&opendetails=false'],
|
|
|
|
['true', 'false', '/index.php/apps/files/files/123456?openfile=true&opendetails=false'],
|
|
|
|
['false', 'true', '/index.php/apps/files/files/123456?openfile=false&opendetails=true'],
|
|
|
|
['false', 'false', '/index.php/apps/files/files/123456?openfile=false&opendetails=false'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestShortRedirect
|
|
|
|
*/
|
|
|
|
public function testShortRedirect($openfile, $opendetails, $result) {
|
|
|
|
$this->appManager->expects($this->any())
|
|
|
|
->method('isEnabledForUser')
|
|
|
|
->with('files')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
|
|
|
|
$this->rootFolder->expects($this->any())
|
|
|
|
->method('getUserFolder')
|
|
|
|
->with('testuser1')
|
|
|
|
->willReturn($baseFolderFiles);
|
|
|
|
|
|
|
|
$parentNode = $this->getMockBuilder(Folder::class)->getMock();
|
|
|
|
$parentNode->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->willReturn('testuser1/files/Folder');
|
|
|
|
|
|
|
|
$node = $this->getMockBuilder(File::class)->getMock();
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getParent')
|
|
|
|
->willReturn($parentNode);
|
|
|
|
|
|
|
|
$baseFolderFiles->expects($this->any())
|
|
|
|
->method('getFirstNodeById')
|
|
|
|
->with(123456)
|
|
|
|
->willReturn($node);
|
|
|
|
|
|
|
|
$response = $this->viewController->showFile(123456, $opendetails, $openfile);
|
|
|
|
$this->assertStringContainsString($result, $response->getHeaders()['Location']);
|
|
|
|
}
|
|
|
|
|
2016-08-19 08:15:30 +02:00
|
|
|
public function testShowFileRouteWithTrashedFile(): void {
|
2025-02-14 09:28:58 +01:00
|
|
|
$this->appManager->expects($this->exactly(2))
|
2016-05-11 19:41:36 +02:00
|
|
|
->method('isEnabledForUser')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn(true);
|
2016-05-11 19:41:36 +02:00
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$parentNode = $this->getMockBuilder(Folder::class)->getMock();
|
2016-05-11 19:41:36 +02:00
|
|
|
$parentNode->expects($this->once())
|
|
|
|
->method('getPath')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
|
2016-05-11 19:41:36 +02:00
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
|
|
|
|
$baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
|
2016-05-11 19:41:36 +02:00
|
|
|
|
2023-08-17 08:55:30 +02:00
|
|
|
$this->rootFolder->expects($this->any())
|
2016-08-19 08:15:30 +02:00
|
|
|
->method('getUserFolder')
|
|
|
|
->with('testuser1')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn($baseFolderFiles);
|
2022-05-24 13:02:07 +02:00
|
|
|
$this->rootFolder->expects($this->once())
|
2016-05-11 19:41:36 +02:00
|
|
|
->method('get')
|
|
|
|
->with('testuser1/files_trashbin/files/')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn($baseFolderTrash);
|
2016-05-11 19:41:36 +02:00
|
|
|
|
2023-08-17 08:55:30 +02:00
|
|
|
$baseFolderFiles->expects($this->any())
|
2024-02-09 09:54:52 +01:00
|
|
|
->method('getFirstNodeById')
|
2016-05-11 19:41:36 +02:00
|
|
|
->with(123)
|
2024-02-09 09:54:52 +01:00
|
|
|
->willReturn(null);
|
2016-05-11 19:41:36 +02:00
|
|
|
|
2017-10-24 15:26:53 +02:00
|
|
|
$node = $this->getMockBuilder(File::class)->getMock();
|
2016-05-11 19:41:36 +02:00
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getParent')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn($parentNode);
|
2016-05-11 19:41:36 +02:00
|
|
|
|
2022-05-24 13:02:07 +02:00
|
|
|
$baseFolderTrash->expects($this->once())
|
2024-02-09 09:54:52 +01:00
|
|
|
->method('getFirstNodeById')
|
2016-05-11 19:41:36 +02:00
|
|
|
->with(123)
|
2024-02-09 09:54:52 +01:00
|
|
|
->willReturn($node);
|
2022-05-24 13:02:07 +02:00
|
|
|
$baseFolderTrash->expects($this->once())
|
2016-05-11 19:41:36 +02:00
|
|
|
->method('getRelativePath')
|
|
|
|
->with('testuser1/files_trashbin/files/test.d1462861890/sub')
|
2020-03-25 22:21:27 +01:00
|
|
|
->willReturn('/test.d1462861890/sub');
|
2016-05-11 19:41:36 +02:00
|
|
|
|
2025-02-14 09:28:58 +01:00
|
|
|
$expected = new RedirectResponse('/index.php/apps/files/trashbin/123?dir=/test.d1462861890/sub');
|
2022-02-21 11:35:22 +01:00
|
|
|
$this->assertEquals($expected, $this->viewController->index('', '', '123'));
|
2016-05-11 19:41:36 +02:00
|
|
|
}
|
2015-11-16 20:57:41 +01:00
|
|
|
}
|