From 46e924aa7b1231d71c6b1db287c44cdded1b47b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Sat, 18 Feb 2023 15:43:39 +0100 Subject: [PATCH 1/2] fix: Use proper path when creating node instances MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- apps/dav/lib/Connector/Sabre/Node.php | 11 ++++++---- .../unit/Connector/Sabre/DirectoryTest.php | 20 +++++++++++++++++-- lib/private/Files/Node/Node.php | 6 +++++- tests/lib/Files/Node/FolderTest.php | 2 ++ 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/apps/dav/lib/Connector/Sabre/Node.php b/apps/dav/lib/Connector/Sabre/Node.php index 1e32e74c325b6..2b5661eec633a 100644 --- a/apps/dav/lib/Connector/Sabre/Node.php +++ b/apps/dav/lib/Connector/Sabre/Node.php @@ -100,11 +100,13 @@ public function __construct(View $view, FileInfo $info, IManager $shareManager = if ($info instanceof Folder || $info instanceof File) { $this->node = $info; } else { + // The Node API assumes that the view passed doesn't have a fake root + $rootView = \OC::$server->get(View::class); $root = \OC::$server->get(IRootFolder::class); if ($info->getType() === FileInfo::TYPE_FOLDER) { - $this->node = new Folder($root, $view, $this->path, $info); + $this->node = new Folder($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info); } else { - $this->node = new File($root, $view, $this->path, $info); + $this->node = new File($root, $rootView, $this->fileView->getAbsolutePath($this->path), $info); } } } @@ -112,10 +114,11 @@ public function __construct(View $view, FileInfo $info, IManager $shareManager = protected function refreshInfo() { $this->info = $this->fileView->getFileInfo($this->path); $root = \OC::$server->get(IRootFolder::class); + $rootView = \OC::$server->get(View::class); if ($this->info->getType() === FileInfo::TYPE_FOLDER) { - $this->node = new Folder($root, $this->fileView, $this->path, $this->info); + $this->node = new Folder($root, $rootView, $this->path, $this->info); } else { - $this->node = new File($root, $this->fileView, $this->path, $this->info); + $this->node = new File($root, $rootView, $this->path, $this->info); } } diff --git a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php index 1de82484ac470..2e3ece283a913 100644 --- a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php @@ -29,9 +29,12 @@ namespace OCA\DAV\Tests\Unit\Connector\Sabre; use OC\Files\FileInfo; +use OC\Files\Filesystem; use OC\Files\Node\Node; use OC\Files\Storage\Wrapper\Quota; +use OC\Files\View; use OCA\DAV\Connector\Sabre\Directory; +use OCP\Constants; use OCP\Files\ForbiddenException; use OCP\Files\Mount\IMountPoint; use Test\Traits\UserTrait; @@ -92,6 +95,10 @@ protected function setUp(): void { ->willReturn(Node::TYPE_FOLDER); $this->info->method('getName') ->willReturn("folder"); + $this->info->method('getPath') + ->willReturn("/admin/files/folder"); + $this->info->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); } private function getDir($path = '/') { @@ -208,12 +215,21 @@ public function testGetChildren() { $this->view->expects($this->once()) ->method('getDirectoryContent') - ->with('') ->willReturn([$info1, $info2]); $this->view->expects($this->any()) ->method('getRelativePath') - ->willReturn(''); + ->willReturnCallback(function($path) { + return str_replace('/admin/files/', '', $path); + }); + + $this->view->expects($this->any()) + ->method('getAbsolutePath') + ->willReturnCallback(function($path) { + return Filesystem::normalizePath('/admin/files' . $path); + }); + + $this->overwriteService(View::class, $this->view); $dir = new Directory($this->view, $this->info); $nodes = $dir->getChildren(); diff --git a/lib/private/Files/Node/Node.php b/lib/private/Files/Node/Node.php index bfa4d6861ea03..161b3e2dd3022 100644 --- a/lib/private/Files/Node/Node.php +++ b/lib/private/Files/Node/Node.php @@ -37,6 +37,7 @@ use OCP\Files\NotFoundException; use OCP\Files\NotPermittedException; use OCP\Lock\LockedException; +use OCP\PreConditionNotMetException; use Symfony\Component\EventDispatcher\GenericEvent; // FIXME: this class really should be abstract @@ -52,7 +53,7 @@ class Node implements \OCP\Files\Node { protected $root; /** - * @var string $path + * @var string $path Absolute path to the node (e.g. /admin/files/folder/file) */ protected $path; @@ -73,6 +74,9 @@ class Node implements \OCP\Files\Node { * @param FileInfo $fileInfo */ public function __construct($root, $view, $path, $fileInfo = null, ?Node $parent = null) { + if (Filesystem::normalizePath($view->getRoot()) !== '/') { + throw new PreConditionNotMetException('The view passed to the node should not have any fake root set'); + } $this->view = $view; $this->root = $root; $this->path = $path; diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index 8a604af384695..f8573f0214df5 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -81,6 +81,8 @@ public function testGetDirectoryContent() { ]); $view->method('getFileInfo') ->willReturn($this->createMock(FileInfo::class)); + $view->method('getRelativePath') + ->willReturn('/bar/foo'); $node = new Folder($root, $view, '/bar/foo'); $children = $node->getDirectoryListing(); From 2e09f075ee61d3eca3c1e836cba6f6d5130f7aa9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Mon, 20 Feb 2023 22:12:00 +0100 Subject: [PATCH 2/2] tests: Adapt node related unit tests mocks to required root view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- .../unit/Connector/Sabre/DirectoryTest.php | 4 +- .../unit/Files/FileSearchBackendTest.php | 8 +- tests/lib/Files/Node/FileTest.php | 4 +- tests/lib/Files/Node/FolderTest.php | 131 +++++------------- tests/lib/Files/Node/NodeTest.php | 14 ++ tests/lib/Files/Node/RootTest.php | 43 +++--- 6 files changed, 73 insertions(+), 131 deletions(-) diff --git a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php index 2e3ece283a913..1fb965dbd4476 100644 --- a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php @@ -219,13 +219,13 @@ public function testGetChildren() { $this->view->expects($this->any()) ->method('getRelativePath') - ->willReturnCallback(function($path) { + ->willReturnCallback(function ($path) { return str_replace('/admin/files/', '', $path); }); $this->view->expects($this->any()) ->method('getAbsolutePath') - ->willReturnCallback(function($path) { + ->willReturnCallback(function ($path) { return Filesystem::normalizePath('/admin/files' . $path); }); diff --git a/apps/dav/tests/unit/Files/FileSearchBackendTest.php b/apps/dav/tests/unit/Files/FileSearchBackendTest.php index b4245ac181b1a..b131fb0fa9311 100644 --- a/apps/dav/tests/unit/Files/FileSearchBackendTest.php +++ b/apps/dav/tests/unit/Files/FileSearchBackendTest.php @@ -86,9 +86,11 @@ protected function setUp(): void { ->disableOriginalConstructor() ->getMock(); - $this->view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); + $this->view = $this->createMock(View::class); + + $this->view->expects($this->any()) + ->method('getRoot') + ->willReturn(''); $this->view->expects($this->any()) ->method('getRelativePath') diff --git a/tests/lib/Files/Node/FileTest.php b/tests/lib/Files/Node/FileTest.php index 3305f9ac17065..218e253172746 100644 --- a/tests/lib/Files/Node/FileTest.php +++ b/tests/lib/Files/Node/FileTest.php @@ -185,7 +185,7 @@ public function testFOpenWrite() { $root = new \OC\Files\Node\Root( $this->manager, - new $this->view, + $this->view, $this->user, $this->userMountCache, $this->logger, @@ -277,7 +277,7 @@ public function testFOpenReadWriteNoWritePermissions() { $root = new \OC\Files\Node\Root( $this->manager, - new $this->view, + $this->view, $this->user, $this->userMountCache, $this->logger, diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index f8573f0214df5..d745a05ba17f4 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -23,7 +23,6 @@ use OC\Files\Search\SearchQuery; use OC\Files\Storage\Temporary; use OC\Files\Storage\Wrapper\Jail; -use OC\Files\View; use OCP\Files\Cache\ICacheEntry; use OCP\Files\Mount\IMountPoint; use OCP\Files\NotFoundException; @@ -40,6 +39,9 @@ */ class FolderTest extends NodeTest { protected function createTestNode($root, $view, $path, array $data = [], $internalPath = '', $storage = null) { + $view->expects($this->any()) + ->method('getRoot') + ->willReturn(''); if ($data || $internalPath || $storage) { return new Folder($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage)); } else { @@ -64,27 +66,26 @@ public function testGetDirectoryContent() { /** * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view */ - $view = $this->createMock(View::class); $root = $this->getMockBuilder(Root::class) - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + ->setConstructorArgs([$manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); $root->expects($this->any()) ->method('getUser') ->willReturn($this->user); - $view->expects($this->any()) + $this->view->expects($this->any()) ->method('getDirectoryContent') ->with('/bar/foo') ->willReturn([ new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null), new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null), ]); - $view->method('getFileInfo') + $this->view->method('getFileInfo') ->willReturn($this->createMock(FileInfo::class)); - $view->method('getRelativePath') + $this->view->method('getRelativePath') ->willReturn('/bar/foo'); - $node = new Folder($root, $view, '/bar/foo'); + $node = new Folder($root, $this->view, '/bar/foo'); $children = $node->getDirectoryListing(); $this->assertEquals(2, count($children)); $this->assertInstanceOf('\OC\Files\Node\File', $children[0]); @@ -97,10 +98,7 @@ public function testGetDirectoryContent() { public function testGet() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -119,10 +117,7 @@ public function testGet() { public function testNodeExists() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -142,10 +137,7 @@ public function testNodeExists() { public function testNodeExistsNotExists() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -163,10 +155,7 @@ public function testNodeExistsNotExists() { public function testNewFolder() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -190,10 +179,7 @@ public function testNewFolder() { public function testNewFolderDeepParent() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -220,10 +206,7 @@ public function testNewFolderNotPermitted() { $this->expectException(\OCP\Files\NotPermittedException::class); $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -240,10 +223,7 @@ public function testNewFolderNotPermitted() { public function testNewFile() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -270,10 +250,7 @@ public function testNewFileNotPermitted() { $this->expectException(\OCP\Files\NotPermittedException::class); $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -290,10 +267,7 @@ public function testNewFileNotPermitted() { public function testGetFreeSpace() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -310,10 +284,7 @@ public function testGetFreeSpace() { public function testSearch() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -353,10 +324,7 @@ public function testSearch() { public function testSearchInRoot() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setMethods(['getUser', 'getMountsIn', 'getMount']) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) @@ -397,10 +365,7 @@ public function testSearchInRoot() { public function testSearchInStorageRoot() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -440,10 +405,7 @@ public function testSearchInStorageRoot() { public function testSearchSubStorages() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); @@ -500,21 +462,18 @@ public function testSearchSubStorages() { } public function testIsSubNode() { - $file = new Node(null, null, '/foo/bar'); - $folder = new Folder(null, null, '/foo'); + $file = new Node(null, $this->view, '/foo/bar'); + $folder = new Folder(null, $this->view, '/foo'); $this->assertTrue($folder->isSubNode($file)); $this->assertFalse($folder->isSubNode($folder)); - $file = new Node(null, null, '/foobar'); + $file = new Node(null, $this->view, '/foobar'); $this->assertFalse($folder->isSubNode($file)); } public function testGetById() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setMethods(['getMountsIn', 'getMount']) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) @@ -561,10 +520,7 @@ public function testGetById() { public function testGetByIdMountRoot() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setMethods(['getMountsIn', 'getMount']) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) @@ -607,10 +563,7 @@ public function testGetByIdMountRoot() { public function testGetByIdOutsideFolder() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setMethods(['getMountsIn', 'getMount']) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) @@ -652,10 +605,7 @@ public function testGetByIdOutsideFolder() { public function testGetByIdMultipleStorages() { $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setMethods(['getMountsIn', 'getMount']) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) @@ -717,10 +667,7 @@ public function uniqueNameProvider() { public function testGetUniqueName($name, $existingFiles, $expected) { $manager = $this->createMock(Manager::class); $folderPath = '/bar/foo'; - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setMethods(['getUser', 'getMountsIn', 'getMount']) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) @@ -744,10 +691,7 @@ public function testGetUniqueName($name, $existingFiles, $expected) { public function testRecent(): void { $manager = $this->createMock(Manager::class); $folderPath = '/bar/foo'; - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ $root = $this->getMockBuilder(Root::class) ->setMethods(['getUser', 'getMountsIn', 'getMount']) @@ -812,10 +756,7 @@ public function testRecent(): void { public function testRecentFolder() { $manager = $this->createMock(Manager::class); $folderPath = '/bar/foo'; - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ $root = $this->getMockBuilder(Root::class) ->setMethods(['getUser', 'getMountsIn', 'getMount']) @@ -879,10 +820,7 @@ public function testRecentFolder() { public function testRecentJail() { $manager = $this->createMock(Manager::class); $folderPath = '/bar/foo'; - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\Node\Root $root */ $root = $this->getMockBuilder(Root::class) ->setMethods(['getUser', 'getMountsIn', 'getMount']) @@ -968,10 +906,7 @@ public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array } $manager = $this->createMock(Manager::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->createMock(View::class); + $view = $this->getRootViewMock(); $root = $this->getMockBuilder(Root::class) ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) ->getMock(); diff --git a/tests/lib/Files/Node/NodeTest.php b/tests/lib/Files/Node/NodeTest.php index 8c0d4cdb293d2..b63d287a1913f 100644 --- a/tests/lib/Files/Node/NodeTest.php +++ b/tests/lib/Files/Node/NodeTest.php @@ -54,6 +54,9 @@ protected function setUp(): void { $this->view = $this->getMockBuilder(View::class) ->disableOriginalConstructor() ->getMock(); + $this->view->expects($this->any()) + ->method('getRoot') + ->willReturn(''); $this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache') ->disableOriginalConstructor() ->getMock(); @@ -65,6 +68,17 @@ protected function setUp(): void { ->getMock(); } + /** + * @return \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view + */ + protected function getRootViewMock() { + $view = $this->createMock(View::class); + $view->expects($this->any()) + ->method('getRoot') + ->willReturn(''); + return $view; + } + /** * @param IRootFolder $root * @param View $view diff --git a/tests/lib/Files/Node/RootTest.php b/tests/lib/Files/Node/RootTest.php index 5d8e2a4ac62e3..aab658c3c36c6 100644 --- a/tests/lib/Files/Node/RootTest.php +++ b/tests/lib/Files/Node/RootTest.php @@ -52,6 +52,17 @@ protected function setUp(): void { $this->eventDispatcher = $this->createMock(IEventDispatcher::class); } + /** + * @return \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view + */ + protected function getRootViewMock() { + $view = $this->createMock(View::class); + $view->expects($this->any()) + ->method('getRoot') + ->willReturn(''); + return $view; + } + protected function getFileInfo($data) { return new FileInfo('', null, '', $data, null); } @@ -63,12 +74,7 @@ public function testGet() { $storage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor() ->getMock(); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); + $view = $this->getRootViewMock(); $root = new \OC\Files\Node\Root( $this->manager, $view, @@ -100,12 +106,7 @@ public function testGetNotFound() { $storage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor() ->getMock(); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); + $view = $this->getRootViewMock(); $root = new \OC\Files\Node\Root( $this->manager, $view, @@ -129,12 +130,7 @@ public function testGetNotFound() { public function testGetInvalidPath() { $this->expectException(\OCP\Files\NotPermittedException::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); + $view = $this->getRootViewMock(); $root = new \OC\Files\Node\Root( $this->manager, $view, @@ -152,12 +148,7 @@ public function testGetInvalidPath() { public function testGetNoStorages() { $this->expectException(\OCP\Files\NotFoundException::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); + $view = $this->getRootViewMock(); $root = new \OC\Files\Node\Root( $this->manager, $view, @@ -174,7 +165,7 @@ public function testGetNoStorages() { public function testGetUserFolder() { $root = new \OC\Files\Node\Root( $this->manager, - $this->createMock(View::class), + $this->getRootViewMock(), $this->user, $this->userMountCache, $this->logger, @@ -215,7 +206,7 @@ public function testGetUserFolderWithNoUserObj() { $root = new \OC\Files\Node\Root( $this->createMock(Manager::class), - $this->createMock(View::class), + $this->getRootViewMock(), null, $this->userMountCache, $this->logger,