From 297d03126e7eebeb7325c0e46bca64fad1f6405d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 30 Dec 2021 11:55:17 +0545 Subject: [PATCH 1/3] make empty prefs object instead of array --- src/Appwrite/Utopia/Response/Model/User.php | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/Appwrite/Utopia/Response/Model/User.php b/src/Appwrite/Utopia/Response/Model/User.php index fd65b6a7e..cd25e5408 100644 --- a/src/Appwrite/Utopia/Response/Model/User.php +++ b/src/Appwrite/Utopia/Response/Model/User.php @@ -4,6 +4,8 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; +use stdClass; +use Utopia\Database\Document; class User extends Model { @@ -61,6 +63,24 @@ class User extends Model ; } + /** + * Get Collection + * + * @return string + */ + public function filter(Document $document): Document + { + $prefs = $document->getAttribute('prefs'); + if($prefs instanceof Document) { + $prefs = $prefs->getArrayCopy(); + } + + if(is_array($prefs) && empty($prefs)) { + $document->setAttribute('prefs', new stdClass); + } + return $document; + } + /** * Get Name * From 7a18639c5c5100ae8ec9ed0529255e6d56d7f47d Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 30 Dec 2021 13:54:23 +0545 Subject: [PATCH 2/3] test for user prefs --- tests/e2e/Client.php | 27 ++++++++++++++------------ tests/e2e/Services/Users/UsersBase.php | 19 ++++++++++++------ 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/tests/e2e/Client.php b/tests/e2e/Client.php index 1dc7b71c1..4e0c138b9 100644 --- a/tests/e2e/Client.php +++ b/tests/e2e/Client.php @@ -151,10 +151,11 @@ class Client * @param string $path * @param array $params * @param array $headers + * @param bool $decode * @return array|string * @throws Exception */ - public function call(string $method, string $path = '', array $headers = [], array $params = []) + public function call(string $method, string $path = '', array $headers = [], array $params = [], bool $decode = true) { $headers = array_merge($this->headers, $headers); $ch = curl_init($this->endpoint . $path . (($method == self::METHOD_GET && !empty($params)) ? '?' . http_build_query($params) : '')); @@ -216,17 +217,19 @@ class Client $responseType = $responseHeaders['content-type'] ?? ''; $responseStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE); - switch (substr($responseType, 0, strpos($responseType, ';'))) { - case 'application/json': - $json = json_decode($responseBody, true); - - if ($json === null) { - throw new Exception('Failed to parse response: '.$responseBody); - } - - $responseBody = $json; - $json = null; - break; + if($decode) { + switch (substr($responseType, 0, strpos($responseType, ';'))) { + case 'application/json': + $json = json_decode($responseBody, true); + + if ($json === null) { + throw new Exception('Failed to parse response: '.$responseBody); + } + + $responseBody = $json; + $json = null; + break; + } } if ((curl_errno($ch)/* || 200 != $responseStatus*/)) { diff --git a/tests/e2e/Services/Users/UsersBase.php b/tests/e2e/Services/Users/UsersBase.php index 17a28377f..3084ff5e2 100644 --- a/tests/e2e/Services/Users/UsersBase.php +++ b/tests/e2e/Services/Users/UsersBase.php @@ -20,13 +20,20 @@ trait UsersBase 'email' => 'cristiano.ronaldo@manchester-united.co.uk', 'password' => 'password', 'name' => 'Cristiano Ronaldo', - ]); + ], false); + + // Test empty prefs is object not array + $bodyString = $user['body']; + $prefs = substr($bodyString, strpos($bodyString, '"prefs":')+8,2); + $this->assertEquals('{}', $prefs); + + $body = json_decode($bodyString, true); $this->assertEquals($user['headers']['status-code'], 201); - $this->assertEquals($user['body']['name'], 'Cristiano Ronaldo'); - $this->assertEquals($user['body']['email'], 'cristiano.ronaldo@manchester-united.co.uk'); - $this->assertEquals($user['body']['status'], true); - $this->assertGreaterThan(0, $user['body']['registration']); + $this->assertEquals($body['name'], 'Cristiano Ronaldo'); + $this->assertEquals($body['email'], 'cristiano.ronaldo@manchester-united.co.uk'); + $this->assertEquals($body['status'], true); + $this->assertGreaterThan(0, $body['registration']); /** * Test Create with Custom ID for SUCCESS @@ -48,7 +55,7 @@ trait UsersBase $this->assertEquals(true, $res['body']['status']); $this->assertGreaterThan(0, $res['body']['registration']); - return ['userId' => $user['body']['$id']]; + return ['userId' => $body['$id']]; } /** From 95afb6aa0b6822026dca401bb992593a679e10c8 Mon Sep 17 00:00:00 2001 From: Damodar Lohani Date: Thu, 30 Dec 2021 15:49:17 +0545 Subject: [PATCH 3/3] Update src/Appwrite/Utopia/Response/Model/User.php --- src/Appwrite/Utopia/Response/Model/User.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Appwrite/Utopia/Response/Model/User.php b/src/Appwrite/Utopia/Response/Model/User.php index cd25e5408..43200ef7c 100644 --- a/src/Appwrite/Utopia/Response/Model/User.php +++ b/src/Appwrite/Utopia/Response/Model/User.php @@ -4,7 +4,6 @@ namespace Appwrite\Utopia\Response\Model; use Appwrite\Utopia\Response; use Appwrite\Utopia\Response\Model; -use stdClass; use Utopia\Database\Document; class User extends Model