2013-09-26 12:50:15 +04:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
* @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/>
|
|
|
|
*
|
2013-09-26 12:50:15 +04:00
|
|
|
*/
|
|
|
|
|
2016-05-25 17:04:15 +03:00
|
|
|
namespace OCA\DAV\Tests\unit\Connector\Sabre;
|
2013-09-26 12:50:15 +04:00
|
|
|
|
|
|
|
|
2014-03-03 17:27:24 +04:00
|
|
|
use OC\Files\FileInfo;
|
2017-03-29 13:15:42 +03:00
|
|
|
use OC\Files\Filesystem;
|
2017-10-25 01:03:28 +03:00
|
|
|
use OC\Files\Mount\Manager;
|
2015-06-11 18:25:06 +03:00
|
|
|
use OC\Files\Storage\Temporary;
|
2017-03-29 13:15:42 +03:00
|
|
|
use OC\Files\View;
|
|
|
|
use OCA\DAV\Connector\Sabre\Directory;
|
|
|
|
use OCA\DAV\Connector\Sabre\ObjectTree;
|
2013-09-26 12:50:15 +04:00
|
|
|
|
2015-11-27 18:26:16 +03:00
|
|
|
/**
|
2016-05-25 17:04:15 +03:00
|
|
|
* Class ObjectTreeTest
|
2015-11-27 18:26:16 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package OCA\DAV\Tests\Unit\Connector\Sabre
|
|
|
|
*/
|
2016-05-25 17:04:15 +03:00
|
|
|
class ObjectTreeTest extends \Test\TestCase {
|
2013-09-26 12:50:15 +04:00
|
|
|
|
2017-03-29 13:15:42 +03: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
|
|
|
|
*/
|
|
|
|
public function testCopy($sourcePath, $targetPath, $targetParent) {
|
|
|
|
$view = $this->createMock(View::class);
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('verifyPath')
|
|
|
|
->with($targetParent)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$view->expects($this->once())
|
2017-04-27 10:53:55 +03:00
|
|
|
->method('file_exists')
|
|
|
|
->with($targetPath)
|
|
|
|
->willReturn(false);
|
2017-03-29 13:15:42 +03:00
|
|
|
$view->expects($this->once())
|
|
|
|
->method('copy')
|
|
|
|
->with($sourcePath, $targetPath)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2017-04-27 10:53:55 +03: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 13:15:42 +03: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))
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
/** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
|
|
|
|
$mountManager = Filesystem::getMountManager();
|
|
|
|
$objectTree->init($rootDir, $view, $mountManager);
|
|
|
|
$objectTree->copy($sourcePath, $targetPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider copyDataProvider
|
|
|
|
*/
|
|
|
|
public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\Sabre\DAV\Exception\Forbidden::class);
|
|
|
|
|
2017-03-29 13:15:42 +03:00
|
|
|
$view = $this->createMock(View::class);
|
2017-04-27 10:53:55 +03:00
|
|
|
$view->expects($this->never())
|
|
|
|
->method('verifyPath');
|
2017-03-29 13:15:42 +03:00
|
|
|
$view->expects($this->once())
|
2017-04-27 10:53:55 +03:00
|
|
|
->method('file_exists')
|
|
|
|
->with($targetPath)
|
|
|
|
->willReturn(false);
|
2017-03-29 13:15:42 +03:00
|
|
|
$view->expects($this->never())
|
|
|
|
->method('copy');
|
|
|
|
|
2017-04-27 10:53:55 +03: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 13:15:42 +03:00
|
|
|
|
|
|
|
$rootDir = new Directory($view, $info);
|
|
|
|
$objectTree = $this->getMockBuilder(ObjectTree::class)
|
|
|
|
->setMethods(['nodeExists', 'getNodeForPath'])
|
|
|
|
->setConstructorArgs([$rootDir, $view])
|
|
|
|
->getMock();
|
|
|
|
|
2017-04-27 10:53:55 +03:00
|
|
|
$objectTree->expects($this->never())
|
|
|
|
->method('getNodeForPath');
|
2017-03-29 13:15:42 +03:00
|
|
|
|
|
|
|
/** @var $objectTree \OCA\DAV\Connector\Sabre\ObjectTree */
|
|
|
|
$mountManager = Filesystem::getMountManager();
|
|
|
|
$objectTree->init($rootDir, $view, $mountManager);
|
|
|
|
$objectTree->copy($sourcePath, $targetPath);
|
|
|
|
}
|
|
|
|
|
2015-02-12 14:29:01 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider nodeForPathProvider
|
|
|
|
*/
|
|
|
|
public function testGetNodeForPath(
|
2015-06-11 18:25:06 +03:00
|
|
|
$inputFileName,
|
|
|
|
$fileInfoQueryPath,
|
|
|
|
$outputFileName,
|
|
|
|
$type,
|
|
|
|
$enableChunkingHeader
|
2015-02-12 14:29:01 +03:00
|
|
|
) {
|
|
|
|
|
|
|
|
if ($enableChunkingHeader) {
|
|
|
|
$_SERVER['HTTP_OC_CHUNKED'] = true;
|
|
|
|
}
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$rootNode = $this->getMockBuilder(Directory::class)
|
2015-02-12 14:29:01 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-25 01:03:28 +03:00
|
|
|
$mountManager = $this->getMockBuilder(Manager::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-25 01:03:28 +03:00
|
|
|
$fileInfo = $this->getMockBuilder(FileInfo::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-02-12 14:29:01 +03:00
|
|
|
$fileInfo->expects($this->once())
|
|
|
|
->method('getType')
|
|
|
|
->will($this->returnValue($type));
|
|
|
|
$fileInfo->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue($outputFileName));
|
2017-12-01 14:34:37 +03:00
|
|
|
$fileInfo->method('getStorage')
|
|
|
|
->willReturn($this->createMock(\OC\Files\Storage\Common::class));
|
2015-02-12 14:29:01 +03:00
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with($fileInfoQueryPath)
|
|
|
|
->will($this->returnValue($fileInfo));
|
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
|
2015-02-12 14:29:01 +03:00
|
|
|
$tree->init($rootNode, $view, $mountManager);
|
|
|
|
|
|
|
|
$node = $tree->getNodeForPath($inputFileName);
|
|
|
|
|
|
|
|
$this->assertNotNull($node);
|
|
|
|
$this->assertEquals($outputFileName, $node->getName());
|
|
|
|
|
|
|
|
if ($type === 'file') {
|
2015-08-30 20:13:01 +03:00
|
|
|
$this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\File);
|
2015-02-12 14:29:01 +03:00
|
|
|
} else {
|
2015-08-30 20:13:01 +03:00
|
|
|
$this->assertTrue($node instanceof \OCA\DAV\Connector\Sabre\Directory);
|
2015-02-12 14:29:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
unset($_SERVER['HTTP_OC_CHUNKED']);
|
|
|
|
}
|
|
|
|
|
|
|
|
function nodeForPathProvider() {
|
|
|
|
return array(
|
|
|
|
// 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
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
|
2015-06-11 18:25:06 +03:00
|
|
|
public function testGetNodeForPathInvalidPath() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCA\DAV\Connector\Sabre\Exception\InvalidPath::class);
|
|
|
|
|
2015-06-11 18:25:06 +03:00
|
|
|
$path = '/foo\bar';
|
|
|
|
|
|
|
|
|
|
|
|
$storage = new Temporary([]);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['resolvePath'])
|
|
|
|
->getMock();
|
2015-06-11 18:25:06 +03:00
|
|
|
$view->expects($this->once())
|
|
|
|
->method('resolvePath')
|
|
|
|
->will($this->returnCallback(function($path) use ($storage){
|
|
|
|
return [$storage, ltrim($path, '/')];
|
|
|
|
}));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$rootNode = $this->getMockBuilder(Directory::class)
|
2015-06-11 18:25:06 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-25 01:03:28 +03:00
|
|
|
$mountManager = $this->getMockBuilder(Manager::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2015-06-11 18:25:06 +03:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
|
2015-06-11 18:25:06 +03:00
|
|
|
$tree->init($rootNode, $view, $mountManager);
|
|
|
|
|
|
|
|
$tree->getNodeForPath($path);
|
|
|
|
}
|
2015-06-22 17:49:04 +03:00
|
|
|
|
2015-06-11 18:25:06 +03:00
|
|
|
public function testGetNodeForPathRoot() {
|
|
|
|
$path = '/';
|
|
|
|
|
|
|
|
|
|
|
|
$storage = new Temporary([]);
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->setMethods(['resolvePath'])
|
|
|
|
->getMock();
|
2015-06-11 18:25:06 +03:00
|
|
|
$view->expects($this->any())
|
|
|
|
->method('resolvePath')
|
|
|
|
->will($this->returnCallback(function ($path) use ($storage) {
|
|
|
|
return [$storage, ltrim($path, '/')];
|
|
|
|
}));
|
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$rootNode = $this->getMockBuilder(Directory::class)
|
2015-06-11 18:25:06 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-25 01:03:28 +03:00
|
|
|
$mountManager = $this->getMockBuilder(Manager::class)
|
2016-07-15 10:52:46 +03:00
|
|
|
->getMock();
|
2015-06-11 18:25:06 +03:00
|
|
|
|
2015-08-30 20:13:01 +03:00
|
|
|
$tree = new \OCA\DAV\Connector\Sabre\ObjectTree();
|
2015-06-11 18:25:06 +03:00
|
|
|
$tree->init($rootNode, $view, $mountManager);
|
|
|
|
|
2015-06-22 17:49:04 +03:00
|
|
|
$this->assertInstanceOf('\Sabre\DAV\INode', $tree->getNodeForPath($path));
|
2015-06-11 18:25:06 +03:00
|
|
|
}
|
2013-09-26 12:50:15 +04:00
|
|
|
}
|