aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <coding@schilljs.com>2017-04-10 16:10:34 +0200
committerRoeland Jago Douma <roeland@famdouma.nl>2017-04-13 12:58:52 +0200
commit2fcf334c6aeafaf13f3f1eee6479db93cec2e576 (patch)
treee69ab421c4359788eea8f1711e27fba9024dec2b
parent5df727d4ad3a735d35a5b13502c93791c84fa948 (diff)
downloadnextcloud-server-2fcf334c6aeafaf13f3f1eee6479db93cec2e576.tar.gz
nextcloud-server-2fcf334c6aeafaf13f3f1eee6479db93cec2e576.zip
Fix tests for ShareHelper
Signed-off-by: Joas Schilling <coding@schilljs.com>
-rw-r--r--lib/private/Share20/ShareHelper.php15
-rw-r--r--tests/lib/Share20/ShareHelperTests.php114
2 files changed, 85 insertions, 44 deletions
diff --git a/lib/private/Share20/ShareHelper.php b/lib/private/Share20/ShareHelper.php
index ba3f64dbc1a..90992005017 100644
--- a/lib/private/Share20/ShareHelper.php
+++ b/lib/private/Share20/ShareHelper.php
@@ -56,6 +56,11 @@ class ShareHelper implements IShareHelper {
return $result;
}
+ /**
+ * @param Node $node
+ * @param array[] $users
+ * @return array
+ */
protected function getPathsForUsers(Node $node, array $users) {
$byId = $results = [];
foreach ($users as $uid => $info) {
@@ -79,6 +84,7 @@ class ShareHelper implements IShareHelper {
$item = $node;
$appendix = '/' . $node->getName();
while (!empty($byId)) {
+ /** @var Node $item */
$item = $item->getParent();
if (!empty($byId[$item->getId()])) {
@@ -94,6 +100,11 @@ class ShareHelper implements IShareHelper {
return $results;
}
+ /**
+ * @param Node $node
+ * @param array[] $remotes
+ * @return array
+ */
protected function getPathsForRemotes(Node $node, array $remotes) {
$byId = $results = [];
foreach ($remotes as $cloudId => $info) {
@@ -121,6 +132,10 @@ class ShareHelper implements IShareHelper {
return $results;
}
+ /**
+ * @param Node $node
+ * @return string
+ */
protected function getMountedPath(Node $node) {
$path = $node->getPath();
$sections = explode('/', $path, 4);
diff --git a/tests/lib/Share20/ShareHelperTests.php b/tests/lib/Share20/ShareHelperTests.php
index b6c737cf444..a6ca581dbb6 100644
--- a/tests/lib/Share20/ShareHelperTests.php
+++ b/tests/lib/Share20/ShareHelperTests.php
@@ -23,16 +23,14 @@
namespace Test\Share20;
use OC\Share20\ShareHelper;
-use OCP\Files\Folder;
-use OCP\Files\IRootFolder;
use OCP\Files\Node;
-use OCP\Files\NotFoundException;
+use OCP\Share\IManager;
use Test\TestCase;
class ShareHelperTests extends TestCase {
- /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
- private $rootFolder;
+ /** @var IManager|\PHPUnit_Framework_MockObject_MockObject */
+ private $manager;
/** @var ShareHelper */
private $helper;
@@ -40,55 +38,83 @@ class ShareHelperTests extends TestCase {
public function 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);
}
- /**
- * uid1 - Exists with valid node
- * uid2 - Does not exist
- * uid3 - Exists but no valid node
- * uid4 - Exists with valid node
- */
- public function testGetPathsForAccessList() {
- /** @var Folder[]|\PHPUnit_Framework_MockObject_MockObject[] $userFolder */
- $userFolder = [
- 'uid1' => $this->createMock(Folder::class),
- 'uid3' => $this->createMock(Folder::class),
- 'uid4' => $this->createMock(Folder::class),
+ 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'],
+ ]],
];
+ }
- $this->rootFolder->method('getUserFolder')
- ->willReturnCallback(function($uid) use ($userFolder) {
- if (isset($userFolder[$uid])) {
- return $userFolder[$uid];
- }
- throw new NotFoundException();
- });
+ /**
+ * @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,
+ ]);
/** @var Node|\PHPUnit_Framework_MockObject_MockObject $node */
$node = $this->createMock(Node::class);
- $node->method('getId')
- ->willReturn(42);
-
- $userFolder['uid1']->method('getById')
- ->with(42)
- ->willReturn([$node]);
- $userFolder['uid3']->method('getById')
- ->with(42)
- ->willReturn([]);
- $userFolder['uid4']->method('getById')
- ->with(42)
- ->willReturn([$node]);
-
- $expects = [
- 'uid1' => [$node],
- 'uid4' => [$node],
+ /** @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));
+ }
+
+ 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]));
}
}