summaryrefslogtreecommitdiffstats
path: root/apps/files_sharing
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@owncloud.com>2015-08-11 16:22:05 +0200
committerJoas Schilling <nickvergessen@owncloud.com>2015-08-26 11:54:24 +0200
commitad450d4f0ed4ebbb5e9e6256750061b0adb950e1 (patch)
treea34b63c6502ff4d7b3e5a0eadac391427edd7b37 /apps/files_sharing
parentbe257bc9cc2330d0e67957525cb66646b346a850 (diff)
downloadnextcloud-server-ad450d4f0ed4ebbb5e9e6256750061b0adb950e1.tar.gz
nextcloud-server-ad450d4f0ed4ebbb5e9e6256750061b0adb950e1.zip
Add tests for "getGroups()"
Diffstat (limited to 'apps/files_sharing')
-rw-r--r--apps/files_sharing/api/sharees.php21
-rw-r--r--apps/files_sharing/tests/api/sharees.php72
2 files changed, 82 insertions, 11 deletions
diff --git a/apps/files_sharing/api/sharees.php b/apps/files_sharing/api/sharees.php
index 6853bd8a269..7a3555e0a5b 100644
--- a/apps/files_sharing/api/sharees.php
+++ b/apps/files_sharing/api/sharees.php
@@ -20,6 +20,7 @@
*/
namespace OCA\Files_Sharing\API;
+use OCP\IGroup;
use OCP\IGroupManager;
use OCP\IUserManager;
use OCP\IAppConfig;
@@ -100,8 +101,8 @@ class Sharees {
'label' => $displayName,
'value' => [
'shareType' => \OCP\Share::SHARE_TYPE_USER,
- 'shareWith' => $uid
- ]
+ 'shareWith' => $uid,
+ ],
];
}
@@ -117,20 +118,22 @@ class Sharees {
private function getGroups($search, $shareWithGroupOnly) {
$sharees = [];
$groups = $this->groupManager->search($search);
+ $groups = array_map(function (IGroup $group) { return $group->getGID(); }, $groups);
- if ($shareWithGroupOnly) {
+ if (!empty($groups) && $shareWithGroupOnly) {
// Intersect all the groups that match with the groups this user is a member of
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
+ $userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
$groups = array_intersect($groups, $userGroups);
}
- foreach ($groups as $group) {
+ foreach ($groups as $gid) {
$sharees[] = [
- 'label' => $group->getGID(),
+ 'label' => $gid,
'value' => [
'shareType' => \OCP\Share::SHARE_TYPE_GROUP,
- 'shareWith' => $group->getGID()
- ]
+ 'shareWith' => $gid,
+ ],
];
}
@@ -150,8 +153,8 @@ class Sharees {
'label' => $search,
'value' => [
'shareType' => \OCP\Share::SHARE_TYPE_REMOTE,
- 'shareWith' => $search
- ]
+ 'shareWith' => $search,
+ ],
];
}
diff --git a/apps/files_sharing/tests/api/sharees.php b/apps/files_sharing/tests/api/sharees.php
index 30397ba8678..25074e4e8c4 100644
--- a/apps/files_sharing/tests/api/sharees.php
+++ b/apps/files_sharing/tests/api/sharees.php
@@ -78,6 +78,18 @@ class ShareesTest extends TestCase {
return $user;
}
+ protected function getGroupMock($gid) {
+ $group = $this->getMockBuilder('OCP\IGroup')
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $group->expects($this->any())
+ ->method('getGID')
+ ->willReturn($gid);
+
+ return $group;
+ }
+
public function dataGetUsers() {
return [
['test', false, [], [], []],
@@ -173,13 +185,14 @@ class ShareesTest extends TestCase {
->with($searchTerm)
->willReturn($userResponse);
} else {
+ $user = $this->getUserMock('admin', 'Administrator');
$this->session->expects($this->any())
->method('getUser')
- ->willReturn($this->getUserMock('admin', 'Administrator'));
+ ->willReturn($user);
$this->groupManager->expects($this->once())
->method('getUserGroupIds')
- ->with($this->anything())
+ ->with($user)
->willReturn($groupResponse);
$this->groupManager->expects($this->exactly(sizeof($groupResponse)))
@@ -193,6 +206,61 @@ class ShareesTest extends TestCase {
$this->assertEquals($expected, $users);
}
+ public function dataGetGroups() {
+ return [
+ ['test', false, [], [], []],
+ [
+ 'test', false,
+ [$this->getGroupMock('test1')],
+ [],
+ [['label' => 'test1', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
+ ],
+ ['test', true, [], [], []],
+ [
+ 'test', true,
+ [
+ $this->getGroupMock('test1'),
+ $this->getGroupMock('test2'),
+ ],
+ [$this->getGroupMock('test1')],
+ [['label' => 'test1', 'value' => ['shareType' => \OCP\Share::SHARE_TYPE_GROUP, 'shareWith' => 'test1']]],
+ ],
+ ];
+ }
+
+ /**
+ * @dataProvider dataGetGroups
+ *
+ * @param string $searchTerm
+ * @param bool $shareWithGroupOnly
+ * @param array $groupResponse
+ * @param array $userGroupsResponse
+ * @param array $expected
+ */
+ public function testGetGroups($searchTerm, $shareWithGroupOnly, $groupResponse, $userGroupsResponse, $expected) {
+ $this->groupManager->expects($this->once())
+ ->method('search')
+ ->with($searchTerm)
+ ->willReturn($groupResponse);
+
+ if ($shareWithGroupOnly) {
+ $user = $this->getUserMock('admin', 'Administrator');
+ $this->session->expects($this->any())
+ ->method('getUser')
+ ->willReturn($user);
+
+ $numGetUserGroupsCalls = empty($groupResponse) ? 0 : 1;
+ $this->groupManager->expects($this->exactly($numGetUserGroupsCalls))
+ ->method('getUserGroups')
+ ->with($user)
+ ->willReturn($userGroupsResponse);
+ }
+
+ $users = $this->invokePrivate($this->sharees, 'getGroups', [$searchTerm, $shareWithGroupOnly]);
+
+ $this->assertEquals($expected, $users);
+ }
+
// public function testArguments() {
//
// }