diff options
Diffstat (limited to 'tests/lib/Files/Node')
-rw-r--r-- | tests/lib/Files/Node/FileTest.php | 144 | ||||
-rw-r--r-- | tests/lib/Files/Node/FolderTest.php | 429 | ||||
-rw-r--r-- | tests/lib/Files/Node/HookConnectorTest.php | 114 | ||||
-rw-r--r-- | tests/lib/Files/Node/IntegrationTest.php | 34 | ||||
-rw-r--r-- | tests/lib/Files/Node/NodeTestCase.php (renamed from tests/lib/Files/Node/NodeTest.php) | 227 | ||||
-rw-r--r-- | tests/lib/Files/Node/RootTest.php | 127 |
6 files changed, 587 insertions, 488 deletions
diff --git a/tests/lib/Files/Node/FileTest.php b/tests/lib/Files/Node/FileTest.php index 3305f9ac170..eec34d156ad 100644 --- a/tests/lib/Files/Node/FileTest.php +++ b/tests/lib/Files/Node/FileTest.php @@ -1,13 +1,18 @@ <?php + /** - * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Files\Node; +use OC\Files\Node\File; +use OC\Files\Node\Root; +use OCP\Constants; +use OCP\Files\NotPermittedException; + /** * Class FileTest * @@ -15,12 +20,12 @@ namespace Test\Files\Node; * * @package Test\Files\Node */ -class FileTest extends NodeTest { +class FileTest extends NodeTestCase { protected function createTestNode($root, $view, $path, array $data = [], $internalPath = '', $storage = null) { if ($data || $internalPath || $storage) { - return new \OC\Files\Node\File($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage)); + return new File($root, $view, $path, $this->getFileInfo($data, $internalPath, $storage)); } else { - return new \OC\Files\Node\File($root, $view, $path); + return new File($root, $view, $path); } } @@ -36,13 +41,13 @@ class FileTest extends NodeTest { return 'unlink'; } - public function testGetContent() { + public function testGetContent(): void { /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ - $root = $this->getMockBuilder('\OC\Files\Node\Root') - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + $root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - $hook = function ($file) { + $hook = function ($file): void { throw new \Exception('Hooks are not supposed to be called'); }; @@ -57,19 +62,19 @@ class FileTest extends NodeTest { $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $this->assertEquals('bar', $node->getContent()); } - public function testGetContentNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testGetContentNotPermitted(): void { + $this->expectException(NotPermittedException::class); /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ - $root = $this->getMockBuilder('\OC\Files\Node\Root') - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + $root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) @@ -81,14 +86,14 @@ class FileTest extends NodeTest { ->with('/bar/foo') ->willReturn($this->getFileInfo(['permissions' => 0])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $node->getContent(); } - public function testPutContent() { + public function testPutContent(): void { /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ - $root = $this->getMockBuilder('\OC\Files\Node\Root') - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + $root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) @@ -98,39 +103,39 @@ class FileTest extends NodeTest { $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); $this->view->expects($this->once()) ->method('file_put_contents') ->with('/bar/foo', 'bar') ->willReturn(true); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $node->putContent('bar'); } - public function testPutContentNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testPutContentNotPermitted(): void { + $this->expectException(NotPermittedException::class); /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ - $root = $this->getMockBuilder('\OC\Files\Node\Root') - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + $root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $node->putContent('bar'); } - public function testGetMimeType() { + public function testGetMimeType(): void { /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */ - $root = $this->getMockBuilder('\OC\Files\Node\Root') - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + $root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $this->view->expects($this->once()) @@ -138,26 +143,27 @@ class FileTest extends NodeTest { ->with('/bar/foo') ->willReturn($this->getFileInfo(['mimetype' => 'text/plain'])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $this->assertEquals('text/plain', $node->getMimeType()); } - public function testFOpenRead() { + public function testFOpenRead(): void { $stream = fopen('php://memory', 'w+'); fwrite($stream, 'bar'); rewind($stream); - $root = new \OC\Files\Node\Root( + $root = new Root( $this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); - $hook = function ($file) { + $hook = function ($file): void { throw new \Exception('Hooks are not supposed to be called'); }; @@ -172,28 +178,29 @@ class FileTest extends NodeTest { $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $fh = $node->fopen('r'); $this->assertEquals($stream, $fh); $this->assertEquals('bar', fread($fh, 3)); } - public function testFOpenWrite() { + public function testFOpenWrite(): void { $stream = fopen('php://memory', 'w+'); - $root = new \OC\Files\Node\Root( + $root = new Root( $this->manager, - new $this->view, + $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $hooksCalled = 0; - $hook = function ($file) use (&$hooksCalled) { + $hook = function ($file) use (&$hooksCalled): void { $hooksCalled++; }; @@ -208,9 +215,9 @@ class FileTest extends NodeTest { $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $fh = $node->fopen('w'); $this->assertEquals($stream, $fh); fwrite($fh, 'bar'); @@ -220,19 +227,20 @@ class FileTest extends NodeTest { } - public function testFOpenReadNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testFOpenReadNotPermitted(): void { + $this->expectException(NotPermittedException::class); - $root = new \OC\Files\Node\Root( + $root = new Root( $this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); - $hook = function ($file) { + $hook = function ($file): void { throw new \Exception('Hooks are not supposed to be called'); }; @@ -241,59 +249,61 @@ class FileTest extends NodeTest { ->with('/bar/foo') ->willReturn($this->getFileInfo(['permissions' => 0])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $node->fopen('r'); } - public function testFOpenReadWriteNoReadPermissions() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testFOpenReadWriteNoReadPermissions(): void { + $this->expectException(NotPermittedException::class); - $root = new \OC\Files\Node\Root( + $root = new Root( $this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); - $hook = function () { + $hook = function (): void { throw new \Exception('Hooks are not supposed to be called'); }; $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_UPDATE])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_UPDATE])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $node->fopen('w'); } - public function testFOpenReadWriteNoWritePermissions() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testFOpenReadWriteNoWritePermissions(): void { + $this->expectException(NotPermittedException::class); - $root = new \OC\Files\Node\Root( + $root = new Root( $this->manager, - new $this->view, + $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); - $hook = function () { + $hook = function (): void { throw new \Exception('Hooks are not supposed to be called'); }; $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); - $node = new \OC\Files\Node\File($root, $this->view, '/bar/foo'); + $node = new File($root, $this->view, '/bar/foo'); $node->fopen('w'); } } diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php index 4cda92b6e83..439535cf2c1 100644 --- a/tests/lib/Files/Node/FolderTest.php +++ b/tests/lib/Files/Node/FolderTest.php @@ -1,9 +1,9 @@ <?php + /** - * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Files\Node; @@ -14,21 +14,30 @@ use OC\Files\Config\CachedMountInfo; use OC\Files\FileInfo; use OC\Files\Mount\Manager; use OC\Files\Mount\MountPoint; +use OC\Files\Node\File; use OC\Files\Node\Folder; use OC\Files\Node\Node; use OC\Files\Node\Root; +use OC\Files\Search\SearchBinaryOperator; use OC\Files\Search\SearchComparison; use OC\Files\Search\SearchOrder; use OC\Files\Search\SearchQuery; +use OC\Files\Storage\Storage; use OC\Files\Storage\Temporary; use OC\Files\Storage\Wrapper\Jail; use OC\Files\View; +use OCP\Constants; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\InvalidPathException; +use OCP\Files\IRootFolder; use OCP\Files\Mount\IMountPoint; use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\Files\Search\ISearchBinaryOperator; use OCP\Files\Search\ISearchComparison; use OCP\Files\Search\ISearchOrder; -use OCP\Files\Storage; +use OCP\Files\Storage\IStorage; +use PHPUnit\Framework\MockObject\MockObject; /** * Class FolderTest @@ -37,8 +46,11 @@ use OCP\Files\Storage; * * @package Test\Files\Node */ -class FolderTest extends NodeTest { +class FolderTest extends NodeTestCase { 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 { @@ -58,30 +70,31 @@ class FolderTest extends NodeTest { return 'rmdir'; } - public function testGetDirectoryContent() { + public function testGetDirectoryContent(): void { $manager = $this->createMock(Manager::class); /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view + * @var 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, $this->cacheFactory]) ->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)); + $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]); @@ -92,34 +105,30 @@ class FolderTest extends NodeTest { $this->assertEquals(3, $children[1]->getId()); } - public function testGet() { + public function testGet(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') ->willReturn($this->user); + $node = new File($root, $view, '/bar/foo/asd'); $root->method('get') - ->with('/bar/foo/asd'); + ->with('/bar/foo/asd') + ->willReturn($node); - $node = new Folder($root, $view, '/bar/foo'); - $node->get('asd'); + $parentNode = new Folder($root, $view, '/bar/foo'); + self::assertEquals($node, $parentNode->get('asd')); } - public function testNodeExists() { + public function testNodeExists(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') @@ -135,14 +144,11 @@ class FolderTest extends NodeTest { $this->assertTrue($node->nodeExists('asd')); } - public function testNodeExistsNotExists() { + public function testNodeExistsNotExists(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') @@ -150,20 +156,17 @@ class FolderTest extends NodeTest { $root->method('get') ->with('/bar/foo/asd') - ->will($this->throwException(new NotFoundException())); + ->willThrowException(new NotFoundException()); $node = new Folder($root, $view, '/bar/foo'); $this->assertFalse($node->nodeExists('asd')); } - public function testNewFolder() { + public function testNewFolder(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') @@ -171,49 +174,67 @@ class FolderTest extends NodeTest { $view->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); $view->method('mkdir') ->with('/bar/foo/asd') ->willReturn(true); $node = new Folder($root, $view, '/bar/foo'); - $child = new Folder($root, $view, '/bar/foo/asd'); + $child = new Folder($root, $view, '/bar/foo/asd', null, $node); $result = $node->newFolder('asd'); $this->assertEquals($child, $result); } + public function testNewFolderDeepParent(): void { + $manager = $this->createMock(Manager::class); + $view = $this->getRootViewMock(); + $root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) + ->getMock(); + $root->expects($this->any()) + ->method('getUser') + ->willReturn($this->user); + + $view->method('getFileInfo') + ->with('/foobar') + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); - public function testNewFolderNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + $view->method('mkdir') + ->with('/foobar/asd/sdf') + ->willReturn(true); + + $node = new Folder($root, $view, '/foobar'); + $child = new Folder($root, $view, '/foobar/asd/sdf', null, null); + $result = $node->newFolder('asd/sdf'); + $this->assertEquals($child, $result); + } + + + public function testNewFolderNotPermitted(): void { + $this->expectException(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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->method('getUser') ->willReturn($this->user); $view->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); $node = new Folder($root, $view, '/bar/foo'); $node->newFolder('asd'); } - public function testNewFile() { + public function testNewFile(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') @@ -221,49 +242,43 @@ class FolderTest extends NodeTest { $view->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); $view->method('touch') ->with('/bar/foo/asd') ->willReturn(true); $node = new Folder($root, $view, '/bar/foo'); - $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd'); + $child = new File($root, $view, '/bar/foo/asd', null, $node); $result = $node->newFile('asd'); $this->assertEquals($child, $result); } - public function testNewFileNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testNewFileNotPermitted(): void { + $this->expectException(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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->method('getUser') ->willReturn($this->user); $view->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); $node = new Folder($root, $view, '/bar/foo'); $node->newFile('asd'); } - public function testGetFreeSpace() { + public function testGetFreeSpace(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->method('getUser') ->willReturn($this->user); @@ -276,31 +291,36 @@ class FolderTest extends NodeTest { $this->assertEquals(100, $node->getFreeSpace()); } - public function testSearch() { + public function testSearch(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->method('getUser') ->willReturn($this->user); - /** @var Storage\IStorage $storage */ - $storage = $this->createMock(Storage\IStorage::class); + /** @var Storage\IStorage&MockObject $storage */ + $storage = $this->createMock(IStorage::class); $storage->method('getId')->willReturn('test::1'); $cache = new Cache($storage); $storage->method('getCache') ->willReturn($cache); + $storage->expects($this->atLeastOnce()) + ->method('getOwner') + ->with('qwerty') + ->willReturn(false); + $mount = $this->createMock(IMountPoint::class); - $mount->method('getStorage') + $mount->expects($this->atLeastOnce()) + ->method('getStorage') ->willReturn($storage); - $mount->method('getInternalPath') + $mount->expects($this->atLeastOnce()) + ->method('getInternalPath') ->willReturn('foo'); + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); @@ -319,21 +339,18 @@ class FolderTest extends NodeTest { $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); } - public function testSearchInRoot() { + public function testSearchInRoot(): void { $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]) + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') ->willReturn($this->user); /** @var \PHPUnit\Framework\MockObject\MockObject|Storage $storage */ - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $storage->method('getId')->willReturn('test::2'); $cache = new Cache($storage); @@ -345,7 +362,10 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('files', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('files/foo', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); @@ -363,18 +383,15 @@ class FolderTest extends NodeTest { $this->assertEquals('/foo', $result[0]->getPath()); } - public function testSearchInStorageRoot() { + public function testSearchInStorageRoot(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->method('getUser') ->willReturn($this->user); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $storage->method('getId')->willReturn('test::1'); $cache = new Cache($storage); @@ -386,7 +403,10 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); @@ -406,22 +426,19 @@ class FolderTest extends NodeTest { $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); } - public function testSearchSubStorages() { + public function testSearchSubStorages(): void { $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') ->willReturn($this->user); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $storage->method('getId')->willReturn('test::1'); $cache = new Cache($storage); - $subStorage = $this->createMock(Storage::class); + $subStorage = $this->createMock(IStorage::class); $subStorage->method('getId')->willReturn('test::2'); $subCache = new Cache($subStorage); $subMount = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); @@ -440,13 +457,19 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); $subStorage->method('getCache') ->willReturn($subCache); + $subStorage->method('getOwner') + ->willReturn('owner'); + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('foo', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('foo/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); + $subCache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $subCache->insert('asd', ['size' => 200, 'mtime' => 55, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $subCache->insert('asd/qwerty', ['size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain']); @@ -467,27 +490,25 @@ class FolderTest extends NodeTest { $this->assertEquals(2, count($result)); } - public function testIsSubNode() { - $file = new Node(null, null, '/foo/bar'); - $folder = new Folder(null, null, '/foo'); + public function testIsSubNode(): void { + $rootFolderMock = $this->createMock(IRootFolder::class); + $file = new Node($rootFolderMock, $this->view, '/foo/bar'); + $folder = new Folder($rootFolderMock, $this->view, '/foo'); $this->assertTrue($folder->isSubNode($file)); $this->assertFalse($folder->isSubNode($folder)); - $file = new Node(null, null, '/foobar'); + $file = new Node($rootFolderMock, $this->view, '/foobar'); $this->assertFalse($folder->isSubNode($file)); } - public function testGetById() { + public function testGetById(): void { $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]) + ->onlyMethods(['getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - $storage = $this->createMock(\OC\Files\Storage\Storage::class); + $storage = $this->createMock(Storage::class); $mount = new MountPoint($storage, '/bar'); $storage->method('getId')->willReturn(''); $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); @@ -496,6 +517,8 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); $this->userMountCache->expects($this->any()) ->method('getMountsForFileId') @@ -527,17 +550,14 @@ class FolderTest extends NodeTest { $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); } - public function testGetByIdMountRoot() { + public function testGetByIdMountRoot(): void { $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]) + ->onlyMethods(['getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - $storage = $this->createMock(\OC\Files\Storage\Storage::class); + $storage = $this->createMock(Storage::class); $mount = new MountPoint($storage, '/bar'); $storage->method('getId')->willReturn(''); $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); @@ -546,6 +566,8 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); $this->userMountCache->expects($this->any()) ->method('getMountsForFileId') @@ -573,17 +595,14 @@ class FolderTest extends NodeTest { $this->assertEquals('/bar', $result[0]->getPath()); } - public function testGetByIdOutsideFolder() { + public function testGetByIdOutsideFolder(): void { $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]) + ->onlyMethods(['getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - $storage = $this->createMock(\OC\Files\Storage\Storage::class); + $storage = $this->createMock(Storage::class); $mount = new MountPoint($storage, '/bar'); $storage->method('getId')->willReturn(''); $cache = $this->getMockBuilder(Cache::class)->setConstructorArgs([$storage])->getMock(); @@ -592,6 +611,8 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); $this->userMountCache->expects($this->any()) ->method('getMountsForFileId') @@ -618,17 +639,14 @@ class FolderTest extends NodeTest { $this->assertEquals(0, count($result)); } - public function testGetByIdMultipleStorages() { + public function testGetByIdMultipleStorages(): void { $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]) + ->onlyMethods(['getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - $storage = $this->createMock(\OC\Files\Storage\Storage::class); + $storage = $this->createMock(Storage::class); $mount1 = new MountPoint($storage, '/bar'); $mount2 = new MountPoint($storage, '/bar/foo/asd'); $storage->method('getId')->willReturn(''); @@ -638,6 +656,8 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); $this->userMountCache->method('getMountsForFileId') ->with(1) @@ -653,9 +673,6 @@ class FolderTest extends NodeTest { ), ]); - $storage->method('getCache') - ->willReturn($cache); - $cache->method('get') ->with(1) ->willReturn($fileInfo); @@ -670,7 +687,7 @@ class FolderTest extends NodeTest { $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath()); } - public function uniqueNameProvider() { + public static function uniqueNameProvider(): array { return [ // input, existing, expected ['foo', [], 'foo'], @@ -679,19 +696,14 @@ class FolderTest extends NodeTest { ]; } - /** - * @dataProvider uniqueNameProvider - */ - public function testGetUniqueName($name, $existingFiles, $expected) { + #[\PHPUnit\Framework\Attributes\DataProvider('uniqueNameProvider')] + public function testGetUniqueName($name, $existingFiles, $expected): 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(); $root = $this->getMockBuilder(Root::class) - ->setMethods(['getUser', 'getMountsIn', 'getMount']) - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $view->expects($this->any()) @@ -709,23 +721,20 @@ class FolderTest extends NodeTest { $this->assertEquals($expected, $node->getNonExistingName($name)); } - public function testRecent() { + 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']) - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ + /** @var \PHPUnit\Framework\MockObject\MockObject|FileInfo $folderInfo */ $folderInfo = $this->getMockBuilder(FileInfo::class) ->disableOriginalConstructor()->getMock(); - $baseTime = 1000; + $baseTime = time(); $storage = new Temporary(); $mount = new MountPoint($storage, ''); @@ -739,19 +748,23 @@ class FolderTest extends NodeTest { $cache = $storage->getCache(); + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); + $cache->insert('bar', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); + $cache->insert('bar/foo', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); + $cache->insert('bar/asd', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $id1 = $cache->put('bar/foo/inside.txt', [ 'storage_mtime' => $baseTime, 'mtime' => $baseTime, 'mimetype' => 'text/plain', 'size' => 3, - 'permissions' => \OCP\Constants::PERMISSION_ALL, + 'permissions' => Constants::PERMISSION_ALL, ]); $id2 = $cache->put('bar/foo/old.txt', [ 'storage_mtime' => $baseTime - 100, 'mtime' => $baseTime - 100, 'mimetype' => 'text/plain', 'size' => 3, - 'permissions' => \OCP\Constants::PERMISSION_READ, + 'permissions' => Constants::PERMISSION_READ, ]); $cache->put('bar/asd/outside.txt', [ 'storage_mtime' => $baseTime, @@ -764,7 +777,7 @@ class FolderTest extends NodeTest { 'mtime' => $baseTime - 600, 'mimetype' => 'text/plain', 'size' => 3, - 'permissions' => \OCP\Constants::PERMISSION_ALL, + 'permissions' => Constants::PERMISSION_ALL, ]); $node = new Folder($root, $view, $folderPath, $folderInfo); @@ -777,23 +790,20 @@ class FolderTest extends NodeTest { $this->assertEquals([$id1, $id2, $id3], $ids); } - public function testRecentFolder() { + public function testRecentFolder(): 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']) - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ + /** @var \PHPUnit\Framework\MockObject\MockObject|FileInfo $folderInfo */ $folderInfo = $this->getMockBuilder(FileInfo::class) ->disableOriginalConstructor()->getMock(); - $baseTime = 1000; + $baseTime = time(); $storage = new Temporary(); $mount = new MountPoint($storage, ''); @@ -808,6 +818,9 @@ class FolderTest extends NodeTest { $cache = $storage->getCache(); + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); + $cache->insert('bar', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); + $cache->insert('bar/foo', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $id1 = $cache->put('bar/foo/folder', [ 'storage_mtime' => $baseTime, 'mtime' => $baseTime, @@ -821,7 +834,7 @@ class FolderTest extends NodeTest { 'mimetype' => 'text/plain', 'size' => 3, 'parent' => $id1, - 'permissions' => \OCP\Constants::PERMISSION_ALL, + 'permissions' => Constants::PERMISSION_ALL, ]); $id3 = $cache->put('bar/foo/folder/asd.txt', [ 'storage_mtime' => $baseTime - 100, @@ -829,7 +842,7 @@ class FolderTest extends NodeTest { 'mimetype' => 'text/plain', 'size' => 3, 'parent' => $id1, - 'permissions' => \OCP\Constants::PERMISSION_ALL, + 'permissions' => Constants::PERMISSION_ALL, ]); $node = new Folder($root, $view, $folderPath, $folderInfo); @@ -844,23 +857,20 @@ class FolderTest extends NodeTest { $this->assertEquals($baseTime - 100, $nodes[1]->getMTime()); } - public function testRecentJail() { + public function testRecentJail(): 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']) - ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) + ->onlyMethods(['getUser', 'getMountsIn', 'getMount']) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); - /** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Files\FileInfo $folderInfo */ + /** @var \PHPUnit\Framework\MockObject\MockObject|FileInfo $folderInfo */ $folderInfo = $this->getMockBuilder(FileInfo::class) ->disableOriginalConstructor()->getMock(); - $baseTime = 1000; + $baseTime = time(); $storage = new Temporary(); $jail = new Jail([ 'storage' => $storage, @@ -878,13 +888,16 @@ class FolderTest extends NodeTest { $cache = $storage->getCache(); + $cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); + $cache->insert('folder', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $id1 = $cache->put('folder/inside.txt', [ 'storage_mtime' => $baseTime, 'mtime' => $baseTime, 'mimetype' => 'text/plain', 'size' => 3, - 'permissions' => \OCP\Constants::PERMISSION_ALL, + 'permissions' => Constants::PERMISSION_ALL, ]); + $cache->put('outside.txt', [ 'storage_mtime' => $baseTime - 100, 'mtime' => $baseTime - 100, @@ -901,7 +914,7 @@ class FolderTest extends NodeTest { $this->assertEquals([$id1], $ids); } - public function offsetLimitProvider() { + public static function offsetLimitProvider(): array { return [ [0, 10, ['/bar/foo/foo1', '/bar/foo/foo2', '/bar/foo/foo3', '/bar/foo/foo4', '/bar/foo/sub1/foo5', '/bar/foo/sub1/foo6', '/bar/foo/sub2/foo7', '/bar/foo/sub2/foo8'], []], [0, 5, ['/bar/foo/foo1', '/bar/foo/foo2', '/bar/foo/foo3', '/bar/foo/foo4', '/bar/foo/sub1/foo5'], []], @@ -922,38 +935,35 @@ class FolderTest extends NodeTest { } /** - * @dataProvider offsetLimitProvider * @param int $offset * @param int $limit * @param string[] $expectedPaths * @param ISearchOrder[] $ordering * @throws NotFoundException - * @throws \OCP\Files\InvalidPathException + * @throws InvalidPathException */ - public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array $expectedPaths, array $ordering) { + #[\PHPUnit\Framework\Attributes\DataProvider('offsetLimitProvider')] + public function testSearchSubStoragesLimitOffset(int $offset, int $limit, array $expectedPaths, array $ordering): void { if (!$ordering) { $ordering = [new SearchOrder(ISearchOrder::DIRECTION_ASCENDING, 'fileid')]; } $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]) + ->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); $root->expects($this->any()) ->method('getUser') ->willReturn($this->user); - $storage = $this->createMock(Storage::class); + $storage = $this->createMock(IStorage::class); $storage->method('getId')->willReturn('test::1'); $cache = new Cache($storage); - $subStorage1 = $this->createMock(Storage::class); + $subStorage1 = $this->createMock(IStorage::class); $subStorage1->method('getId')->willReturn('test::2'); $subCache1 = new Cache($subStorage1); $subMount1 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); - $subStorage2 = $this->createMock(Storage::class); + $subStorage2 = $this->createMock(IStorage::class); $subStorage2->method('getId')->willReturn('test::3'); $subCache2 = new Cache($subStorage2); $subMount2 = $this->getMockBuilder(MountPoint::class)->setConstructorArgs([Temporary::class, ''])->getMock(); @@ -972,9 +982,13 @@ class FolderTest extends NodeTest { $storage->method('getCache') ->willReturn($cache); + $storage->method('getOwner') + ->willReturn('owner'); $subStorage1->method('getCache') ->willReturn($subCache1); + $subStorage1->method('getOwner') + ->willReturn('owner'); $subMount2->method('getStorage') ->willReturn($subStorage2); @@ -984,15 +998,22 @@ class FolderTest extends NodeTest { $subStorage2->method('getCache') ->willReturn($subCache2); + $subStorage2->method('getOwner') + ->willReturn('owner'); + + $cache->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); + $cache->insert('foo', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $cache->insert('foo/foo1', ['size' => 200, 'mtime' => 10, 'mimetype' => 'text/plain']); $cache->insert('foo/foo2', ['size' => 200, 'mtime' => 20, 'mimetype' => 'text/plain']); $cache->insert('foo/foo3', ['size' => 200, 'mtime' => 30, 'mimetype' => 'text/plain']); $cache->insert('foo/foo4', ['size' => 200, 'mtime' => 40, 'mimetype' => 'text/plain']); + $subCache1->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $subCache1->insert('foo5', ['size' => 300, 'mtime' => 15, 'mimetype' => 'text/plain']); $subCache1->insert('foo6', ['size' => 300, 'mtime' => 50, 'mimetype' => 'text/plain']); + $subCache2->insert('', ['size' => 0, 'mtime' => 10, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]); $subCache2->insert('foo7', ['size' => 200, 'mtime' => 5, 'mimetype' => 'text/plain']); $subCache2->insert('foo8', ['size' => 200, 'mtime' => 60, 'mimetype' => 'text/plain']); @@ -1006,7 +1027,11 @@ class FolderTest extends NodeTest { $node = new Folder($root, $view, '/bar/foo'); $comparison = new SearchComparison(ISearchComparison::COMPARE_LIKE, 'name', '%foo%'); - $query = new SearchQuery($comparison, $limit, $offset, $ordering); + $operator = new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_AND, [ + $comparison, + new SearchBinaryOperator(ISearchBinaryOperator::OPERATOR_NOT, [new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'mimetype', ICacheEntry::DIRECTORY_MIMETYPE)]), + ]); + $query = new SearchQuery($operator, $limit, $offset, $ordering); $result = $node->search($query); $cache->clear(); $subCache1->clear(); diff --git a/tests/lib/Files/Node/HookConnectorTest.php b/tests/lib/Files/Node/HookConnectorTest.php index f99db6599fd..3f3957bab1d 100644 --- a/tests/lib/Files/Node/HookConnectorTest.php +++ b/tests/lib/Files/Node/HookConnectorTest.php @@ -1,9 +1,9 @@ <?php + /** - * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Files\Node; @@ -13,8 +13,10 @@ use OC\Files\Node\HookConnector; use OC\Files\Node\Root; use OC\Files\Storage\Temporary; use OC\Files\View; +use OC\Memcache\ArrayCache; use OCP\EventDispatcher\GenericEvent as APIGenericEvent; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Config\IUserMountCache; use OCP\Files\Events\Node\AbstractNodeEvent; use OCP\Files\Events\Node\AbstractNodesEvent; use OCP\Files\Events\Node\BeforeNodeCopiedEvent; @@ -30,10 +32,10 @@ use OCP\Files\Events\Node\NodeRenamedEvent; use OCP\Files\Events\Node\NodeTouchedEvent; use OCP\Files\Events\Node\NodeWrittenEvent; use OCP\Files\Node; +use OCP\ICacheFactory; use OCP\IUserManager; -use PHPUnit\Framework\MockObject\MockObject; +use OCP\Server; use Psr\Log\LoggerInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\GenericEvent; use Test\TestCase; use Test\Traits\MountProviderTrait; @@ -50,12 +52,11 @@ class HookConnectorTest extends TestCase { use UserTrait; use MountProviderTrait; - /** @var EventDispatcherInterface|MockObject */ - protected $legacyDispatcher; - - /** @var IEventDispatcher */ + /** @var IEventDispatcher */ protected $eventDispatcher; + private LoggerInterface $logger; + /** @var View */ private $view; @@ -69,20 +70,27 @@ class HookConnectorTest extends TestCase { parent::setUp(); $this->userId = $this->getUniqueID(); $this->createUser($this->userId, 'pass'); + // this will setup the FS + $this->loginAsUser($this->userId); $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/'); - \OC_Util::setupFS($this->userId); + $cacheFactory = $this->createMock(ICacheFactory::class); + $cacheFactory->method('createLocal') + ->willReturnCallback(function () { + return new ArrayCache(); + }); $this->view = new View(); $this->root = new Root( Filesystem::getMountManager(), $this->view, - \OC::$server->getUserManager()->get($this->userId), - \OC::$server->getUserMountCache(), + Server::get(IUserManager::class)->get($this->userId), + Server::get(IUserMountCache::class), $this->createMock(LoggerInterface::class), $this->createMock(IUserManager::class), - $this->createMock(IEventDispatcher::class) + $this->createMock(IEventDispatcher::class), + $cacheFactory, ); - $this->legacyDispatcher = \OC::$server->getEventDispatcher(); - $this->eventDispatcher = \OC::$server->query(IEventDispatcher::class); + $this->eventDispatcher = Server::get(IEventDispatcher::class); + $this->logger = Server::get(LoggerInterface::class); } protected function tearDown(): void { @@ -91,51 +99,51 @@ class HookConnectorTest extends TestCase { \OC_Util::tearDownFS(); } - public function viewToNodeProvider() { + public static function viewToNodeProvider(): array { return [ - [function () { + [function (): void { Filesystem::file_put_contents('test.txt', 'asd'); }, 'preWrite', '\OCP\Files::preWrite', BeforeNodeWrittenEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('test.txt', 'asd'); }, 'postWrite', '\OCP\Files::postWrite', NodeWrittenEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('test.txt', 'asd'); }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('test.txt', 'asd'); }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], - [function () { + [function (): void { Filesystem::mkdir('test.txt'); }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], - [function () { + [function (): void { Filesystem::mkdir('test.txt'); }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], - [function () { + [function (): void { Filesystem::touch('test.txt'); }, 'preTouch', '\OCP\Files::preTouch', BeforeNodeTouchedEvent::class], - [function () { + [function (): void { Filesystem::touch('test.txt'); }, 'postTouch', '\OCP\Files::postTouch', NodeTouchedEvent::class], - [function () { + [function (): void { Filesystem::touch('test.txt'); }, 'preCreate', '\OCP\Files::preCreate', BeforeNodeCreatedEvent::class], - [function () { + [function (): void { Filesystem::touch('test.txt'); }, 'postCreate', '\OCP\Files::postCreate', NodeCreatedEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::unlink('test.txt'); }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('test.txt', 'asd'); Filesystem::unlink('test.txt'); }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], - [function () { + [function (): void { Filesystem::mkdir('test.txt'); Filesystem::rmdir('test.txt'); }, 'preDelete', '\OCP\Files::preDelete', BeforeNodeDeletedEvent::class], - [function () { + [function (): void { Filesystem::mkdir('test.txt'); Filesystem::rmdir('test.txt'); }, 'postDelete', '\OCP\Files::postDelete', NodeDeletedEvent::class], @@ -145,16 +153,16 @@ class HookConnectorTest extends TestCase { /** * @param callable $operation * @param string $expectedHook - * @dataProvider viewToNodeProvider */ - public function testViewToNode(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent) { - $connector = new HookConnector($this->root, $this->view, $this->legacyDispatcher, $this->eventDispatcher); + #[\PHPUnit\Framework\Attributes\DataProvider('viewToNodeProvider')] + public function testViewToNode(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent): void { + $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); $connector->viewToNode(); $hookCalled = false; /** @var Node $hookNode */ $hookNode = null; - $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) { + $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled): void { $hookCalled = true; $hookNode = $node; }); @@ -162,7 +170,7 @@ class HookConnectorTest extends TestCase { $dispatcherCalled = false; /** @var Node $dispatcherNode */ $dispatcherNode = null; - $this->legacyDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode) { + $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherCalled, &$dispatcherNode): void { /** @var GenericEvent|APIGenericEvent $event */ $dispatcherCalled = true; $dispatcherNode = $event->getSubject(); @@ -170,7 +178,7 @@ class HookConnectorTest extends TestCase { $newDispatcherCalled = false; $newDispatcherNode = null; - $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherCalled, &$newDispatcherNode) { + $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherCalled, &$newDispatcherNode): void { if ($event instanceof $expectedEvent) { /** @var AbstractNodeEvent $event */ $newDispatcherCalled = true; @@ -190,21 +198,21 @@ class HookConnectorTest extends TestCase { $this->assertEquals('/' . $this->userId . '/files/test.txt', $newDispatcherNode->getPath()); } - public function viewToNodeProviderCopyRename() { + public static function viewToNodeProviderCopyRename(): array { return [ - [function () { + [function (): void { Filesystem::file_put_contents('source', 'asd'); Filesystem::rename('source', 'target'); }, 'preRename', '\OCP\Files::preRename', BeforeNodeRenamedEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('source', 'asd'); Filesystem::rename('source', 'target'); }, 'postRename', '\OCP\Files::postRename', NodeRenamedEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('source', 'asd'); Filesystem::copy('source', 'target'); }, 'preCopy', '\OCP\Files::preCopy', BeforeNodeCopiedEvent::class], - [function () { + [function (): void { Filesystem::file_put_contents('source', 'asd'); Filesystem::copy('source', 'target'); }, 'postCopy', '\OCP\Files::postCopy', NodeCopiedEvent::class], @@ -214,10 +222,10 @@ class HookConnectorTest extends TestCase { /** * @param callable $operation * @param string $expectedHook - * @dataProvider viewToNodeProviderCopyRename */ - public function testViewToNodeCopyRename(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent) { - $connector = new HookConnector($this->root, $this->view, $this->legacyDispatcher, $this->eventDispatcher); + #[\PHPUnit\Framework\Attributes\DataProvider('viewToNodeProviderCopyRename')] + public function testViewToNodeCopyRename(callable $operation, $expectedHook, $expectedLegacyEvent, $expectedEvent): void { + $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); $connector->viewToNode(); $hookCalled = false; /** @var Node $hookSourceNode */ @@ -225,7 +233,7 @@ class HookConnectorTest extends TestCase { /** @var Node $hookTargetNode */ $hookTargetNode = null; - $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) { + $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode): void { $hookCalled = true; $hookSourceNode = $sourceNode; $hookTargetNode = $targetNode; @@ -236,7 +244,7 @@ class HookConnectorTest extends TestCase { $dispatcherSourceNode = null; /** @var Node $dispatcherTargetNode */ $dispatcherTargetNode = null; - $this->legacyDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled) { + $this->eventDispatcher->addListener($expectedLegacyEvent, function ($event) use (&$dispatcherSourceNode, &$dispatcherTargetNode, &$dispatcherCalled): void { /** @var GenericEvent|APIGenericEvent $event */ $dispatcherCalled = true; [$dispatcherSourceNode, $dispatcherTargetNode] = $event->getSubject(); @@ -247,7 +255,7 @@ class HookConnectorTest extends TestCase { $newDispatcherSourceNode = null; /** @var Node $dispatcherTargetNode */ $newDispatcherTargetNode = null; - $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherSourceNode, &$newDispatcherTargetNode, &$newDispatcherCalled) { + $this->eventDispatcher->addListener($expectedEvent, function ($event) use ($expectedEvent, &$newDispatcherSourceNode, &$newDispatcherTargetNode, &$newDispatcherCalled): void { if ($event instanceof $expectedEvent) { /** @var AbstractNodesEvent$event */ $newDispatcherCalled = true; @@ -271,14 +279,14 @@ class HookConnectorTest extends TestCase { $this->assertEquals('/' . $this->userId . '/files/target', $newDispatcherTargetNode->getPath()); } - public function testPostDeleteMeta() { - $connector = new HookConnector($this->root, $this->view, $this->legacyDispatcher, $this->eventDispatcher); + public function testPostDeleteMeta(): void { + $connector = new HookConnector($this->root, $this->view, $this->eventDispatcher, $this->logger); $connector->viewToNode(); $hookCalled = false; /** @var Node $hookNode */ $hookNode = null; - $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled) { + $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled): void { $hookCalled = true; $hookNode = $node; }); @@ -286,7 +294,7 @@ class HookConnectorTest extends TestCase { $dispatcherCalled = false; /** @var Node $dispatcherNode */ $dispatcherNode = null; - $this->legacyDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode) { + $this->eventDispatcher->addListener('\OCP\Files::postDelete', function ($event) use (&$dispatcherCalled, &$dispatcherNode): void { /** @var GenericEvent|APIGenericEvent $event */ $dispatcherCalled = true; $dispatcherNode = $event->getSubject(); @@ -295,7 +303,7 @@ class HookConnectorTest extends TestCase { $newDispatcherCalled = false; /** @var Node $dispatcherNode */ $newDispatcherNode = null; - $this->eventDispatcher->addListener(NodeDeletedEvent::class, function ($event) use (&$newDispatcherCalled, &$newDispatcherNode) { + $this->eventDispatcher->addListener(NodeDeletedEvent::class, function ($event) use (&$newDispatcherCalled, &$newDispatcherNode): void { if ($event instanceof NodeDeletedEvent) { /** @var AbstractNodeEvent $event */ $newDispatcherCalled = true; diff --git a/tests/lib/Files/Node/IntegrationTest.php b/tests/lib/Files/Node/IntegrationTest.php index 5ef5a134e1b..f059afa1625 100644 --- a/tests/lib/Files/Node/IntegrationTest.php +++ b/tests/lib/Files/Node/IntegrationTest.php @@ -1,19 +1,24 @@ <?php + /** - * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Files\Node; use OC\Files\Node\Root; +use OC\Files\Storage\Storage; use OC\Files\Storage\Temporary; use OC\Files\View; +use OC\Memcache\ArrayCache; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Config\IUserMountCache; use OCP\Files\Mount\IMountManager; +use OCP\ICacheFactory; use OCP\IUserManager; +use OCP\Server; use Psr\Log\LoggerInterface; use Test\Traits\UserTrait; @@ -33,35 +38,40 @@ class IntegrationTest extends \Test\TestCase { private $root; /** - * @var \OC\Files\Storage\Storage[] + * @var Storage[] */ private $storages; /** - * @var \OC\Files\View $view + * @var View $view */ private $view; protected function setUp(): void { parent::setUp(); - /** @var IMountManager $manager */ - $manager = \OC::$server->get(IMountManager::class); + $manager = Server::get(IMountManager::class); \OC_Hook::clear('OC_Filesystem'); $user = $this->createUser($this->getUniqueID('user'), ''); $this->loginAsUser($user->getUID()); + $cacheFactory = $this->createMock(ICacheFactory::class); + $cacheFactory->method('createLocal') + ->willReturnCallback(function () { + return new ArrayCache(); + }); $this->view = new View(); $this->root = new Root( $manager, $this->view, $user, - \OC::$server->getUserMountCache(), + Server::get(IUserMountCache::class), $this->createMock(LoggerInterface::class), $this->createMock(IUserManager::class), - $this->createMock(IEventDispatcher::class) + $this->createMock(IEventDispatcher::class), + $cacheFactory, ); $storage = new Temporary([]); $subStorage = new Temporary([]); @@ -81,7 +91,7 @@ class IntegrationTest extends \Test\TestCase { parent::tearDown(); } - public function testBasicFile() { + public function testBasicFile(): void { $file = $this->root->newFile('/foo.txt'); $this->assertCount(2, $this->root->getDirectoryListing()); $this->assertTrue($this->root->nodeExists('/foo.txt')); @@ -104,7 +114,7 @@ class IntegrationTest extends \Test\TestCase { $this->assertEquals('qwerty', $file->getContent()); } - public function testBasicFolder() { + public function testBasicFolder(): void { $folder = $this->root->newFolder('/foo'); $this->assertTrue($this->root->nodeExists('/foo')); $file = $folder->newFile('/bar'); diff --git a/tests/lib/Files/Node/NodeTest.php b/tests/lib/Files/Node/NodeTestCase.php index 8c0d4cdb293..4aecd0fef11 100644 --- a/tests/lib/Files/Node/NodeTest.php +++ b/tests/lib/Files/Node/NodeTestCase.php @@ -1,22 +1,33 @@ <?php + /** - * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Files\Node; use OC\Files\FileInfo; use OC\Files\Mount\Manager; +use OC\Files\Node\File; +use OC\Files\Node\Folder; +use OC\Files\Node\Root; +use OC\Files\Storage\Storage; use OC\Files\View; +use OC\Memcache\ArrayCache; +use OC\User\User; +use OCP\Constants; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Config\IUserMountCache; +use OCP\Files\InvalidPathException; use OCP\Files\IRootFolder; use OCP\Files\Mount\IMountPoint; use OCP\Files\Node; use OCP\Files\NotFoundException; -use OCP\Files\Storage; +use OCP\Files\NotPermittedException; +use OCP\Files\Storage\IStorage; +use OCP\ICacheFactory; use OCP\IUser; use OCP\IUserManager; use Psr\Log\LoggerInterface; @@ -26,16 +37,16 @@ use Psr\Log\LoggerInterface; * * @package Test\Files\Node */ -abstract class NodeTest extends \Test\TestCase { - /** @var \OC\User\User */ +abstract class NodeTestCase extends \Test\TestCase { + /** @var User */ protected $user; /** @var \OC\Files\Mount\Manager */ protected $manager; - /** @var \OC\Files\View|\PHPUnit\Framework\MockObject\MockObject */ + /** @var View|\PHPUnit\Framework\MockObject\MockObject */ protected $view; /** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject */ protected $root; - /** @var \OCP\Files\Config\IUserMountCache|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IUserMountCache|\PHPUnit\Framework\MockObject\MockObject */ protected $userMountCache; /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ protected $logger; @@ -43,6 +54,8 @@ abstract class NodeTest extends \Test\TestCase { protected $userManager; /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */ protected $eventDispatcher; + /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ + protected $cacheFactory; protected function setUp(): void { parent::setUp(); @@ -54,18 +67,37 @@ abstract class NodeTest extends \Test\TestCase { $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(); $this->logger = $this->createMock(LoggerInterface::class); $this->userManager = $this->createMock(IUserManager::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); - $this->root = $this->getMockBuilder('\OC\Files\Node\Root') - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->eventDispatcher]) + $this->cacheFactory = $this->createMock(ICacheFactory::class); + $this->cacheFactory->method('createLocal') + ->willReturnCallback(function () { + return new ArrayCache(); + }); + $this->root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) ->getMock(); } /** + * @return 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 * @param string $path @@ -89,7 +121,7 @@ abstract class NodeTest extends \Test\TestCase { abstract protected function getViewDeleteMethod(); protected function getMockStorage() { - $storage = $this->getMockBuilder(Storage::class) + $storage = $this->getMockBuilder(IStorage::class) ->disableOriginalConstructor() ->getMock(); $storage->expects($this->any()) @@ -105,7 +137,7 @@ abstract class NodeTest extends \Test\TestCase { return new FileInfo('', $this->getMockStorage(), $internalPath, $data, $mount); } - public function testDelete() { + public function testDelete(): void { $this->root->expects($this->exactly(2)) ->method('emit') ->willReturn(true); @@ -116,7 +148,7 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); $this->view->expects($this->once()) ->method($this->getViewDeleteMethod()) @@ -127,13 +159,13 @@ abstract class NodeTest extends \Test\TestCase { $node->delete(); } - public function testDeleteHooks() { + public function testDeleteHooks(): void { $test = $this; $hooksRun = 0; /** * @param \OC\Files\Node\File $node */ - $preListener = function ($node) use (&$test, &$hooksRun) { + $preListener = function ($node) use (&$test, &$hooksRun): void { $test->assertInstanceOf($this->getNodeClass(), $node); $test->assertEquals('foo', $node->getInternalPath()); $test->assertEquals('/bar/foo', $node->getPath()); @@ -144,7 +176,7 @@ abstract class NodeTest extends \Test\TestCase { /** * @param \OC\Files\Node\File $node */ - $postListener = function ($node) use (&$test, &$hooksRun) { + $postListener = function ($node) use (&$test, &$hooksRun): void { $test->assertInstanceOf($this->getNonExistingNodeClass(), $node); $test->assertEquals('foo', $node->getInternalPath()); $test->assertEquals('/bar/foo', $node->getPath()); @@ -153,14 +185,15 @@ abstract class NodeTest extends \Test\TestCase { $hooksRun++; }; - $root = new \OC\Files\Node\Root( + $root = new Root( $this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $root->listen('\OC\Files', 'preDelete', $preListener); @@ -169,7 +202,7 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain'], 'foo')); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain'], 'foo')); $this->view->expects($this->once()) ->method($this->getViewDeleteMethod()) @@ -182,8 +215,8 @@ abstract class NodeTest extends \Test\TestCase { } - public function testDeleteNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testDeleteNotPermitted(): void { + $this->expectException(NotPermittedException::class); $this->root->expects($this->any()) ->method('getUser') @@ -192,14 +225,14 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); $node->delete(); } - public function testStat() { + public function testStat(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -221,7 +254,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals($stat, $node->stat()); } - public function testGetId() { + public function testGetId(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -242,7 +275,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals(1, $node->getId()); } - public function testGetSize() { + public function testGetSize(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -264,7 +297,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals(100, $node->getSize()); } - public function testGetEtag() { + public function testGetEtag(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -285,7 +318,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals('qwerty', $node->getEtag()); } - public function testGetMTime() { + public function testGetMTime(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -306,12 +339,12 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals(50, $node->getMTime()); } - public function testGetStorage() { + public function testGetStorage(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); /** - * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage + * @var Storage|\PHPUnit\Framework\MockObject\MockObject $storage */ $storage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor() @@ -321,7 +354,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals($storage, $node->getStorage()); } - public function testGetPath() { + public function testGetPath(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -330,12 +363,12 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals('/bar/foo', $node->getPath()); } - public function testGetInternalPath() { + public function testGetInternalPath(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); /** - * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage + * @var Storage|\PHPUnit\Framework\MockObject\MockObject $storage */ $storage = $this->getMockBuilder('\OC\Files\Storage\Storage') ->disableOriginalConstructor() @@ -351,7 +384,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals('foo', $node->getInternalPath()); } - public function testGetName() { + public function testGetName(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -360,7 +393,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals('foo', $node->getName()); } - public function testTouchSetMTime() { + public function testTouchSetMTime(): void { $this->root->expects($this->any()) ->method('getUser') ->willReturn($this->user); @@ -373,20 +406,20 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->once()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); $node->touch(100); $this->assertEquals(100, $node->getMTime()); } - public function testTouchHooks() { + public function testTouchHooks(): void { $test = $this; $hooksRun = 0; /** * @param \OC\Files\Node\File $node */ - $preListener = function ($node) use (&$test, &$hooksRun) { + $preListener = function ($node) use (&$test, &$hooksRun): void { $test->assertEquals('foo', $node->getInternalPath()); $test->assertEquals('/bar/foo', $node->getPath()); $hooksRun++; @@ -395,20 +428,21 @@ abstract class NodeTest extends \Test\TestCase { /** * @param \OC\Files\Node\File $node */ - $postListener = function ($node) use (&$test, &$hooksRun) { + $postListener = function ($node) use (&$test, &$hooksRun): void { $test->assertEquals('foo', $node->getInternalPath()); $test->assertEquals('/bar/foo', $node->getPath()); $hooksRun++; }; - $root = new \OC\Files\Node\Root( + $root = new Root( $this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $root->listen('\OC\Files', 'preTouch', $preListener); $root->listen('\OC\Files', 'postTouch', $postListener); @@ -421,7 +455,7 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL], 'foo')); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL], 'foo')); $node = $this->createTestNode($root, $this->view, '/bar/foo'); $node->touch(100); @@ -429,8 +463,8 @@ abstract class NodeTest extends \Test\TestCase { } - public function testTouchNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testTouchNotPermitted(): void { + $this->expectException(NotPermittedException::class); $this->root->expects($this->any()) ->method('getUser') @@ -439,21 +473,21 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') ->with('/bar/foo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); $node->touch(100); } - public function testInvalidPath() { - $this->expectException(\OCP\Files\InvalidPathException::class); + public function testInvalidPath(): void { + $this->expectException(InvalidPathException::class); $node = $this->createTestNode($this->root, $this->view, '/../foo'); $node->getFileInfo(); } - public function testCopySameStorage() { + public function testCopySameStorage(): void { $this->view->expects($this->any()) ->method('copy') ->with('/bar/foo', '/bar/asd') @@ -461,14 +495,13 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL, 'fileid' => 3])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar'); + $parentNode = new Folder($this->root, $this->view, '/bar'); $newNode = $this->createTestNode($this->root, $this->view, '/bar/asd'); - $this->root->expects($this->exactly(2)) - ->method('get') + $this->root->method('get') ->willReturnMap([ ['/bar/asd', $newNode], ['/bar', $parentNode] @@ -480,11 +513,11 @@ abstract class NodeTest extends \Test\TestCase { } - public function testCopyNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testCopyNotPermitted(): void { + $this->expectException(NotPermittedException::class); /** - * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage + * @var Storage|\PHPUnit\Framework\MockObject\MockObject $storage */ $storage = $this->createMock('\OC\Files\Storage\Storage'); @@ -496,10 +529,10 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ, 'fileid' => 3])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar'); + $parentNode = new Folder($this->root, $this->view, '/bar'); $this->root->expects($this->once()) ->method('get') @@ -511,8 +544,8 @@ abstract class NodeTest extends \Test\TestCase { } - public function testCopyNoParent() { - $this->expectException(\OCP\Files\NotFoundException::class); + public function testCopyNoParent(): void { + $this->expectException(NotFoundException::class); $this->view->expects($this->never()) ->method('copy'); @@ -522,20 +555,20 @@ abstract class NodeTest extends \Test\TestCase { $this->root->expects($this->once()) ->method('get') ->with('/bar/asd') - ->will($this->throwException(new NotFoundException())); + ->willThrowException(new NotFoundException()); $node->copy('/bar/asd/foo'); } - public function testCopyParentIsFile() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testCopyParentIsFile(): void { + $this->expectException(NotPermittedException::class); $this->view->expects($this->never()) ->method('copy'); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar'); + $parentNode = new File($this->root, $this->view, '/bar'); $this->root->expects($this->once()) ->method('get') @@ -546,7 +579,7 @@ abstract class NodeTest extends \Test\TestCase { $node->copy('/bar/asd'); } - public function testMoveSameStorage() { + public function testMoveSameStorage(): void { $this->view->expects($this->any()) ->method('rename') ->with('/bar/foo', '/bar/asd') @@ -554,10 +587,10 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL, 'fileid' => 1])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar'); + $parentNode = new Folder($this->root, $this->view, '/bar'); $this->root->expects($this->any()) ->method('get') @@ -569,7 +602,7 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals('/bar/asd', $node->getPath()); } - public function moveOrCopyProvider() { + public static function moveOrCopyProvider(): array { return [ ['move', 'rename', 'preRename', 'postRename'], ['copy', 'copy', 'preCopy', 'postCopy'], @@ -577,17 +610,17 @@ abstract class NodeTest extends \Test\TestCase { } /** - * @dataProvider moveOrCopyProvider * @param string $operationMethod * @param string $viewMethod * @param string $preHookName * @param string $postHookName */ - public function testMoveCopyHooks($operationMethod, $viewMethod, $preHookName, $postHookName) { + #[\PHPUnit\Framework\Attributes\DataProvider('moveOrCopyProvider')] + public function testMoveCopyHooks($operationMethod, $viewMethod, $preHookName, $postHookName): void { /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject $root */ - $root = $this->getMockBuilder('\OC\Files\Node\Root') - ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher]) - ->setMethods(['get']) + $root = $this->getMockBuilder(Root::class) + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher, $this->cacheFactory]) + ->onlyMethods(['get']) ->getMock(); $this->view->expects($this->any()) @@ -597,13 +630,13 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL, 'fileid' => 1])); /** * @var \OC\Files\Node\File|\PHPUnit\Framework\MockObject\MockObject $node */ $node = $this->createTestNode($root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar'); + $parentNode = new Folder($root, $this->view, '/bar'); $targetTestNode = $this->createTestNode($root, $this->view, '/bar/asd'); $root->expects($this->any()) @@ -612,7 +645,7 @@ abstract class NodeTest extends \Test\TestCase { $hooksRun = 0; - $preListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node) { + $preListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node): void { $this->assertSame($node, $sourceNode); $this->assertInstanceOf($this->getNodeClass(), $sourceNode); $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode); @@ -620,7 +653,7 @@ abstract class NodeTest extends \Test\TestCase { $hooksRun++; }; - $postListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node, $targetTestNode) { + $postListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node, $targetTestNode): void { $this->assertSame($node, $sourceNode); $this->assertNotSame($node, $targetNode); $this->assertSame($targetTestNode, $targetNode); @@ -629,13 +662,13 @@ abstract class NodeTest extends \Test\TestCase { $hooksRun++; }; - $preWriteListener = function (Node $targetNode) use (&$hooksRun) { + $preWriteListener = function (Node $targetNode) use (&$hooksRun): void { $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode); $this->assertEquals('/bar/asd', $targetNode->getPath()); $hooksRun++; }; - $postWriteListener = function (Node $targetNode) use (&$hooksRun, $targetTestNode) { + $postWriteListener = function (Node $targetNode) use (&$hooksRun, $targetTestNode): void { $this->assertSame($targetTestNode, $targetNode); $hooksRun++; }; @@ -651,18 +684,18 @@ abstract class NodeTest extends \Test\TestCase { } - public function testMoveNotPermitted() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testMoveNotPermitted(): void { + $this->expectException(NotPermittedException::class); $this->view->expects($this->any()) ->method('getFileInfo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_READ])); $this->view->expects($this->never()) ->method('rename'); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar'); + $parentNode = new Folder($this->root, $this->view, '/bar'); $this->root->expects($this->once()) ->method('get') @@ -673,11 +706,11 @@ abstract class NodeTest extends \Test\TestCase { } - public function testMoveNoParent() { - $this->expectException(\OCP\Files\NotFoundException::class); + public function testMoveNoParent(): void { + $this->expectException(NotFoundException::class); /** - * @var \OC\Files\Storage\Storage | \PHPUnit\Framework\MockObject\MockObject $storage + * @var Storage|\PHPUnit\Framework\MockObject\MockObject $storage */ $storage = $this->createMock('\OC\Files\Storage\Storage'); @@ -689,20 +722,20 @@ abstract class NodeTest extends \Test\TestCase { $this->root->expects($this->once()) ->method('get') ->with('/bar') - ->will($this->throwException(new NotFoundException())); + ->willThrowException(new NotFoundException()); $node->move('/bar/asd'); } - public function testMoveParentIsFile() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testMoveParentIsFile(): void { + $this->expectException(NotPermittedException::class); $this->view->expects($this->never()) ->method('rename'); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar'); + $parentNode = new File($this->root, $this->view, '/bar'); $this->root->expects($this->once()) ->method('get') @@ -713,8 +746,8 @@ abstract class NodeTest extends \Test\TestCase { } - public function testMoveFailed() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testMoveFailed(): void { + $this->expectException(NotPermittedException::class); $this->view->expects($this->any()) ->method('rename') @@ -723,10 +756,10 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL, 'fileid' => 1])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar'); + $parentNode = new Folder($this->root, $this->view, '/bar'); $this->root->expects($this->any()) ->method('get') @@ -736,8 +769,8 @@ abstract class NodeTest extends \Test\TestCase { } - public function testCopyFailed() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testCopyFailed(): void { + $this->expectException(NotPermittedException::class); $this->view->expects($this->any()) ->method('copy') @@ -746,10 +779,10 @@ abstract class NodeTest extends \Test\TestCase { $this->view->expects($this->any()) ->method('getFileInfo') - ->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])); + ->willReturn($this->getFileInfo(['permissions' => Constants::PERMISSION_ALL, 'fileid' => 1])); $node = $this->createTestNode($this->root, $this->view, '/bar/foo'); - $parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar'); + $parentNode = new Folder($this->root, $this->view, '/bar'); $this->root->expects($this->any()) ->method('get') diff --git a/tests/lib/Files/Node/RootTest.php b/tests/lib/Files/Node/RootTest.php index ee86eab5675..d90e6a2cc6e 100644 --- a/tests/lib/Files/Node/RootTest.php +++ b/tests/lib/Files/Node/RootTest.php @@ -1,19 +1,28 @@ <?php + /** - * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Files\Node; -use OC\Cache\CappedMemoryCache; use OC\Files\FileInfo; use OC\Files\Mount\Manager; use OC\Files\Node\Folder; +use OC\Files\Node\Root; +use OC\Files\Storage\Storage; use OC\Files\View; +use OC\Memcache\ArrayCache; +use OC\User\NoUserException; +use OC\User\User; +use OCP\Cache\CappedMemoryCache; use OCP\EventDispatcher\IEventDispatcher; +use OCP\Files\Config\IUserMountCache; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OCP\ICacheFactory; use OCP\IUser; use OCP\IUserManager; use Psr\Log\LoggerInterface; @@ -24,11 +33,11 @@ use Psr\Log\LoggerInterface; * @package Test\Files\Node */ class RootTest extends \Test\TestCase { - /** @var \OC\User\User */ + /** @var User */ private $user; /** @var \OC\Files\Mount\Manager */ private $manager; - /** @var \OCP\Files\Config\IUserMountCache|\PHPUnit\Framework\MockObject\MockObject */ + /** @var IUserMountCache|\PHPUnit\Framework\MockObject\MockObject */ private $userMountCache; /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ private $logger; @@ -36,6 +45,8 @@ class RootTest extends \Test\TestCase { private $userManager; /** @var IEventDispatcher|\PHPUnit\Framework\MockObject\MockObject */ private $eventDispatcher; + /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */ + protected $cacheFactory; protected function setUp(): void { parent::setUp(); @@ -50,33 +61,45 @@ class RootTest extends \Test\TestCase { $this->logger = $this->createMock(LoggerInterface::class); $this->userManager = $this->createMock(IUserManager::class); $this->eventDispatcher = $this->createMock(IEventDispatcher::class); + $this->cacheFactory = $this->createMock(ICacheFactory::class); + $this->cacheFactory->method('createLocal') + ->willReturnCallback(function () { + return new ArrayCache(); + }); + } + + /** + * @return 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); } - public function testGet() { + public function testGet(): void { /** - * @var \OC\Files\Storage\Storage $storage + * @var Storage $storage */ $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(); - $root = new \OC\Files\Node\Root( + $view = $this->getRootViewMock(); + $root = new Root( $this->manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $view->expects($this->once()) @@ -91,29 +114,25 @@ class RootTest extends \Test\TestCase { } - public function testGetNotFound() { - $this->expectException(\OCP\Files\NotFoundException::class); + public function testGetNotFound(): void { + $this->expectException(NotFoundException::class); /** - * @var \OC\Files\Storage\Storage $storage + * @var Storage $storage */ $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(); - $root = new \OC\Files\Node\Root( + $view = $this->getRootViewMock(); + $root = new Root( $this->manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $view->expects($this->once()) @@ -126,60 +145,53 @@ class RootTest extends \Test\TestCase { } - public function testGetInvalidPath() { - $this->expectException(\OCP\Files\NotPermittedException::class); + public function testGetInvalidPath(): void { + $this->expectException(NotPermittedException::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); - $root = new \OC\Files\Node\Root( + $view = $this->getRootViewMock(); + $root = new Root( $this->manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $root->get('/../foo'); } - public function testGetNoStorages() { - $this->expectException(\OCP\Files\NotFoundException::class); + public function testGetNoStorages(): void { + $this->expectException(NotFoundException::class); - /** - * @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view - */ - $view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); - $root = new \OC\Files\Node\Root( + $view = $this->getRootViewMock(); + $root = new Root( $this->manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $root->get('/bar/foo'); } - public function testGetUserFolder() { - $root = new \OC\Files\Node\Root( + public function testGetUserFolder(): void { + $root = new Root( $this->manager, - $this->createMock(View::class), + $this->getRootViewMock(), $this->user, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $user = $this->createMock(IUser::class); $user @@ -209,18 +221,19 @@ class RootTest extends \Test\TestCase { } - public function testGetUserFolderWithNoUserObj() { - $this->expectException(\OC\User\NoUserException::class); + public function testGetUserFolderWithNoUserObj(): void { + $this->expectException(NoUserException::class); $this->expectExceptionMessage('Backends provided no user object'); - $root = new \OC\Files\Node\Root( + $root = new Root( $this->createMock(Manager::class), - $this->createMock(View::class), + $this->getRootViewMock(), null, $this->userMountCache, $this->logger, $this->userManager, - $this->eventDispatcher + $this->eventDispatcher, + $this->cacheFactory, ); $this->userManager ->expects($this->once()) |