2013-09-01 21:47:48 +04:00
|
|
|
<?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;
|
|
|
|
|
2014-12-04 15:27:08 +03:00
|
|
|
use OC\Files\FileInfo;
|
2017-10-25 01:03:28 +03:00
|
|
|
use OC\Files\Mount\Manager;
|
2016-11-14 20:20:33 +03:00
|
|
|
use OC\Files\View;
|
|
|
|
use OCP\Files\Config\IUserMountCache;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\Node;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\Files\Storage;
|
|
|
|
use OCP\IConfig;
|
2016-11-03 01:16:51 +03:00
|
|
|
use OCP\ILogger;
|
2017-10-24 16:26:53 +03:00
|
|
|
use OCP\IURLGenerator;
|
2016-11-03 01:16:51 +03:00
|
|
|
use OCP\IUserManager;
|
2016-11-14 20:20:33 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2014-12-04 15:27:08 +03:00
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
/**
|
|
|
|
* Class NodeTest
|
|
|
|
*
|
|
|
|
* @package Test\Files\Node
|
|
|
|
*/
|
|
|
|
abstract class NodeTest extends \Test\TestCase {
|
2016-07-11 16:41:02 +03:00
|
|
|
/** @var \OC\User\User */
|
2016-11-14 20:20:33 +03:00
|
|
|
protected $user;
|
2016-07-11 16:41:02 +03:00
|
|
|
/** @var \OC\Files\Mount\Manager */
|
2016-11-14 20:20:33 +03:00
|
|
|
protected $manager;
|
2016-07-11 16:41:02 +03:00
|
|
|
/** @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject */
|
2016-11-14 20:20:33 +03:00
|
|
|
protected $view;
|
2016-07-11 16:41:02 +03:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject */
|
2016-11-14 20:20:33 +03:00
|
|
|
protected $root;
|
2016-09-18 19:36:53 +03:00
|
|
|
/** @var \OCP\Files\Config\IUserMountCache|\PHPUnit_Framework_MockObject_MockObject */
|
2016-11-14 20:20:33 +03:00
|
|
|
protected $userMountCache;
|
2016-11-03 01:16:51 +03:00
|
|
|
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
|
2016-11-14 20:20:33 +03:00
|
|
|
protected $logger;
|
2016-11-03 01:16:51 +03:00
|
|
|
/** @var IUserManager|\PHPUnit_Framework_MockObject_MockObject */
|
2016-11-14 20:20:33 +03:00
|
|
|
protected $userManager;
|
2016-07-11 16:41:02 +03:00
|
|
|
|
2014-11-07 17:23:15 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2016-07-11 16:41:02 +03:00
|
|
|
|
2017-10-24 16:26:53 +03:00
|
|
|
$config = $this->getMockBuilder(IConfig::class)
|
2016-07-11 16:41:02 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$urlGenerator = $this->getMockBuilder(IURLGenerator
|
|
|
|
::class)
|
2016-07-11 16:41:02 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config, $urlGenerator);
|
2017-10-25 01:03:28 +03:00
|
|
|
$this->manager = $this->getMockBuilder(Manager::class)
|
2016-07-11 16:41:02 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2017-10-24 16:26:53 +03:00
|
|
|
$this->view = $this->getMockBuilder(View::class)
|
2016-07-11 16:41:02 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-18 19:36:53 +03:00
|
|
|
$this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-11-03 01:16:51 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root = $this->getMockBuilder('\OC\Files\Node\Root')
|
2016-11-03 01:16:51 +03:00
|
|
|
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
|
2016-07-11 16:41:02 +03:00
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
/**
|
|
|
|
* @param IRootFolder $root
|
|
|
|
* @param View $view
|
|
|
|
* @param string $path
|
|
|
|
* @return Node
|
|
|
|
*/
|
|
|
|
protected abstract function createTestNode($root, $view, $path);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected abstract function getNodeClass();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected abstract function getNonExistingNodeClass();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected abstract function getViewDeleteMethod();
|
|
|
|
|
2015-12-08 18:33:39 +03:00
|
|
|
protected function getMockStorage() {
|
2017-10-24 16:26:53 +03:00
|
|
|
$storage = $this->getMockBuilder(Storage::class)
|
2016-07-11 16:41:02 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2015-12-08 18:33:39 +03:00
|
|
|
$storage->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->will($this->returnValue('home::someuser'));
|
|
|
|
return $storage;
|
|
|
|
}
|
|
|
|
|
2014-12-04 15:27:08 +03:00
|
|
|
protected function getFileInfo($data) {
|
2015-12-08 18:33:39 +03:00
|
|
|
return new FileInfo('', $this->getMockStorage(), '', $data, null);
|
2014-12-04 15:27:08 +03:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
public function testDelete() {
|
|
|
|
$this->root->expects($this->exactly(2))
|
|
|
|
->method('emit')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
$this->root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
|
|
|
$this->view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL])));
|
|
|
|
|
|
|
|
$this->view->expects($this->once())
|
|
|
|
->method($this->getViewDeleteMethod())
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$node->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteHooks() {
|
|
|
|
$test = $this;
|
|
|
|
$hooksRun = 0;
|
|
|
|
/**
|
|
|
|
* @param \OC\Files\Node\File $node
|
|
|
|
*/
|
|
|
|
$preListener = function ($node) use (&$test, &$hooksRun) {
|
|
|
|
$test->assertInstanceOf($this->getNodeClass(), $node);
|
|
|
|
$test->assertEquals('foo', $node->getInternalPath());
|
|
|
|
$test->assertEquals('/bar/foo', $node->getPath());
|
|
|
|
$test->assertEquals(1, $node->getId());
|
|
|
|
$hooksRun++;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \OC\Files\Node\File $node
|
|
|
|
*/
|
|
|
|
$postListener = function ($node) use (&$test, &$hooksRun) {
|
|
|
|
$test->assertInstanceOf($this->getNonExistingNodeClass(), $node);
|
|
|
|
$test->assertEquals('foo', $node->getInternalPath());
|
|
|
|
$test->assertEquals('/bar/foo', $node->getPath());
|
|
|
|
$test->assertEquals(1, $node->getId());
|
|
|
|
$test->assertEquals('text/plain', $node->getMimeType());
|
|
|
|
$hooksRun++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
|
|
|
|
|
|
|
$root->listen('\OC\Files', 'preDelete', $preListener);
|
|
|
|
$root->listen('\OC\Files', 'postDelete', $postListener);
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain'])));
|
|
|
|
|
|
|
|
$this->view->expects($this->once())
|
|
|
|
->method($this->getViewDeleteMethod())
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('resolvePath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue([null, 'foo']));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($root, $this->view, '/bar/foo');
|
|
|
|
$node->delete();
|
|
|
|
$this->assertEquals(2, $hooksRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
|
|
|
*/
|
|
|
|
public function testDeleteNotPermitted() {
|
|
|
|
$this->root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
|
|
|
$this->view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$node->delete();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testStat() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
|
|
|
$stat = array(
|
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50,
|
|
|
|
'permissions' => 0
|
|
|
|
);
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('stat')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($stat));
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals($stat, $node->stat());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetId() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2014-12-04 15:27:08 +03:00
|
|
|
$stat = $this->getFileInfo(array(
|
2013-09-01 21:47:48 +04:00
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
2014-12-04 15:27:08 +03:00
|
|
|
));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($stat));
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals(1, $node->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSize() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2014-12-04 15:27:08 +03:00
|
|
|
|
|
|
|
$stat = $this->getFileInfo(array(
|
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
|
|
|
));
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2014-12-04 15:27:08 +03:00
|
|
|
->method('getFileInfo')
|
2013-09-01 21:47:48 +04:00
|
|
|
->with('/bar/foo')
|
2014-12-04 15:27:08 +03:00
|
|
|
->will($this->returnValue($stat));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals(100, $node->getSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetEtag() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2014-12-04 15:27:08 +03:00
|
|
|
$stat = $this->getFileInfo(array(
|
2013-09-01 21:47:48 +04:00
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
2014-12-04 15:27:08 +03:00
|
|
|
));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue($stat));
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals('qwerty', $node->getEtag());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMTime() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
2014-12-04 15:27:08 +03:00
|
|
|
|
|
|
|
$stat = $this->getFileInfo(array(
|
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
|
|
|
));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2014-12-04 15:27:08 +03:00
|
|
|
->method('getFileInfo')
|
2013-09-01 21:47:48 +04:00
|
|
|
->with('/bar/foo')
|
2014-12-04 15:27:08 +03:00
|
|
|
->will($this->returnValue($stat));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals(50, $node->getMTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetStorage() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
|
|
|
|
*/
|
2016-07-11 16:41:02 +03:00
|
|
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('resolvePath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue(array($storage, 'foo')));
|
|
|
|
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals($storage, $node->getStorage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPath() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals('/bar/foo', $node->getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInternalPath() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
|
|
|
|
*/
|
2016-07-11 16:41:02 +03:00
|
|
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('resolvePath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue(array($storage, 'foo')));
|
|
|
|
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals('foo', $node->getInternalPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetName() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals('foo', $node->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTouchSetMTime() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('touch')
|
|
|
|
->with('/bar/foo', 100)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2014-12-04 15:27:08 +03:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$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++;
|
|
|
|
};
|
|
|
|
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 21:47:48 +04:00
|
|
|
$root->listen('\OC\Files', 'preTouch', $preListener);
|
|
|
|
$root->listen('\OC\Files', 'postTouch', $postListener);
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('touch')
|
|
|
|
->with('/bar/foo', 100)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('resolvePath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue(array(null, 'foo')));
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2014-12-04 15:27:08 +03:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->touch(100);
|
|
|
|
$this->assertEquals(2, $hooksRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-11 00:21:49 +04:00
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
|
|
|
public function testTouchNotPermitted() {
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->root->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2016-07-11 16:41:02 +03:00
|
|
|
$this->view->expects($this->any())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2014-12-04 15:27:08 +03:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->touch(100);
|
|
|
|
}
|
2015-02-26 14:20:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\InvalidPathException
|
|
|
|
*/
|
|
|
|
public function testInvalidPath() {
|
2016-11-14 20:20:33 +03:00
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/../foo');
|
2015-02-26 14:20:20 +03:00
|
|
|
$node->getFileInfo();
|
|
|
|
}
|
2016-11-14 20:20:33 +03:00
|
|
|
|
|
|
|
public function testCopySameStorage() {
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('copy')
|
|
|
|
->with('/bar/foo', '/bar/asd')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3])));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
|
|
|
|
$newNode = $this->createTestNode($this->root, $this->view, '/bar/asd');
|
|
|
|
|
|
|
|
$this->root->expects($this->exactly(2))
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['/bar/asd', $newNode],
|
|
|
|
['/bar', $parentNode]
|
|
|
|
]));
|
|
|
|
|
|
|
|
$target = $node->copy('/bar/asd');
|
|
|
|
$this->assertInstanceOf($this->getNodeClass(), $target);
|
|
|
|
$this->assertEquals(3, $target->getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
|
|
|
*/
|
|
|
|
public function testCopyNotPermitted() {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
|
|
|
|
*/
|
|
|
|
$storage = $this->createMock('\OC\Files\Storage\Storage');
|
|
|
|
|
|
|
|
$this->root->expects($this->never())
|
|
|
|
->method('getMount');
|
|
|
|
|
|
|
|
$storage->expects($this->never())
|
|
|
|
->method('copy');
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3])));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
|
|
|
|
|
|
|
|
$this->root->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['/bar', $parentNode]
|
|
|
|
]));
|
|
|
|
|
|
|
|
$node->copy('/bar/asd');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotFoundException
|
|
|
|
*/
|
|
|
|
public function testCopyNoParent() {
|
|
|
|
$this->view->expects($this->never())
|
|
|
|
->method('copy');
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
|
|
|
|
$this->root->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('/bar/asd')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
|
|
|
|
|
|
|
$node->copy('/bar/asd/foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
|
|
|
*/
|
|
|
|
public function testCopyParentIsFile() {
|
|
|
|
$this->view->expects($this->never())
|
|
|
|
->method('copy');
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
|
|
|
|
|
|
|
|
$this->root->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap([
|
|
|
|
['/bar', $parentNode]
|
|
|
|
]));
|
|
|
|
|
|
|
|
$node->copy('/bar/asd');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testMoveSameStorage() {
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('rename')
|
|
|
|
->with('/bar/foo', '/bar/asd')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
|
|
|
|
|
|
|
|
$this->root->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
|
|
|
|
|
|
|
|
$target = $node->move('/bar/asd');
|
|
|
|
$this->assertInstanceOf($this->getNodeClass(), $target);
|
|
|
|
$this->assertEquals(1, $target->getId());
|
|
|
|
$this->assertEquals('/bar/asd', $node->getPath());
|
|
|
|
}
|
|
|
|
|
2016-11-24 19:17:15 +03:00
|
|
|
public function moveOrCopyProvider() {
|
|
|
|
return [
|
|
|
|
['move', 'rename', 'preRename', 'postRename'],
|
|
|
|
['copy', 'copy', 'preCopy', 'postCopy'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider moveOrCopyProvider
|
|
|
|
* @param string $operationMethod
|
|
|
|
* @param string $viewMethod
|
|
|
|
* @param string $preHookName
|
|
|
|
* @param string $postHookName
|
|
|
|
*/
|
|
|
|
public function testMoveCopyHooks($operationMethod, $viewMethod, $preHookName, $postHookName) {
|
|
|
|
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject $root */
|
|
|
|
$root = $this->getMockBuilder('\OC\Files\Node\Root')
|
|
|
|
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
|
|
|
|
->setMethods(['get'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method($viewMethod)
|
|
|
|
->with('/bar/foo', '/bar/asd')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Node\File|\PHPUnit_Framework_MockObject_MockObject $node
|
|
|
|
*/
|
|
|
|
$node = $this->createTestNode($root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
|
|
|
|
$targetTestNode = $this->createTestNode($root, $this->view, '/bar/asd');
|
|
|
|
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $targetTestNode]]));
|
|
|
|
|
|
|
|
$hooksRun = 0;
|
|
|
|
|
|
|
|
$preListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node) {
|
|
|
|
$this->assertSame($node, $sourceNode);
|
|
|
|
$this->assertInstanceOf($this->getNodeClass(), $sourceNode);
|
|
|
|
$this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
|
|
|
|
$this->assertEquals('/bar/asd', $targetNode->getPath());
|
|
|
|
$hooksRun++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$postListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node, $targetTestNode) {
|
|
|
|
$this->assertSame($node, $sourceNode);
|
|
|
|
$this->assertNotSame($node, $targetNode);
|
|
|
|
$this->assertSame($targetTestNode, $targetNode);
|
|
|
|
$this->assertInstanceOf($this->getNodeClass(), $sourceNode);
|
|
|
|
$this->assertInstanceOf($this->getNodeClass(), $targetNode);
|
|
|
|
$hooksRun++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$preWriteListener = function (Node $targetNode) use (&$hooksRun) {
|
|
|
|
$this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode);
|
|
|
|
$this->assertEquals('/bar/asd', $targetNode->getPath());
|
|
|
|
$hooksRun++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$postWriteListener = function (Node $targetNode) use (&$hooksRun, $targetTestNode) {
|
|
|
|
$this->assertSame($targetTestNode, $targetNode);
|
|
|
|
$hooksRun++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$root->listen('\OC\Files', $preHookName, $preListener);
|
|
|
|
$root->listen('\OC\Files', 'preWrite', $preWriteListener);
|
|
|
|
$root->listen('\OC\Files', $postHookName, $postListener);
|
|
|
|
$root->listen('\OC\Files', 'postWrite', $postWriteListener);
|
|
|
|
|
|
|
|
$node->$operationMethod('/bar/asd');
|
|
|
|
|
|
|
|
$this->assertEquals(4, $hooksRun);
|
|
|
|
}
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
|
|
|
*/
|
|
|
|
public function testMoveNotPermitted() {
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ])));
|
|
|
|
|
|
|
|
$this->view->expects($this->never())
|
|
|
|
->method('rename');
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
|
|
|
|
|
|
|
|
$this->root->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('/bar')
|
|
|
|
->will($this->returnValue($parentNode));
|
|
|
|
|
|
|
|
$node->move('/bar/asd');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotFoundException
|
|
|
|
*/
|
|
|
|
public function testMoveNoParent() {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
|
|
|
|
*/
|
|
|
|
$storage = $this->createMock('\OC\Files\Storage\Storage');
|
|
|
|
|
|
|
|
$storage->expects($this->never())
|
|
|
|
->method('rename');
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
|
|
|
|
$this->root->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('/bar')
|
|
|
|
->will($this->throwException(new NotFoundException()));
|
|
|
|
|
|
|
|
$node->move('/bar/asd');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
|
|
|
*/
|
|
|
|
public function testMoveParentIsFile() {
|
|
|
|
$this->view->expects($this->never())
|
|
|
|
->method('rename');
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\File($this->root, $this->view, '/bar');
|
|
|
|
|
|
|
|
$this->root->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('/bar')
|
|
|
|
->will($this->returnValue($parentNode));
|
|
|
|
|
|
|
|
$node->move('/bar/asd');
|
|
|
|
}
|
2016-11-14 20:29:11 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
|
|
|
*/
|
|
|
|
public function testMoveFailed() {
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('rename')
|
|
|
|
->with('/bar/foo', '/bar/asd')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
|
|
|
|
|
|
|
|
$this->root->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
|
|
|
|
|
|
|
|
$node->move('/bar/asd');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
|
|
|
*/
|
|
|
|
public function testCopyFailed() {
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('copy')
|
|
|
|
->with('/bar/foo', '/bar/asd')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$this->view->expects($this->any())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1])));
|
|
|
|
|
|
|
|
$node = $this->createTestNode($this->root, $this->view, '/bar/foo');
|
|
|
|
$parentNode = new \OC\Files\Node\Folder($this->root, $this->view, '/bar');
|
|
|
|
|
|
|
|
$this->root->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $node]]));
|
|
|
|
|
|
|
|
$node->copy('/bar/asd');
|
|
|
|
}
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|