123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- <?php
- /**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Kesselberg <mail@danielkesselberg.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author John Molakvoæ <skjnldsv@protonmail.com>
- * @author Julius Härtl <jus@bitgrid.net>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Michael Weimann <mail@michael-weimann.eu>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Nina Pypchenko <22447785+nina-py@users.noreply.github.com>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @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,
- * along with this program. If not, see <http://www.gnu.org/licenses/>
- *
- */
- namespace OCA\Files\Tests\Controller;
- use OCA\Files\Activity\Helper;
- use OCA\Files\Controller\ViewController;
- use OCA\Files\Service\UserConfig;
- use OCP\App\IAppManager;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Services\IInitialState;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Files\File;
- use OCP\Files\Folder;
- use OCP\Files\IRootFolder;
- use OCP\Files\Template\ITemplateManager;
- use OCP\IConfig;
- use OCP\IL10N;
- use OCP\IRequest;
- use OCP\IURLGenerator;
- use OCP\IUser;
- use OCP\IUserSession;
- use OCP\Share\IManager;
- use OCP\Template;
- use Symfony\Component\EventDispatcher\EventDispatcherInterface;
- use Test\TestCase;
- /**
- * Class ViewControllerTest
- *
- * @package OCA\Files\Tests\Controller
- */
- class ViewControllerTest extends TestCase {
- /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject */
- private $request;
- /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
- private $urlGenerator;
- /** @var IL10N */
- private $l10n;
- /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $config;
- /** @var EventDispatcherInterface */
- private $eventDispatcher;
- /** @var ViewController|\PHPUnit\Framework\MockObject\MockObject */
- private $viewController;
- /** @var IUser */
- private $user;
- /** @var IUserSession */
- private $userSession;
- /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
- private $appManager;
- /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
- private $rootFolder;
- /** @var Helper|\PHPUnit\Framework\MockObject\MockObject */
- private $activityHelper;
- /** @var IInitialState|\PHPUnit\Framework\MockObject\MockObject */
- private $initialState;
- /** @var ITemplateManager|\PHPUnit\Framework\MockObject\MockObject */
- private $templateManager;
- /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
- private $shareManager;
- /** @var UserConfig|\PHPUnit\Framework\MockObject\MockObject */
- private $userConfig;
- protected function setUp(): void {
- parent::setUp();
- $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();
- $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
- $this->userSession = $this->getMockBuilder(IUserSession::class)->getMock();
- $this->appManager = $this->getMockBuilder('\OCP\App\IAppManager')->getMock();
- $this->user = $this->getMockBuilder(IUser::class)->getMock();
- $this->user->expects($this->any())
- ->method('getUID')
- ->willReturn('testuser1');
- $this->userSession->expects($this->any())
- ->method('getUser')
- ->willReturn($this->user);
- $this->rootFolder = $this->getMockBuilder('\OCP\Files\IRootFolder')->getMock();
- $this->activityHelper = $this->createMock(Helper::class);
- $this->initialState = $this->createMock(IInitialState::class);
- $this->templateManager = $this->createMock(ITemplateManager::class);
- $this->shareManager = $this->createMock(IManager::class);
- $this->userConfig = $this->createMock(UserConfig::class);
- $this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
- ->setConstructorArgs([
- 'files',
- $this->request,
- $this->urlGenerator,
- $this->l10n,
- $this->config,
- $this->eventDispatcher,
- $this->userSession,
- $this->appManager,
- $this->rootFolder,
- $this->activityHelper,
- $this->initialState,
- $this->templateManager,
- $this->shareManager,
- $this->userConfig,
- ])
- ->setMethods([
- 'getStorageInfo',
- 'renderScript'
- ])
- ->getMock();
- }
- public function testIndexWithRegularBrowser() {
- $this->viewController
- ->expects($this->any())
- ->method('getStorageInfo')
- ->willReturn([
- 'used' => 123,
- 'quota' => 100,
- 'total' => 100,
- 'relative' => 123,
- 'owner' => 'MyName',
- 'ownerDisplayName' => 'MyDisplayName',
- ]);
- $this->config
- ->method('getUserValue')
- ->willReturnMap([
- [$this->user->getUID(), 'files', 'file_sorting', 'name', 'name'],
- [$this->user->getUID(), 'files', 'file_sorting_direction', 'asc', 'asc'],
- [$this->user->getUID(), 'files', 'show_hidden', false, false],
- [$this->user->getUID(), 'files', 'crop_image_previews', true, true],
- [$this->user->getUID(), 'files', 'show_grid', true],
- ]);
- $this->config
- ->expects($this->any())
- ->method('getAppValue')
- ->willReturnArgument(2);
- $this->shareManager->method('shareApiAllowLinks')
- ->willReturn(true);
- $nav = new Template('files', 'appnavigation');
- $nav->assign('navigationItems', [
- 'files' => [
- 'id' => 'files',
- 'appname' => 'files',
- 'script' => 'list.php',
- 'order' => 0,
- 'name' => \OC::$server->getL10N('files')->t('All files'),
- 'active' => false,
- 'icon' => '',
- 'type' => 'link',
- 'classes' => '',
- 'expanded' => false,
- 'unread' => 0,
- ],
- 'recent' => [
- 'id' => 'recent',
- 'appname' => 'files',
- 'script' => 'recentlist.php',
- 'order' => 2,
- 'name' => \OC::$server->getL10N('files')->t('Recent'),
- 'active' => false,
- 'icon' => '',
- 'type' => 'link',
- 'classes' => '',
- 'expanded' => false,
- 'unread' => 0,
- ],
- 'favorites' => [
- 'id' => 'favorites',
- 'appname' => 'files',
- 'script' => 'simplelist.php',
- 'order' => 5,
- 'name' => \OC::$server->getL10N('files')->t('Favorites'),
- 'active' => false,
- 'icon' => '',
- 'type' => 'link',
- 'classes' => 'collapsible',
- 'sublist' => [
- [
- 'id' => '-test1',
- 'dir' => '/test1',
- 'order' => 6,
- 'name' => 'test1',
- 'icon' => 'folder',
- 'params' => [
- 'view' => 'files',
- 'dir' => '/test1',
- ],
- ],
- [
- 'name' => 'test2',
- 'id' => '-test2-',
- 'dir' => '/test2/',
- 'order' => 7,
- 'icon' => 'folder',
- 'params' => [
- 'view' => 'files',
- 'dir' => '/test2/',
- ],
- ],
- [
- 'name' => 'sub4',
- 'id' => '-test3-sub4',
- 'dir' => '/test3/sub4',
- 'order' => 8,
- 'icon' => 'folder',
- 'params' => [
- 'view' => 'files',
- 'dir' => '/test3/sub4',
- ],
- ],
- [
- 'name' => 'sub6',
- 'id' => '-test5-sub6-',
- 'dir' => '/test5/sub6/',
- 'order' => 9,
- 'icon' => 'folder',
- 'params' => [
- 'view' => 'files',
- 'dir' => '/test5/sub6/',
- ],
- ],
- ],
- 'expanded' => false,
- 'unread' => 0,
- ],
- 'systemtagsfilter' => [
- 'id' => 'systemtagsfilter',
- 'appname' => 'systemtags',
- 'script' => 'list.php',
- 'order' => 25,
- 'name' => \OC::$server->getL10N('systemtags')->t('Tags'),
- 'active' => false,
- 'icon' => '',
- 'type' => 'link',
- 'classes' => '',
- 'expanded' => false,
- 'unread' => 0,
- ],
- 'trashbin' => [
- 'id' => 'trashbin',
- 'appname' => 'files_trashbin',
- 'script' => 'list.php',
- 'order' => 50,
- 'name' => \OC::$server->getL10N('files_trashbin')->t('Deleted files'),
- 'active' => false,
- 'icon' => '',
- 'type' => 'link',
- 'classes' => 'pinned',
- 'expanded' => false,
- 'unread' => 0,
- ],
- 'shareoverview' => [
- 'id' => 'shareoverview',
- 'appname' => 'files_sharing',
- 'script' => 'list.php',
- 'order' => 18,
- 'name' => \OC::$server->getL10N('files_sharing')->t('Shares'),
- 'classes' => 'collapsible',
- 'sublist' => [
- [
- 'id' => 'sharingout',
- 'appname' => 'files_sharing',
- 'script' => 'list.php',
- 'order' => 16,
- 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with others'),
- ],
- [
- 'id' => 'sharingin',
- 'appname' => 'files_sharing',
- 'script' => 'list.php',
- 'order' => 15,
- 'name' => \OC::$server->getL10N('files_sharing')->t('Shared with you'),
- ],
- [
- 'id' => 'sharinglinks',
- 'appname' => 'files_sharing',
- 'script' => 'list.php',
- 'order' => 17,
- 'name' => \OC::$server->getL10N('files_sharing')->t('Shared by link', []),
- ],
- [
- 'id' => 'deletedshares',
- 'appname' => 'files_sharing',
- 'script' => 'list.php',
- 'order' => 19,
- 'name' => \OC::$server->getL10N('files_sharing')->t('Deleted shares'),
- ],
- [
- 'id' => 'pendingshares',
- 'appname' => 'files_sharing',
- 'script' => 'list.php',
- 'order' => 19,
- 'name' => \OC::$server->getL10N('files_sharing')->t('Pending shares'),
- ],
- ],
- 'active' => false,
- 'icon' => '',
- 'type' => 'link',
- 'expanded' => false,
- 'unread' => 0,
- ]
- ]);
- $expected = new Http\TemplateResponse(
- 'files',
- 'index',
- [
- 'usedSpacePercent' => 123,
- 'owner' => 'MyName',
- 'ownerDisplayName' => 'MyDisplayName',
- 'isPublic' => false,
- 'defaultFileSorting' => 'name',
- 'defaultFileSortingDirection' => 'asc',
- 'showHiddenFiles' => 0,
- 'cropImagePreviews' => 1,
- 'fileNotFound' => 0,
- 'allowShareWithLink' => 'yes',
- 'appNavigation' => $nav,
- 'appContents' => [
- 'files' => [
- 'id' => 'files',
- 'content' => null,
- ],
- 'recent' => [
- 'id' => 'recent',
- 'content' => null,
- ],
- 'favorites' => [
- 'id' => 'favorites',
- 'content' => null,
- ],
- 'systemtagsfilter' => [
- 'id' => 'systemtagsfilter',
- 'content' => null,
- ],
- 'trashbin' => [
- 'id' => 'trashbin',
- 'content' => null,
- ],
- 'sharingout' => [
- 'id' => 'sharingout',
- 'content' => null,
- ],
- 'sharingin' => [
- 'id' => 'sharingin',
- 'content' => null,
- ],
- 'sharinglinks' => [
- 'id' => 'sharinglinks',
- 'content' => null,
- ],
- 'deletedshares' => [
- 'id' => 'deletedshares',
- 'content' => null,
- ],
- 'pendingshares' => [
- 'id' => 'pendingshares',
- 'content' => null
- ],
- 'shareoverview' => [
- 'id' => 'shareoverview',
- 'content' => null,
- ],
- '-test1' => [
- 'id' => '-test1',
- 'content' => '',
- ],
- '-test2-' => [
- 'id' => '-test2-',
- 'content' => '',
- ],
- '-test3-sub4' => [
- 'id' => '-test3-sub4',
- 'content' => '',
- ],
- '-test5-sub6-' => [
- 'id' => '-test5-sub6-',
- 'content' => '',
- ],
- ],
- 'hiddenFields' => [],
- 'showgridview' => null
- ]
- );
- $policy = new Http\ContentSecurityPolicy();
- $policy->addAllowedFrameDomain('\'self\'');
- $expected->setContentSecurityPolicy($policy);
- $this->activityHelper->method('getFavoriteFilePaths')
- ->with($this->user->getUID())
- ->willReturn([
- 'item' => [],
- 'folders' => [
- '/test1',
- '/test2/',
- '/test3/sub4',
- '/test5/sub6/',
- ],
- ]);
- $this->assertEquals($expected, $this->viewController->index('MyDir', 'MyView'));
- }
- public function testShowFileRouteWithFolder() {
- $node = $this->getMockBuilder(Folder::class)->getMock();
- $node->expects($this->once())
- ->method('getPath')
- ->willReturn('/testuser1/files/test/sub');
- $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
- $this->rootFolder->expects($this->once())
- ->method('getUserFolder')
- ->with('testuser1')
- ->willReturn($baseFolder);
- $baseFolder->expects($this->once())
- ->method('getById')
- ->with(123)
- ->willReturn([$node]);
- $baseFolder->expects($this->once())
- ->method('getRelativePath')
- ->with('/testuser1/files/test/sub')
- ->willReturn('/test/sub');
- $this->urlGenerator
- ->expects($this->once())
- ->method('linkToRoute')
- ->with('files.view.index', ['dir' => '/test/sub'])
- ->willReturn('/apps/files/?dir=/test/sub');
- $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub');
- $this->assertEquals($expected, $this->viewController->index('', '', '123'));
- }
- public function testShowFileRouteWithFile() {
- $parentNode = $this->getMockBuilder(Folder::class)->getMock();
- $parentNode->expects($this->once())
- ->method('getPath')
- ->willReturn('testuser1/files/test');
- $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
- $this->rootFolder->expects($this->once())
- ->method('getUserFolder')
- ->with('testuser1')
- ->willReturn($baseFolder);
- $node = $this->getMockBuilder(File::class)->getMock();
- $node->expects($this->once())
- ->method('getParent')
- ->willReturn($parentNode);
- $node->expects($this->once())
- ->method('getName')
- ->willReturn('somefile.txt');
- $baseFolder->expects($this->once())
- ->method('getById')
- ->with(123)
- ->willReturn([$node]);
- $baseFolder->expects($this->once())
- ->method('getRelativePath')
- ->with('testuser1/files/test')
- ->willReturn('/test');
- $this->urlGenerator
- ->expects($this->once())
- ->method('linkToRoute')
- ->with('files.view.index', ['dir' => '/test', 'scrollto' => 'somefile.txt'])
- ->willReturn('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
- $expected = new Http\RedirectResponse('/apps/files/?dir=/test/sub&scrollto=somefile.txt');
- $this->assertEquals($expected, $this->viewController->index('', '', '123'));
- }
- public function testShowFileRouteWithInvalidFileId() {
- $baseFolder = $this->getMockBuilder(Folder::class)->getMock();
- $this->rootFolder->expects($this->once())
- ->method('getUserFolder')
- ->with('testuser1')
- ->willReturn($baseFolder);
- $baseFolder->expects($this->once())
- ->method('getById')
- ->with(123)
- ->willReturn([]);
- $this->urlGenerator->expects($this->once())
- ->method('linkToRoute')
- ->with('files.view.index', ['fileNotFound' => true])
- ->willReturn('redirect.url');
- $response = $this->viewController->index('', 'MyView', '123');
- $this->assertInstanceOf('OCP\AppFramework\Http\RedirectResponse', $response);
- $this->assertEquals('redirect.url', $response->getRedirectURL());
- }
- public function testShowFileRouteWithTrashedFile() {
- $this->appManager->expects($this->once())
- ->method('isEnabledForUser')
- ->with('files_trashbin')
- ->willReturn(true);
- $parentNode = $this->getMockBuilder(Folder::class)->getMock();
- $parentNode->expects($this->once())
- ->method('getPath')
- ->willReturn('testuser1/files_trashbin/files/test.d1462861890/sub');
- $baseFolderFiles = $this->getMockBuilder(Folder::class)->getMock();
- $baseFolderTrash = $this->getMockBuilder(Folder::class)->getMock();
- $this->rootFolder->expects($this->once())
- ->method('getUserFolder')
- ->with('testuser1')
- ->willReturn($baseFolderFiles);
- $this->rootFolder->expects($this->once())
- ->method('get')
- ->with('testuser1/files_trashbin/files/')
- ->willReturn($baseFolderTrash);
- $baseFolderFiles->expects($this->once())
- ->method('getById')
- ->with(123)
- ->willReturn([]);
- $node = $this->getMockBuilder(File::class)->getMock();
- $node->expects($this->once())
- ->method('getParent')
- ->willReturn($parentNode);
- $node->expects($this->once())
- ->method('getName')
- ->willReturn('somefile.txt');
- $baseFolderTrash->expects($this->once())
- ->method('getById')
- ->with(123)
- ->willReturn([$node]);
- $baseFolderTrash->expects($this->once())
- ->method('getRelativePath')
- ->with('testuser1/files_trashbin/files/test.d1462861890/sub')
- ->willReturn('/test.d1462861890/sub');
- $this->urlGenerator
- ->expects($this->once())
- ->method('linkToRoute')
- ->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt'])
- ->willReturn('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
- $expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
- $this->assertEquals($expected, $this->viewController->index('', '', '123'));
- }
- }
|