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;
|
|
|
|
|
2016-11-14 20:20:33 +03: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 15:27:08 +03:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
protected function getNodeClass() {
|
|
|
|
return '\OC\Files\Node\File';
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
protected function getNonExistingNodeClass() {
|
|
|
|
return '\OC\Files\Node\NonExistingFile';
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
2016-11-14 20:20:33 +03:00
|
|
|
protected function getViewDeleteMethod() {
|
|
|
|
return 'unlink';
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetContent() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
|
2016-07-08 20:33:49 +03:00
|
|
|
$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-08 20:33:49 +03:00
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04: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 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('file_get_contents')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn('bar');
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals('bar', $node->getContent());
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testGetContentNotPermitted() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotPermittedException::class);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
|
2016-07-08 20:33:49 +03:00
|
|
|
$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-08 20:33:49 +03:00
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($this->user);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => 0]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->getContent();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPutContent() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
|
2016-07-08 20:33:49 +03:00
|
|
|
$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-08 20:33:49 +03:00
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$root->expects($this->any())
|
|
|
|
->method('getUser')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($this->user);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('file_put_contents')
|
|
|
|
->with('/bar/foo', 'bar')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->putContent('bar');
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testPutContentNotPermitted() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotPermittedException::class);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
|
2016-07-08 20:33:49 +03:00
|
|
|
$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-08 20:33:49 +03:00
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->putContent('bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetMimeType() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\Files\Node\Root|\PHPUnit\Framework\MockObject\MockObject $root */
|
2016-07-08 20:33:49 +03:00
|
|
|
$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-08 20:33:49 +03:00
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +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')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['mimetype' => 'text/plain']));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$this->assertEquals('text/plain', $node->getMimeType());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFOpenRead() {
|
|
|
|
$stream = fopen('php://memory', 'w+');
|
|
|
|
fwrite($stream, 'bar');
|
|
|
|
rewind($stream);
|
|
|
|
|
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
|
|
|
|
|
|
|
$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 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('fopen')
|
|
|
|
->with('/bar/foo', 'r')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($stream);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04: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-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
new $this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 21:47:48 +04:00
|
|
|
$hooksCalled = 0;
|
|
|
|
$hook = function ($file) use (&$hooksCalled) {
|
|
|
|
$hooksCalled++;
|
|
|
|
};
|
|
|
|
|
|
|
|
$root->listen('\OC\Files', 'preWrite', $hook);
|
|
|
|
$root->listen('\OC\Files', 'postWrite', $hook);
|
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('fopen')
|
|
|
|
->with('/bar/foo', 'w')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($stream);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$fh = $node->fopen('w');
|
|
|
|
$this->assertEquals($stream, $fh);
|
|
|
|
fwrite($fh, 'bar');
|
|
|
|
rewind($fh);
|
|
|
|
$this->assertEquals('bar', fread($stream, 3));
|
|
|
|
$this->assertEquals(2, $hooksCalled);
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testFOpenReadNotPermitted() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotPermittedException::class);
|
|
|
|
|
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
|
|
|
$hook = function ($file) {
|
|
|
|
throw new \Exception('Hooks are not supposed to be called');
|
|
|
|
};
|
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => 0]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->fopen('r');
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testFOpenReadWriteNoReadPermissions() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotPermittedException::class);
|
|
|
|
|
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
|
|
|
$hook = function () {
|
|
|
|
throw new \Exception('Hooks are not supposed to be called');
|
|
|
|
};
|
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_UPDATE]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->fopen('w');
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testFOpenReadWriteNoWritePermissions() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotPermittedException::class);
|
|
|
|
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
new $this->view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 21:47:48 +04:00
|
|
|
$hook = function () {
|
|
|
|
throw new \Exception('Hooks are not supposed to be called');
|
|
|
|
};
|
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$this->view->expects($this->once())
|
2013-09-01 21:47:48 +04:00
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_READ]));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-07-08 20:33:49 +03:00
|
|
|
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
|
2013-09-01 21:47:48 +04:00
|
|
|
$node->fopen('w');
|
|
|
|
}
|
|
|
|
}
|