Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 2.8.0: Add query filtering on role to members.listMembers #96

Merged
merged 11 commits into from
Jun 19, 2024
21 changes: 15 additions & 6 deletions SilMock/Google/Service/Directory/Resource/Members.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,19 @@ public function listMembers($groupKey, $optParams = [])
$this->validateGroupExists($groupKey);
$pageSize = $optParams['pageSize'] ?? 10;
$pageToken = $optParams['pageToken'] ?? 0;
$query = $optParams['query'] ?? null;
$expectedRole = $query ? substr($query, strpos($query, '=') + 1) : null;
mtompset marked this conversation as resolved.
Show resolved Hide resolved
$members = new GoogleDirectory_Members();
$directoryMemberRecords = $this->getRecords();
$memberCounter = 0;
foreach ($directoryMemberRecords as $memberRecord) {
$memberData = json_decode($memberRecord['data'], true);
if ($memberData['groupKey'] === $groupKey) {
$memberCounter = $memberCounter + 1;
if ($memberCounter >= ($pageToken * $pageSize)) {
$currentMembers = $members->getMembers();
$currentMember = new GoogleDirectory_Member();
ObjectUtils::initialize($currentMember, $memberData['member']);
$currentMembers[] = $currentMember;
$members->setMembers($currentMembers);
if (is_null($expectedRole) || $expectedRole === $memberData['member']['role']) {
$memberCounter = $memberCounter + 1;
$this->addToMembers($memberData, $members);
}
}
}
$currentMembers = $members->getMembers();
Expand All @@ -98,6 +98,15 @@ public function listMembers($groupKey, $optParams = [])
return $members;
}

protected function addToMembers(array $memberData, GoogleDirectory_Members $members): void
{
$currentMembers = $members->getMembers();
$currentMember = new GoogleDirectory_Member();
ObjectUtils::initialize($currentMember, $memberData['member']);
$currentMembers[] = $currentMember;
$members->setMembers($currentMembers);
}

protected function validateGroupExists(string $groupKey): void
{
$mockGroupsObject = new Groups($this->dbFile);
Expand Down
59 changes: 56 additions & 3 deletions SilMock/tests/Google/Service/Directory/Resource/MembersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function testInsert()

$member = new GoogleDirectory_Member();
$member->setEmail($emailAddress);
$member->setRole('MEMBER');

$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
try {
Expand Down Expand Up @@ -72,7 +73,7 @@ public function testHasMember()
self::assertTrue($hasMember);
}

public function testListMembers()
public function testListMembersAll()
{
$groupEmailAddress = '[email protected]';
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
Expand All @@ -88,8 +89,60 @@ public function testListMembers()
);
}
self::assertNotEmpty(
$members,
'Was expecting the members.list method to have at least one member.'
$members->getMembers(),
'Was expecting the members.list method to have at least one member entry.'
);
}

public function testListMembersOnlyMember()
{
$groupEmailAddress = '[email protected]';
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$members = [];
try {
$members = $mockGoogleServiceDirectory->members->listMembers(
$groupEmailAddress,
[
'query' => 'roles=MEMBER'
]
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the members.list method to function, but got: %s',
$exception->getMessage()
)
);
}
self::assertNotEmpty(
$members->getMembers(),
'Was expecting the members.list method to have at least one member type entry.'
);
}

public function testListMembersOnlyOwner()
{
$groupEmailAddress = '[email protected]';
$mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile);
$members = [];
try {
$members = $mockGoogleServiceDirectory->members->listMembers(
$groupEmailAddress,
[
'query' => 'roles=OWNER'
]
);
} catch (Exception $exception) {
self::fail(
sprintf(
'Was expecting the members.list method to function, but got: %s',
$exception->getMessage()
)
);
}
self::assertEmpty(
$members->getMembers(),
'Was expecting the members.list method to have no owner types.'
);
}
}