diff options
Diffstat (limited to 'tests/lib/Files/Node')
-rw-r--r-- | tests/lib/Files/Node/FileTest.php | 680 | ||||
-rw-r--r-- | tests/lib/Files/Node/FolderTest.php | 795 | ||||
-rw-r--r-- | tests/lib/Files/Node/HookConnectorTest.php | 203 | ||||
-rw-r--r-- | tests/lib/Files/Node/IntegrationTest.php | 125 | ||||
-rw-r--r-- | tests/lib/Files/Node/NodeTest.php | 366 | ||||
-rw-r--r-- | tests/lib/Files/Node/RootTest.php | 104 |
6 files changed, 2273 insertions, 0 deletions
diff --git a/tests/lib/Files/Node/FileTest.php b/tests/lib/Files/Node/FileTest.php new file mode 100644 index 00000000000..180c7b12ce4 --- /dev/null +++ b/tests/lib/Files/Node/FileTest.php @@ -0,0 +1,680 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Node; + +use OC\Files\FileInfo; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OC\Files\View; + +class FileTest extends \Test\TestCase { + private $user; + + protected function setUp() { + parent::setUp(); + $this->user = new \OC\User\User('', new \Test\Util\User\Dummy); + } + + protected function getMockStorage() { + $storage = $this->getMock('\OCP\Files\Storage'); + $storage->expects($this->any()) + ->method('getId') + ->will($this->returnValue('home::someuser')); + return $storage; + } + + protected function getFileInfo($data) { + return new FileInfo('', $this->getMockStorage(), '', $data, null); + } + + public function testDelete() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->exactly(2)) + ->method('emit') + ->will($this->returnValue(true)); + $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_ALL)))); + + $view->expects($this->once()) + ->method('unlink') + ->with('/bar/foo') + ->will($this->returnValue(true)); + + $node = new \OC\Files\Node\File($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\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++; + }; + + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root->listen('\OC\Files', 'preDelete', $preListener); + $root->listen('\OC\Files', 'postDelete', $postListener); + + $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')))); + + $view->expects($this->once()) + ->method('unlink') + ->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\File($root, $view, '/bar/foo'); + $node->delete(); + $this->assertEquals(2, $hooksRun); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testDeleteNotPermitted() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $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\File($root, $view, '/bar/foo'); + $node->delete(); + } + + public function testGetContent() { + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $hook = function ($file) { + throw new \Exception('Hooks are not supposed to be called'); + }; + + $root->listen('\OC\Files', 'preWrite', $hook); + $root->listen('\OC\Files', 'postWrite', $hook); + + $view->expects($this->once()) + ->method('file_get_contents') + ->with('/bar/foo') + ->will($this->returnValue('bar')); + + $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, $view, '/bar/foo'); + $this->assertEquals('bar', $node->getContent()); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testGetContentNotPermitted() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $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' => 0)))); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $node->getContent(); + } + + public function testPutContent() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $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_ALL)))); + + $view->expects($this->once()) + ->method('file_put_contents') + ->with('/bar/foo', 'bar') + ->will($this->returnValue(true)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $node->putContent('bar'); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testPutContentNotPermitted() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $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\File($root, $view, '/bar/foo'); + $node->putContent('bar'); + } + + public function testGetMimeType() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('mimetype' => 'text/plain')))); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals('text/plain', $node->getMimeType()); + } + + public function testFOpenRead() { + $stream = fopen('php://memory', 'w+'); + fwrite($stream, 'bar'); + rewind($stream); + + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $hook = function ($file) { + throw new \Exception('Hooks are not supposed to be called'); + }; + + $root->listen('\OC\Files', 'preWrite', $hook); + $root->listen('\OC\Files', 'postWrite', $hook); + + $view->expects($this->once()) + ->method('fopen') + ->with('/bar/foo', 'r') + ->will($this->returnValue($stream)); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL)))); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $fh = $node->fopen('r'); + $this->assertEquals($stream, $fh); + $this->assertEquals('bar', fread($fh, 3)); + } + + public function testFOpenWrite() { + $stream = fopen('php://memory', 'w+'); + + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, new $view, $this->user); + + $hooksCalled = 0; + $hook = function ($file) use (&$hooksCalled) { + $hooksCalled++; + }; + + $root->listen('\OC\Files', 'preWrite', $hook); + $root->listen('\OC\Files', 'postWrite', $hook); + + $view->expects($this->once()) + ->method('fopen') + ->with('/bar/foo', 'w') + ->will($this->returnValue($stream)); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL)))); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $fh = $node->fopen('w'); + $this->assertEquals($stream, $fh); + fwrite($fh, 'bar'); + rewind($fh); + $this->assertEquals('bar', fread($stream, 3)); + $this->assertEquals(2, $hooksCalled); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testFOpenReadNotPermitted() { + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $hook = function ($file) { + throw new \Exception('Hooks are not supposed to be called'); + }; + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => 0)))); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $node->fopen('r'); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testFOpenReadWriteNoReadPermissions() { + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $hook = function () { + throw new \Exception('Hooks are not supposed to be called'); + }; + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_UPDATE)))); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $node->fopen('w'); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testFOpenReadWriteNoWritePermissions() { + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, new $view, $this->user); + + $hook = function () { + throw new \Exception('Hooks are not supposed to be called'); + }; + + $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, $view, '/bar/foo'); + $node->fopen('w'); + } + + public function testCopySameStorage() { + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $view->expects($this->any()) + ->method('copy') + ->with('/bar/foo', '/bar/asd'); + + $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, $view, '/bar/foo'); + $parentNode = new \OC\Files\Node\Folder($root, $view, '/bar'); + $newNode = new \OC\Files\Node\File($root, $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\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + /** + * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage + */ + $storage = $this->getMock('\OC\Files\Storage\Storage'); + + $root->expects($this->never()) + ->method('getMount'); + + $storage->expects($this->never()) + ->method('copy'); + + $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, $view, '/bar/foo'); + $parentNode = new \OC\Files\Node\Folder($root, $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\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $view->expects($this->never()) + ->method('copy'); + + $node = new \OC\Files\Node\File($root, $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\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $view->expects($this->never()) + ->method('copy'); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $parentNode = new \OC\Files\Node\File($root, $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\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $view->expects($this->any()) + ->method('rename') + ->with('/bar/foo', '/bar/asd'); + + $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, $view, '/bar/foo'); + $parentNode = new \OC\Files\Node\Folder($root, $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\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $view->expects($this->any()) + ->method('getFileInfo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ)))); + + $view->expects($this->never()) + ->method('rename'); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $parentNode = new \OC\Files\Node\Folder($root, $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\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + /** + * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage + */ + $storage = $this->getMock('\OC\Files\Storage\Storage'); + + $storage->expects($this->never()) + ->method('rename'); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $parentNode = new \OC\Files\Node\Folder($root, $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\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $view->expects($this->never()) + ->method('rename'); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $parentNode = new \OC\Files\Node\File($root, $view, '/bar'); + + $root->expects($this->once()) + ->method('get') + ->with('/bar') + ->will($this->returnValue($parentNode)); + + $node->move('/bar/asd'); + } +} diff --git a/tests/lib/Files/Node/FolderTest.php b/tests/lib/Files/Node/FolderTest.php new file mode 100644 index 00000000000..7ce9fff1419 --- /dev/null +++ b/tests/lib/Files/Node/FolderTest.php @@ -0,0 +1,795 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Node; + +use OC\Files\Cache\Cache; +use OC\Files\FileInfo; +use OC\Files\Mount\MountPoint; +use OC\Files\Node\Node; +use OCP\Files\NotFoundException; +use OCP\Files\NotPermittedException; +use OC\Files\View; + +/** + * Class FolderTest + * + * @group DB + * + * @package Test\Files\Node + */ +class FolderTest extends \Test\TestCase { + private $user; + + protected function setUp() { + parent::setUp(); + $this->user = new \OC\User\User('', new \Test\Util\User\Dummy); + } + + protected function getMockStorage() { + $storage = $this->getMock('\OCP\Files\Storage'); + $storage->expects($this->any()) + ->method('getId') + ->will($this->returnValue('home::someuser')); + return $storage; + } + + protected function getFileInfo($data) { + return new FileInfo('', $this->getMockStorage(), '', $data, null); + } + + public function testDelete() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $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->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $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->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $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(); + } + + public function testGetDirectoryContent() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $view->expects($this->any()) + ->method('getDirectoryContent') + ->with('/bar/foo') + ->will($this->returnValue(array( + new FileInfo('/bar/foo/asd', null, 'foo/asd', ['fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'], null), + new FileInfo('/bar/foo/qwerty', null, 'foo/qwerty', ['fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory'], null) + ))); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $children = $node->getDirectoryListing(); + $this->assertEquals(2, count($children)); + $this->assertInstanceOf('\OC\Files\Node\File', $children[0]); + $this->assertInstanceOf('\OC\Files\Node\Folder', $children[1]); + $this->assertEquals('asd', $children[0]->getName()); + $this->assertEquals('qwerty', $children[1]->getName()); + $this->assertEquals(2, $children[0]->getId()); + $this->assertEquals(3, $children[1]->getId()); + } + + public function testGet() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $root->expects($this->once()) + ->method('get') + ->with('/bar/foo/asd'); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $node->get('asd'); + } + + public function testNodeExists() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd'); + + $root->expects($this->once()) + ->method('get') + ->with('/bar/foo/asd') + ->will($this->returnValue($child)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $this->assertTrue($node->nodeExists('asd')); + } + + public function testNodeExistsNotExists() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $root->expects($this->once()) + ->method('get') + ->with('/bar/foo/asd') + ->will($this->throwException(new NotFoundException())); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $this->assertFalse($node->nodeExists('asd')); + } + + public function testNewFolder() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $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_ALL)))); + + $view->expects($this->once()) + ->method('mkdir') + ->with('/bar/foo/asd') + ->will($this->returnValue(true)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $child = new \OC\Files\Node\Folder($root, $view, '/bar/foo/asd'); + $result = $node->newFolder('asd'); + $this->assertEquals($child, $result); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testNewFolderNotPermitted() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $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->newFolder('asd'); + } + + public function testNewFile() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $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_ALL)))); + + $view->expects($this->once()) + ->method('touch') + ->with('/bar/foo/asd') + ->will($this->returnValue(true)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $child = new \OC\Files\Node\File($root, $view, '/bar/foo/asd'); + $result = $node->newFile('asd'); + $this->assertEquals($child, $result); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testNewFileNotPermitted() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $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->newFile('asd'); + } + + public function testGetFreeSpace() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $view->expects($this->once()) + ->method('free_space') + ->with('/bar/foo') + ->will($this->returnValue(100)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $this->assertEquals(100, $node->getFreeSpace()); + } + + public function testSearch() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $mount = $this->getMock('\OCP\Files\Mount\IMountPoint'); + $mount->expects($this->once()) + ->method('getStorage') + ->will($this->returnValue($storage)); + $mount->expects($this->once()) + ->method('getInternalPath') + ->will($this->returnValue('foo')); + + $cache->expects($this->once()) + ->method('search') + ->with('%qw%') + ->will($this->returnValue(array( + array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain') + ))); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array())); + + $root->expects($this->once()) + ->method('getMount') + ->with('/bar/foo') + ->will($this->returnValue($mount)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->search('qw'); + $this->assertEquals(1, count($result)); + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); + } + + public function testSearchInRoot() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $mount = $this->getMock('\OCP\Files\Mount\IMountPoint'); + $mount->expects($this->once()) + ->method('getStorage') + ->will($this->returnValue($storage)); + $mount->expects($this->once()) + ->method('getInternalPath') + ->will($this->returnValue('files')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->once()) + ->method('search') + ->with('%qw%') + ->will($this->returnValue(array( + array('fileid' => 3, 'path' => 'files/foo', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'), + array('fileid' => 3, 'path' => 'files_trashbin/foo2.d12345', 'name' => 'foo2.d12345', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain'), + ))); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('') + ->will($this->returnValue(array())); + + $root->expects($this->once()) + ->method('getMount') + ->with('') + ->will($this->returnValue($mount)); + + $result = $root->search('qw'); + $this->assertEquals(1, count($result)); + $this->assertEquals('/foo', $result[0]->getPath()); + } + + public function testSearchInStorageRoot() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $mount = $this->getMock('\OCP\Files\Mount\IMountPoint'); + $mount->expects($this->once()) + ->method('getStorage') + ->will($this->returnValue($storage)); + $mount->expects($this->once()) + ->method('getInternalPath') + ->will($this->returnValue('')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->once()) + ->method('search') + ->with('%qw%') + ->will($this->returnValue(array( + array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain') + ))); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar') + ->will($this->returnValue(array())); + + $root->expects($this->once()) + ->method('getMount') + ->with('/bar') + ->will($this->returnValue($mount)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar'); + $result = $node->search('qw'); + $this->assertEquals(1, count($result)); + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); + } + + public function testSearchByTag() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $mount = $this->getMock('\OCP\Files\Mount\IMountPoint'); + $mount->expects($this->once()) + ->method('getStorage') + ->will($this->returnValue($storage)); + $mount->expects($this->once()) + ->method('getInternalPath') + ->will($this->returnValue('foo')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->once()) + ->method('searchByTag') + ->with('tag1', 'user1') + ->will($this->returnValue(array( + array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain') + ))); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array())); + + $root->expects($this->once()) + ->method('getMount') + ->with('/bar/foo') + ->will($this->returnValue($mount)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->searchByTag('tag1', 'user1'); + $this->assertEquals(1, count($result)); + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); + } + + public function testSearchSubStorages() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + $subCache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + $subStorage = $this->getMock('\OC\Files\Storage\Storage'); + $subMount = $this->getMock('\OC\Files\Mount\MountPoint', array(), array(null, '')); + + $mount = $this->getMock('\OCP\Files\Mount\IMountPoint'); + $mount->expects($this->once()) + ->method('getStorage') + ->will($this->returnValue($storage)); + $mount->expects($this->once()) + ->method('getInternalPath') + ->will($this->returnValue('foo')); + + $subMount->expects($this->once()) + ->method('getStorage') + ->will($this->returnValue($subStorage)); + + $subMount->expects($this->once()) + ->method('getMountPoint') + ->will($this->returnValue('/bar/foo/bar/')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $subStorage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($subCache)); + + $cache->expects($this->once()) + ->method('search') + ->with('%qw%') + ->will($this->returnValue(array( + array('fileid' => 3, 'path' => 'foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain') + ))); + + $subCache->expects($this->once()) + ->method('search') + ->with('%qw%') + ->will($this->returnValue(array( + array('fileid' => 4, 'path' => 'asd/qweasd', 'name' => 'qweasd', 'size' => 200, 'mtime' => 55, 'mimetype' => 'text/plain') + ))); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array($subMount))); + + $root->expects($this->once()) + ->method('getMount') + ->with('/bar/foo') + ->will($this->returnValue($mount)); + + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->search('qw'); + $this->assertEquals(2, count($result)); + } + + public function testIsSubNode() { + $file = new Node(null, null, '/foo/bar'); + $folder = new \OC\Files\Node\Folder(null, null, '/foo'); + $this->assertTrue($folder->isSubNode($file)); + $this->assertFalse($folder->isSubNode($folder)); + + $file = new Node(null, null, '/foobar'); + $this->assertFalse($folder->isSubNode($file)); + } + + public function testGetById() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $mount = new MountPoint($storage, '/bar'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $view->expects($this->once()) + ->method('getFileInfo') + ->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', ['mimetype' => 'text/plain'], null))); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->once()) + ->method('getPathById') + ->with('1') + ->will($this->returnValue('foo/qwerty')); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array())); + + $root->expects($this->once()) + ->method('getMount') + ->with('/bar/foo') + ->will($this->returnValue($mount)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->getById(1); + $this->assertEquals(1, count($result)); + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); + } + + public function testGetByIdOutsideFolder() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $mount = new MountPoint($storage, '/bar'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $storage->expects($this->once()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->once()) + ->method('getPathById') + ->with('1') + ->will($this->returnValue('foobar')); + + $root->expects($this->once()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array())); + + $root->expects($this->once()) + ->method('getMount') + ->with('/bar/foo') + ->will($this->returnValue($mount)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->getById(1); + $this->assertCount(0, $result); + } + + public function testGetByIdMultipleStorages() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $storage = $this->getMock('\OC\Files\Storage\Storage'); + $mount1 = new MountPoint($storage, '/bar'); + $mount2 = new MountPoint($storage, '/bar/foo/asd'); + $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array('')); + + $view->expects($this->any()) + ->method('getFileInfo') + ->will($this->returnValue(new FileInfo('/bar/foo/qwerty', null, 'qwerty', ['mimetype' => 'plain'], null))); + + $storage->expects($this->any()) + ->method('getCache') + ->will($this->returnValue($cache)); + + $cache->expects($this->any()) + ->method('getPathById') + ->with('1') + ->will($this->returnValue('foo/qwerty')); + + $root->expects($this->any()) + ->method('getMountsIn') + ->with('/bar/foo') + ->will($this->returnValue(array($mount2))); + + $root->expects($this->once()) + ->method('getMount') + ->with('/bar/foo') + ->will($this->returnValue($mount1)); + + $node = new \OC\Files\Node\Folder($root, $view, '/bar/foo'); + $result = $node->getById(1); + $this->assertEquals(2, count($result)); + $this->assertEquals('/bar/foo/qwerty', $result[0]->getPath()); + $this->assertEquals('/bar/foo/asd/foo/qwerty', $result[1]->getPath()); + } + + public function uniqueNameProvider() { + return [ + // input, existing, expected + ['foo', [] , 'foo'], + ['foo', ['foo'] , 'foo (2)'], + ['foo', ['foo', 'foo (2)'] , 'foo (3)'] + ]; + } + + /** + * @dataProvider uniqueNameProvider + */ + public function testGetUniqueName($name, $existingFiles, $expected) { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + $folderPath = '/bar/foo'; + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array('getUser', 'getMountsIn', 'getMount'), array($manager, $view, $this->user)); + + $view->expects($this->any()) + ->method('file_exists') + ->will($this->returnCallback(function ($path) use ($existingFiles, $folderPath) { + foreach ($existingFiles as $existing) { + if ($folderPath . '/' . $existing === $path){ + return true; + } + } + return false; + })); + + $node = new \OC\Files\Node\Folder($root, $view, $folderPath); + $this->assertEquals($expected, $node->getNonExistingName($name)); + } +} diff --git a/tests/lib/Files/Node/HookConnectorTest.php b/tests/lib/Files/Node/HookConnectorTest.php new file mode 100644 index 00000000000..7245dd37593 --- /dev/null +++ b/tests/lib/Files/Node/HookConnectorTest.php @@ -0,0 +1,203 @@ +<?php +/** + * Copyright (c) 2015 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Node; + +use OC\Files\Filesystem; +use OC\Files\Node\Root; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use OCP\Files\Node; +use Test\TestCase; +use Test\Traits\MountProviderTrait; +use Test\Traits\UserTrait; + +/** + * Class HookConnectorTest + * + * @group DB + * + * @package Test\Files\Node + */ +class HookConnectorTest extends TestCase { + use UserTrait; + use MountProviderTrait; + + /** + * @var View + */ + private $view; + + /** + * @var Root + */ + private $root; + + /** + * @var string + */ + private $userId; + + public function setUp() { + parent::setUp(); + $this->userId = $this->getUniqueID(); + $this->createUser($this->userId, 'pass'); + $this->registerMount($this->userId, new Temporary(), '/' . $this->userId . '/files/'); + \OC_Util::setupFS($this->userId); + $this->view = new View(); + $this->root = new Root(Filesystem::getMountManager(), $this->view, \OC::$server->getUserManager()->get($this->userId)); + } + + public function tearDown() { + parent::tearDown(); + \OC_Hook::clear('OC_Filesystem'); + \OC_Util::tearDownFS(); + } + + public function viewToNodeProvider() { + return [ + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'preWrite'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'postWrite'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'preCreate'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + }, 'postCreate'], + [function () { + Filesystem::mkdir('test.txt'); + }, 'preCreate'], + [function () { + Filesystem::mkdir('test.txt'); + }, 'postCreate'], + [function () { + Filesystem::touch('test.txt'); + }, 'preTouch'], + [function () { + Filesystem::touch('test.txt'); + }, 'postTouch'], + [function () { + Filesystem::touch('test.txt'); + }, 'preCreate'], + [function () { + Filesystem::touch('test.txt'); + }, 'postCreate'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + Filesystem::unlink('test.txt'); + }, 'preDelete'], + [function () { + Filesystem::file_put_contents('test.txt', 'asd'); + Filesystem::unlink('test.txt'); + }, 'postDelete'], + [function () { + Filesystem::mkdir('test.txt'); + Filesystem::rmdir('test.txt'); + }, 'preDelete'], + [function () { + Filesystem::mkdir('test.txt'); + Filesystem::rmdir('test.txt'); + }, 'postDelete'], + ]; + } + + /** + * @param callable $operation + * @param string $expectedHook + * @dataProvider viewToNodeProvider + */ + public function testViewToNode(callable $operation, $expectedHook) { + $connector = new \OC\Files\Node\HookConnector($this->root, $this->view); + $connector->viewToNode(); + $hookCalled = false; + /** @var Node $hookNode */ + $hookNode = null; + + $this->root->listen('\OC\Files', $expectedHook, function ($node) use (&$hookNode, &$hookCalled) { + $hookCalled = true; + $hookNode = $node; + }); + + $operation(); + + $this->assertTrue($hookCalled); + $this->assertEquals('/' . $this->userId . '/files/test.txt', $hookNode->getPath()); + } + + public function viewToNodeProviderCopyRename() { + return [ + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::rename('source', 'target'); + }, 'preRename'], + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::rename('source', 'target'); + }, 'postRename'], + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::copy('source', 'target'); + }, 'preCopy'], + [function () { + Filesystem::file_put_contents('source', 'asd'); + Filesystem::copy('source', 'target'); + }, 'postCopy'], + ]; + } + + /** + * @param callable $operation + * @param string $expectedHook + * @dataProvider viewToNodeProviderCopyRename + */ + public function testViewToNodeCopyRename(callable $operation, $expectedHook) { + $connector = new \OC\Files\Node\HookConnector($this->root, $this->view); + $connector->viewToNode(); + $hookCalled = false; + /** @var Node $hookSourceNode */ + $hookSourceNode = null; + /** @var Node $hookTargetNode */ + $hookTargetNode = null; + + $this->root->listen('\OC\Files', $expectedHook, function ($sourceNode, $targetNode) use (&$hookCalled, &$hookSourceNode, &$hookTargetNode) { + $hookCalled = true; + $hookSourceNode = $sourceNode; + $hookTargetNode = $targetNode; + }); + + $operation(); + + $this->assertTrue($hookCalled); + $this->assertEquals('/' . $this->userId . '/files/source', $hookSourceNode->getPath()); + $this->assertEquals('/' . $this->userId . '/files/target', $hookTargetNode->getPath()); + } + + public function testPostDeleteMeta() { + $connector = new \OC\Files\Node\HookConnector($this->root, $this->view); + $connector->viewToNode(); + $hookCalled = false; + /** @var Node $hookNode */ + $hookNode = null; + + $this->root->listen('\OC\Files', 'postDelete', function ($node) use (&$hookNode, &$hookCalled) { + $hookCalled = true; + $hookNode = $node; + }); + + Filesystem::file_put_contents('test.txt', 'asd'); + $info = Filesystem::getFileInfo('test.txt'); + Filesystem::unlink('test.txt'); + + $this->assertTrue($hookCalled); + $this->assertEquals($hookNode->getId(), $info->getId()); + } +} diff --git a/tests/lib/Files/Node/IntegrationTest.php b/tests/lib/Files/Node/IntegrationTest.php new file mode 100644 index 00000000000..f52e0623e14 --- /dev/null +++ b/tests/lib/Files/Node/IntegrationTest.php @@ -0,0 +1,125 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Node; + +use OC\Files\Node\Root; +use OC\Files\Storage\Temporary; +use OC\Files\View; +use OC\User\User; + +/** + * Class IntegrationTest + * + * @group DB + * + * @package Test\Files\Node + */ +class IntegrationTest extends \Test\TestCase { + /** + * @var \OC\Files\Node\Root $root + */ + private $root; + + /** + * @var \OC\Files\Storage\Storage[] + */ + private $storages; + + /** + * @var \OC\Files\View $view + */ + private $view; + + protected function setUp() { + parent::setUp(); + + $manager = \OC\Files\Filesystem::getMountManager(); + + \OC_Hook::clear('OC_Filesystem'); + + $user = new User($this->getUniqueID('user'), new \Test\Util\User\Dummy); + $this->loginAsUser($user->getUID()); + + $this->view = new View(); + $this->root = new Root($manager, $this->view, $user); + $storage = new Temporary(array()); + $subStorage = new Temporary(array()); + $this->storages[] = $storage; + $this->storages[] = $subStorage; + $this->root->mount($storage, '/'); + $this->root->mount($subStorage, '/substorage/'); + } + + protected function tearDown() { + foreach ($this->storages as $storage) { + $storage->getCache()->clear(); + } + + $this->logout(); + parent::tearDown(); + } + + public function testBasicFile() { + $file = $this->root->newFile('/foo.txt'); + $this->assertCount(2, $this->root->getDirectoryListing()); + $this->assertTrue($this->root->nodeExists('/foo.txt')); + $id = $file->getId(); + $this->assertInstanceOf('\OC\Files\Node\File', $file); + $file->putContent('qwerty'); + $this->assertEquals('text/plain', $file->getMimeType()); + $this->assertEquals('qwerty', $file->getContent()); + $this->assertFalse($this->root->nodeExists('/bar.txt')); + $target = $file->move('/bar.txt'); + $this->assertEquals($id, $target->getId()); + $this->assertEquals($id, $file->getId()); + $this->assertFalse($this->root->nodeExists('/foo.txt')); + $this->assertTrue($this->root->nodeExists('/bar.txt')); + $this->assertEquals('bar.txt', $file->getName()); + $this->assertEquals('bar.txt', $file->getInternalPath()); + + $file->move('/substorage/bar.txt'); + $this->assertEquals($id, $file->getId()); + $this->assertEquals('qwerty', $file->getContent()); + } + + public function testBasicFolder() { + $folder = $this->root->newFolder('/foo'); + $this->assertTrue($this->root->nodeExists('/foo')); + $file = $folder->newFile('/bar'); + $this->assertTrue($this->root->nodeExists('/foo/bar')); + $file->putContent('qwerty'); + + $listing = $folder->getDirectoryListing(); + $this->assertEquals(1, count($listing)); + $this->assertEquals($file->getId(), $listing[0]->getId()); + $this->assertEquals($file->getStorage(), $listing[0]->getStorage()); + + + $rootListing = $this->root->getDirectoryListing(); + $this->assertEquals(2, count($rootListing)); + + $folder->move('/asd'); + /** + * @var \OC\Files\Node\File $file + */ + $file = $folder->get('/bar'); + $this->assertInstanceOf('\OC\Files\Node\File', $file); + $this->assertFalse($this->root->nodeExists('/foo/bar')); + $this->assertTrue($this->root->nodeExists('/asd/bar')); + $this->assertEquals('qwerty', $file->getContent()); + $folder->move('/substorage/foo'); + /** + * @var \OC\Files\Node\File $file + */ + $file = $folder->get('/bar'); + $this->assertInstanceOf('\OC\Files\Node\File', $file); + $this->assertTrue($this->root->nodeExists('/substorage/foo/bar')); + $this->assertEquals('qwerty', $file->getContent()); + } +} diff --git a/tests/lib/Files/Node/NodeTest.php b/tests/lib/Files/Node/NodeTest.php new file mode 100644 index 00000000000..a9575507b4f --- /dev/null +++ b/tests/lib/Files/Node/NodeTest.php @@ -0,0 +1,366 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Node; + +use OC\Files\FileInfo; + +class NodeTest extends \Test\TestCase { + private $user; + + protected function setUp() { + parent::setUp(); + $this->user = new \OC\User\User('', new \Test\Util\User\Dummy); + } + + protected function getMockStorage() { + $storage = $this->getMock('\OCP\Files\Storage'); + $storage->expects($this->any()) + ->method('getId') + ->will($this->returnValue('home::someuser')); + return $storage; + } + + protected function getFileInfo($data) { + return new FileInfo('', $this->getMockStorage(), '', $data, null); + } + + public function testStat() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $stat = array( + 'fileid' => 1, + 'size' => 100, + 'etag' => 'qwerty', + 'mtime' => 50, + 'permissions' => 0 + ); + + $view->expects($this->once()) + ->method('stat') + ->with('/bar/foo') + ->will($this->returnValue($stat)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals($stat, $node->stat()); + } + + public function testGetId() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $stat = $this->getFileInfo(array( + 'fileid' => 1, + 'size' => 100, + 'etag' => 'qwerty', + 'mtime' => 50 + )); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($stat)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals(1, $node->getId()); + } + + public function testGetSize() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + + $stat = $this->getFileInfo(array( + 'fileid' => 1, + 'size' => 100, + 'etag' => 'qwerty', + 'mtime' => 50 + )); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($stat)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals(100, $node->getSize()); + } + + public function testGetEtag() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $stat = $this->getFileInfo(array( + 'fileid' => 1, + 'size' => 100, + 'etag' => 'qwerty', + 'mtime' => 50 + )); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($stat)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals('qwerty', $node->getEtag()); + } + + public function testGetMTime() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $stat = $this->getFileInfo(array( + 'fileid' => 1, + 'size' => 100, + 'etag' => 'qwerty', + 'mtime' => 50 + )); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($stat)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals(50, $node->getMTime()); + } + + public function testGetStorage() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + /** + * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage + */ + $storage = $this->getMock('\OC\Files\Storage\Storage'); + + $view->expects($this->once()) + ->method('resolvePath') + ->with('/bar/foo') + ->will($this->returnValue(array($storage, 'foo'))); + + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals($storage, $node->getStorage()); + } + + public function testGetPath() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals('/bar/foo', $node->getPath()); + } + + public function testGetInternalPath() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + /** + * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage + */ + $storage = $this->getMock('\OC\Files\Storage\Storage'); + + $view->expects($this->once()) + ->method('resolvePath') + ->with('/bar/foo') + ->will($this->returnValue(array($storage, 'foo'))); + + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals('foo', $node->getInternalPath()); + } + + public function testGetName() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $node = new \OC\Files\Node\File($root, $view, '/bar/foo'); + $this->assertEquals('foo', $node->getName()); + } + + public function testTouchSetMTime() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $view->expects($this->once()) + ->method('touch') + ->with('/bar/foo', 100) + ->will($this->returnValue(true)); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL)))); + + $node = new \OC\Files\Node\Node($root, $view, '/bar/foo'); + $node->touch(100); + $this->assertEquals(100, $node->getMTime()); + } + + public function testTouchHooks() { + $test = $this; + $hooksRun = 0; + /** + * @param \OC\Files\Node\File $node + */ + $preListener = function ($node) use (&$test, &$hooksRun) { + $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->assertEquals('foo', $node->getInternalPath()); + $test->assertEquals('/bar/foo', $node->getPath()); + $hooksRun++; + }; + + /** + * @var \OC\Files\Mount\Manager $manager + */ + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + $root->listen('\OC\Files', 'preTouch', $preListener); + $root->listen('\OC\Files', 'postTouch', $postListener); + + $view->expects($this->once()) + ->method('touch') + ->with('/bar/foo', 100) + ->will($this->returnValue(true)); + + $view->expects($this->any()) + ->method('resolvePath') + ->with('/bar/foo') + ->will($this->returnValue(array(null, 'foo'))); + + $view->expects($this->any()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL)))); + + $node = new \OC\Files\Node\Node($root, $view, '/bar/foo'); + $node->touch(100); + $this->assertEquals(2, $hooksRun); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testTouchNotPermitted() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + $root->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + + $view->expects($this->any()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ)))); + + $node = new \OC\Files\Node\Node($root, $view, '/bar/foo'); + $node->touch(100); + } + + /** + * @expectedException \OCP\Files\InvalidPathException + */ + public function testInvalidPath() { + $manager = $this->getMock('\OC\Files\Mount\Manager'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user)); + + $node = new \OC\Files\Node\Node($root, $view, '/../foo'); + $node->getFileInfo(); + } +} diff --git a/tests/lib/Files/Node/RootTest.php b/tests/lib/Files/Node/RootTest.php new file mode 100644 index 00000000000..1b4824cba76 --- /dev/null +++ b/tests/lib/Files/Node/RootTest.php @@ -0,0 +1,104 @@ +<?php +/** + * Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Files\Node; + +use OC\Files\FileInfo; +use OCP\Files\NotPermittedException; +use OC\Files\Mount\Manager; + +/** + * @group DB + */ +class RootTest extends \Test\TestCase { + private $user; + + protected function setUp() { + parent::setUp(); + $this->user = new \OC\User\User('', new \Test\Util\User\Dummy); + } + + protected function getFileInfo($data) { + return new FileInfo('', null, '', $data, null); + } + + public function testGet() { + $manager = new Manager(); + /** + * @var \OC\Files\Storage\Storage $storage + */ + $storage = $this->getMock('\OC\Files\Storage\Storage'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue($this->getFileInfo(array('fileid' => 10, 'path' => 'bar/foo', 'name', 'mimetype' => 'text/plain')))); + + $root->mount($storage, ''); + $node = $root->get('/bar/foo'); + $this->assertEquals(10, $node->getId()); + $this->assertInstanceOf('\OC\Files\Node\File', $node); + } + + /** + * @expectedException \OCP\Files\NotFoundException + */ + public function testGetNotFound() { + $manager = new Manager(); + /** + * @var \OC\Files\Storage\Storage $storage + */ + $storage = $this->getMock('\OC\Files\Storage\Storage'); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $view->expects($this->once()) + ->method('getFileInfo') + ->with('/bar/foo') + ->will($this->returnValue(false)); + + $root->mount($storage, ''); + $root->get('/bar/foo'); + } + + /** + * @expectedException \OCP\Files\NotPermittedException + */ + public function testGetInvalidPath() { + $manager = new Manager(); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $root->get('/../foo'); + } + + /** + * @expectedException \OCP\Files\NotFoundException + */ + public function testGetNoStorages() { + $manager = new Manager(); + /** + * @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view + */ + $view = $this->getMock('\OC\Files\View'); + $root = new \OC\Files\Node\Root($manager, $view, $this->user); + + $root->get('/bar/foo'); + } +} |