diff options
Diffstat (limited to 'apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php')
-rw-r--r-- | apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php | 224 |
1 files changed, 119 insertions, 105 deletions
diff --git a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php index e8297c2ac66..421ee1bdc12 100644 --- a/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/DirectoryTest.php @@ -1,49 +1,36 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Christoph Wurst <christoph@winzerhof-wurst.at> - * @author Joas Schilling <coding@schilljs.com> - * @author Julius Härtl <jus@bitgrid.net> - * @author Morris Jobke <hey@morrisjobke.de> - * @author Robin Appelman <robin@icewind.nl> - * @author Roeland Jago Douma <roeland@famdouma.nl> - * @author Thomas Müller <thomas.mueller@tmit.eu> - * @author Vincent Petry <vincent@nextcloud.com> - * - * @license AGPL-3.0 - * - * This code is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License, version 3, - * as published by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License, version 3, - * along with this program. If not, see <http://www.gnu.org/licenses/> - * + * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-FileCopyrightText: 2016 ownCloud, Inc. + * SPDX-License-Identifier: AGPL-3.0-only */ -namespace OCA\DAV\Tests\Unit\Connector\Sabre; +namespace OCA\DAV\Tests\unit\Connector\Sabre; use OC\Files\FileInfo; +use OC\Files\Filesystem; use OC\Files\Node\Node; use OC\Files\Storage\Wrapper\Quota; +use OC\Files\View; use OCA\DAV\Connector\Sabre\Directory; +use OCA\DAV\Connector\Sabre\Exception\Forbidden; +use OCA\DAV\Connector\Sabre\Exception\InvalidPath; +use OCA\Files_Sharing\External\Storage; +use OCP\Constants; use OCP\Files\ForbiddenException; +use OCP\Files\InvalidPathException; use OCP\Files\Mount\IMountPoint; - -class TestViewDirectory extends \OC\Files\View { - private $updatables; - private $deletables; - private $canRename; - - public function __construct($updatables, $deletables, $canRename = true) { - $this->updatables = $updatables; - $this->deletables = $deletables; - $this->canRename = $canRename; +use OCP\Files\StorageNotAvailableException; +use PHPUnit\Framework\MockObject\MockObject; +use Test\Traits\UserTrait; + +class TestViewDirectory extends View { + public function __construct( + private $updatables, + private $deletables, + private $canRename = true, + ) { } public function isUpdatable($path) { @@ -58,11 +45,11 @@ class TestViewDirectory extends \OC\Files\View { return $this->deletables[$path]; } - public function rename($path1, $path2) { + public function rename($source, $target, array $options = []) { return $this->canRename; } - public function getRelativePath($path) { + public function getRelativePath($path): ?string { return $path; } } @@ -72,26 +59,29 @@ class TestViewDirectory extends \OC\Files\View { * @group DB */ class DirectoryTest extends \Test\TestCase { + use UserTrait; - /** @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject */ - private $view; - /** @var \OC\Files\FileInfo | \PHPUnit\Framework\MockObject\MockObject */ - private $info; + private View&MockObject $view; + private FileInfo&MockObject $info; protected function setUp(): void { parent::setUp(); - $this->view = $this->createMock('OC\Files\View'); - $this->info = $this->createMock('OC\Files\FileInfo'); + $this->view = $this->createMock(View::class); + $this->info = $this->createMock(FileInfo::class); $this->info->method('isReadable') ->willReturn(true); $this->info->method('getType') ->willReturn(Node::TYPE_FOLDER); $this->info->method('getName') - ->willReturn("folder"); + ->willReturn('folder'); + $this->info->method('getPath') + ->willReturn('/admin/files/folder'); + $this->info->method('getPermissions') + ->willReturn(Constants::PERMISSION_READ); } - private function getDir($path = '/') { + private function getDir(string $path = '/'): Directory { $this->view->expects($this->once()) ->method('getRelativePath') ->willReturn($path); @@ -104,7 +94,7 @@ class DirectoryTest extends \Test\TestCase { } - public function testDeleteRootFolderFails() { + public function testDeleteRootFolderFails(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->info->expects($this->any()) @@ -117,8 +107,8 @@ class DirectoryTest extends \Test\TestCase { } - public function testDeleteForbidden() { - $this->expectException(\OCA\DAV\Connector\Sabre\Exception\Forbidden::class); + public function testDeleteForbidden(): void { + $this->expectException(Forbidden::class); // deletion allowed $this->info->expects($this->once()) @@ -136,7 +126,7 @@ class DirectoryTest extends \Test\TestCase { } - public function testDeleteFolderWhenAllowed() { + public function testDeleteFolderWhenAllowed(): void { // deletion allowed $this->info->expects($this->once()) ->method('isDeletable') @@ -153,7 +143,7 @@ class DirectoryTest extends \Test\TestCase { } - public function testDeleteFolderFailsWhenNotAllowed() { + public function testDeleteFolderFailsWhenNotAllowed(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->info->expects($this->once()) @@ -165,7 +155,7 @@ class DirectoryTest extends \Test\TestCase { } - public function testDeleteFolderThrowsWhenDeletionFailed() { + public function testDeleteFolderThrowsWhenDeletionFailed(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); // deletion allowed @@ -183,13 +173,9 @@ class DirectoryTest extends \Test\TestCase { $dir->delete(); } - public function testGetChildren() { - $info1 = $this->getMockBuilder(FileInfo::class) - ->disableOriginalConstructor() - ->getMock(); - $info2 = $this->getMockBuilder(FileInfo::class) - ->disableOriginalConstructor() - ->getMock(); + public function testGetChildren(): void { + $info1 = $this->createMock(FileInfo::class); + $info2 = $this->createMock(FileInfo::class); $info1->method('getName') ->willReturn('first'); $info1->method('getPath') @@ -205,17 +191,26 @@ class DirectoryTest extends \Test\TestCase { $this->view->expects($this->once()) ->method('getDirectoryContent') - ->with('') ->willReturn([$info1, $info2]); $this->view->expects($this->any()) ->method('getRelativePath') - ->willReturn(''); + ->willReturnCallback(function ($path) { + return str_replace('/admin/files/', '', $path); + }); + + $this->view->expects($this->any()) + ->method('getAbsolutePath') + ->willReturnCallback(function ($path) { + return Filesystem::normalizePath('/admin/files' . $path); + }); + + $this->overwriteService(View::class, $this->view); $dir = new Directory($this->view, $this->info); $nodes = $dir->getChildren(); - $this->assertEquals(2, count($nodes)); + $this->assertCount(2, $nodes); // calling a second time just returns the cached values, // does not call getDirectoryContents again @@ -223,7 +218,7 @@ class DirectoryTest extends \Test\TestCase { } - public function testGetChildrenNoPermission() { + public function testGetChildrenNoPermission(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); $info = $this->createMock(FileInfo::class); @@ -236,7 +231,7 @@ class DirectoryTest extends \Test\TestCase { } - public function testGetChildNoPermission() { + public function testGetChildNoPermission(): void { $this->expectException(\Sabre\DAV\Exception\NotFound::class); $this->info->expects($this->any()) @@ -248,24 +243,24 @@ class DirectoryTest extends \Test\TestCase { } - public function testGetChildThrowStorageNotAvailableException() { + public function testGetChildThrowStorageNotAvailableException(): void { $this->expectException(\Sabre\DAV\Exception\ServiceUnavailable::class); $this->view->expects($this->once()) ->method('getFileInfo') - ->willThrowException(new \OCP\Files\StorageNotAvailableException()); + ->willThrowException(new StorageNotAvailableException()); $dir = new Directory($this->view, $this->info); $dir->getChild('.'); } - public function testGetChildThrowInvalidPath() { - $this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class); + public function testGetChildThrowInvalidPath(): void { + $this->expectException(InvalidPath::class); $this->view->expects($this->once()) ->method('verifyPath') - ->willThrowException(new \OCP\Files\InvalidPathException()); + ->willThrowException(new InvalidPathException()); $this->view->expects($this->never()) ->method('getFileInfo'); @@ -273,21 +268,26 @@ class DirectoryTest extends \Test\TestCase { $dir->getChild('.'); } - public function testGetQuotaInfoUnlimited() { + public function testGetQuotaInfoUnlimited(): void { + $this->createUser('user', 'password'); + self::loginAsUser('user'); $mountPoint = $this->createMock(IMountPoint::class); - $storage = $this->getMockBuilder(Quota::class) - ->disableOriginalConstructor() - ->getMock(); + $storage = $this->createMock(Quota::class); $mountPoint->method('getStorage') ->willReturn($storage); $storage->expects($this->any()) ->method('instanceOfStorage') ->willReturnMap([ - '\OCA\Files_Sharing\SharedStorage' => false, - '\OC\Files\Storage\Wrapper\Quota' => false, + ['\OCA\Files_Sharing\SharedStorage', false], + ['\OC\Files\Storage\Wrapper\Quota', false], + [Storage::class, false], ]); + $storage->expects($this->once()) + ->method('getOwner') + ->willReturn('user'); + $storage->expects($this->never()) ->method('getQuota'); @@ -295,6 +295,10 @@ class DirectoryTest extends \Test\TestCase { ->method('free_space') ->willReturn(800); + $this->info->expects($this->any()) + ->method('getPath') + ->willReturn('/admin/files/foo'); + $this->info->expects($this->once()) ->method('getSize') ->willReturn(200); @@ -303,6 +307,14 @@ class DirectoryTest extends \Test\TestCase { ->method('getMountPoint') ->willReturn($mountPoint); + $this->view->expects($this->any()) + ->method('getRelativePath') + ->willReturn('/foo'); + + $this->info->expects($this->once()) + ->method('getInternalPath') + ->willReturn('/foo'); + $mountPoint->method('getMountPoint') ->willReturn('/user/files/mymountpoint'); @@ -310,11 +322,11 @@ class DirectoryTest extends \Test\TestCase { $this->assertEquals([200, -3], $dir->getQuotaInfo()); //200 used, unlimited } - public function testGetQuotaInfoSpecific() { + public function testGetQuotaInfoSpecific(): void { + $this->createUser('user', 'password'); + self::loginAsUser('user'); $mountPoint = $this->createMock(IMountPoint::class); - $storage = $this->getMockBuilder(Quota::class) - ->disableOriginalConstructor() - ->getMock(); + $storage = $this->createMock(Quota::class); $mountPoint->method('getStorage') ->willReturn($storage); @@ -323,9 +335,14 @@ class DirectoryTest extends \Test\TestCase { ->willReturnMap([ ['\OCA\Files_Sharing\SharedStorage', false], ['\OC\Files\Storage\Wrapper\Quota', true], + [Storage::class, false], ]); $storage->expects($this->once()) + ->method('getOwner') + ->willReturn('user'); + + $storage->expects($this->once()) ->method('getQuota') ->willReturn(1000); @@ -341,46 +358,48 @@ class DirectoryTest extends \Test\TestCase { ->method('getMountPoint') ->willReturn($mountPoint); + $this->info->expects($this->once()) + ->method('getInternalPath') + ->willReturn('/foo'); + $mountPoint->method('getMountPoint') ->willReturn('/user/files/mymountpoint'); + $this->view->expects($this->any()) + ->method('getRelativePath') + ->willReturn('/foo'); + $dir = new Directory($this->view, $this->info); $this->assertEquals([200, 800], $dir->getQuotaInfo()); //200 used, 800 free } - /** - * @dataProvider moveFailedProvider - */ - public function testMoveFailed($source, $destination, $updatables, $deletables) { + #[\PHPUnit\Framework\Attributes\DataProvider('moveFailedProvider')] + public function testMoveFailed(string $source, string $destination, array $updatables, array $deletables): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->moveTest($source, $destination, $updatables, $deletables); } - /** - * @dataProvider moveSuccessProvider - */ - public function testMoveSuccess($source, $destination, $updatables, $deletables) { + #[\PHPUnit\Framework\Attributes\DataProvider('moveSuccessProvider')] + public function testMoveSuccess(string $source, string $destination, array $updatables, array $deletables): void { $this->moveTest($source, $destination, $updatables, $deletables); $this->addToAssertionCount(1); } - /** - * @dataProvider moveFailedInvalidCharsProvider - */ - public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) { - $this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class); + #[\PHPUnit\Framework\Attributes\DataProvider('moveFailedInvalidCharsProvider')] + public function testMoveFailedInvalidChars(string $source, string $destination, array $updatables, array $deletables): void { + $this->expectException(InvalidPath::class); $this->moveTest($source, $destination, $updatables, $deletables); } - public function moveFailedInvalidCharsProvider() { + public static function moveFailedInvalidCharsProvider(): array { return [ - ['a/b', 'a/*', ['a' => true, 'a/b' => true, 'a/c*' => false], []], + ['a/valid', "a/i\nvalid", ['a' => true, 'a/valid' => true, 'a/c*' => false], []], ]; } - public function moveFailedProvider() { + public static function moveFailedProvider(): array { return [ ['a/b', 'a/c', ['a' => false, 'a/b' => false, 'a/c' => false], []], ['a/b', 'b/b', ['a' => false, 'a/b' => false, 'b' => false, 'b/b' => false], []], @@ -391,7 +410,7 @@ class DirectoryTest extends \Test\TestCase { ]; } - public function moveSuccessProvider() { + public static function moveSuccessProvider(): array { return [ ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => true]], // older files with special chars can still be renamed to valid names @@ -399,12 +418,7 @@ class DirectoryTest extends \Test\TestCase { ]; } - /** - * @param $source - * @param $destination - * @param $updatables - */ - private function moveTest($source, $destination, $updatables, $deletables) { + private function moveTest(string $source, string $destination, array $updatables, array $deletables): void { $view = new TestViewDirectory($updatables, $deletables); $sourceInfo = new FileInfo($source, null, null, [ @@ -416,7 +430,7 @@ class DirectoryTest extends \Test\TestCase { $sourceNode = new Directory($view, $sourceInfo); $targetNode = $this->getMockBuilder(Directory::class) - ->setMethods(['childExists']) + ->onlyMethods(['childExists']) ->setConstructorArgs([$view, $targetInfo]) ->getMock(); $targetNode->expects($this->any())->method('childExists') @@ -426,7 +440,7 @@ class DirectoryTest extends \Test\TestCase { } - public function testFailingMove() { + public function testFailingMove(): void { $this->expectException(\Sabre\DAV\Exception\Forbidden::class); $this->expectExceptionMessage('Could not copy directory b, target exists'); @@ -442,7 +456,7 @@ class DirectoryTest extends \Test\TestCase { $sourceNode = new Directory($view, $sourceInfo); $targetNode = $this->getMockBuilder(Directory::class) - ->setMethods(['childExists']) + ->onlyMethods(['childExists']) ->setConstructorArgs([$view, $targetInfo]) ->getMock(); $targetNode->expects($this->once())->method('childExists') |