summaryrefslogtreecommitdiffstats
path: root/tests/lib/files
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/files')
-rw-r--r--tests/lib/files/cache/permissions.php17
-rw-r--r--tests/lib/files/cache/scanner.php57
-rw-r--r--tests/lib/files/node/file.php664
-rw-r--r--tests/lib/files/node/folder.php479
-rw-r--r--tests/lib/files/node/integration.php122
-rw-r--r--tests/lib/files/node/node.php330
-rw-r--r--tests/lib/files/node/root.php106
-rw-r--r--tests/lib/files/view.php45
8 files changed, 1808 insertions, 12 deletions
diff --git a/tests/lib/files/cache/permissions.php b/tests/lib/files/cache/permissions.php
index 7e6e11e2eb2..4b284c2c8e2 100644
--- a/tests/lib/files/cache/permissions.php
+++ b/tests/lib/files/cache/permissions.php
@@ -8,6 +8,8 @@
namespace Test\Files\Cache;
+use OC\Files\Storage\Temporary;
+
class Permissions extends \PHPUnit_Framework_TestCase {
/***
* @var \OC\Files\Cache\Permissions $permissionsCache
@@ -55,4 +57,19 @@ class Permissions extends \PHPUnit_Framework_TestCase {
$this->permissionsCache->removeMultiple($ids, $user);
}
+
+ public function testUpdatePermissionsOnRescan() {
+ $storage = new Temporary(array());
+ $scanner = $storage->getScanner();
+ $cache = $storage->getCache();
+ $permissionsCache = $storage->getPermissionsCache();
+
+ $storage->file_put_contents('foo.txt', 'bar');
+ $scanner->scan('');
+ $id = $cache->getId('foo.txt');
+ $permissionsCache->set($id, 'test', 1);
+
+ $scanner->scan('');
+ $this->assertEquals(-1, $permissionsCache->get($id, 'test'));
+ }
}
diff --git a/tests/lib/files/cache/scanner.php b/tests/lib/files/cache/scanner.php
index f6deb93a49e..3f3a045377a 100644
--- a/tests/lib/files/cache/scanner.php
+++ b/tests/lib/files/cache/scanner.php
@@ -24,6 +24,21 @@ class Scanner extends \PHPUnit_Framework_TestCase {
*/
private $cache;
+ function setUp() {
+ $this->storage = new \OC\Files\Storage\Temporary(array());
+ $this->scanner = new \OC\Files\Cache\Scanner($this->storage);
+ $this->cache = new \OC\Files\Cache\Cache($this->storage);
+ }
+
+ function tearDown() {
+ if ($this->cache) {
+ $ids = $this->cache->getAll();
+ $permissionsCache = $this->storage->getPermissionsCache();
+ $permissionsCache->removeMultiple($ids, \OC_User::getUser());
+ $this->cache->clear();
+ }
+ }
+
function testFile() {
$data = "dummy file data\n";
$this->storage->file_put_contents('foo.txt', $data);
@@ -184,18 +199,38 @@ class Scanner extends \PHPUnit_Framework_TestCase {
$this->assertFalse($this->cache->inCache('folder/bar.txt'));
}
- function setUp() {
- $this->storage = new \OC\Files\Storage\Temporary(array());
- $this->scanner = new \OC\Files\Cache\Scanner($this->storage);
- $this->cache = new \OC\Files\Cache\Cache($this->storage);
+ public function testScanRemovedFile(){
+ $this->fillTestFolders();
+
+ $this->scanner->scan('');
+ $this->assertTrue($this->cache->inCache('folder/bar.txt'));
+ $this->storage->unlink('folder/bar.txt');
+ $this->scanner->scanFile('folder/bar.txt');
+ $this->assertFalse($this->cache->inCache('folder/bar.txt'));
}
- function tearDown() {
- if ($this->cache) {
- $ids = $this->cache->getAll();
- $permissionsCache = $this->storage->getPermissionsCache();
- $permissionsCache->removeMultiple($ids, \OC_User::getUser());
- $this->cache->clear();
- }
+ public function testETagRecreation() {
+ $this->fillTestFolders();
+
+ $this->scanner->scan('folder/bar.txt');
+
+ // manipulate etag to simulate an empty etag
+ $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
+ $data0 = $this->cache->get('folder/bar.txt');
+ $data1 = $this->cache->get('folder');
+ $data2 = $this->cache->get('');
+ $data0['etag'] = '';
+ $this->cache->put('folder/bar.txt', $data0);
+
+ // rescan
+ $this->scanner->scan('folder/bar.txt', \OC\Files\Cache\Scanner::SCAN_SHALLOW, \OC\Files\Cache\Scanner::REUSE_ETAG);
+
+ // verify cache content
+ $newData0 = $this->cache->get('folder/bar.txt');
+ $newData1 = $this->cache->get('folder');
+ $newData2 = $this->cache->get('');
+ $this->assertNotEmpty($newData0['etag']);
+ $this->assertNotEquals($data1['etag'], $newData1['etag']);
+ $this->assertNotEquals($data2['etag'], $newData2['etag']);
}
}
diff --git a/tests/lib/files/node/file.php b/tests/lib/files/node/file.php
new file mode 100644
index 00000000000..76938a0dcc8
--- /dev/null
+++ b/tests/lib/files/node/file.php
@@ -0,0 +1,664 @@
+<?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 OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
+use OC\Files\View;
+
+class File extends \PHPUnit_Framework_TestCase {
+ private $user;
+
+ public function setUp() {
+ $this->user = new \OC\User\User('', new \OC_User_Dummy);
+ }
+
+ 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(array('permissions' => \OCP\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());
+ $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(array('permissions' => \OCP\PERMISSION_ALL, 'fileid' => 1)));
+
+ $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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(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(array('permissions' => \OCP\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(array('permissions' => \OCP\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('getMimeType')
+ ->with('/bar/foo')
+ ->will($this->returnValue('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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(array('permissions' => \OCP\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/folder.php b/tests/lib/files/node/folder.php
new file mode 100644
index 00000000000..b1589a276ba
--- /dev/null
+++ b/tests/lib/files/node/folder.php
@@ -0,0 +1,479 @@
+<?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\Node\Node;
+use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
+use OC\Files\View;
+
+class Folder extends \PHPUnit_Framework_TestCase {
+ private $user;
+
+ public function setUp() {
+ $this->user = new \OC\User\User('', new \OC_User_Dummy);
+ }
+
+ 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(array('permissions' => \OCP\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());
+ $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(array('permissions' => \OCP\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(array('permissions' => \OCP\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));
+
+ /**
+ * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
+ */
+ $storage = $this->getMock('\OC\Files\Storage\Storage');
+
+ $cache = $this->getMock('\OC\Files\Cache\Cache', array(), array(''));
+ $cache->expects($this->any())
+ ->method('getStatus')
+ ->with('foo')
+ ->will($this->returnValue(Cache::COMPLETE));
+
+ $cache->expects($this->once())
+ ->method('getFolderContents')
+ ->with('foo')
+ ->will($this->returnValue(array(
+ array('fileid' => 2, 'path' => '/bar/foo/asd', 'name' => 'asd', 'size' => 100, 'mtime' => 50, 'mimetype' => 'text/plain'),
+ array('fileid' => 3, 'path' => '/bar/foo/qwerty', 'name' => 'qwerty', 'size' => 200, 'mtime' => 55, 'mimetype' => 'httpd/unix-directory')
+ )));
+
+ $permissionsCache = $this->getMock('\OC\Files\Cache\Permissions', array(), array('/'));
+ $permissionsCache->expects($this->once())
+ ->method('getDirectoryPermissions')
+ ->will($this->returnValue(array(2 => \OCP\PERMISSION_ALL)));
+
+ $root->expects($this->once())
+ ->method('getMountsIn')
+ ->with('/bar/foo')
+ ->will($this->returnValue(array()));
+
+ $storage->expects($this->any())
+ ->method('getPermissionsCache')
+ ->will($this->returnValue($permissionsCache));
+ $storage->expects($this->any())
+ ->method('getCache')
+ ->will($this->returnValue($cache));
+
+ $view->expects($this->any())
+ ->method('resolvePath')
+ ->with('/bar/foo')
+ ->will($this->returnValue(array($storage, 'foo')));
+
+ $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());
+ }
+
+ 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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(array('permissions' => \OCP\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(array('permissions' => \OCP\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));
+
+ $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()));
+
+ $view->expects($this->once())
+ ->method('resolvePath')
+ ->will($this->returnValue(array($storage, 'foo')));
+
+ $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 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\Mount', array(), array(null, ''));
+
+ $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)));
+
+ $view->expects($this->once())
+ ->method('resolvePath')
+ ->will($this->returnValue(array($storage, 'foo')));
+
+
+ $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));
+ }
+}
diff --git a/tests/lib/files/node/integration.php b/tests/lib/files/node/integration.php
new file mode 100644
index 00000000000..14e1d05853d
--- /dev/null
+++ b/tests/lib/files/node/integration.php
@@ -0,0 +1,122 @@
+<?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\Mount\Manager;
+use OC\Files\Node\Root;
+use OCP\Files\NotFoundException;
+use OCP\Files\NotPermittedException;
+use OC\Files\Storage\Temporary;
+use OC\Files\View;
+use OC\User\User;
+
+class IntegrationTests extends \PHPUnit_Framework_TestCase {
+ /**
+ * @var \OC\Files\Node\Root $root
+ */
+ private $root;
+
+ /**
+ * @var \OC\Files\Storage\Storage[]
+ */
+ private $storages;
+
+ /**
+ * @var \OC\Files\View $view
+ */
+ private $view;
+
+ public function setUp() {
+ \OC\Files\Filesystem::init('', '');
+ \OC\Files\Filesystem::clearMounts();
+ $manager = \OC\Files\Filesystem::getMountManager();
+
+ \OC_Hook::clear('OC_Filesystem');
+
+ \OC_Hook::connect('OC_Filesystem', 'post_write', '\OC\Files\Cache\Updater', 'writeHook');
+ \OC_Hook::connect('OC_Filesystem', 'post_delete', '\OC\Files\Cache\Updater', 'deleteHook');
+ \OC_Hook::connect('OC_Filesystem', 'post_rename', '\OC\Files\Cache\Updater', 'renameHook');
+ \OC_Hook::connect('OC_Filesystem', 'post_touch', '\OC\Files\Cache\Updater', 'touchHook');
+
+ $user = new User(uniqid('user'), new \OC_User_Dummy);
+ \OC_User::setUserId($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/');
+ }
+
+ public function tearDown() {
+ foreach ($this->storages as $storage) {
+ $storage->getCache()->clear();
+ }
+ \OC\Files\Filesystem::clearMounts();
+ }
+
+ 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'));
+ $file->move('/bar.txt');
+ $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->assertNotEquals($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/node.php b/tests/lib/files/node/node.php
new file mode 100644
index 00000000000..cf5fec30522
--- /dev/null
+++ b/tests/lib/files/node/node.php
@@ -0,0 +1,330 @@
+<?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;
+
+class Node extends \PHPUnit_Framework_TestCase {
+ private $user;
+
+ public function setUp() {
+ $this->user = new \OC\User\User('', new \OC_User_Dummy);
+ }
+
+ 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 = 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));
+
+ $view->expects($this->once())
+ ->method('filesize')
+ ->with('/bar/foo')
+ ->will($this->returnValue(100));
+
+ $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 = 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));
+ /**
+ * @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
+ */
+ $storage = $this->getMock('\OC\Files\Storage\Storage');
+
+ $view->expects($this->once())
+ ->method('filemtime')
+ ->with('/bar/foo')
+ ->will($this->returnValue(50));
+
+ $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('filemtime')
+ ->with('/bar/foo')
+ ->will($this->returnValue(100));
+
+ $view->expects($this->once())
+ ->method('getFileInfo')
+ ->with('/bar/foo')
+ ->will($this->returnValue(array('permissions' => \OCP\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(array('permissions' => \OCP\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(array('permissions' => \OCP\PERMISSION_READ)));
+
+ $node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
+ $node->touch(100);
+ }
+}
diff --git a/tests/lib/files/node/root.php b/tests/lib/files/node/root.php
new file mode 100644
index 00000000000..97eaf7f7162
--- /dev/null
+++ b/tests/lib/files/node/root.php
@@ -0,0 +1,106 @@
+<?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 OCP\Files\NotPermittedException;
+use OC\Files\Mount\Manager;
+
+class Root extends \PHPUnit_Framework_TestCase {
+ private $user;
+
+ public function setUp() {
+ $this->user = new \OC\User\User('', new \OC_User_Dummy);
+ }
+
+ 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(array('fileid' => 10, 'path' => 'bar/foo', 'name', 'mimetype' => 'text/plain')));
+
+ $view->expects($this->once())
+ ->method('is_dir')
+ ->with('/bar/foo')
+ ->will($this->returnValue(false));
+
+ $view->expects($this->once())
+ ->method('file_exists')
+ ->with('/bar/foo')
+ ->will($this->returnValue(true));
+
+ $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('file_exists')
+ ->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');
+ }
+}
diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php
index 3bac9e770aa..3043f132b73 100644
--- a/tests/lib/files/view.php
+++ b/tests/lib/files/view.php
@@ -25,7 +25,7 @@ class View extends \PHPUnit_Framework_TestCase {
//login
\OC_User::createUser('test', 'test');
- $this->user=\OC_User::getUser();
+ $this->user = \OC_User::getUser();
\OC_User::setUserId('test');
\OC\Files\Filesystem::clearMounts();
@@ -326,7 +326,50 @@ class View extends \PHPUnit_Framework_TestCase {
}
/**
+ * @medium
+ */
+ function testViewHooks() {
+ $storage1 = $this->getTestStorage();
+ $storage2 = $this->getTestStorage();
+ $defaultRoot = \OC\Files\Filesystem::getRoot();
+ \OC\Files\Filesystem::mount($storage1, array(), '/');
+ \OC\Files\Filesystem::mount($storage2, array(), $defaultRoot . '/substorage');
+ \OC_Hook::connect('OC_Filesystem', 'post_write', $this, 'dummyHook');
+
+ $rootView = new \OC\Files\View('');
+ $subView = new \OC\Files\View($defaultRoot . '/substorage');
+ $this->hookPath = null;
+
+ $rootView->file_put_contents('/foo.txt', 'asd');
+ $this->assertNull($this->hookPath);
+
+ $subView->file_put_contents('/foo.txt', 'asd');
+ $this->assertNotNull($this->hookPath);
+ $this->assertEquals('/substorage/foo.txt', $this->hookPath);
+ }
+
+ private $hookPath;
+
+ function dummyHook($params) {
+ $this->hookPath = $params['path'];
+ }
+
+ public function testSearchNotOutsideView() {
+ $storage1 = $this->getTestStorage();
+ \OC\Files\Filesystem::mount($storage1, array(), '/');
+ $storage1->rename('folder', 'foo');
+ $scanner = $storage1->getScanner();
+ $scanner->scan('');
+
+ $view = new \OC\Files\View('/foo');
+
+ $result = $view->search('.txt');
+ $this->assertCount(1, $result);
+ }
+
+ /**
* @param bool $scan
+ * @param string $class
* @return \OC\Files\Storage\Storage
*/
private function getTestStorage($scan = true, $class = '\OC\Files\Storage\Temporary') {