123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480 |
- <?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));
- }
- }
|