diff --git a/apps/files_sharing/api/sharees.php b/apps/files_sharing/api/sharees.php index 6853bd8a26..7a3555e0a5 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 30397ba867..25074e4e8c 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() { // // }