2013-09-01 17:47:48 +00: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 12:27:08 +00:00
|
|
|
use OC\Files\FileInfo;
|
|
|
|
|
2014-11-07 14:23:15 +00:00
|
|
|
class Node extends \Test\TestCase {
|
2013-09-01 17:47:48 +00:00
|
|
|
private $user;
|
|
|
|
|
2014-11-07 14:23:15 +00:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
2015-09-21 22:56:36 +00:00
|
|
|
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy);
|
2013-09-01 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
2014-12-04 12:27:08 +00:00
|
|
|
protected function getFileInfo($data) {
|
2014-12-16 13:24:48 +00:00
|
|
|
return new FileInfo('', null, '', $data, null);
|
2014-12-04 12:27:08 +00:00
|
|
|
}
|
|
|
|
|
2013-09-01 17:47:48 +00:00
|
|
|
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));
|
|
|
|
|
2014-12-04 12:27:08 +00:00
|
|
|
$stat = $this->getFileInfo(array(
|
2013-09-01 17:47:48 +00:00
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
2014-12-04 12:27:08 +00:00
|
|
|
));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$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));
|
|
|
|
|
2014-12-04 12:27:08 +00:00
|
|
|
|
|
|
|
$stat = $this->getFileInfo(array(
|
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
|
|
|
));
|
|
|
|
|
2013-09-01 17:47:48 +00:00
|
|
|
$view->expects($this->once())
|
2014-12-04 12:27:08 +00:00
|
|
|
->method('getFileInfo')
|
2013-09-01 17:47:48 +00:00
|
|
|
->with('/bar/foo')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($stat));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$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));
|
|
|
|
|
2014-12-04 12:27:08 +00:00
|
|
|
$stat = $this->getFileInfo(array(
|
2013-09-01 17:47:48 +00:00
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
2014-12-04 12:27:08 +00:00
|
|
|
));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$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));
|
2014-12-04 12:27:08 +00:00
|
|
|
|
|
|
|
$stat = $this->getFileInfo(array(
|
|
|
|
'fileid' => 1,
|
|
|
|
'size' => 100,
|
|
|
|
'etag' => 'qwerty',
|
|
|
|
'mtime' => 50
|
|
|
|
));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$view->expects($this->once())
|
2014-12-04 12:27:08 +00:00
|
|
|
->method('getFileInfo')
|
2013-09-01 17:47:48 +00:00
|
|
|
->with('/bar/foo')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($stat));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
|
|
|
|
$this->assertEquals(50, $node->getMTime());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetStorage() {
|
|
|
|
$manager = $this->getMock('\OC\Files\Mount\Manager');
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
|
|
|
|
*/
|
|
|
|
$view = $this->getMock('\OC\Files\View');
|
|
|
|
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
|
|
|
|
*/
|
|
|
|
$storage = $this->getMock('\OC\Files\Storage\Storage');
|
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('resolvePath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue(array($storage, 'foo')));
|
|
|
|
|
|
|
|
|
|
|
|
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
|
|
|
|
$this->assertEquals($storage, $node->getStorage());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPath() {
|
|
|
|
$manager = $this->getMock('\OC\Files\Mount\Manager');
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
|
|
|
|
*/
|
|
|
|
$view = $this->getMock('\OC\Files\View');
|
|
|
|
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
|
|
|
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
|
|
|
|
$this->assertEquals('/bar/foo', $node->getPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetInternalPath() {
|
|
|
|
$manager = $this->getMock('\OC\Files\Mount\Manager');
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
|
|
|
|
*/
|
|
|
|
$view = $this->getMock('\OC\Files\View');
|
|
|
|
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
|
|
|
|
*/
|
|
|
|
$storage = $this->getMock('\OC\Files\Storage\Storage');
|
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('resolvePath')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue(array($storage, 'foo')));
|
|
|
|
|
|
|
|
|
|
|
|
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
|
|
|
|
$this->assertEquals('foo', $node->getInternalPath());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetName() {
|
|
|
|
$manager = $this->getMock('\OC\Files\Mount\Manager');
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
|
|
|
|
*/
|
|
|
|
$view = $this->getMock('\OC\Files\View');
|
|
|
|
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
|
|
|
$node = new \OC\Files\Node\File($root, $view, '/bar/foo');
|
|
|
|
$this->assertEquals('foo', $node->getName());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTouchSetMTime() {
|
|
|
|
$manager = $this->getMock('\OC\Files\Mount\Manager');
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
|
|
|
|
*/
|
|
|
|
$view = $this->getMock('\OC\Files\View');
|
|
|
|
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('touch')
|
|
|
|
->with('/bar/foo', 100)
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$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')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
|
|
|
|
$node->touch(100);
|
|
|
|
$this->assertEquals(2, $hooksRun);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-10 20:21:49 +00:00
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
2013-09-01 17:47:48 +00:00
|
|
|
*/
|
|
|
|
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')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$node = new \OC\Files\Node\Node($root, $view, '/bar/foo');
|
|
|
|
$node->touch(100);
|
|
|
|
}
|
2015-02-26 11:20:20 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException \OCP\Files\InvalidPathException
|
|
|
|
*/
|
|
|
|
public function testInvalidPath() {
|
|
|
|
$manager = $this->getMock('\OC\Files\Mount\Manager');
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\View | \PHPUnit_Framework_MockObject_MockObject $view
|
|
|
|
*/
|
|
|
|
$view = $this->getMock('\OC\Files\View');
|
|
|
|
$root = $this->getMock('\OC\Files\Node\Root', array(), array($manager, $view, $this->user));
|
|
|
|
|
|
|
|
$node = new \OC\Files\Node\Node($root, $view, '/../foo');
|
|
|
|
$node->getFileInfo();
|
|
|
|
}
|
2013-09-01 17:47:48 +00:00
|
|
|
}
|