2013-09-26 04:50:15 -04:00
|
|
|
<?php
|
2024-05-28 06:34:11 -04:00
|
|
|
|
2013-09-26 04:50:15 -04:00
|
|
|
/**
|
2024-05-28 06:34:11 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-09-26 04:50:15 -04:00
|
|
|
*/
|
2016-05-25 10:04:15 -04:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2013-09-26 04:50:15 -04:00
|
|
|
|
2014-03-03 08:27:24 -05:00
|
|
|
use OC\Files\FileInfo;
|
2017-03-29 06:15:42 -04:00
|
|
|
use OC\Files\Filesystem;
|
2017-10-24 18:03:28 -04:00
|
|
|
use OC\Files\Mount\Manager;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OC\Files\Storage\Common;
|
2015-06-11 11:25:06 -04:00
|
|
|
use OC\Files\Storage\Temporary;
|
2017-03-29 06:15:42 -04:00
|
|
|
use OC\Files\View;
|
|
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
2024-10-10 06:40:31 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\Exception\InvalidPath;
|
|
|
|
|
use OCA\DAV\Connector\Sabre\File;
|
2017-03-29 06:15:42 -04:00
|
|
|
use OCA\DAV\Connector\Sabre\ObjectTree;
|
2022-02-23 12:29:08 -05:00
|
|
|
use OCP\Files\Mount\IMountManager;
|
2013-09-26 04:50:15 -04:00
|
|
|
|
2015-11-27 10:26:16 -05:00
|
|
|
/**
|
2016-05-25 10:04:15 -04:00
|
|
|
* Class ObjectTreeTest
|
2015-11-27 10:26:16 -05:00
|
|
|
*
|
|
|
|
|
* @group DB
|
|
|
|
|
*
|
|
|
|
|
* @package OCA\DAV\Tests\Unit\Connector\Sabre
|
|
|
|
|
*/
|
2016-05-25 10:04:15 -04:00
|
|
|
class ObjectTreeTest extends \Test\TestCase {
|
2017-03-29 06:15:42 -04:00
|
|
|
public function copyDataProvider() {
|
|
|
|
|
return [
|
|
|
|
|
// copy into same dir
|
|
|
|
|
['a', 'b', ''],
|
|
|
|
|
// copy into same dir
|
|
|
|
|
['a/a', 'a/b', 'a'],
|
|
|
|
|
// copy into another dir
|
|
|
|
|
['a', 'sub/a', 'sub'],
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider copyDataProvider
|
|
|
|
|
*/
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testCopy($sourcePath, $targetPath, $targetParent): void {
|
2017-03-29 06:15:42 -04:00
|
|
|
$view = $this->createMock(View::class);
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
|
->method('verifyPath')
|
2023-03-30 08:50:09 -04:00
|
|
|
->with($targetParent);
|
2017-03-29 06:15:42 -04:00
|
|
|
$view->expects($this->once())
|
2017-04-27 03:53:55 -04:00
|
|
|
->method('file_exists')
|
|
|
|
|
->with($targetPath)
|
|
|
|
|
->willReturn(false);
|
2017-03-29 06:15:42 -04:00
|
|
|
$view->expects($this->once())
|
|
|
|
|
->method('copy')
|
|
|
|
|
->with($sourcePath, $targetPath)
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(true);
|
2017-03-29 06:15:42 -04:00
|
|
|
|
2017-04-27 03:53:55 -04:00
|
|
|
$info = $this->createMock(FileInfo::class);
|
|
|
|
|
$info->expects($this->once())
|
|
|
|
|
->method('isCreatable')
|
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
|
->method('getFileInfo')
|
|
|
|
|
->with($targetParent === '' ? '.' : $targetParent)
|
|
|
|
|
->willReturn($info);
|
2017-03-29 06:15:42 -04:00
|
|
|
|
|
|
|
|
$rootDir = new Directory($view, $info);
|
|
|
|
|
$objectTree = $this->getMockBuilder(ObjectTree::class)
|
|
|
|
|
->setMethods(['nodeExists', 'getNodeForPath'])
|
|
|
|
|
->setConstructorArgs([$rootDir, $view])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$objectTree->expects($this->once())
|
|
|
|
|
->method('getNodeForPath')
|
|
|
|
|
->with($this->identicalTo($sourcePath))
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn(false);
|
2017-03-29 06:15:42 -04:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
/** @var ObjectTree $objectTree */
|
2017-03-29 06:15:42 -04:00
|
|
|
$mountManager = Filesystem::getMountManager();
|
|
|
|
|
$objectTree->init($rootDir, $view, $mountManager);
|
|
|
|
|
$objectTree->copy($sourcePath, $targetPath);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider copyDataProvider
|
|
|
|
|
*/
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent): void {
|
2019-11-27 09:27:18 -05:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
|
|
|
|
|
|
2017-03-29 06:15:42 -04:00
|
|
|
$view = $this->createMock(View::class);
|
2017-04-27 03:53:55 -04:00
|
|
|
$view->expects($this->never())
|
|
|
|
|
->method('verifyPath');
|
2017-03-29 06:15:42 -04:00
|
|
|
$view->expects($this->once())
|
2017-04-27 03:53:55 -04:00
|
|
|
->method('file_exists')
|
|
|
|
|
->with($targetPath)
|
|
|
|
|
->willReturn(false);
|
2017-03-29 06:15:42 -04:00
|
|
|
$view->expects($this->never())
|
|
|
|
|
->method('copy');
|
|
|
|
|
|
2017-04-27 03:53:55 -04:00
|
|
|
$info = $this->createMock(FileInfo::class);
|
|
|
|
|
$info->expects($this->once())
|
|
|
|
|
->method('isCreatable')
|
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
|
->method('getFileInfo')
|
|
|
|
|
->with($targetParent === '' ? '.' : $targetParent)
|
|
|
|
|
->willReturn($info);
|
2017-03-29 06:15:42 -04:00
|
|
|
|
|
|
|
|
$rootDir = new Directory($view, $info);
|
|
|
|
|
$objectTree = $this->getMockBuilder(ObjectTree::class)
|
|
|
|
|
->setMethods(['nodeExists', 'getNodeForPath'])
|
|
|
|
|
->setConstructorArgs([$rootDir, $view])
|
|
|
|
|
->getMock();
|
|
|
|
|
|
2017-04-27 03:53:55 -04:00
|
|
|
$objectTree->expects($this->never())
|
|
|
|
|
->method('getNodeForPath');
|
2017-03-29 06:15:42 -04:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
/** @var ObjectTree $objectTree */
|
2017-03-29 06:15:42 -04:00
|
|
|
$mountManager = Filesystem::getMountManager();
|
|
|
|
|
$objectTree->init($rootDir, $view, $mountManager);
|
|
|
|
|
$objectTree->copy($sourcePath, $targetPath);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 06:29:01 -05:00
|
|
|
/**
|
|
|
|
|
* @dataProvider nodeForPathProvider
|
|
|
|
|
*/
|
|
|
|
|
public function testGetNodeForPath(
|
2015-06-11 11:25:06 -04:00
|
|
|
$inputFileName,
|
|
|
|
|
$fileInfoQueryPath,
|
|
|
|
|
$outputFileName,
|
2024-06-19 08:55:05 -04:00
|
|
|
$type,
|
2023-01-20 02:38:43 -05:00
|
|
|
): void {
|
2017-10-24 09:26:53 -04:00
|
|
|
$rootNode = $this->getMockBuilder(Directory::class)
|
2015-02-12 06:29:01 -05:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2017-10-24 18:03:28 -04:00
|
|
|
$mountManager = $this->getMockBuilder(Manager::class)
|
2016-07-15 03:52:46 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2017-10-24 09:26:53 -04:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 03:52:46 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2017-10-24 18:03:28 -04:00
|
|
|
$fileInfo = $this->getMockBuilder(FileInfo::class)
|
2016-07-15 03:52:46 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2022-04-05 09:30:10 -04:00
|
|
|
$fileInfo->method('getType')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($type);
|
2022-04-05 09:30:10 -04:00
|
|
|
$fileInfo->method('getName')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($outputFileName);
|
2017-12-01 06:34:37 -05:00
|
|
|
$fileInfo->method('getStorage')
|
2024-10-10 06:40:31 -04:00
|
|
|
->willReturn($this->createMock(Common::class));
|
2015-02-12 06:29:01 -05:00
|
|
|
|
2022-04-05 09:30:10 -04:00
|
|
|
$view->method('getFileInfo')
|
2015-02-12 06:29:01 -05:00
|
|
|
->with($fileInfoQueryPath)
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturn($fileInfo);
|
2015-02-12 06:29:01 -05:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$tree = new ObjectTree();
|
2015-02-12 06:29:01 -05:00
|
|
|
$tree->init($rootNode, $view, $mountManager);
|
|
|
|
|
|
|
|
|
|
$node = $tree->getNodeForPath($inputFileName);
|
|
|
|
|
|
|
|
|
|
$this->assertNotNull($node);
|
|
|
|
|
$this->assertEquals($outputFileName, $node->getName());
|
|
|
|
|
|
|
|
|
|
if ($type === 'file') {
|
2024-10-10 06:40:31 -04:00
|
|
|
$this->assertTrue($node instanceof File);
|
2015-02-12 06:29:01 -05:00
|
|
|
} else {
|
2024-10-10 06:40:31 -04:00
|
|
|
$this->assertTrue($node instanceof Directory);
|
2015-02-12 06:29:01 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-10 10:51:06 -04:00
|
|
|
public function nodeForPathProvider() {
|
2020-03-26 04:30:18 -04:00
|
|
|
return [
|
2015-02-12 06:29:01 -05:00
|
|
|
// regular file
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2015-02-12 06:29:01 -05:00
|
|
|
'regularfile.txt',
|
|
|
|
|
'regularfile.txt',
|
|
|
|
|
'regularfile.txt',
|
2023-02-21 01:36:43 -05:00
|
|
|
'file',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2015-02-12 06:29:01 -05:00
|
|
|
// regular directory
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2015-02-12 06:29:01 -05:00
|
|
|
'regulardir',
|
|
|
|
|
'regulardir',
|
|
|
|
|
'regulardir',
|
2023-02-21 01:36:43 -05:00
|
|
|
'dir',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2015-02-12 06:29:01 -05:00
|
|
|
// regular file in subdir
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2015-02-12 06:29:01 -05:00
|
|
|
'subdir/regularfile.txt',
|
|
|
|
|
'subdir/regularfile.txt',
|
|
|
|
|
'regularfile.txt',
|
2023-02-21 01:36:43 -05:00
|
|
|
'file',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
2015-02-12 06:29:01 -05:00
|
|
|
// regular directory in subdir
|
2020-03-26 04:30:18 -04:00
|
|
|
[
|
2015-02-12 06:29:01 -05:00
|
|
|
'subdir/regulardir',
|
|
|
|
|
'subdir/regulardir',
|
|
|
|
|
'regulardir',
|
2023-02-21 01:36:43 -05:00
|
|
|
'dir',
|
2020-03-26 04:30:18 -04:00
|
|
|
],
|
|
|
|
|
];
|
2015-02-12 06:29:01 -05:00
|
|
|
}
|
|
|
|
|
|
2022-02-23 12:29:08 -05:00
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testGetNodeForPathInvalidPath(): void {
|
2024-10-10 06:40:31 -04:00
|
|
|
$this->expectException(InvalidPath::class);
|
2023-02-21 01:36:43 -05:00
|
|
|
|
2015-06-11 11:25:06 -04:00
|
|
|
$path = '/foo\bar';
|
2023-02-21 01:36:43 -05:00
|
|
|
|
|
|
|
|
|
2015-06-11 11:25:06 -04:00
|
|
|
$storage = new Temporary([]);
|
2023-02-21 01:36:43 -05:00
|
|
|
|
2017-10-24 09:26:53 -04:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 03:52:46 -04:00
|
|
|
->setMethods(['resolvePath'])
|
|
|
|
|
->getMock();
|
2015-06-11 11:25:06 -04:00
|
|
|
$view->expects($this->once())
|
|
|
|
|
->method('resolvePath')
|
2020-04-09 07:53:40 -04:00
|
|
|
->willReturnCallback(function ($path) use ($storage) {
|
2015-06-11 11:25:06 -04:00
|
|
|
return [$storage, ltrim($path, '/')];
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2015-06-11 11:25:06 -04:00
|
|
|
|
2023-02-21 01:36:43 -05:00
|
|
|
$rootNode = $this->getMockBuilder(Directory::class)
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
$mountManager = $this->createMock(IMountManager::class);
|
|
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$tree = new ObjectTree();
|
2015-06-11 11:25:06 -04:00
|
|
|
$tree->init($rootNode, $view, $mountManager);
|
2023-02-21 01:36:43 -05:00
|
|
|
|
2015-06-11 11:25:06 -04:00
|
|
|
$tree->getNodeForPath($path);
|
|
|
|
|
}
|
2015-06-22 10:49:04 -04:00
|
|
|
|
2023-01-20 02:38:43 -05:00
|
|
|
public function testGetNodeForPathRoot(): void {
|
2015-06-11 11:25:06 -04:00
|
|
|
$path = '/';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$storage = new Temporary([]);
|
|
|
|
|
|
2017-10-24 09:26:53 -04:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 03:52:46 -04:00
|
|
|
->setMethods(['resolvePath'])
|
|
|
|
|
->getMock();
|
2015-06-11 11:25:06 -04:00
|
|
|
$view->expects($this->any())
|
|
|
|
|
->method('resolvePath')
|
2020-03-25 17:21:27 -04:00
|
|
|
->willReturnCallback(function ($path) use ($storage) {
|
2015-06-11 11:25:06 -04:00
|
|
|
return [$storage, ltrim($path, '/')];
|
2020-03-25 17:21:27 -04:00
|
|
|
});
|
2015-06-11 11:25:06 -04:00
|
|
|
|
2017-10-24 09:26:53 -04:00
|
|
|
$rootNode = $this->getMockBuilder(Directory::class)
|
2015-06-11 11:25:06 -04:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2022-02-23 12:29:08 -05:00
|
|
|
$mountManager = $this->createMock(IMountManager::class);
|
2015-06-11 11:25:06 -04:00
|
|
|
|
2024-10-10 06:40:31 -04:00
|
|
|
$tree = new ObjectTree();
|
2015-06-11 11:25:06 -04:00
|
|
|
$tree->init($rootNode, $view, $mountManager);
|
|
|
|
|
|
2015-06-22 10:49:04 -04:00
|
|
|
$this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path));
|
2015-06-11 11:25:06 -04:00
|
|
|
}
|
2013-09-26 04:50:15 -04:00
|
|
|
}
|