2017-01-04 11:13:08 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-01-04 11:13:08 +03:00
|
|
|
namespace Test\Share20;
|
|
|
|
|
|
|
|
use OC\Share20\ShareHelper;
|
|
|
|
use OCP\Files\Node;
|
2017-04-11 15:45:02 +03:00
|
|
|
use OCP\Files\NotFoundException;
|
2017-04-10 17:10:34 +03:00
|
|
|
use OCP\Share\IManager;
|
2017-01-04 11:13:08 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2017-04-11 15:08:35 +03:00
|
|
|
class ShareHelperTest extends TestCase {
|
2017-01-04 11:13:08 +03:00
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $manager;
|
2017-01-04 11:13:08 +03:00
|
|
|
|
|
|
|
/** @var ShareHelper */
|
|
|
|
private $helper;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-01-04 11:13:08 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
$this->manager = $this->createMock(IManager::class);
|
2017-01-04 11:13:08 +03:00
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
$this->helper = new ShareHelper($this->manager);
|
2017-01-04 11:13:08 +03:00
|
|
|
}
|
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
public function dataGetPathsForAccessList() {
|
|
|
|
return [
|
|
|
|
[[], [], false, [], [], false, [
|
|
|
|
'users' => [],
|
|
|
|
'remotes' => [],
|
|
|
|
]],
|
|
|
|
[['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, [], [], false, [
|
|
|
|
'users' => ['user1' => 'foo', 'user2' => 'bar'],
|
|
|
|
'remotes' => [],
|
|
|
|
]],
|
|
|
|
[[], [], false, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
|
|
|
|
'users' => [],
|
|
|
|
'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
|
|
|
|
]],
|
|
|
|
[['user1', 'user2'], ['user1' => 'foo', 'user2' => 'bar'], true, ['remote1', 'remote2'], ['remote1' => 'qwe', 'remote2' => 'rtz'], true, [
|
|
|
|
'users' => ['user1' => 'foo', 'user2' => 'bar'],
|
|
|
|
'remotes' => ['remote1' => 'qwe', 'remote2' => 'rtz'],
|
|
|
|
]],
|
2017-01-04 11:13:08 +03:00
|
|
|
];
|
2017-04-10 17:10:34 +03:00
|
|
|
}
|
2017-01-04 11:13:08 +03:00
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataGetPathsForAccessList
|
|
|
|
*/
|
|
|
|
public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected) {
|
|
|
|
$this->manager->expects($this->once())
|
|
|
|
->method('getAccessList')
|
|
|
|
->willReturn([
|
|
|
|
'users' => $userList,
|
|
|
|
'remote' => $remoteList,
|
|
|
|
]);
|
2017-01-04 11:13:08 +03:00
|
|
|
|
|
|
|
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
|
|
|
|
$node = $this->createMock(Node::class);
|
2017-04-10 17:10:34 +03:00
|
|
|
/** @var ShareHelper|\PHPUnit_Framework_MockObject_MockObject $helper */
|
|
|
|
$helper = $this->getMockBuilder(ShareHelper::class)
|
|
|
|
->setConstructorArgs([$this->manager])
|
|
|
|
->setMethods(['getPathsForUsers', 'getPathsForRemotes'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$helper->expects($resolveUsers ? $this->once() : $this->never())
|
|
|
|
->method('getPathsForUsers')
|
|
|
|
->with($node, $userList)
|
|
|
|
->willReturn($userMap);
|
|
|
|
|
|
|
|
$helper->expects($resolveRemotes ? $this->once() : $this->never())
|
|
|
|
->method('getPathsForRemotes')
|
|
|
|
->with($node, $remoteList)
|
|
|
|
->willReturn($remoteMap);
|
|
|
|
|
|
|
|
$this->assertSame($expected, $helper->getPathsForAccessList($node));
|
|
|
|
}
|
|
|
|
|
2017-04-11 15:45:02 +03:00
|
|
|
public function dataGetPathsForUsers() {
|
|
|
|
return [
|
|
|
|
[[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'test1' => ['node_id' => 16, 'node_path' => '/foo'],
|
|
|
|
'test2' => ['node_id' => 23, 'node_path' => '/bar'],
|
|
|
|
'test3' => ['node_id' => 42, 'node_path' => '/cat'],
|
|
|
|
'test4' => ['node_id' => 48, 'node_path' => '/dog'],
|
|
|
|
],
|
|
|
|
[16 => 'SixTeen', 23 => 'TwentyThree', 42 => 'FortyTwo'],
|
|
|
|
[
|
|
|
|
'test1' => '/foo/TwentyThree/FortyTwo',
|
|
|
|
'test2' => '/bar/FortyTwo',
|
|
|
|
'test3' => '/cat',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetPathsForUsers
|
|
|
|
*
|
|
|
|
* @param array $users
|
|
|
|
* @param array $nodes
|
|
|
|
* @param array $expected
|
|
|
|
*/
|
|
|
|
public function testGetPathsForUsers(array $users, array $nodes, array $expected) {
|
|
|
|
$lastNode = null;
|
|
|
|
foreach ($nodes as $nodeId => $nodeName) {
|
|
|
|
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
|
|
|
|
$node = $this->createMock(Node::class);
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn($nodeId);
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getName')
|
|
|
|
->willReturn($nodeName);
|
|
|
|
if ($lastNode === null) {
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getParent')
|
|
|
|
->willThrowException(new NotFoundException());
|
|
|
|
} else {
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getParent')
|
|
|
|
->willReturn($lastNode);
|
|
|
|
}
|
|
|
|
$lastNode = $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForUsers', [$lastNode, $users]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataGetPathsForRemotes() {
|
|
|
|
return [
|
|
|
|
[[], [23 => 'TwentyThree', 42 => 'FortyTwo'], []],
|
|
|
|
[
|
|
|
|
[
|
|
|
|
'test1' => ['node_id' => 16, 'token' => 't1'],
|
|
|
|
'test2' => ['node_id' => 23, 'token' => 't2'],
|
|
|
|
'test3' => ['node_id' => 42, 'token' => 't3'],
|
|
|
|
'test4' => ['node_id' => 48, 'token' => 't4'],
|
|
|
|
],
|
|
|
|
[
|
|
|
|
16 => '/admin/files/SixTeen',
|
|
|
|
23 => '/admin/files/SixTeen/TwentyThree',
|
|
|
|
42 => '/admin/files/SixTeen/TwentyThree/FortyTwo',
|
|
|
|
],
|
|
|
|
[
|
|
|
|
'test1' => ['token' => 't1', 'node_path' => '/SixTeen'],
|
|
|
|
'test2' => ['token' => 't2', 'node_path' => '/SixTeen/TwentyThree'],
|
|
|
|
'test3' => ['token' => 't3', 'node_path' => '/SixTeen/TwentyThree/FortyTwo'],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataGetPathsForRemotes
|
|
|
|
*
|
|
|
|
* @param array $remotes
|
|
|
|
* @param array $nodes
|
|
|
|
* @param array $expected
|
|
|
|
*/
|
|
|
|
public function testGetPathsForRemotes(array $remotes, array $nodes, array $expected) {
|
|
|
|
$lastNode = null;
|
|
|
|
foreach ($nodes as $nodeId => $nodePath) {
|
|
|
|
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
|
|
|
|
$node = $this->createMock(Node::class);
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getId')
|
|
|
|
->willReturn($nodeId);
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getPath')
|
|
|
|
->willReturn($nodePath);
|
|
|
|
if ($lastNode === null) {
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getParent')
|
|
|
|
->willThrowException(new NotFoundException());
|
|
|
|
} else {
|
|
|
|
$node->expects($this->any())
|
|
|
|
->method('getParent')
|
|
|
|
->willReturn($lastNode);
|
|
|
|
}
|
|
|
|
$lastNode = $node;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertEquals($expected, self::invokePrivate($this->helper, 'getPathsForRemotes', [$lastNode, $remotes]));
|
|
|
|
}
|
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
public function dataGetMountedPath() {
|
|
|
|
return [
|
|
|
|
['/admin/files/foobar', '/foobar'],
|
|
|
|
['/admin/files/foo/bar', '/foo/bar'],
|
2017-01-04 11:13:08 +03:00
|
|
|
];
|
2017-04-10 17:10:34 +03:00
|
|
|
}
|
2017-01-04 11:13:08 +03:00
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataGetMountedPath
|
|
|
|
* @param string $path
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testGetMountedPath($path, $expected) {
|
|
|
|
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
|
|
|
|
$node = $this->createMock(Node::class);
|
|
|
|
$node->expects($this->once())
|
|
|
|
->method('getPath')
|
|
|
|
->willReturn($path);
|
2017-01-04 11:13:08 +03:00
|
|
|
|
2017-04-10 17:10:34 +03:00
|
|
|
$this->assertSame($expected, self::invokePrivate($this->helper, 'getMountedPath', [$node]));
|
2017-01-04 11:13:08 +03:00
|
|
|
}
|
|
|
|
}
|