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;
|
|
|
|
|
2016-11-14 17:20:33 +00:00
|
|
|
/**
|
|
|
|
* Class FileTest
|
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
|
|
|
* @package Test\Files\Node
|
|
|
|
*/
|
|
|
|
class FileTest extends NodeTest {
|
|
|
|
protected function createTestNode($root, $view, $path) {
|
|
|
|
return new \OC\Files\Node\File($root, $view, $path);
|
2014-12-04 12:27:08 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 17:20:33 +00:00
|
|
|
protected function getNodeClass() {
|
|
|
|
return '\OC\Files\Node\File';
|
2013-09-01 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 17:20:33 +00:00
|
|
|
protected function getNonExistingNodeClass() {
|
|
|
|
return '\OC\Files\Node\NonExistingFile';
|
2013-09-01 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
2016-11-14 17:20:33 +00:00
|
|
|
protected function getViewDeleteMethod() {
|
|
|
|
return 'unlink';
|
2013-09-01 17:47:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetContent() {
|
2016-07-08 17:33:49 +00:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
|
|
|
|
$root = $this->getMockBuilder('\OC\Files\Node\Root')
|
2016-11-02 22:16:51 +00:00
|
|
|
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
|
2016-07-08 17:33:49 +00:00
|
|
|
->getMock();
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->method('file_get_contents')
|
|
|
|
->with('/bar/foo')
|
|
|
|
->will($this->returnValue('bar'));
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->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
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$this->assertEquals('bar', $node->getContent());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-10 20:21:49 +00:00
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
2013-09-01 17:47:48 +00:00
|
|
|
*/
|
|
|
|
public function testGetContentNotPermitted() {
|
2016-07-08 17:33:49 +00:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
|
|
|
|
$root = $this->getMockBuilder('\OC\Files\Node\Root')
|
2016-11-02 22:16:51 +00:00
|
|
|
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
|
2016-07-08 17:33:49 +00:00
|
|
|
->getMock();
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => 0))));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$node->getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPutContent() {
|
2016-07-08 17:33:49 +00:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
|
|
|
|
$root = $this->getMockBuilder('\OC\Files\Node\Root')
|
2016-11-02 22:16:51 +00:00
|
|
|
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
|
2016-07-08 17:33:49 +00:00
|
|
|
->getMock();
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->will($this->returnValue($this->user));
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->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
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->method('file_put_contents')
|
|
|
|
->with('/bar/foo', 'bar')
|
|
|
|
->will($this->returnValue(true));
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$node->putContent('bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-10 20:21:49 +00:00
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
2013-09-01 17:47:48 +00:00
|
|
|
*/
|
|
|
|
public function testPutContentNotPermitted() {
|
2016-07-08 17:33:49 +00:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
|
|
|
|
$root = $this->getMockBuilder('\OC\Files\Node\Root')
|
2016-11-02 22:16:51 +00:00
|
|
|
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
|
2016-07-08 17:33:49 +00:00
|
|
|
->getMock();
|
2013-09-01 17:47:48 +00:00
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->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
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$node->putContent('bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMimeType() {
|
2016-07-08 17:33:49 +00:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
|
|
|
|
$root = $this->getMockBuilder('\OC\Files\Node\Root')
|
2016-11-02 22:16:51 +00:00
|
|
|
->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager])
|
2016-07-08 17:33:49 +00:00
|
|
|
->getMock();
|
2013-09-01 17:47:48 +00:00
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->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($this->getFileInfo(array('mimetype' => 'text/plain'))));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$this->assertEquals('text/plain', $node->getMimeType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFOpenRead() {
|
|
|
|
$stream = fopen('php://memory', 'w+');
|
|
|
|
fwrite($stream, 'bar');
|
|
|
|
rewind($stream);
|
|
|
|
|
2016-11-02 22:16:51 +00:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 17:47:48 +00:00
|
|
|
|
|
|
|
$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);
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->method('fopen')
|
|
|
|
->with('/bar/foo', 'r')
|
|
|
|
->will($this->returnValue($stream));
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->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
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$fh = $node->fopen('r');
|
|
|
|
$this->assertEquals($stream, $fh);
|
|
|
|
$this->assertEquals('bar', fread($fh, 3));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFOpenWrite() {
|
|
|
|
$stream = fopen('php://memory', 'w+');
|
|
|
|
|
2016-11-02 22:16:51 +00:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
new $this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 17:47:48 +00:00
|
|
|
$hooksCalled = 0;
|
|
|
|
$hook = function ($file) use (&$hooksCalled) {
|
|
|
|
$hooksCalled++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$root->listen('\OC\Files', 'preWrite', $hook);
|
|
|
|
$root->listen('\OC\Files', 'postWrite', $hook);
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->method('fopen')
|
|
|
|
->with('/bar/foo', 'w')
|
|
|
|
->will($this->returnValue($stream));
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->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
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$fh = $node->fopen('w');
|
|
|
|
$this->assertEquals($stream, $fh);
|
|
|
|
fwrite($fh, 'bar');
|
|
|
|
rewind($fh);
|
|
|
|
$this->assertEquals('bar', fread($stream, 3));
|
|
|
|
$this->assertEquals(2, $hooksCalled);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-10 20:21:49 +00:00
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
2013-09-01 17:47:48 +00:00
|
|
|
*/
|
|
|
|
public function testFOpenReadNotPermitted() {
|
2016-11-02 22:16:51 +00:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 17:47:48 +00:00
|
|
|
$hook = function ($file) {
|
|
|
|
throw new \Exception('Hooks are not supposed to be called');
|
|
|
|
};
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => 0))));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$node->fopen('r');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-10 20:21:49 +00:00
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
2013-09-01 17:47:48 +00:00
|
|
|
*/
|
|
|
|
public function testFOpenReadWriteNoReadPermissions() {
|
2016-11-02 22:16:51 +00:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 17:47:48 +00:00
|
|
|
$hook = function () {
|
|
|
|
throw new \Exception('Hooks are not supposed to be called');
|
|
|
|
};
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2014-12-04 12:27:08 +00:00
|
|
|
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_UPDATE))));
|
2013-09-01 17:47:48 +00:00
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$node->fopen('w');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-09-10 20:21:49 +00:00
|
|
|
* @expectedException \OCP\Files\NotPermittedException
|
2013-09-01 17:47:48 +00:00
|
|
|
*/
|
|
|
|
public function testFOpenReadWriteNoWritePermissions() {
|
2016-11-02 22:16:51 +00:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
new $this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 17:47:48 +00:00
|
|
|
$hook = function () {
|
|
|
|
throw new \Exception('Hooks are not supposed to be called');
|
|
|
|
};
|
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 17:47:48 +00:00
|
|
|
->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
|
|
|
|
2016-07-08 17:33:49 +00:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 17:47:48 +00:00
|
|
|
$node->fopen('w');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|