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