namespace OCA\DAV\Tests\Unit\Connector\Sabre;
use OCP\Files\ForbiddenException;
+use OC\Files\FileInfo;
+use OCA\DAV\Connector\Sabre\Directory;
+
+class TestDoubleFileView 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;
+ }
+
+ public function isUpdatable($path) {
+ return $this->updatables[$path];
+ }
+
+ public function isCreatable($path) {
+ return $this->updatables[$path];
+ }
+
+ public function isDeletable($path) {
+ return $this->deletables[$path];
+ }
+
+ public function rename($path1, $path2) {
+ return $this->canRename;
+ }
+
+ public function getRelativePath($path) {
+ return $path;
+ }
+}
+
/**
* @group DB
->method('getPath')
->will($this->returnValue($path));
- return new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
+ return new Directory($this->view, $this->info);
}
/**
->method('getRelativePath')
->will($this->returnValue(''));
- $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
+ $dir = new Directory($this->view, $this->info);
$nodes = $dir->getChildren();
$this->assertEquals(2, count($nodes));
$dir->getChildren();
}
+ /**
+ * @expectedException \Sabre\DAV\Exception\Forbidden
+ */
+ public function testGetChildrenNoPermission() {
+ $this->info->expects($this->any())
+ ->method('isReadable')
+ ->will($this->returnValue(false));
+
+ $dir = new Directory($this->view, $this->info);
+ $dir->getChildren();
+ }
+
+ /**
+ * @expectedException \Sabre\DAV\Exception\NotFound
+ */
+ public function testGetChildNoPermission() {
+ $this->info->expects($this->any())
+ ->method('isReadable')
+ ->will($this->returnValue(false));
+
+ $dir = new Directory($this->view, $this->info);
+ $dir->getChild('test');
+ }
+
/**
* @expectedException \Sabre\DAV\Exception\ServiceUnavailable
*/
->method('getFileInfo')
->willThrowException(new \OCP\Files\StorageNotAvailableException());
- $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
+ $dir = new Directory($this->view, $this->info);
$dir->getChild('.');
}
$this->view->expects($this->never())
->method('getFileInfo');
- $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
+ $dir = new Directory($this->view, $this->info);
$dir->getChild('.');
}
->method('getStorage')
->will($this->returnValue($storage));
- $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
+ $dir = new Directory($this->view, $this->info);
$this->assertEquals([200, -3], $dir->getQuotaInfo()); //200 used, unlimited
}
->method('getStorage')
->will($this->returnValue($storage));
- $dir = new \OCA\DAV\Connector\Sabre\Directory($this->view, $this->info);
+ $dir = new Directory($this->view, $this->info);
$this->assertEquals([200, 800], $dir->getQuotaInfo()); //200 used, 800 free
}
+
+ /**
+ * @dataProvider moveFailedProvider
+ * @expectedException \Sabre\DAV\Exception\Forbidden
+ */
+ public function testMoveFailed($source, $destination, $updatables, $deletables) {
+ $this->moveTest($source, $destination, $updatables, $deletables);
+ }
+
+ /**
+ * @dataProvider moveSuccessProvider
+ */
+ public function testMoveSuccess($source, $destination, $updatables, $deletables) {
+ $this->moveTest($source, $destination, $updatables, $deletables);
+ $this->assertTrue(true);
+ }
+
+ /**
+ * @dataProvider moveFailedInvalidCharsProvider
+ * @expectedException \OCA\DAV\Connector\Sabre\Exception\InvalidPath
+ */
+ public function testMoveFailedInvalidChars($source, $destination, $updatables, $deletables) {
+ $this->moveTest($source, $destination, $updatables, $deletables);
+ }
+
+ public function moveFailedInvalidCharsProvider() {
+ return [
+ ['a/b', 'a/*', ['a' => true, 'a/b' => true, 'a/c*' => false], []],
+ ];
+ }
+
+ public function moveFailedProvider() {
+ 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], []],
+ ['a/b', 'b/b', ['a' => false, 'a/b' => true, 'b' => false, 'b/b' => false], []],
+ ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => false, 'b/b' => false], []],
+ ['a/b', 'b/b', ['a' => true, 'a/b' => true, 'b' => true, 'b/b' => false], ['a/b' => false]],
+ ['a/b', 'a/c', ['a' => false, 'a/b' => true, 'a/c' => false], []],
+ ];
+ }
+
+ public function moveSuccessProvider() {
+ 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
+ ['a/b*', 'b/b', ['a' => true, 'a/b*' => true, 'b' => true, 'b/b' => false], ['a/b*' => true]],
+ ];
+ }
+
+ /**
+ * @param $source
+ * @param $destination
+ * @param $updatables
+ */
+ private function moveTest($source, $destination, $updatables, $deletables) {
+ $view = new TestDoubleFileView($updatables, $deletables);
+
+ $sourceInfo = new FileInfo($source, null, null, [], null);
+ $targetInfo = new FileInfo(dirname($destination), null, null, [], null);
+
+ $sourceNode = new Directory($view, $sourceInfo);
+ $targetNode = $this->getMockBuilder(Directory::class)
+ ->setMethods(['childExists'])
+ ->setConstructorArgs([$view, $targetInfo])
+ ->getMock();
+ $targetNode->expects($this->any())->method('childExists')
+ ->with(basename($destination))
+ ->willReturn(false);
+ $this->assertTrue($targetNode->moveInto(basename($destination), $source, $sourceNode));
+ }
+
+ /**
+ * @expectedException \Sabre\DAV\Exception\Forbidden
+ * @expectedExceptionMessage Could not copy directory b, target exists
+ */
+ public function testFailingMove() {
+ $source = 'a/b';
+ $destination = 'c/b';
+ $updatables = ['a' => true, 'a/b' => true, 'b' => true, 'c/b' => false];
+ $deletables = ['a/b' => true];
+
+ $view = new TestDoubleFileView($updatables, $deletables);
+
+ $sourceInfo = new FileInfo($source, null, null, [], null);
+ $targetInfo = new FileInfo(dirname($destination), null, null, [], null);
+
+ $sourceNode = new Directory($view, $sourceInfo);
+ $targetNode = $this->getMockBuilder(Directory::class)
+ ->setMethods(['childExists'])
+ ->setConstructorArgs([$view, $targetInfo])
+ ->getMock();
+ $targetNode->expects($this->once())->method('childExists')
+ ->with(basename($destination))
+ ->willReturn(true);
+
+ $targetNode->moveInto(basename($destination), $source, $sourceNode);
+ }
}
use OC\Files\FileInfo;
+use OC\Files\Filesystem;
use OC\Files\Storage\Temporary;
+use OC\Files\View;
+use OCA\DAV\Connector\Sabre\Directory;
+use OCA\DAV\Connector\Sabre\ObjectTree;
class TestDoubleFileView extends \OC\Files\View {
$this->moveTest($source, $destination, $updatables, $updatables, $deletables);
}
- function moveFailedInvalidCharsProvider() {
+ public function moveFailedInvalidCharsProvider() {
return array(
array('a/b', 'a/*', array('a' => true, 'a/b' => true, 'a/c*' => false), array()),
);
}
- function moveFailedProvider() {
+ public function moveFailedProvider() {
return array(
array('a/b', 'a/c', array('a' => false, 'a/b' => false, 'a/c' => false), array()),
array('a/b', 'b/b', array('a' => false, 'a/b' => false, 'b' => false, 'b/b' => false), array()),
);
}
- function moveSuccessProvider() {
+ public function moveSuccessProvider() {
return array(
array('a/b', 'b/b', array('a' => true, 'a/b' => true, 'b' => true, 'b/b' => false), array('a/b' => true)),
// older files with special chars can still be renamed to valid names
$objectTree->move($source, $destination);
}
+ 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())
+ ->method('isCreatable')
+ ->with($targetParent)
+ ->will($this->returnValue(true));
+ $view->expects($this->once())
+ ->method('copy')
+ ->with($sourcePath, $targetPath)
+ ->will($this->returnValue(true));
+
+ $info = new FileInfo('', null, null, [], null);
+
+ $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
+ * @expectedException \Sabre\DAV\Exception\Forbidden
+ */
+ public function testCopyFailNotCreatable($sourcePath, $targetPath, $targetParent) {
+ $view = $this->createMock(View::class);
+ $view->expects($this->once())
+ ->method('verifyPath')
+ ->with($targetParent)
+ ->will($this->returnValue(true));
+ $view->expects($this->once())
+ ->method('isCreatable')
+ ->with($targetParent)
+ ->will($this->returnValue(false));
+ $view->expects($this->never())
+ ->method('copy');
+
+ $info = new FileInfo('', null, null, [], null);
+
+ $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 nodeForPathProvider
*/