Fix tests for ShareHelper
Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
parent
5df727d4ad
commit
2fcf334c6a
|
@ -56,6 +56,11 @@ class ShareHelper implements IShareHelper {
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Node $node
|
||||||
|
* @param array[] $users
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
protected function getPathsForUsers(Node $node, array $users) {
|
protected function getPathsForUsers(Node $node, array $users) {
|
||||||
$byId = $results = [];
|
$byId = $results = [];
|
||||||
foreach ($users as $uid => $info) {
|
foreach ($users as $uid => $info) {
|
||||||
|
@ -79,6 +84,7 @@ class ShareHelper implements IShareHelper {
|
||||||
$item = $node;
|
$item = $node;
|
||||||
$appendix = '/' . $node->getName();
|
$appendix = '/' . $node->getName();
|
||||||
while (!empty($byId)) {
|
while (!empty($byId)) {
|
||||||
|
/** @var Node $item */
|
||||||
$item = $item->getParent();
|
$item = $item->getParent();
|
||||||
|
|
||||||
if (!empty($byId[$item->getId()])) {
|
if (!empty($byId[$item->getId()])) {
|
||||||
|
@ -94,6 +100,11 @@ class ShareHelper implements IShareHelper {
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Node $node
|
||||||
|
* @param array[] $remotes
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
protected function getPathsForRemotes(Node $node, array $remotes) {
|
protected function getPathsForRemotes(Node $node, array $remotes) {
|
||||||
$byId = $results = [];
|
$byId = $results = [];
|
||||||
foreach ($remotes as $cloudId => $info) {
|
foreach ($remotes as $cloudId => $info) {
|
||||||
|
@ -121,6 +132,10 @@ class ShareHelper implements IShareHelper {
|
||||||
return $results;
|
return $results;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Node $node
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
protected function getMountedPath(Node $node) {
|
protected function getMountedPath(Node $node) {
|
||||||
$path = $node->getPath();
|
$path = $node->getPath();
|
||||||
$sections = explode('/', $path, 4);
|
$sections = explode('/', $path, 4);
|
||||||
|
|
|
@ -23,16 +23,14 @@
|
||||||
namespace Test\Share20;
|
namespace Test\Share20;
|
||||||
|
|
||||||
use OC\Share20\ShareHelper;
|
use OC\Share20\ShareHelper;
|
||||||
use OCP\Files\Folder;
|
|
||||||
use OCP\Files\IRootFolder;
|
|
||||||
use OCP\Files\Node;
|
use OCP\Files\Node;
|
||||||
use OCP\Files\NotFoundException;
|
use OCP\Share\IManager;
|
||||||
use Test\TestCase;
|
use Test\TestCase;
|
||||||
|
|
||||||
class ShareHelperTests extends TestCase {
|
class ShareHelperTests extends TestCase {
|
||||||
|
|
||||||
/** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
|
/** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
|
||||||
private $rootFolder;
|
private $manager;
|
||||||
|
|
||||||
/** @var ShareHelper */
|
/** @var ShareHelper */
|
||||||
private $helper;
|
private $helper;
|
||||||
|
@ -40,55 +38,83 @@ class ShareHelperTests extends TestCase {
|
||||||
public function setUp() {
|
public function setUp() {
|
||||||
parent::setUp();
|
parent::setUp();
|
||||||
|
|
||||||
$this->rootFolder = $this->createMock(IRootFolder::class);
|
$this->manager = $this->createMock(IManager::class);
|
||||||
|
|
||||||
$this->helper = new ShareHelper($this->rootFolder);
|
$this->helper = new ShareHelper($this->manager);
|
||||||
|
}
|
||||||
|
|
||||||
|
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'],
|
||||||
|
]],
|
||||||
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* uid1 - Exists with valid node
|
* @dataProvider dataGetPathsForAccessList
|
||||||
* uid2 - Does not exist
|
|
||||||
* uid3 - Exists but no valid node
|
|
||||||
* uid4 - Exists with valid node
|
|
||||||
*/
|
*/
|
||||||
public function testGetPathsForAccessList() {
|
public function testGetPathsForAccessList(array $userList, array $userMap, $resolveUsers, array $remoteList, array $remoteMap, $resolveRemotes, array $expected) {
|
||||||
/** @var Folder[]|\PHPUnit_Framework_MockObject_MockObject[] $userFolder */
|
$this->manager->expects($this->once())
|
||||||
$userFolder = [
|
->method('getAccessList')
|
||||||
'uid1' => $this->createMock(Folder::class),
|
->willReturn([
|
||||||
'uid3' => $this->createMock(Folder::class),
|
'users' => $userList,
|
||||||
'uid4' => $this->createMock(Folder::class),
|
'remote' => $remoteList,
|
||||||
];
|
]);
|
||||||
|
|
||||||
$this->rootFolder->method('getUserFolder')
|
|
||||||
->willReturnCallback(function($uid) use ($userFolder) {
|
|
||||||
if (isset($userFolder[$uid])) {
|
|
||||||
return $userFolder[$uid];
|
|
||||||
}
|
|
||||||
throw new NotFoundException();
|
|
||||||
});
|
|
||||||
|
|
||||||
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
|
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
|
||||||
$node = $this->createMock(Node::class);
|
$node = $this->createMock(Node::class);
|
||||||
$node->method('getId')
|
/** @var ShareHelper|\PHPUnit_Framework_MockObject_MockObject $helper */
|
||||||
->willReturn(42);
|
$helper = $this->getMockBuilder(ShareHelper::class)
|
||||||
|
->setConstructorArgs([$this->manager])
|
||||||
|
->setMethods(['getPathsForUsers', 'getPathsForRemotes'])
|
||||||
|
->getMock();
|
||||||
|
|
||||||
$userFolder['uid1']->method('getById')
|
$helper->expects($resolveUsers ? $this->once() : $this->never())
|
||||||
->with(42)
|
->method('getPathsForUsers')
|
||||||
->willReturn([$node]);
|
->with($node, $userList)
|
||||||
$userFolder['uid3']->method('getById')
|
->willReturn($userMap);
|
||||||
->with(42)
|
|
||||||
->willReturn([]);
|
|
||||||
$userFolder['uid4']->method('getById')
|
|
||||||
->with(42)
|
|
||||||
->willReturn([$node]);
|
|
||||||
|
|
||||||
$expects = [
|
$helper->expects($resolveRemotes ? $this->once() : $this->never())
|
||||||
'uid1' => [$node],
|
->method('getPathsForRemotes')
|
||||||
'uid4' => [$node],
|
->with($node, $remoteList)
|
||||||
|
->willReturn($remoteMap);
|
||||||
|
|
||||||
|
$this->assertSame($expected, $helper->getPathsForAccessList($node));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function dataGetMountedPath() {
|
||||||
|
return [
|
||||||
|
['/admin/files/foobar', '/foobar'],
|
||||||
|
['/admin/files/foo/bar', '/foo/bar'],
|
||||||
];
|
];
|
||||||
|
}
|
||||||
|
|
||||||
$result = $this->helper->getPathsForAccessList($node, ['uid1', 'uid2', 'uid3', 'uid4']);
|
/**
|
||||||
|
* @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);
|
||||||
|
|
||||||
$this->assertSame($expects, $result);
|
$this->assertSame($expected, self::invokePrivate($this->helper, 'getMountedPath', [$node]));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue