Add tests for "getGroups()"

This commit is contained in:
Joas Schilling 2015-08-11 16:22:05 +02:00
parent be257bc9cc
commit ad450d4f0e
2 changed files with 82 additions and 11 deletions

View File

@ -20,6 +20,7 @@
*/ */
namespace OCA\Files_Sharing\API; namespace OCA\Files_Sharing\API;
use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IAppConfig; use OCP\IAppConfig;
@ -100,8 +101,8 @@ class Sharees {
'label' => $displayName, 'label' => $displayName,
'value' => [ 'value' => [
'shareType' => \OCP\Share::SHARE_TYPE_USER, 'shareType' => \OCP\Share::SHARE_TYPE_USER,
'shareWith' => $uid 'shareWith' => $uid,
] ],
]; ];
} }
@ -117,20 +118,22 @@ class Sharees {
private function getGroups($search, $shareWithGroupOnly) { private function getGroups($search, $shareWithGroupOnly) {
$sharees = []; $sharees = [];
$groups = $this->groupManager->search($search); $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 // Intersect all the groups that match with the groups this user is a member of
$userGroups = $this->groupManager->getUserGroups($this->userSession->getUser()); $userGroups = $this->groupManager->getUserGroups($this->userSession->getUser());
$userGroups = array_map(function (IGroup $group) { return $group->getGID(); }, $userGroups);
$groups = array_intersect($groups, $userGroups); $groups = array_intersect($groups, $userGroups);
} }
foreach ($groups as $group) { foreach ($groups as $gid) {
$sharees[] = [ $sharees[] = [
'label' => $group->getGID(), 'label' => $gid,
'value' => [ 'value' => [
'shareType' => \OCP\Share::SHARE_TYPE_GROUP, 'shareType' => \OCP\Share::SHARE_TYPE_GROUP,
'shareWith' => $group->getGID() 'shareWith' => $gid,
] ],
]; ];
} }
@ -150,8 +153,8 @@ class Sharees {
'label' => $search, 'label' => $search,
'value' => [ 'value' => [
'shareType' => \OCP\Share::SHARE_TYPE_REMOTE, 'shareType' => \OCP\Share::SHARE_TYPE_REMOTE,
'shareWith' => $search 'shareWith' => $search,
] ],
]; ];
} }

View File

@ -78,6 +78,18 @@ class ShareesTest extends TestCase {
return $user; 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() { public function dataGetUsers() {
return [ return [
['test', false, [], [], []], ['test', false, [], [], []],
@ -173,13 +185,14 @@ class ShareesTest extends TestCase {
->with($searchTerm) ->with($searchTerm)
->willReturn($userResponse); ->willReturn($userResponse);
} else { } else {
$user = $this->getUserMock('admin', 'Administrator');
$this->session->expects($this->any()) $this->session->expects($this->any())
->method('getUser') ->method('getUser')
->willReturn($this->getUserMock('admin', 'Administrator')); ->willReturn($user);
$this->groupManager->expects($this->once()) $this->groupManager->expects($this->once())
->method('getUserGroupIds') ->method('getUserGroupIds')
->with($this->anything()) ->with($user)
->willReturn($groupResponse); ->willReturn($groupResponse);
$this->groupManager->expects($this->exactly(sizeof($groupResponse))) $this->groupManager->expects($this->exactly(sizeof($groupResponse)))
@ -193,6 +206,61 @@ class ShareesTest extends TestCase {
$this->assertEquals($expected, $users); $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() { // public function testArguments() {
// //
// } // }