2015-11-16 14:57:41 -05:00
|
|
|
<?php
|
2024-05-28 10:42:42 -04:00
|
|
|
|
2015-11-16 14:57:41 -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-11-16 14:57:41 -05:00
|
|
|
*/
|
|
|
|
|
namespace OCA\Files\Tests\Controller;
|
|
|
|
|
|
2024-07-15 10:29:44 -04:00
|
|
|
use OC\Files\FilenameValidator;
|
2015-11-16 14:57:41 -05:00
|
|
|
use OCA\Files\Controller\ViewController;
|
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;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\App\IAppManager;
|
2015-11-16 14:57:41 -05:00
|
|
|
use OCP\AppFramework\Http;
|
2021-01-12 05:28:04 -05:00
|
|
|
use OCP\AppFramework\Services\IInitialState;
|
2019-08-02 14:08:54 -04:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2017-10-24 09:26:53 -04:00
|
|
|
use OCP\Files\File;
|
|
|
|
|
use OCP\Files\Folder;
|
2016-08-19 02:15:30 -04:00
|
|
|
use OCP\Files\IRootFolder;
|
2021-01-12 05:28:04 -05:00
|
|
|
use OCP\Files\Template\ITemplateManager;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IConfig;
|
|
|
|
|
use OCP\IL10N;
|
2015-11-16 14:57:41 -05:00
|
|
|
use OCP\IRequest;
|
|
|
|
|
use OCP\IURLGenerator;
|
2019-11-22 14:52:10 -05:00
|
|
|
use OCP\IUser;
|
2016-04-12 05:51:50 -04:00
|
|
|
use OCP\IUserSession;
|
2024-08-21 13:31:21 -04:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-11-22 14:52:10 -05:00
|
|
|
use Test\TestCase;
|
2015-11-16 14:57:41 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class ViewControllerTest
|
|
|
|
|
*
|
|
|
|
|
* @package OCA\Files\Tests\Controller
|
|
|
|
|
*/
|
|
|
|
|
class ViewControllerTest extends TestCase {
|
2024-08-21 13:31:21 -04:00
|
|
|
private IRequest&MockObject $request;
|
|
|
|
|
private IURLGenerator&MockObject $urlGenerator;
|
|
|
|
|
private IL10N&MockObject $l10n;
|
|
|
|
|
private IConfig&MockObject $config;
|
|
|
|
|
private IEventDispatcher $eventDispatcher;
|
|
|
|
|
private IUser&MockObject $user;
|
|
|
|
|
private IUserSession&MockObject $userSession;
|
|
|
|
|
private IAppManager&MockObject $appManager;
|
|
|
|
|
private IRootFolder&MockObject $rootFolder;
|
|
|
|
|
private IInitialState&MockObject $initialState;
|
|
|
|
|
private ITemplateManager&MockObject $templateManager;
|
|
|
|
|
private UserConfig&MockObject $userConfig;
|
|
|
|
|
private ViewConfig&MockObject $viewConfig;
|
|
|
|
|
|
|
|
|
|
private ViewController&MockObject $viewController;
|
2015-11-16 14:57:41 -05:00
|
|
|
|
2019-11-27 09:27:18 -05:00
|
|
|
protected function setUp(): void {
|
2015-11-16 14:57:41 -05:00
|
|
|
parent::setUp();
|
2017-10-24 09:26:53 -04:00
|
|
|
$this->request = $this->getMockBuilder(IRequest::class)->getMock();
|
|
|
|
|
$this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
|
|
|
|
|
$this->l10n = $this->getMockBuilder(IL10N::class)->getMock();
|
|
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
|
2019-08-02 14:08:54 -04:00
|
|
|
$this->eventDispatcher = $this->createMock(IEventDispatcher::class);
|
2017-10-24 09:26:53 -04:00
|
|
|
$this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
|
2016-08-16 14:58:33 -04:00
|
|
|
$this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock();
|
2017-10-24 09:26:53 -04:00
|
|
|
$this->user = $this->getMockBuilder(IUser::class)->getMock();
|
2016-05-11 13:41:36 -04:00
|
|
|
$this->user->expects($this->any())
|
|
|
|
|
->method('getUID')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('testuser1');
|
2016-04-12 05:51:50 -04:00
|
|
|
$this->userSession->expects($this->any())
|
|
|
|
|
->method('getUser')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($this->user);
|
2016-08-19 02:15:30 -04:00
|
|
|
$this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
|
2021-01-12 05:28:04 -05:00
|
|
|
$this->initialState = $this->createMock(IInitialState::class);
|
|
|
|
|
$this->templateManager = $this->createMock(ITemplateManager::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-15 10:29:44 -04:00
|
|
|
|
|
|
|
|
$filenameValidator = $this->createMock(FilenameValidator::class);
|
|
|
|
|
|
|
|
|
|
$this->viewController = $this->getMockBuilder(ViewController::class)
|
2015-11-16 14:57:41 -05:00
|
|
|
->setConstructorArgs([
|
2020-04-09 03:22:29 -04:00
|
|
|
'files',
|
|
|
|
|
$this->request,
|
|
|
|
|
$this->urlGenerator,
|
|
|
|
|
$this->l10n,
|
|
|
|
|
$this->config,
|
|
|
|
|
$this->eventDispatcher,
|
|
|
|
|
$this->userSession,
|
|
|
|
|
$this->appManager,
|
|
|
|
|
$this->rootFolder,
|
2021-01-12 05:28:04 -05:00
|
|
|
$this->initialState,
|
|
|
|
|
$this->templateManager,
|
2022-12-28 13:08:54 -05:00
|
|
|
$this->userConfig,
|
2023-04-14 06:40:08 -04:00
|
|
|
$this->viewConfig,
|
2024-07-15 10:29:44 -04:00
|
|
|
$filenameValidator,
|
2020-04-09 03:22:29 -04:00
|
|
|
])
|
2024-08-23 09:10:27 -04:00
|
|
|
->onlyMethods([
|
|
|
|
|
'getStorageInfo',
|
|
|
|
|
])
|
|
|
|
|
->getMock();
|
2015-11-16 14:57:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testIndexWithRegularBrowser() {
|
|
|
|
|
$this->viewController
|
2023-01-04 13:06:52 -05:00
|
|
|
->expects($this->any())
|
2015-11-16 14:57:41 -05:00
|
|
|
->method('getStorageInfo')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn([
|
2017-06-12 04:32:25 -04:00
|
|
|
'used' => 123,
|
|
|
|
|
'quota' => 100,
|
|
|
|
|
'total' => 100,
|
2015-11-16 14:57:41 -05:00
|
|
|
'relative' => 123,
|
|
|
|
|
'owner' => 'MyName',
|
|
|
|
|
'ownerDisplayName' => 'MyDisplayName',
|
2020-03-25 17:21:27 -04:00
|
|
|
]);
|
2023-09-22 08:22:04 -04:00
|
|
|
|
2018-07-12 08:30:39 -04:00
|
|
|
$this->config
|
2016-04-12 05:51:50 -04:00
|
|
|
->method('getUserValue')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnMap([
|
2016-04-12 05:51:50 -04:00
|
|
|
[$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
|
2016-04-12 11:10:09 -04:00
|
|
|
[$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
|
2023-08-14 11:03:56 -04:00
|
|
|
[$this->user->getUID(), 'files', 'files_sorting_configs', '{}', '{}'],
|
2016-04-12 11:10:09 -04:00
|
|
|
[$this->user->getUID(), 'files', 'show_hidden', false, false],
|
2021-01-10 03:14:49 -05:00
|
|
|
[$this->user->getUID(), 'files', 'crop_image_previews', true, true],
|
2018-11-15 14:29:10 -05:00
|
|
|
[$this->user->getUID(), 'files', 'show_grid', true],
|
2020-03-25 17:21:27 -04:00
|
|
|
]);
|
2024-02-09 03:54:52 -05:00
|
|
|
|
2023-08-17 02:55:30 -04:00
|
|
|
$baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
|
|
|
|
|
|
|
|
|
|
$this->rootFolder->expects($this->any())
|
|
|
|
|
->method('getUserFolder')
|
|
|
|
|
->with('testuser1')
|
|
|
|
|
->willReturn($baseFolderFiles);
|
2015-11-16 14:57:41 -05:00
|
|
|
|
2020-04-10 08:19:56 -04:00
|
|
|
$this->config
|
2023-01-04 13:06:52 -05:00
|
|
|
->expects($this->any())
|
|
|
|
|
->method('getAppValue')
|
|
|
|
|
->willReturnArgument(2);
|
2015-11-16 14:57:41 -05:00
|
|
|
|
|
|
|
|
$expected = new Http\TemplateResponse(
|
|
|
|
|
'files',
|
|
|
|
|
'index',
|
|
|
|
|
);
|
2015-12-02 11:30:40 -05:00
|
|
|
$policy = new Http\ContentSecurityPolicy();
|
2023-08-17 02:55:30 -04:00
|
|
|
$policy->addAllowedWorkerSrcDomain('\'self\'');
|
2015-12-02 11:30:40 -05:00
|
|
|
$policy->addAllowedFrameDomain('\'self\'');
|
|
|
|
|
$expected->setContentSecurityPolicy($policy);
|
2018-07-12 08:30:39 -04:00
|
|
|
|
2015-11-16 14:57:41 -05:00
|
|
|
$this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
|
|
|
|
|
}
|
2016-05-04 04:28:06 -04:00
|
|
|
|
2016-08-19 02:15:30 -04:00
|
|
|
public function testShowFileRouteWithTrashedFile() {
|
2016-05-11 13:41:36 -04:00
|
|
|
$this->appManager->expects($this->once())
|
|
|
|
|
->method('isEnabledForUser')
|
|
|
|
|
->with('files_trashbin')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(true);
|
2016-05-11 13:41:36 -04:00
|
|
|
|
2017-10-24 09:26:53 -04:00
|
|
|
$parentNode = $this->getMockBuilder(Folder::class)->getMock();
|
2016-05-11 13:41:36 -04:00
|
|
|
$parentNode->expects($this->once())
|
|
|
|
|
->method('getPath')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
|
2016-05-11 13:41:36 -04:00
|
|
|
|
2017-10-24 09:26:53 -04:00
|
|
|
$baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
|
|
|
|
|
$baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
|
2016-05-11 13:41:36 -04:00
|
|
|
|
2023-08-17 02:55:30 -04:00
|
|
|
$this->rootFolder->expects($this->any())
|
2016-08-19 02:15:30 -04:00
|
|
|
->method('getUserFolder')
|
|
|
|
|
->with('testuser1')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($baseFolderFiles);
|
2022-05-24 07:02:07 -04:00
|
|
|
$this->rootFolder->expects($this->once())
|
2016-05-11 13:41:36 -04:00
|
|
|
->method('get')
|
|
|
|
|
->with('testuser1/files_trashbin/files/')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($baseFolderTrash);
|
2016-05-11 13:41:36 -04:00
|
|
|
|
2023-08-17 02:55:30 -04:00
|
|
|
$baseFolderFiles->expects($this->any())
|
2024-02-09 03:54:52 -05:00
|
|
|
->method('getFirstNodeById')
|
2016-05-11 13:41:36 -04:00
|
|
|
->with(123)
|
2024-02-09 03:54:52 -05:00
|
|
|
->willReturn(null);
|
2016-05-11 13:41:36 -04:00
|
|
|
|
2017-10-24 09:26:53 -04:00
|
|
|
$node = $this->getMockBuilder(File::class)->getMock();
|
2016-05-11 13:41:36 -04:00
|
|
|
$node->expects($this->once())
|
|
|
|
|
->method('getParent')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($parentNode);
|
2016-05-11 13:41:36 -04:00
|
|
|
|
2022-05-24 07:02:07 -04:00
|
|
|
$baseFolderTrash->expects($this->once())
|
2024-02-09 03:54:52 -05:00
|
|
|
->method('getFirstNodeById')
|
2016-05-11 13:41:36 -04:00
|
|
|
->with(123)
|
2024-02-09 03:54:52 -05:00
|
|
|
->willReturn($node);
|
2022-05-24 07:02:07 -04:00
|
|
|
$baseFolderTrash->expects($this->once())
|
2016-05-11 13:41:36 -04:00
|
|
|
->method('getRelativePath')
|
|
|
|
|
->with('testuser1/files_trashbin/files/test.d1462861890/sub')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn('/test.d1462861890/sub');
|
2016-05-11 13:41:36 -04:00
|
|
|
|
|
|
|
|
$this->urlGenerator
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('linkToRoute')
|
2023-08-17 02:55:30 -04:00
|
|
|
->with('files.view.indexViewFileid', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'fileid' => '123'])
|
|
|
|
|
->willReturn('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
|
2016-05-11 13:41:36 -04:00
|
|
|
|
2023-08-17 02:55:30 -04:00
|
|
|
$expected = new Http\RedirectResponse('/apps/files/trashbin/123?dir=/test.d1462861890/sub');
|
2022-02-21 05:35:22 -05:00
|
|
|
$this->assertEquals($expected, $this->viewController->index('', '', '123'));
|
2016-05-11 13:41:36 -04:00
|
|
|
}
|
2015-11-16 14:57:41 -05:00
|
|
|
}
|