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-03 01:16:51 +03:00
|
|
|
use OC\Cache\CappedMemoryCache;
|
2014-12-04 15:27:08 +03:00
|
|
|
use OC\Files\FileInfo;
|
2016-10-04 13:28:41 +03:00
|
|
|
use OC\Files\Mount\Manager;
|
2016-11-03 01:16:51 +03:00
|
|
|
use OC\Files\Node\Folder;
|
|
|
|
use OC\Files\View;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserManager;
|
2013-09-01 21:47:48 +04:00
|
|
|
|
2016-11-03 01:16:51 +03:00
|
|
|
/**
|
|
|
|
* Class RootTest
|
|
|
|
*
|
|
|
|
* @package Test\Files\Node
|
|
|
|
*/
|
2016-05-20 16:38:20 +03:00
|
|
|
class RootTest extends \Test\TestCase {
|
2016-07-13 15:35:34 +03:00
|
|
|
/** @var \OC\User\User */
|
2013-09-01 21:47:48 +04:00
|
|
|
private $user;
|
2016-07-13 15:35:34 +03:00
|
|
|
/** @var \OC\Files\Mount\Manager */
|
|
|
|
private $manager;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OCP\Files\Config\IUserMountCache|\PHPUnit\Framework\MockObject\MockObject */
|
2016-09-18 19:36:53 +03:00
|
|
|
private $userMountCache;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ILogger|\PHPUnit\Framework\MockObject\MockObject */
|
2016-11-03 01:16:51 +03:00
|
|
|
private $logger;
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
|
2016-11-03 01:16:51 +03:00
|
|
|
private $userManager;
|
2016-07-13 15:35:34 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-07 17:23:15 +03:00
|
|
|
parent::setUp();
|
2016-07-13 15:35:34 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
2017-10-25 01:03:28 +03:00
|
|
|
$this->manager = $this->getMockBuilder(Manager::class)
|
2016-07-13 15:35:34 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-09-18 19:36:53 +03:00
|
|
|
$this->userMountCache = $this->getMockBuilder('\OCP\Files\Config\IUserMountCache')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-11-03 01:16:51 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
|
|
|
$this->userManager = $this->createMock(IUserManager::class);
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|
|
|
|
|
2014-12-04 15:27:08 +03:00
|
|
|
protected function getFileInfo($data) {
|
2014-12-16 16:24:48 +03:00
|
|
|
return new FileInfo('', null, '', $data, null);
|
2014-12-04 15:27:08 +03:00
|
|
|
}
|
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testGet() {
|
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
*/
|
2016-07-13 15:35:34 +03:00
|
|
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-13 15:35:34 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$view->expects($this->once())
|
|
|
|
->method('getFileInfo')
|
|
|
|
->with('/bar/foo')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn($this->getFileInfo(['fileid' => 10, 'path' => 'bar/foo', 'name', 'mimetype' => 'text/plain']));
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$root->mount($storage, '');
|
|
|
|
$node = $root->get('/bar/foo');
|
|
|
|
$this->assertEquals(10, $node->getId());
|
|
|
|
$this->assertInstanceOf('\OC\Files\Node\File', $node);
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testGetNotFound() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotFoundException::class);
|
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
|
|
|
* @var \OC\Files\Storage\Storage $storage
|
|
|
|
*/
|
2016-07-13 15:35:34 +03:00
|
|
|
$storage = $this->getMockBuilder('\OC\Files\Storage\Storage')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-13 15:35:34 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$view->expects($this->once())
|
2016-02-18 17:41:10 +03:00
|
|
|
->method('getFileInfo')
|
2013-09-01 21:47:48 +04:00
|
|
|
->with('/bar/foo')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$root->mount($storage, '');
|
|
|
|
$root->get('/bar/foo');
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testGetInvalidPath() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotPermittedException::class);
|
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-13 15:35:34 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$root->get('/../foo');
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
public function testGetNoStorages() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OCP\Files\NotFoundException::class);
|
|
|
|
|
2013-09-01 21:47:48 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
|
2013-09-01 21:47:48 +04:00
|
|
|
*/
|
2017-10-24 16:26:53 +03:00
|
|
|
$view = $this->getMockBuilder(View::class)
|
2016-07-13 15:35:34 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$view,
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
2013-09-01 21:47:48 +04:00
|
|
|
|
|
|
|
$root->get('/bar/foo');
|
|
|
|
}
|
2016-10-04 13:28:41 +03:00
|
|
|
|
|
|
|
public function testGetUserFolder() {
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->manager,
|
|
|
|
$this->createMock(View::class),
|
|
|
|
$this->user,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('MyUserId');
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('MyUserId')
|
|
|
|
->willReturn($user);
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var CappedMemoryCache|\PHPUnit\Framework\MockObject\MockObject $cappedMemoryCache */
|
2016-11-03 01:16:51 +03:00
|
|
|
$cappedMemoryCache = $this->createMock(CappedMemoryCache::class);
|
|
|
|
$cappedMemoryCache
|
|
|
|
->expects($this->once())
|
|
|
|
->method('hasKey')
|
|
|
|
->willReturn(true);
|
|
|
|
$folder = $this->createMock(Folder::class);
|
|
|
|
$cappedMemoryCache
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('MyUserId')
|
|
|
|
->willReturn($folder);
|
|
|
|
|
|
|
|
$this->invokePrivate($root, 'userFolderCache', [$cappedMemoryCache]);
|
|
|
|
$this->assertEquals($folder, $root->getUserFolder('MyUserId'));
|
|
|
|
}
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2016-11-03 01:16:51 +03:00
|
|
|
public function testGetUserFolderWithNoUserObj() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OC\User\NoUserException::class);
|
|
|
|
$this->expectExceptionMessage('Backends provided no user object');
|
|
|
|
|
2016-11-03 01:16:51 +03:00
|
|
|
$root = new \OC\Files\Node\Root(
|
|
|
|
$this->createMock(Manager::class),
|
|
|
|
$this->createMock(View::class),
|
|
|
|
null,
|
|
|
|
$this->userMountCache,
|
|
|
|
$this->logger,
|
|
|
|
$this->userManager
|
|
|
|
);
|
|
|
|
$this->userManager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->with('NotExistingUser')
|
|
|
|
->willReturn(null);
|
|
|
|
$this->logger
|
|
|
|
->expects($this->once())
|
|
|
|
->method('error')
|
|
|
|
->with(
|
|
|
|
'Backends provided no user object for NotExistingUser',
|
|
|
|
[
|
|
|
|
'app' => 'files',
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
|
|
|
$root->getUserFolder('NotExistingUser');
|
2016-10-04 13:28:41 +03:00
|
|
|
}
|
2013-09-01 21:47:48 +04:00
|
|
|
}
|