Merge pull request #3030 from nextcloud/filesapi-renamefailcase

Fix files node API failed rename/copy
This commit is contained in:
Morris Jobke 2017-01-24 21:23:59 -06:00 committed by GitHub
commit 95ab46e3d2
6 changed files with 565 additions and 641 deletions

View File

@ -29,6 +29,16 @@ namespace OC\Files\Node;
use OCP\Files\NotPermittedException;
class File extends Node implements \OCP\Files\File {
/**
* Creates a Folder that represents a non-existing path
*
* @param string $path path
* @return string non-existing node class
*/
protected function createNonExistingNode($path) {
return new NonExistingFile($this->root, $this->view, $path);
}
/**
* @return string
* @throws \OCP\Files\NotPermittedException
@ -113,52 +123,6 @@ class File extends Node implements \OCP\Files\File {
}
}
/**
* @param string $targetPath
* @throws \OCP\Files\NotPermittedException
* @return \OC\Files\Node\Node
*/
public function copy($targetPath) {
$targetPath = $this->normalizePath($targetPath);
$parent = $this->root->get(dirname($targetPath));
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
$nonExisting = new NonExistingFile($this->root, $this->view, $targetPath);
$this->root->emit('\OC\Files', 'preCopy', array($this, $nonExisting));
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
$this->view->copy($this->path, $targetPath);
$targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postCopy', array($this, $targetNode));
$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
return $targetNode;
} else {
throw new NotPermittedException();
}
}
/**
* @param string $targetPath
* @throws \OCP\Files\NotPermittedException
* @return \OC\Files\Node\Node
*/
public function move($targetPath) {
$targetPath = $this->normalizePath($targetPath);
$parent = $this->root->get(dirname($targetPath));
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
$nonExisting = new NonExistingFile($this->root, $this->view, $targetPath);
$this->root->emit('\OC\Files', 'preRename', array($this, $nonExisting));
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
$this->view->rename($this->path, $targetPath);
$targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
$this->path = $targetPath;
$this->fileInfo = null;
return $targetNode;
} else {
throw new NotPermittedException();
}
}
/**
* @param string $type
* @param bool $raw

View File

@ -35,6 +35,16 @@ use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
class Folder extends Node implements \OCP\Files\Folder {
/**
* Creates a Folder that represents a non-existing path
*
* @param string $path path
* @return string non-existing node class
*/
protected function createNonExistingNode($path) {
return new NonExistingFolder($this->root, $this->view, $path);
}
/**
* @param string $path path relative to the folder
* @return string
@ -325,51 +335,6 @@ class Folder extends Node implements \OCP\Files\Folder {
}
}
/**
* @param string $targetPath
* @throws \OCP\Files\NotPermittedException
* @return \OC\Files\Node\Node
*/
public function copy($targetPath) {
$targetPath = $this->normalizePath($targetPath);
$parent = $this->root->get(dirname($targetPath));
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
$nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
$this->root->emit('\OC\Files', 'preCopy', array($this, $nonExisting));
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
$this->view->copy($this->path, $targetPath);
$targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postCopy', array($this, $targetNode));
$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
return $targetNode;
} else {
throw new NotPermittedException('No permission to copy to path');
}
}
/**
* @param string $targetPath
* @throws \OCP\Files\NotPermittedException
* @return \OC\Files\Node\Node
*/
public function move($targetPath) {
$targetPath = $this->normalizePath($targetPath);
$parent = $this->root->get(dirname($targetPath));
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
$nonExisting = new NonExistingFolder($this->root, $this->view, $targetPath);
$this->root->emit('\OC\Files', 'preRename', array($this, $nonExisting));
$this->root->emit('\OC\Files', 'preWrite', array($nonExisting));
$this->view->rename($this->path, $targetPath);
$targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postRename', array($this, $targetNode));
$this->root->emit('\OC\Files', 'postWrite', array($targetNode));
$this->path = $targetPath;
return $targetNode;
} else {
throw new NotPermittedException('No permission to move to path');
}
}
/**
* Add a suffix to the name in case the file exists
*

View File

@ -33,6 +33,7 @@ use OCP\Files\InvalidPathException;
use OCP\Files\NotFoundException;
use OCP\Files\NotPermittedException;
// FIXME: this class really should be abstract
class Node implements \OCP\Files\Node {
/**
* @var \OC\Files\View $view
@ -56,7 +57,7 @@ class Node implements \OCP\Files\Node {
/**
* @param \OC\Files\View $view
* @param \OC\Files\Node\Root $root
* @param \OCP\Files\IRootFolder $root
* @param string $path
* @param FileInfo $fileInfo
*/
@ -67,6 +68,16 @@ class Node implements \OCP\Files\Node {
$this->fileInfo = $fileInfo;
}
/**
* Creates a Node of the same type that represents a non-existing path
*
* @param string $path path
* @return string non-existing node class
*/
protected function createNonExistingNode($path) {
throw new \Exception('Must be implemented by subclasses');
}
/**
* Returns the matching file info
*
@ -106,27 +117,10 @@ class Node implements \OCP\Files\Node {
return ($this->getPermissions() & $permissions) === $permissions;
}
/**
* @param string $targetPath
* @throws \OCP\Files\NotPermittedException
* @return \OC\Files\Node\Node
*/
public function move($targetPath) {
return;
}
public function delete() {
return;
}
/**
* @param string $targetPath
* @return \OC\Files\Node\Node
*/
public function copy($targetPath) {
return;
}
/**
* @param int $mtime
* @throws \OCP\Files\NotPermittedException
@ -381,4 +375,54 @@ class Node implements \OCP\Files\Node {
public function unlock($type) {
$this->view->unlockFile($this->path, $type);
}
/**
* @param string $targetPath
* @throws \OCP\Files\NotPermittedException if copy not allowed or failed
* @return \OC\Files\Node\Node
*/
public function copy($targetPath) {
$targetPath = $this->normalizePath($targetPath);
$parent = $this->root->get(dirname($targetPath));
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
$nonExisting = $this->createNonExistingNode($targetPath);
$this->root->emit('\OC\Files', 'preCopy', [$this, $nonExisting]);
$this->root->emit('\OC\Files', 'preWrite', [$nonExisting]);
if (!$this->view->copy($this->path, $targetPath)) {
throw new NotPermittedException('Could not copy ' . $this->path . ' to ' . $targetPath);
}
$targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postCopy', [$this, $targetNode]);
$this->root->emit('\OC\Files', 'postWrite', [$targetNode]);
return $targetNode;
} else {
throw new NotPermittedException('No permission to copy to path ' . $targetPath);
}
}
/**
* @param string $targetPath
* @throws \OCP\Files\NotPermittedException if move not allowed or failed
* @return \OC\Files\Node\Node
*/
public function move($targetPath) {
$targetPath = $this->normalizePath($targetPath);
$parent = $this->root->get(dirname($targetPath));
if ($parent instanceof Folder and $this->isValidPath($targetPath) and $parent->isCreatable()) {
$nonExisting = $this->createNonExistingNode($targetPath);
$this->root->emit('\OC\Files', 'preRename', [$this, $nonExisting]);
$this->root->emit('\OC\Files', 'preWrite', [$nonExisting]);
if (!$this->view->rename($this->path, $targetPath)) {
throw new NotPermittedException('Could not move ' . $this->path . ' to ' . $targetPath);
}
$targetNode = $this->root->get($targetPath);
$this->root->emit('\OC\Files', 'postRename', [$this, $targetNode]);
$this->root->emit('\OC\Files', 'postWrite', [$targetNode]);
$this->path = $targetPath;
return $targetNode;
} else {
throw new NotPermittedException('No permission to move to path ' . $targetPath);
}
}
}

View File

@ -8,161 +8,28 @@
namespace Test\Files\Node;
use OC\Files\FileInfo;
use OCP\Files\NotFoundException;
use OCP\ILogger;
use OCP\IUserManager;
class FileTest extends \Test\TestCase {
/** @var \OC\User\User */
private $user;
/** @var \OC\Files\Mount\Manager */
private $manager;
/** @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject */
private $view;
/** @var \OCP\Files\Config\IUserMountCache|\PHPUnit_Framework_MockObject_MockObject */
private $userMountCache;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $userManager;
protected function setUp() {
parent::setUp();
$config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config);
$this->manager = $this->getMockBuilder('\OC\Files\Mount\Manager')
->disableOriginalConstructor()
->getMock();
$this->view = $this->getMockBuilder('\OC\Files\View')
->disableOriginalConstructor()
->getMock();
$this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
->disableOriginalConstructor()
->getMock();
$this->logger = $this->createMock(ILogger::class);
$this->userManager = $this->createMock(IUserManager::class);
/**
* Class FileTest
*
* @group DB
*
* @package Test\Files\Node
*/
class FileTest extends NodeTest {
protected function createTestNode($root, $view, $path) {
return new \OC\Files\Node\File($root, $view, $path);
}
protected function getMockStorage() {
$storage = $this->getMockBuilder('\OCP\Files\Storage')
->getMock();
$storage->expects($this->any())
->method('getId')
->will($this->returnValue('home::someuser'));
return $storage;
protected function getNodeClass() {
return '\OC\Files\Node\File';
}
protected function getFileInfo($data) {
return new FileInfo('', $this->getMockStorage(), '', $data, null);
protected function getNonExistingNodeClass() {
return '\OC\Files\Node\NonExistingFile';
}
public function testDelete() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$root->expects($this->exactly(2))
->method('emit')
->will($this->returnValue(true));
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$this->view->expects($this->once())
->method('unlink')
->with('/bar/foo')
->will($this->returnValue(true));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->delete();
}
public function testDeleteHooks() {
$test = $this;
$hooksRun = 0;
/**
* @param \OC\Files\Node\File $node
*/
$preListener = function ($node) use (&$test, &$hooksRun) {
$test->assertInstanceOf('\OC\Files\Node\File', $node);
$test->assertEquals('foo', $node->getInternalPath());
$test->assertEquals('/bar/foo', $node->getPath());
$test->assertEquals(1, $node->getId());
$hooksRun++;
};
/**
* @param \OC\Files\Node\File $node
*/
$postListener = function ($node) use (&$test, &$hooksRun) {
$test->assertInstanceOf('\OC\Files\Node\NonExistingFile', $node);
$test->assertEquals('foo', $node->getInternalPath());
$test->assertEquals('/bar/foo', $node->getPath());
$test->assertEquals(1, $node->getId());
$test->assertEquals('text/plain', $node->getMimeType());
$hooksRun++;
};
$root = new \OC\Files\Node\Root(
$this->manager,
$this->view,
$this->user,
$this->userMountCache,
$this->logger,
$this->userManager
);
$root->listen('\OC\Files', 'preDelete', $preListener);
$root->listen('\OC\Files', 'postDelete', $postListener);
$this->view->expects($this->any())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain'))));
$this->view->expects($this->once())
->method('unlink')
->with('/bar/foo')
->will($this->returnValue(true));
$this->view->expects($this->any())
->method('resolvePath')
->with('/bar/foo')
->will($this->returnValue(array(null, 'foo')));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->delete();
$this->assertEquals(2, $hooksRun);
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testDeleteNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->delete();
protected function getViewDeleteMethod() {
return 'unlink';
}
public function testGetContent() {
@ -421,224 +288,5 @@ class FileTest extends \Test\TestCase {
$node->fopen('w');
}
public function testCopySameStorage() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$this->view->expects($this->any())
->method('copy')
->with('/bar/foo', '/bar/asd');
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$newNode = new \OC\Files\Node\File($root, $this->view, '/bar/asd');
$root->expects($this->exactly(2))
->method('get')
->will($this->returnValueMap(array(
array('/bar/asd', $newNode),
array('/bar', $parentNode)
)));
$target = $node->copy('/bar/asd');
$this->assertInstanceOf('\OC\Files\Node\File', $target);
$this->assertEquals(3, $target->getId());
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testCopyNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
->disableOriginalConstructor()
->getMock();
$root->expects($this->never())
->method('getMount');
$storage->expects($this->never())
->method('copy');
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->will($this->returnValueMap(array(
array('/bar', $parentNode)
)));
$node->copy('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotFoundException
*/
public function testCopyNoParent() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$this->view->expects($this->never())
->method('copy');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$root->expects($this->once())
->method('get')
->with('/bar/asd')
->will($this->throwException(new NotFoundException()));
$node->copy('/bar/asd/foo');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testCopyParentIsFile() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$this->view->expects($this->never())
->method('copy');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\File($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->will($this->returnValueMap(array(
array('/bar', $parentNode)
)));
$node->copy('/bar/asd');
}
public function testMoveSameStorage() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$this->view->expects($this->any())
->method('rename')
->with('/bar/foo', '/bar/asd');
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$root->expects($this->any())
->method('get')
->will($this->returnValueMap(array(array('/bar', $parentNode), array('/bar/asd', $node))));
$target = $node->move('/bar/asd');
$this->assertInstanceOf('\OC\Files\Node\File', $target);
$this->assertEquals(1, $target->getId());
$this->assertEquals('/bar/asd', $node->getPath());
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testMoveNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$this->view->expects($this->never())
->method('rename');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->with('/bar')
->will($this->returnValue($parentNode));
$node->move('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotFoundException
*/
public function testMoveNoParent() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
->disableOriginalConstructor()
->getMock();
$storage->expects($this->never())
->method('rename');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->with('/bar')
->will($this->throwException(new NotFoundException()));
$node->move('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testMoveParentIsFile() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$this->view->expects($this->never())
->method('rename');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\File($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->with('/bar')
->will($this->returnValue($parentNode));
$node->move('/bar/asd');
}
}

View File

@ -18,13 +18,11 @@ use OC\Files\Node\Node;
use OC\Files\Node\Root;
use OC\Files\Storage\Temporary;
use OC\Files\Storage\Wrapper\Jail;
use OC\Files\View;
use OC\User\User;
use OCP\Files\Mount\IMountPoint;
use OCP\Files\NotFoundException;
use OC\Files\View;
use OCP\Files\Storage;
use OCP\ILogger;
use OCP\IUserManager;
/**
* Class FolderTest
@ -33,152 +31,21 @@ use OCP\IUserManager;
*
* @package Test\Files\Node
*/
class FolderTest extends \Test\TestCase {
/** @var User */
private $user;
/** @var \OCP\Files\Config\IUserMountCache|\PHPUnit_Framework_MockObject_MockObject */
private $userMountCache;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $userManager;
protected function setUp() {
parent::setUp();
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy);
$this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
->disableOriginalConstructor()
->getMock();
$this->logger = $this->createMock(ILogger::class);
$this->userManager = $this->createMock(IUserManager::class);
class FolderTest extends NodeTest {
protected function createTestNode($root, $view, $path) {
return new \OC\Files\Node\Folder($root, $view, $path);
}
protected function getMockStorage() {
$storage = $this->createMock(Storage::class);
$storage->expects($this->any())
->method('getId')
->will($this->returnValue('home::someuser'));
return $storage;
protected function getNodeClass() {
return '\OC\Files\Node\Folder';
}
protected function getFileInfo($data) {
return new FileInfo('', $this->getMockStorage(), '', $data, null);
protected function getNonExistingNodeClass() {
return '\OC\Files\Node\NonExistingFolder';
}
public function testDelete() {
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$root->expects($this->exactly(2))
->method('emit')
->will($this->returnValue(true));
$view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$view->expects($this->once())
->method('rmdir')
->with('/bar/foo')
->will($this->returnValue(true));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$node->delete();
}
public function testDeleteHooks() {
$test = $this;
$hooksRun = 0;
/**
* @param \OC\Files\Node\File $node
*/
$preListener = function ($node) use (&$test, &$hooksRun) {
$test->assertInstanceOf('\OC\Files\Node\Folder', $node);
$test->assertEquals('foo', $node->getInternalPath());
$test->assertEquals('/bar/foo', $node->getPath());
$hooksRun++;
};
/**
* @param \OC\Files\Node\File $node
*/
$postListener = function ($node) use (&$test, &$hooksRun) {
$test->assertInstanceOf('\OC\Files\Node\NonExistingFolder', $node);
$test->assertEquals('foo', $node->getInternalPath());
$test->assertEquals('/bar/foo', $node->getPath());
$test->assertEquals(1, $node->getId());
$hooksRun++;
};
/**
* @var \OC\Files\Mount\Manager $manager
*/
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->createMock(View::class);
$root = new \OC\Files\Node\Root(
$manager,
$view,
$this->user,
$this->userMountCache,
$this->logger,
$this->userManager
);
$root->listen('\OC\Files', 'preDelete', $preListener);
$root->listen('\OC\Files', 'postDelete', $postListener);
$view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))));
$view->expects($this->once())
->method('rmdir')
->with('/bar/foo')
->will($this->returnValue(true));
$view->expects($this->any())
->method('resolvePath')
->with('/bar/foo')
->will($this->returnValue(array(null, 'foo')));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$node->delete();
$this->assertEquals(2, $hooksRun);
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testDeleteNotPermitted() {
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
*/
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->getMock();
$root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\Folder($root, $view, '/bar/foo');
$node->delete();
protected function getViewDeleteMethod() {
return 'rmdir';
}
public function testGetDirectoryContent() {

View File

@ -9,24 +9,34 @@
namespace Test\Files\Node;
use OC\Files\FileInfo;
use OC\Files\View;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\ILogger;
use OCP\IUserManager;
use OCP\Files\NotFoundException;
class NodeTest extends \Test\TestCase {
/**
* Class NodeTest
*
* @package Test\Files\Node
*/
abstract class NodeTest extends \Test\TestCase {
/** @var \OC\User\User */
private $user;
protected $user;
/** @var \OC\Files\Mount\Manager */
private $manager;
protected $manager;
/** @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject */
private $view;
protected $view;
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject */
private $root;
protected $root;
/** @var \OCP\Files\Config\IUserMountCache|\PHPUnit_Framework_MockObject_MockObject */
private $userMountCache;
protected $userMountCache;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
private $logger;
protected $logger;
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
private $userManager;
protected $userManager;
protected function setUp() {
parent::setUp();
@ -54,6 +64,29 @@ class NodeTest extends \Test\TestCase {
->getMock();
}
/**
* @param IRootFolder $root
* @param View $view
* @param string $path
* @return Node
*/
protected abstract function createTestNode($root, $view, $path);
/**
* @return string
*/
protected abstract function getNodeClass();
/**
* @return string
*/
protected abstract function getNonExistingNodeClass();
/**
* @return string
*/
protected abstract function getViewDeleteMethod();
protected function getMockStorage() {
$storage = $this->getMockBuilder('\OCP\Files\Storage')
->disableOriginalConstructor()
@ -68,6 +101,104 @@ class NodeTest extends \Test\TestCase {
return new FileInfo('', $this->getMockStorage(), '', $data, null);
}
public function testDelete() {
$this->root->expects($this->exactly(2))
->method('emit')
->will($this->returnValue(true));
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])));
$this->view->expects($this->once())
->method($this->getViewDeleteMethod())
->with('/bar/foo')
->will($this->returnValue(true));
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$node->delete();
}
public function testDeleteHooks() {
$test = $this;
$hooksRun = 0;
/**
* @param \OC\Files\Node\File $node
*/
$preListener = function ($node) use (&$test, &$hooksRun) {
$test->assertInstanceOf($this->getNodeClass(), $node);
$test->assertEquals('foo', $node->getInternalPath());
$test->assertEquals('/bar/foo', $node->getPath());
$test->assertEquals(1, $node->getId());
$hooksRun++;
};
/**
* @param \OC\Files\Node\File $node
*/
$postListener = function ($node) use (&$test, &$hooksRun) {
$test->assertInstanceOf($this->getNonExistingNodeClass(), $node);
$test->assertEquals('foo', $node->getInternalPath());
$test->assertEquals('/bar/foo', $node->getPath());
$test->assertEquals(1, $node->getId());
$test->assertEquals('text/plain', $node->getMimeType());
$hooksRun++;
};
$root = new \OC\Files\Node\Root(
$this->manager,
$this->view,
$this->user,
$this->userMountCache,
$this->logger,
$this->userManager
);
$root->listen('\OC\Files', 'preDelete', $preListener);
$root->listen('\OC\Files', 'postDelete', $postListener);
$this->view->expects($this->any())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain'])));
$this->view->expects($this->once())
->method($this->getViewDeleteMethod())
->with('/bar/foo')
->will($this->returnValue(true));
$this->view->expects($this->any())
->method('resolvePath')
->with('/bar/foo')
->will($this->returnValue([null, 'foo']));
$node = $this->createTestNode($root, $this->view, '/bar/foo');
$node->delete();
$this->assertEquals(2, $hooksRun);
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testDeleteNotPermitted() {
$this->root->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])));
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$node->delete();
}
public function testStat() {
$this->root->expects($this->any())
->method('getUser')
@ -86,7 +217,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals($stat, $node->stat());
}
@ -107,7 +238,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals(1, $node->getId());
}
@ -129,7 +260,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals(100, $node->getSize());
}
@ -150,7 +281,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals('qwerty', $node->getEtag());
}
@ -171,7 +302,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($stat));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals(50, $node->getMTime());
}
@ -192,7 +323,7 @@ class NodeTest extends \Test\TestCase {
->will($this->returnValue(array($storage, 'foo')));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals($storage, $node->getStorage());
}
@ -201,7 +332,7 @@ class NodeTest extends \Test\TestCase {
->method('getUser')
->will($this->returnValue($this->user));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals('/bar/foo', $node->getPath());
}
@ -222,7 +353,7 @@ class NodeTest extends \Test\TestCase {
->will($this->returnValue(array($storage, 'foo')));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals('foo', $node->getInternalPath());
}
@ -231,7 +362,7 @@ class NodeTest extends \Test\TestCase {
->method('getUser')
->will($this->returnValue($this->user));
$node = new \OC\Files\Node\File($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->assertEquals('foo', $node->getName());
}
@ -250,7 +381,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\Node($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$node->touch(100);
$this->assertEquals(100, $node->getMTime());
}
@ -302,7 +433,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\Node($root, $this->view, '/bar/foo');
$node = $this->createTestNode($root, $this->view, '/bar/foo');
$node->touch(100);
$this->assertEquals(2, $hooksRun);
}
@ -320,7 +451,7 @@ class NodeTest extends \Test\TestCase {
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\Node($this->root, $this->view, '/bar/foo');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$node->touch(100);
}
@ -328,7 +459,312 @@ class NodeTest extends \Test\TestCase {
* @expectedException \OCP\Files\InvalidPathException
*/
public function testInvalidPath() {
$node = new \OC\Files\Node\Node($this->root, $this->view, '/../foo');
$node = $this->createTestNode($this->root, $this->view, '/../foo');
$node->getFileInfo();
}
public function testCopySameStorage() {
$this->view->expects($this->any())
->method('copy')
->with('/bar/foo', '/bar/asd')
->will($this->returnValue(true));
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3])));
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
$newNode = $this->createTestNode($this->root, $this->view, '/bar/asd');
$this->root->expects($this->exactly(2))
->method('get')
->will($this->returnValueMap([
['/bar/asd', $newNode],
['/bar', $parentNode]
]));
$target = $node->copy('/bar/asd');
$this->assertInstanceOf($this->getNodeClass(), $target);
$this->assertEquals(3, $target->getId());
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testCopyNotPermitted() {
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->createMock('\OC\Files\Storage\Storage');
$this->root->expects($this->never())
->method('getMount');
$storage->expects($this->never())
->method('copy');
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3])));
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
$this->root->expects($this->once())
->method('get')
->will($this->returnValueMap([
['/bar', $parentNode]
]));
$node->copy('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotFoundException
*/
public function testCopyNoParent() {
$this->view->expects($this->never())
->method('copy');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->root->expects($this->once())
->method('get')
->with('/bar/asd')
->will($this->throwException(new NotFoundException()));
$node->copy('/bar/asd/foo');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testCopyParentIsFile() {
$this->view->expects($this->never())
->method('copy');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
$this->root->expects($this->once())
->method('get')
->will($this->returnValueMap([
['/bar', $parentNode]
]));
$node->copy('/bar/asd');
}
public function testMoveSameStorage() {
$this->view->expects($this->any())
->method('rename')
->with('/bar/foo', '/bar/asd')
->will($this->returnValue(true));
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
$this->root->expects($this->any())
->method('get')
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
$target = $node->move('/bar/asd');
$this->assertInstanceOf($this->getNodeClass(), $target);
$this->assertEquals(1, $target->getId());
$this->assertEquals('/bar/asd', $node->getPath());
}
public function moveOrCopyProvider() {
return [
['move', 'rename', 'preRename', 'postRename'],
['copy', 'copy', 'preCopy', 'postCopy'],
];
}
/**
* @dataProvider moveOrCopyProvider
* @param string $operationMethod
* @param string $viewMethod
* @param string $preHookName
* @param string $postHookName
*/
public function testMoveCopyHooks($operationMethod, $viewMethod, $preHookName, $postHookName) {
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
->setMethods(['get'])
->getMock();
$this->view->expects($this->any())
->method($viewMethod)
->with('/bar/foo', '/bar/asd')
->will($this->returnValue(true));
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
/**
* @var \OC\Files\Node\File|\PHPUnit_Framework_MockObject_MockObject $node
*/
$node = $this->createTestNode($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$targetTestNode = $this->createTestNode($root, $this->view, '/bar/asd');
$root->expects($this->any())
->method('get')
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $targetTestNode]]));
$hooksRun = 0;
$preListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node) {
$this->assertSame($node, $sourceNode);
$this->assertInstanceOf($this->getNodeClass(), $sourceNode);
$this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
$this->assertEquals('/bar/asd', $targetNode->getPath());
$hooksRun++;
};
$postListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node, $targetTestNode) {
$this->assertSame($node, $sourceNode);
$this->assertNotSame($node, $targetNode);
$this->assertSame($targetTestNode, $targetNode);
$this->assertInstanceOf($this->getNodeClass(), $sourceNode);
$this->assertInstanceOf($this->getNodeClass(), $targetNode);
$hooksRun++;
};
$preWriteListener = function (Node $targetNode) use (&$hooksRun) {
$this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
$this->assertEquals('/bar/asd', $targetNode->getPath());
$hooksRun++;
};
$postWriteListener = function (Node $targetNode) use (&$hooksRun, $targetTestNode) {
$this->assertSame($targetTestNode, $targetNode);
$hooksRun++;
};
$root->listen('\OC\Files', $preHookName, $preListener);
$root->listen('\OC\Files', 'preWrite', $preWriteListener);
$root->listen('\OC\Files', $postHookName, $postListener);
$root->listen('\OC\Files', 'postWrite', $postWriteListener);
$node->$operationMethod('/bar/asd');
$this->assertEquals(4, $hooksRun);
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testMoveNotPermitted() {
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])));
$this->view->expects($this->never())
->method('rename');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
$this->root->expects($this->once())
->method('get')
->with('/bar')
->will($this->returnValue($parentNode));
$node->move('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotFoundException
*/
public function testMoveNoParent() {
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->createMock('\OC\Files\Storage\Storage');
$storage->expects($this->never())
->method('rename');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$this->root->expects($this->once())
->method('get')
->with('/bar')
->will($this->throwException(new NotFoundException()));
$node->move('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testMoveParentIsFile() {
$this->view->expects($this->never())
->method('rename');
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
$this->root->expects($this->once())
->method('get')
->with('/bar')
->will($this->returnValue($parentNode));
$node->move('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testMoveFailed() {
$this->view->expects($this->any())
->method('rename')
->with('/bar/foo', '/bar/asd')
->will($this->returnValue(false));
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
$this->root->expects($this->any())
->method('get')
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
$node->move('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testCopyFailed() {
$this->view->expects($this->any())
->method('copy')
->with('/bar/foo', '/bar/asd')
->will($this->returnValue(false));
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
$this->root->expects($this->any())
->method('get')
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
$node->copy('/bar/asd');
}
}