From e75d1231e95842b047dde5b455f295891784e017 Mon Sep 17 00:00:00 2001 From: Mark Tompsett Date: Mon, 11 Jan 2021 16:30:45 -0500 Subject: [PATCH] Fixed id to be a string and corrected tests and fixtures --- .../Google/Service/Directory/ObjectUtils.php | 14 ++- .../Directory/UsersAliasesResource.php | 40 ++++---- .../Service/Directory/UsersResource.php | 19 ++-- .../Directory/VerificationCodesResource.php | 3 +- .../tests/Google/Service/DirectoryTest.php | 91 ++++++++++--------- 5 files changed, 87 insertions(+), 80 deletions(-) diff --git a/SilMock/Google/Service/Directory/ObjectUtils.php b/SilMock/Google/Service/Directory/ObjectUtils.php index 990034a..f1f4608 100644 --- a/SilMock/Google/Service/Directory/ObjectUtils.php +++ b/SilMock/Google/Service/Directory/ObjectUtils.php @@ -1,27 +1,25 @@ $value) { + foreach ($propArray as $key => $value) { $newObject->$key = $value; } } - -} \ No newline at end of file +} diff --git a/SilMock/Google/Service/Directory/UsersAliasesResource.php b/SilMock/Google/Service/Directory/UsersAliasesResource.php index 767b772..e1ac064 100644 --- a/SilMock/Google/Service/Directory/UsersAliasesResource.php +++ b/SilMock/Google/Service/Directory/UsersAliasesResource.php @@ -1,16 +1,18 @@ _dbFile = $dbFile; } @@ -27,9 +29,8 @@ public function delete($userKey, $alias) { // If the $userKey is not an email address, it must be an id $key = 'primaryEmail'; - if ( ! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { + if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { $key = 'id'; - $userKey = intval($userKey); } // ensure that user exists in db @@ -42,10 +43,14 @@ public function delete($userKey, $alias) // Get all the aliases for that user $sqliteUtils = new SqliteUtils($this->_dbFile); - $aliases = $sqliteUtils->getAllRecordsByDataKey($this->_dataType, - $this->_dataClass, $key, $userKey); + $aliases = $sqliteUtils->getAllRecordsByDataKey( + $this->_dataType, + $this->_dataClass, + $key, + $userKey + ); - if ( ! $aliases) { + if (! $aliases) { return null; } @@ -74,9 +79,8 @@ public function insert($userKey, $postBody) { // If the $userKey is not an email address, it must be an id $key = 'primaryEmail'; - if ( ! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { + if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { $key = 'id'; - $userKey = intval($userKey); } // ensure that user exists in db @@ -111,7 +115,7 @@ public function insertAssumingUserExists($postBody) ); $allAliases = $sqliteUtils->getData($this->_dataType, $this->_dataClass); - if ( ! $allAliases) { + if (! $allAliases) { return null; } @@ -134,9 +138,8 @@ public function listUsersAliases($userKey) { // If the $userKey is not an email address, it must be an id $key = 'primaryEmail'; - if ( ! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { + if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { $key = 'id'; - $userKey = intval($userKey); } // ensure that user exists in db $dir = new \SilMock\Google\Service\Directory('anything', $this->_dbFile); @@ -160,12 +163,17 @@ public function listUsersAliases($userKey) * @param string $userKey - The Email or immutable Id of the user * @return null|Google_Service_Directory_Aliases */ - public function fetchAliasesByUser($keyType, $userKey) { + public function fetchAliasesByUser($keyType, $userKey) + { $sqliteUtils = new SqliteUtils($this->_dbFile); - $aliases = $sqliteUtils->getAllRecordsByDataKey($this->_dataType, - $this->_dataClass, $keyType, $userKey); + $aliases = $sqliteUtils->getAllRecordsByDataKey( + $this->_dataType, + $this->_dataClass, + $keyType, + $userKey + ); - if ( ! $aliases) { + if (! $aliases) { return null; } diff --git a/SilMock/Google/Service/Directory/UsersResource.php b/SilMock/Google/Service/Directory/UsersResource.php index af9c40a..5efc882 100644 --- a/SilMock/Google/Service/Directory/UsersResource.php +++ b/SilMock/Google/Service/Directory/UsersResource.php @@ -1,8 +1,8 @@ _dbFile); @@ -157,7 +156,7 @@ protected function getAllDbUsers() public function insert($postBody) { $defaults = array( - 'id' => intval(str_replace(array(' ', '.'), '', microtime())), + 'id' => str_replace(array(' ', '.'), '', microtime()), 'suspended' => false, 'changePasswordAtNextLogin' => false, 'isAdmin' => false, @@ -219,11 +218,10 @@ public function insert($postBody) */ public function update($userKey, $postBody) { - $userEntry = $this->getDbUser($userKey); if ($userEntry === null) { throw new \Exception( - "Account doesn't exist: " . $userKey, + "Account doesn't exist: " . json_encode($userKey, true), 201407101130 ); } @@ -232,7 +230,6 @@ public function update($userKey, $postBody) * only keep the non-null properties of the $postBody user, * except for suspensionReason. */ - $dbUserProps = json_decode($userEntry['data'], true); $newUserProps = get_object_vars($postBody); @@ -257,7 +254,6 @@ public function update($userKey, $postBody) // Save the user's aliases if (isset($postBody->aliases) && $postBody->aliases) { - foreach ($postBody->aliases as $alias) { $newAlias = new \Google_Service_Directory_Alias(); $newAlias->alias = $alias; @@ -275,15 +271,14 @@ public function update($userKey, $postBody) * Retrieves a user record from the database (users.delete) * * @param string $userKey - The Email or immutable Id of the user - * @return null|nested array for the matching database entry + * @return null|array -- nested array for the matching database entry */ - private function getDbUser($userKey) + private function getDbUser(string $userKey) { $key = 'primaryEmail'; if (! filter_var($userKey, FILTER_VALIDATE_EMAIL)) { $key = 'id'; - $userKey = intval($userKey); } $sqliteUtils = new SqliteUtils($this->_dbFile); @@ -345,7 +340,7 @@ public function listUsers($parameters = []) $allResultsUsers[] = $newEntry; $results->setUsers($allResultsUsers); } - if (count($results->getUsers())>= $parameters['maxResults']) { + if (count($results->getUsers()) >= $parameters['maxResults']) { break; } } @@ -354,7 +349,7 @@ public function listUsers($parameters = []) private function doesUserMatch($entry, $query = '') { - if ($query==='') { + if ($query === '') { return true; } $query = str_replace('*', '', $query); diff --git a/SilMock/Google/Service/Directory/VerificationCodesResource.php b/SilMock/Google/Service/Directory/VerificationCodesResource.php index fc19910..125d8fc 100644 --- a/SilMock/Google/Service/Directory/VerificationCodesResource.php +++ b/SilMock/Google/Service/Directory/VerificationCodesResource.php @@ -1,4 +1,5 @@ _dbFile); diff --git a/SilMock/tests/Google/Service/DirectoryTest.php b/SilMock/tests/Google/Service/DirectoryTest.php index a6e8ddf..e44f888 100644 --- a/SilMock/tests/Google/Service/DirectoryTest.php +++ b/SilMock/tests/Google/Service/DirectoryTest.php @@ -13,9 +13,11 @@ class DirectoryTest extends TestCase { use SampleUser; + public $dataFile = DATAFILE2; - public function getProperties($object, $propKeys = null) { + public function getProperties($object, $propKeys = null) + { if ($propKeys === null) { $propKeys = array( "changePasswordAtNextLogin", @@ -153,10 +155,11 @@ public function testUsersInsert_WithAlias() $this->assertEquals($expected, $results, $msg); } - public function getFixtures() { + public function getFixtures() + { $user4Data = '{"changePasswordAtNextLogin":false,' . '"hashFunction":"SHA-1",' . - '"id":999991,"password":"testP4ss",' . + '"id":"999991","password":"testP4ss",' . '"primaryEmail":"user_test4@sil.org",' . '"isEnforcedIn2Sv":false,' . '"isEnrolledIn2Sv":true,' . @@ -168,11 +171,11 @@ public function getFixtures() { $alias6 = new Google_Service_Directory_Alias(); $alias6->setAlias("users_alias6@sil.org"); - $alias6->setId(1); + $alias6->setId("1"); $fixtures = array( array('directory', 'user', '{"primaryEmail":"user_test1@sil.org",' . - '"id":999990}'), + '"id":"999990"}'), array('directory', 'users_alias', json_encode($alias2)), array('app_engine', 'webapp', 'webapp3 test data'), array('directory', 'user', $user4Data), @@ -183,7 +186,7 @@ public function getFixtures() { return $fixtures; } - public function getAliasFixture($alias, $email, $id) + public function getAliasFixture($alias, $email, ?string $id) { $newAlias = new Google_Service_Directory_Alias(); $newAlias->setAlias($alias); @@ -191,7 +194,7 @@ public function getAliasFixture($alias, $email, $id) $newAlias->setPrimaryEmail($email); } - if ($id) { + if (! empty($id)) { $newAlias->setId($id); } @@ -214,7 +217,7 @@ public function testUsersGet() "suspended" => false, "isEnforcedIn2Sv" => false, "isEnrolledIn2Sv" => true, - "aliases" =>null, + "aliases" => null, ); $fixtures = $this->getFixtures(); @@ -245,7 +248,7 @@ public function testUsersGet_ById() "suspended" => false, "isEnforcedIn2Sv" => false, "isEnrolledIn2Sv" => true, - "aliases" =>null, + "aliases" => null, ); $fixtures = $this->getFixtures(); @@ -274,7 +277,7 @@ public function testUsersGet_Aliases() $userData = array( "changePasswordAtNextLogin" => false, "hashFunction" => "SHA-1", - "id" => intval($userId), + "id" => $userId, "password" => "testP4ss", "primaryEmail" => $email, "suspended" => false, @@ -375,7 +378,7 @@ public function testUsersUpdate_ById() $fixturesClass = new GoogleFixtures($this->dataFile); $fixturesClass->removeAllFixtures(); - $userId = 999991; + $userId = '999991'; $userData = array( "changePasswordAtNextLogin" => false, @@ -447,8 +450,11 @@ public function testUsersUpdate_WithDifferentAliases() $primaryEmail = "user_test4@sil.org"; - $aliasFixture = $this->getAliasFixture("users_alias4B@sil.org", - $primaryEmail, null); + $aliasFixture = $this->getAliasFixture( + "users_alias4B@sil.org", + $primaryEmail, + null + ); $newFixtures = array( array('directory', 'users_alias', json_encode($aliasFixture)), ); @@ -533,7 +539,7 @@ public function testUsersDelete() $expected = array( array('id' => 1, 'type' => 'directory', 'class' => 'user', 'data' => '{"primaryEmail":"user_test1@sil.org",' . - '"id":999990}'), + '"id":"999990"}'), array('id' => 2, 'type' => 'directory', 'class' => 'users_alias', 'data' => '{"alias":"users_alias2@sil.org","etag":null,' . '"id":null,"kind":null,' . @@ -544,7 +550,7 @@ public function testUsersDelete() 'data' => 'user5 test data'), array('id' => 6, 'type' => 'directory', 'class' => 'users_alias', 'data' => '{"alias":"users_alias6@sil.org","etag":null,' . - '"id":1,"kind":null,"primaryEmail":null}'), + '"id":"1","kind":null,"primaryEmail":null}'), ); $msg = " *** Bad database data returned"; @@ -570,7 +576,7 @@ public function testUsersDelete_ById() $expected = array( array('id' => 1, 'type' => 'directory', 'class' => 'user', 'data' => '{"primaryEmail":"user_test1@sil.org",' . - '"id":999990}'), + '"id":"999990"}'), array('id' => 2, 'type' => 'directory', 'class' => 'users_alias', 'data' => '{"alias":"users_alias2@sil.org","etag":null,' . '"id":null,"kind":null,' . @@ -581,7 +587,7 @@ public function testUsersDelete_ById() 'data' => 'user5 test data'), array('id' => 6, 'type' => 'directory', 'class' => 'users_alias', 'data' => '{"alias":"users_alias6@sil.org","etag":null,' . - '"id":1,"kind":null,"primaryEmail":null}'), + '"id":"1","kind":null,"primaryEmail":null}'), ); $msg = " *** Bad database data returned"; @@ -648,8 +654,11 @@ public function testUsersAliasesListUsersAliases_Email() $fixtures = $this->getFixtures(); $fixturesClass->addFixtures($fixtures); - $aliasFixture = $this->getAliasFixture("users_alias7@sil.org", - "user_test1@sil.org", 1); + $aliasFixture = $this->getAliasFixture( + "users_alias7@sil.org", + "user_test1@sil.org", + "1" + ); $newFixtures = array( array('directory', 'users_alias', json_encode($aliasFixture)), @@ -667,13 +676,12 @@ public function testUsersAliasesListUsersAliases_Email() $expected = array( '{"alias":"users_alias2@sil.org","etag":null,"id":null,' . '"kind":null,"primaryEmail":"user_test1@sil.org"}', - '{"alias":"users_alias7@sil.org","etag":null,"id":1,' . + '{"alias":"users_alias7@sil.org","etag":null,"id":"1",' . '"kind":null,"primaryEmail":"user_test1@sil.org"}' ); $msg = " *** Bad returned Aliases"; $this->assertEquals($expected, $results, $msg); - } public function testUsersAliasesListUsersAliases_ID() @@ -685,12 +693,12 @@ public function testUsersAliasesListUsersAliases_ID() $fixturesClass->addFixtures($fixtures); $email = "user_test7@sil.org"; - $aliasB = $this->getAliasFixture("users_alias7b@sil.org", $email, 7); - $aliasC = $this->getAliasFixture("users_alias7c@sil.org", null, 7); + $aliasB = $this->getAliasFixture("users_alias7b@sil.org", $email, "7"); + $aliasC = $this->getAliasFixture("users_alias7c@sil.org", null, "7"); $newFixtures = array( array('directory', 'user', - '{"id":7,"primaryEmail":"' . $email . '",' . + '{"id":"7","primaryEmail":"' . $email . '",' . '"aliases":[]}'), array('directory', 'users_alias', json_encode($aliasB)), array('directory', 'users_alias', json_encode($aliasC)), @@ -706,15 +714,14 @@ public function testUsersAliasesListUsersAliases_ID() } $expected = array( - '{"alias":"users_alias7b@sil.org","etag":null,"id":7,"kind":null,' . + '{"alias":"users_alias7b@sil.org","etag":null,"id":"7","kind":null,' . '"primaryEmail":"user_test7@sil.org"}', - '{"alias":"users_alias7c@sil.org","etag":null,"id":7,"kind":null,' . + '{"alias":"users_alias7c@sil.org","etag":null,"id":"7","kind":null,' . '"primaryEmail":null}', ); $msg = " *** Bad returned Aliases"; $this->assertEquals($expected, $results, $msg); - } public function testUsersAliasesListUsersAliases_Structure() @@ -726,7 +733,7 @@ public function testUsersAliasesListUsersAliases_Structure() $fixturesClass->addFixtures($fixtures); $email = "user_test1@sil.org"; - $alias = $this->getAliasFixture("users_alias7@sil.org", $email, 1); + $alias = $this->getAliasFixture("users_alias7@sil.org", $email, "1"); $newFixtures = array( array('directory', 'users_alias', json_encode($alias)), ); @@ -744,7 +751,7 @@ public function testUsersAliasesListUsersAliases_Structure() $user_aliases = array(); - foreach($aliases['aliases'] as $alias) { + foreach ($aliases['aliases'] as $alias) { $user_aliases[] = $alias['alias']; } @@ -761,8 +768,11 @@ public function testUsersAliasesListUsersAliases_UserNotThere() $fixtures = $this->getFixtures(); $fixturesClass->addFixtures($fixtures); - $alias = $this->getAliasFixture("users_alias7@sil.org", - "user_test1@sil.org", 1); + $alias = $this->getAliasFixture( + "users_alias7@sil.org", + "user_test1@sil.org", + "1" + ); $newFixtures = array( array('directory', 'users_alias', json_encode($alias)), ); @@ -783,7 +793,7 @@ public function testUsersAliasesDelete() $fixturesClass->addFixtures($fixtures); $email = "user_test1@sil.org"; - $alias = $this->getAliasFixture("users_alias7@sil.org", $email, 1); + $alias = $this->getAliasFixture("users_alias7@sil.org", $email, "1"); $newFixtures = array( array('directory', 'users_alias', json_encode($alias)), @@ -791,8 +801,10 @@ public function testUsersAliasesDelete() $fixturesClass->addFixtures($newFixtures); $newDir = new Directory('anyclient', $this->dataFile); - $results = $newDir->users_aliases->delete("user_test1@sil.org", - "users_alias2@sil.org"); + $results = $newDir->users_aliases->delete( + "user_test1@sil.org", + "users_alias2@sil.org" + ); $this->assertTrue($results, " *** Didn't appear to delete the alias."); @@ -804,17 +816,16 @@ public function testUsersAliasesDelete() 'type' => 'directory', 'class' => 'users_alias', 'data' => '{"alias":"users_alias6@sil.org","etag":null,' . - '"id":1,"kind":null,"primaryEmail":null}', + '"id":"1","kind":null,"primaryEmail":null}', ), array('id' => '7', 'type' => 'directory', 'class' => 'users_alias', 'data' => '{"alias":"users_alias7@sil.org","etag":null,' . - '"id":1,"kind":null,"primaryEmail":"' . $email . '"}'), + '"id":"1","kind":null,"primaryEmail":"' . $email . '"}'), ); $msg = " *** Mismatching users_aliases in db"; $this->assertEquals($expected, $results, $msg); - } public function testUserArrayAccess() @@ -824,7 +835,6 @@ public function testUserArrayAccess() $this->assertFalse($user->suspended, ' *** class access failed'); $this->assertFalse($user['suspended'], ' *** array access failed'); - } public function testUserClassAccess() @@ -834,7 +844,6 @@ public function testUserClassAccess() $this->assertFalse($user->suspended, ' *** class access failed'); $this->assertFalse($user['suspended'], ' *** array access failed'); - } public function testAliasArrayAccess() @@ -845,7 +854,6 @@ public function testAliasArrayAccess() $this->assertEquals($email, $alias->primaryEmail, ' *** class access failed'); $this->assertEquals($email, $alias['primaryEmail'], ' *** array access failed'); - } public function testAliasClassAccess() @@ -856,6 +864,5 @@ public function testAliasClassAccess() $this->assertEquals($email, $alias->primaryEmail, ' *** class access failed'); $this->assertEquals($email, $alias['primaryEmail'], ' *** array access failed'); - } -} +}