* This file is licensed under the Affero General Public License version 3 or
* later.
* See the COPYING-README file.
*/
namespace Test\Files\Node;
use OC\Files\FileInfo;
use OCP\Files\NotFoundException;
class FileTest extends \Test\TestCase {
/** @var \OC\User\User */
private $user;
/** @var \OC\Files\Mount\Manager */
private $manager;
/** @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject */
private $view;
protected function setUp() {
parent::setUp();
$config = $this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock();
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config);
$this->manager = $this->getMockBuilder('\OC\Files\Mount\Manager')
->disableOriginalConstructor()
->getMock();
$this->view = $this->getMockBuilder('\OC\Files\View')
->disableOriginalConstructor()
->getMock();
}
protected function getMockStorage() {
$storage = $this->getMockBuilder('\OCP\Files\Storage')
->getMock();
$storage->expects($this->any())
->method('getId')
->will($this->returnValue('home::someuser'));
return $storage;
}
protected function getFileInfo($data) {
return new FileInfo('', $this->getMockStorage(), '', $data, null);
}
public function testDelete() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$root->expects($this->exactly(2))
->method('emit')
->will($this->returnValue(true));
$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(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$this->view->expects($this->once())
->method('unlink')
->with('/bar/foo')
->will($this->returnValue(true));
$node = new \OC\Files\Node\File($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('\OC\Files\Node\File', $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('\OC\Files\Node\NonExistingFile', $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);
$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(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1, 'mimetype' => 'text/plain'))));
$this->view->expects($this->once())
->method('unlink')
->with('/bar/foo')
->will($this->returnValue(true));
$this->view->expects($this->any())
->method('resolvePath')
->with('/bar/foo')
->will($this->returnValue(array(null, 'foo')));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->delete();
$this->assertEquals(2, $hooksRun);
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testDeleteNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$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(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->delete();
}
public function testGetContent() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$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);
$this->view->expects($this->once())
->method('file_get_contents')
->with('/bar/foo')
->will($this->returnValue('bar'));
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$this->assertEquals('bar', $node->getContent());
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testGetContentNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$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(array('permissions' => 0))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->getContent();
}
public function testPutContent() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$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(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$this->view->expects($this->once())
->method('file_put_contents')
->with('/bar/foo', 'bar')
->will($this->returnValue(true));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->putContent('bar');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testPutContentNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->putContent('bar');
}
public function testGetMimeType() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('mimetype' => 'text/plain'))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$this->assertEquals('text/plain', $node->getMimeType());
}
public function testFOpenRead() {
$stream = fopen('php://memory', 'w+');
fwrite($stream, 'bar');
rewind($stream);
$root = new \OC\Files\Node\Root($this->manager, $this->view, $this->user);
$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);
$this->view->expects($this->once())
->method('fopen')
->with('/bar/foo', 'r')
->will($this->returnValue($stream));
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$fh = $node->fopen('r');
$this->assertEquals($stream, $fh);
$this->assertEquals('bar', fread($fh, 3));
}
public function testFOpenWrite() {
$stream = fopen('php://memory', 'w+');
$root = new \OC\Files\Node\Root($this->manager, new $this->view, $this->user);
$hooksCalled = 0;
$hook = function ($file) use (&$hooksCalled) {
$hooksCalled++;
};
$root->listen('\OC\Files', 'preWrite', $hook);
$root->listen('\OC\Files', 'postWrite', $hook);
$this->view->expects($this->once())
->method('fopen')
->with('/bar/foo', 'w')
->will($this->returnValue($stream));
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$fh = $node->fopen('w');
$this->assertEquals($stream, $fh);
fwrite($fh, 'bar');
rewind($fh);
$this->assertEquals('bar', fread($stream, 3));
$this->assertEquals(2, $hooksCalled);
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testFOpenReadNotPermitted() {
$root = new \OC\Files\Node\Root($this->manager, $this->view, $this->user);
$hook = function ($file) {
throw new \Exception('Hooks are not supposed to be called');
};
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => 0))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->fopen('r');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testFOpenReadWriteNoReadPermissions() {
$root = new \OC\Files\Node\Root($this->manager, $this->view, $this->user);
$hook = function () {
throw new \Exception('Hooks are not supposed to be called');
};
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_UPDATE))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->fopen('w');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testFOpenReadWriteNoWritePermissions() {
$root = new \OC\Files\Node\Root($this->manager, new $this->view, $this->user);
$hook = function () {
throw new \Exception('Hooks are not supposed to be called');
};
$this->view->expects($this->once())
->method('getFileInfo')
->with('/bar/foo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$node->fopen('w');
}
public function testCopySameStorage() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->any())
->method('copy')
->with('/bar/foo', '/bar/asd');
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 3))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$newNode = new \OC\Files\Node\File($root, $this->view, '/bar/asd');
$root->expects($this->exactly(2))
->method('get')
->will($this->returnValueMap(array(
array('/bar/asd', $newNode),
array('/bar', $parentNode)
)));
$target = $node->copy('/bar/asd');
$this->assertInstanceOf('\OC\Files\Node\File', $target);
$this->assertEquals(3, $target->getId());
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testCopyNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
->disableOriginalConstructor()
->getMock();
$root->expects($this->never())
->method('getMount');
$storage->expects($this->never())
->method('copy');
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ, 'fileid' => 3))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->will($this->returnValueMap(array(
array('/bar', $parentNode)
)));
$node->copy('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotFoundException
*/
public function testCopyNoParent() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->never())
->method('copy');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$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() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->never())
->method('copy');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\File($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->will($this->returnValueMap(array(
array('/bar', $parentNode)
)));
$node->copy('/bar/asd');
}
public function testMoveSameStorage() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->any())
->method('rename')
->with('/bar/foo', '/bar/asd');
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1))));
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$root->expects($this->any())
->method('get')
->will($this->returnValueMap(array(array('/bar', $parentNode), array('/bar/asd', $node))));
$target = $node->move('/bar/asd');
$this->assertInstanceOf('\OC\Files\Node\File', $target);
$this->assertEquals(1, $target->getId());
$this->assertEquals('/bar/asd', $node->getPath());
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testMoveNotPermitted() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->any())
->method('getFileInfo')
->will($this->returnValue($this->getFileInfo(array('permissions' => \OCP\Constants::PERMISSION_READ))));
$this->view->expects($this->never())
->method('rename');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$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\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
/**
* @var \OC\Files\Storage\Storage | \PHPUnit_Framework_MockObject_MockObject $storage
*/
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
->disableOriginalConstructor()
->getMock();
$storage->expects($this->never())
->method('rename');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->with('/bar')
->will($this->throwException(new NotFoundException()));
$node->move('/bar/asd');
}
/**
* @expectedException \OCP\Files\NotPermittedException
*/
public function testMoveParentIsFile() {
/** @var \OC\Files\Node\Root|\PHPUnit_Framework_MockObject_MockObject $root */
$root = $this->getMockBuilder('\OC\Files\Node\Root')
->setConstructorArgs([$this->manager, $this->view, $this->user])
->getMock();
$this->view->expects($this->never())
->method('rename');
$node = new \OC\Files\Node\File($root, $this->view, '/bar/foo');
$parentNode = new \OC\Files\Node\File($root, $this->view, '/bar');
$root->expects($this->once())
->method('get')
->with('/bar')
->will($this->returnValue($parentNode));
$node->move('/bar/asd');
}
}
option>
Nextcloud server, a safe home for all your data: https://github.com/nextcloud/server | www-data |
blob: 65d4c7938f9583984644c203b1939ba0dca81f27 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
<?php
/**
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Template;
use OC\SystemConfig;
use OC\Template\ResourceNotFoundException;
use Psr\Log\LoggerInterface;
class ResourceLocatorTest extends \Test\TestCase {
/** @var \PHPUnit\Framework\MockObject\MockObject */
protected $logger;
protected function setUp(): void {
parent::setUp();
$this->logger = $this->createMock(LoggerInterface::class);
}
/**
* @param string $theme
* @return \PHPUnit\Framework\MockObject\MockObject
*/
public function getResourceLocator($theme) {
$systemConfig = $this->createMock(SystemConfig::class);
$systemConfig
->expects($this->any())
->method('getValue')
->with('theme', '')
->willReturn($theme);
$this->overwriteService(SystemConfig::class, $systemConfig);
return $this->getMockForAbstractClass('OC\Template\ResourceLocator',
[$this->logger],
'', true, true, true, []);
}
public function testFind(): void {
$locator = $this->getResourceLocator('theme');
$locator->expects($this->once())
->method('doFind')
->with('foo');
$locator->expects($this->once())
->method('doFindTheme')
->with('foo');
/** @var \OC\Template\ResourceLocator $locator */
$locator->find(['foo']);
}
public function testFindNotFound(): void {
$systemConfig = $this->createMock(SystemConfig::class);
$systemConfig->method('getValue')
->with('theme', '')
->willReturn('theme');
$this->overwriteService(SystemConfig::class, $systemConfig);
$locator = $this->getResourceLocator('theme',
['core' => 'map'], ['3rd' => 'party'], ['foo' => 'bar']);
$locator->expects($this->once())
->method('doFind')
->with('foo')
->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
$locator->expects($this->once())
->method('doFindTheme')
->with('foo')
->will($this->throwException(new ResourceNotFoundException('foo', 'map')));
$this->logger->expects($this->exactly(2))
->method('debug')
->with($this->stringContains('map/foo'));
/** @var \OC\Template\ResourceLocator $locator */
$locator->find(['foo']);
}
public function testAppendIfExist(): void {
$locator = $this->getResourceLocator('theme');
/** @var \OC\Template\ResourceLocator $locator */
$method = new \ReflectionMethod($locator, 'appendIfExist');
$method->setAccessible(true);
$method->invoke($locator, __DIR__, basename(__FILE__), 'webroot');
$resource1 = [__DIR__, 'webroot', basename(__FILE__)];
$this->assertEquals([$resource1], $locator->getResources());
$method->invoke($locator, __DIR__, 'does-not-exist');
$this->assertEquals([$resource1], $locator->getResources());
}
}
|