diff options
Diffstat (limited to 'apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php')
-rw-r--r-- | apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php | 228 |
1 files changed, 72 insertions, 156 deletions
diff --git a/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php b/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php index cd575e4ff3c..b07778e4fbd 100644 --- a/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/ObjectTreeTest.php @@ -1,40 +1,24 @@ <?php + +declare(strict_types=1); /** - * @copyright Copyright (c) 2016, ownCloud, Inc. - * - * @author Joas Schilling <coding@schilljs.com> - * @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 <pvince81@owncloud.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; - use OC\Files\FileInfo; use OC\Files\Filesystem; use OC\Files\Mount\Manager; +use OC\Files\Storage\Common; use OC\Files\Storage\Temporary; use OC\Files\View; use OCA\DAV\Connector\Sabre\Directory; +use OCA\DAV\Connector\Sabre\Exception\InvalidPath; +use OCA\DAV\Connector\Sabre\File; use OCA\DAV\Connector\Sabre\ObjectTree; +use OCP\Files\Mount\IMountManager; /** * Class ObjectTreeTest @@ -44,8 +28,7 @@ use OCA\DAV\Connector\Sabre\ObjectTree; * @package OCA\DAV\Tests\Unit\Connector\Sabre */ class ObjectTreeTest extends \Test\TestCase { - - public function copyDataProvider() { + public static function copyDataProvider(): array { return [ // copy into same dir ['a', 'b', ''], @@ -56,15 +39,12 @@ class ObjectTreeTest extends \Test\TestCase { ]; } - /** - * @dataProvider copyDataProvider - */ - public function testCopy($sourcePath, $targetPath, $targetParent) { + #[\PHPUnit\Framework\Attributes\DataProvider('copyDataProvider')] + public function testCopy(string $sourcePath, string $targetPath, string $targetParent): void { $view = $this->createMock(View::class); $view->expects($this->once()) ->method('verifyPath') - ->with($targetParent) - ->will($this->returnValue(true)); + ->with($targetParent); $view->expects($this->once()) ->method('file_exists') ->with($targetPath) @@ -72,7 +52,7 @@ class ObjectTreeTest extends \Test\TestCase { $view->expects($this->once()) ->method('copy') ->with($sourcePath, $targetPath) - ->will($this->returnValue(true)); + ->willReturn(true); $info = $this->createMock(FileInfo::class); $info->expects($this->once()) @@ -86,26 +66,25 @@ class ObjectTreeTest extends \Test\TestCase { $rootDir = new Directory($view, $info); $objectTree = $this->getMockBuilder(ObjectTree::class) - ->setMethods(['nodeExists', 'getNodeForPath']) + ->onlyMethods(['nodeExists', 'getNodeForPath']) ->setConstructorArgs([$rootDir, $view]) ->getMock(); $objectTree->expects($this->once()) ->method('getNodeForPath') ->with($this->identicalTo($sourcePath)) - ->will($this->returnValue(false)); + ->willReturn(false); - /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */ + /** @var ObjectTree $objectTree */ $mountManager = Filesystem::getMountManager(); $objectTree->init($rootDir, $view, $mountManager); $objectTree->copy($sourcePath, $targetPath); } - /** - * @dataProvider copyDataProvider - * @expectedException \Sabre\DAV\Exception\Forbidden - */ - public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) { + #[\PHPUnit\Framework\Attributes\DataProvider('copyDataProvider')] + public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent): void { + $this->expectException(\Sabre\DAV\Exception\Forbidden::class); + $view = $this->createMock(View::class); $view->expects($this->never()) ->method('verifyPath'); @@ -128,61 +107,42 @@ class ObjectTreeTest extends \Test\TestCase { $rootDir = new Directory($view, $info); $objectTree = $this->getMockBuilder(ObjectTree::class) - ->setMethods(['nodeExists', 'getNodeForPath']) + ->onlyMethods(['nodeExists', 'getNodeForPath']) ->setConstructorArgs([$rootDir, $view]) ->getMock(); $objectTree->expects($this->never()) ->method('getNodeForPath'); - /** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */ + /** @var ObjectTree $objectTree */ $mountManager = Filesystem::getMountManager(); $objectTree->init($rootDir, $view, $mountManager); $objectTree->copy($sourcePath, $targetPath); } - /** - * @dataProvider nodeForPathProvider - */ + #[\PHPUnit\Framework\Attributes\DataProvider('nodeForPathProvider')] public function testGetNodeForPath( - $inputFileName, - $fileInfoQueryPath, - $outputFileName, - $type, - $enableChunkingHeader - ) { - - if ($enableChunkingHeader) { - $_SERVER['HTTP_OC_CHUNKED'] = true; - } - - $rootNode = $this->getMockBuilder(Directory::class) - ->disableOriginalConstructor() - ->getMock(); - $mountManager = $this->getMockBuilder(Manager::class) - ->disableOriginalConstructor() - ->getMock(); - $view = $this->getMockBuilder(View::class) - ->disableOriginalConstructor() - ->getMock(); - $fileInfo = $this->getMockBuilder(FileInfo::class) - ->disableOriginalConstructor() - ->getMock(); - $fileInfo->expects($this->once()) - ->method('getType') - ->will($this->returnValue($type)); - $fileInfo->expects($this->once()) - ->method('getName') - ->will($this->returnValue($outputFileName)); + string $inputFileName, + string $fileInfoQueryPath, + string $outputFileName, + string $type, + ): void { + $rootNode = $this->createMock(Directory::class); + $mountManager = $this->createMock(Manager::class); + $view = $this->createMock(View::class); + $fileInfo = $this->createMock(FileInfo::class); + $fileInfo->method('getType') + ->willReturn($type); + $fileInfo->method('getName') + ->willReturn($outputFileName); $fileInfo->method('getStorage') - ->willReturn($this->createMock(\OC\Files\Storage\Common::class)); + ->willReturn($this->createMock(Common::class)); - $view->expects($this->once()) - ->method('getFileInfo') + $view->method('getFileInfo') ->with($fileInfoQueryPath) - ->will($this->returnValue($fileInfo)); + ->willReturn($fileInfo); - $tree = new \OCA\DAV\Connector\Sabre\ObjectTree(); + $tree = new ObjectTree(); $tree->init($rootNode, $view, $mountManager); $node = $tree->getNodeForPath($inputFileName); @@ -191,135 +151,91 @@ class ObjectTreeTest extends \Test\TestCase { $this->assertEquals($outputFileName, $node->getName()); if ($type === 'file') { - $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\File); + $this->assertInstanceOf(File::class, $node); } else { - $this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\Directory); + $this->assertInstanceOf(Directory::class, $node); } - - unset($_SERVER['HTTP_OC_CHUNKED']); } - function nodeForPathProvider() { - return array( + public static function nodeForPathProvider(): array { + return [ // regular file - array( + [ 'regularfile.txt', 'regularfile.txt', 'regularfile.txt', 'file', - false - ), + ], // regular directory - array( - 'regulardir', - 'regulardir', - 'regulardir', - 'dir', - false - ), - // regular file with chunking - array( - 'regularfile.txt', - 'regularfile.txt', - 'regularfile.txt', - 'file', - true - ), - // regular directory with chunking - array( + [ 'regulardir', 'regulardir', 'regulardir', 'dir', - true - ), - // file with chunky file name - array( - 'regularfile.txt-chunking-123566789-10-1', - 'regularfile.txt', - 'regularfile.txt', - 'file', - true - ), + ], // regular file in subdir - array( + [ 'subdir/regularfile.txt', 'subdir/regularfile.txt', 'regularfile.txt', 'file', - false - ), + ], // regular directory in subdir - array( + [ 'subdir/regulardir', 'subdir/regulardir', 'regulardir', 'dir', - false - ), - // file with chunky file name in subdir - array( - 'subdir/regularfile.txt-chunking-123566789-10-1', - 'subdir/regularfile.txt', - 'regularfile.txt', - 'file', - true - ), - ); + ], + ]; } - /** - * @expectedException \OCA\DAV\Connector\Sabre\Exception\InvalidPath - */ - public function testGetNodeForPathInvalidPath() { + + public function testGetNodeForPathInvalidPath(): void { + $this->expectException(InvalidPath::class); + $path = '/foo\bar'; $storage = new Temporary([]); $view = $this->getMockBuilder(View::class) - ->setMethods(['resolvePath']) + ->onlyMethods(['resolvePath']) ->getMock(); $view->expects($this->once()) ->method('resolvePath') - ->will($this->returnCallback(function($path) use ($storage){ - return [$storage, ltrim($path, '/')]; - })); + ->willReturnCallback(function ($path) use ($storage) { + return [$storage, ltrim($path, '/')]; + }); - $rootNode = $this->getMockBuilder(Directory::class) - ->disableOriginalConstructor() - ->getMock(); - $mountManager = $this->getMockBuilder(Manager::class) - ->getMock(); + $rootNode = $this->createMock(Directory::class); + $mountManager = $this->createMock(IMountManager::class); - $tree = new \OCA\DAV\Connector\Sabre\ObjectTree(); + $tree = new ObjectTree(); $tree->init($rootNode, $view, $mountManager); $tree->getNodeForPath($path); } - public function testGetNodeForPathRoot() { + public function testGetNodeForPathRoot(): void { $path = '/'; $storage = new Temporary([]); $view = $this->getMockBuilder(View::class) - ->setMethods(['resolvePath']) + ->onlyMethods(['resolvePath']) ->getMock(); $view->expects($this->any()) ->method('resolvePath') - ->will($this->returnCallback(function ($path) use ($storage) { + ->willReturnCallback(function ($path) use ($storage) { return [$storage, ltrim($path, '/')]; - })); + }); - $rootNode = $this->getMockBuilder(Directory::class) - ->disableOriginalConstructor() - ->getMock(); - $mountManager = $this->getMockBuilder(Manager::class) - ->getMock(); + $rootNode = $this->createMock(Directory::class); + $mountManager = $this->createMock(IMountManager::class); - $tree = new \OCA\DAV\Connector\Sabre\ObjectTree(); + $tree = new ObjectTree(); $tree->init($rootNode, $view, $mountManager); $this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path)); |