diff --git a/SilMock/Google/Service/Directory/Resource/Members.php b/SilMock/Google/Service/Directory/Resource/Members.php index 6d22472..f0b8ddb 100644 --- a/SilMock/Google/Service/Directory/Resource/Members.php +++ b/SilMock/Google/Service/Directory/Resource/Members.php @@ -69,20 +69,20 @@ public function listMembers($groupKey, $optParams = []) $this->validateGroupExists($groupKey); $pageSize = $optParams['pageSize'] ?? 10; $pageToken = $optParams['pageToken'] ?? 0; + $query = $optParams['query'] ?? null; + $expectedRoles = $this->extractRoles($query); $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 ( + $memberData['groupKey'] === $groupKey // Matches the expected group + && $memberCounter >= ($pageToken * $pageSize) // Matches the subsection of all the members + && (empty($expectedRoles) || in_array($memberData['member']['role'], $expectedRoles)) // Matches role + ) { + $memberCounter = $memberCounter + 1; + $this->addToMembers($memberData, $members); } $currentMembers = $members->getMembers(); $currentResultSize = count($currentMembers); @@ -98,6 +98,32 @@ public function listMembers($groupKey, $optParams = []) return $members; } + protected function extractRoles(?string $query): array + { + if (! empty($query) && str_contains($query, 'roles')) { + $roleSegmentStart = substr($query, strpos($query, 'roles')); + $roleSegmentEnd = strrpos($roleSegmentStart, ' '); + if ($roleSegmentEnd === false) { + $roleSegmentEnd = strlen($roleSegmentStart); + } + $roleSegment = trim(substr($roleSegmentStart, 0, $roleSegmentEnd)); + $roleValue = substr($roleSegment, 6); // roles= is 0-5 + $expectedRoles = explode(',', $roleValue); + } else { + $expectedRoles = []; + } + return $expectedRoles; + } + + 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); diff --git a/SilMock/Google/Service/Directory/Tokens.php b/SilMock/Google/Service/Directory/Tokens.php index 7a6ebb1..b3cd1ea 100644 --- a/SilMock/Google/Service/Directory/Tokens.php +++ b/SilMock/Google/Service/Directory/Tokens.php @@ -50,7 +50,7 @@ protected function assertIsValidUserKey(string $userId) */ protected function isValidEmailAddress(string $email): bool { - return (filter_var($email, FILTER_VALIDATE_EMAIL) !== false); + return filter_var($email, FILTER_VALIDATE_EMAIL) !== false; } protected function listTokensFor(string $userId): array diff --git a/SilMock/Google/Service/Directory/UsersAliasesResource.php b/SilMock/Google/Service/Directory/UsersAliasesResource.php index d7faf8e..6168749 100644 --- a/SilMock/Google/Service/Directory/UsersAliasesResource.php +++ b/SilMock/Google/Service/Directory/UsersAliasesResource.php @@ -11,6 +11,8 @@ class UsersAliasesResource extends DbClass { + public const ACCOUNT_DOESNT_EXIST = "Account doesn't exist: "; + public function __construct(?string $dbFile = null) { parent::__construct($dbFile, 'directory', 'users_alias'); @@ -37,7 +39,7 @@ public function delete($userKey, $alias) $matchingUsers = $dir->users->get($userKey); if ($matchingUsers === null) { - throw new Exception("Account doesn't exist: " . $userKey, 201407101645); + throw new Exception(self::ACCOUNT_DOESNT_EXIST . $userKey, 201407101645); } // Get all the aliases for that user @@ -87,7 +89,7 @@ public function insert($userKey, $postBody) $matchingUsers = $dir->users->get($userKey); if ($matchingUsers === null) { - throw new Exception("Account doesn't exist: " . $userKey, 201407110830); + throw new Exception(self::ACCOUNT_DOESNT_EXIST . $userKey, 201407110830); } if ($postBody->$key === null) { @@ -145,7 +147,7 @@ public function listUsersAliases($userKey): ?Google_Service_Directory_Aliases $matchingUsers = $dir->users->get($userKey); if ($matchingUsers === null) { - throw new Exception("Account doesn't exist: " . $userKey, 201407101420); + throw new Exception(self::ACCOUNT_DOESNT_EXIST . $userKey, 201407101420); } $foundAliases = $this->fetchAliasesByUser($key, $userKey); diff --git a/SilMock/Google/Service/Directory/UsersResource.php b/SilMock/Google/Service/Directory/UsersResource.php index a6bc211..7fb787e 100644 --- a/SilMock/Google/Service/Directory/UsersResource.php +++ b/SilMock/Google/Service/Directory/UsersResource.php @@ -112,18 +112,20 @@ protected function getDbUserByAlias($userKey) } $allUsers = $this->getAllDbUsers(); - - foreach ($allUsers as $aUser) { - if (! isset($aUser['data'])) { - continue; + $usersWithData = array_filter( + $allUsers, + function ($user) { + return isset($user['data']); } - + ); + + foreach ($usersWithData as $aUser) { $userData = json_decode($aUser['data'], true); if ($userData === null) { continue; } - $primaryEmail = isset($userData['primaryEmail']) ? $userData['primaryEmail'] : null; + $primaryEmail = $userData['primaryEmail'] ?? null; $aliasesResource = $this->getAliasesForUser($primaryEmail); if ($aliasesResource) { @@ -408,7 +410,8 @@ private function doesUserMatch($entry, $query = '') } } elseif (is_array($checkValue)) { throw new \Exception( - "Did not expect something other than name as an array. Got VALUE: " . var_dump($checkValue) + "Did not expect something other than name as an array. Got VALUE: " + . var_export($checkValue, true) ); } } elseif (isset($entry['name'][$field])) { diff --git a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php index a82c0ca..e43cc4f 100644 --- a/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php +++ b/SilMock/tests/Google/Service/Directory/Resource/MembersTest.php @@ -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 { @@ -72,7 +73,7 @@ public function testHasMember() self::assertTrue($hasMember); } - public function testListMembers() + public function testListMembersAll() { $groupEmailAddress = 'sample_group@groups.example.com'; $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); @@ -80,16 +81,63 @@ public function testListMembers() try { $members = $mockGoogleServiceDirectory->members->listMembers($groupEmailAddress); } catch (Exception $exception) { - self::fail( - sprintf( - 'Was expecting the members.list method to function, but got: %s', - $exception->getMessage() - ) + $this->failure($exception); + } + self::assertNotEmpty( + $members->getMembers(), + 'Was expecting the members.list method to have at least one member entry.' + ); + } + + public function testListMembersOnlyMember() + { + $groupEmailAddress = 'sample_group@groups.example.com'; + $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); + $members = []; + try { + $members = $mockGoogleServiceDirectory->members->listMembers( + $groupEmailAddress, + [ + 'query' => 'roles=MEMBER' + ] ); + } catch (Exception $exception) { + $this->failure($exception); } 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 type entry.' + ); + } + + public function testListMembersOnlyOwner() + { + $groupEmailAddress = 'sample_group@groups.example.com'; + $mockGoogleServiceDirectory = new GoogleMock_Directory('anyclient', $this->dataFile); + $members = []; + try { + $members = $mockGoogleServiceDirectory->members->listMembers( + $groupEmailAddress, + [ + 'query' => 'roles=OWNER' + ] + ); + } catch (Exception $exception) { + $this->failure($exception); + } + self::assertEmpty( + $members->getMembers(), + 'Was expecting the members.list method to have no owner types.' + ); + } + + protected function failure(Exception $exception): void + { + self::fail( + sprintf( + 'Was expecting the members.insert method to function, but got: %s', + $exception->getMessage() + ) ); } }