From 7d6132516f2122e034ed727f22a1f13158141b98 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 09:51:41 +0300 Subject: [PATCH 01/10] check isset --- src/Database/Document.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index 6b82d73cf..e30be6d36 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -27,11 +27,11 @@ class Document extends ArrayObject */ public function __construct(array $input = []) { - if (array_key_exists('$id', $input) && !\is_string($input['$id'])) { + if (isset($input['$id']) && !\is_string($input['$id'])) { throw new StructureException('$id must be of type string'); } - if (array_key_exists('$permissions', $input) && !is_array($input['$permissions'])) { + if (isset($input['$permissions']) && !is_array($input['$permissions'])) { throw new StructureException('$permissions must be of type array'); } From cdec2abbf0d485eab0fbeabf1024312d4f145191 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 10:11:01 +0300 Subject: [PATCH 02/10] Use array_key_exists --- src/Database/Document.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index e30be6d36..909853e47 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -27,14 +27,22 @@ class Document extends ArrayObject */ public function __construct(array $input = []) { - if (isset($input['$id']) && !\is_string($input['$id'])) { + if (array_key_exists('$id', $input) && !\is_string($input['$id'])) { throw new StructureException('$id must be of type string'); } - if (isset($input['$permissions']) && !is_array($input['$permissions'])) { + if (array_key_exists('$permissions', $input) && !is_array($input['$permissions'])) { throw new StructureException('$permissions must be of type array'); } +// if (isset($input['$id']) && !\is_string($input['$id'])) { +// throw new StructureException('$id must be of type string'); +// } +// +// if (isset($input['$permissions']) && !is_array($input['$permissions'])) { +// throw new StructureException('$permissions must be of type array'); +// } + foreach ($input as $key => $value) { if (!\is_array($value)) { continue; From 2613dde3f769da4459abeff7f26a66f59bf90092 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 10:16:57 +0300 Subject: [PATCH 03/10] Use isset --- src/Database/Document.php | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index 909853e47..e30be6d36 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -27,22 +27,14 @@ class Document extends ArrayObject */ public function __construct(array $input = []) { - if (array_key_exists('$id', $input) && !\is_string($input['$id'])) { + if (isset($input['$id']) && !\is_string($input['$id'])) { throw new StructureException('$id must be of type string'); } - if (array_key_exists('$permissions', $input) && !is_array($input['$permissions'])) { + if (isset($input['$permissions']) && !is_array($input['$permissions'])) { throw new StructureException('$permissions must be of type array'); } -// if (isset($input['$id']) && !\is_string($input['$id'])) { -// throw new StructureException('$id must be of type string'); -// } -// -// if (isset($input['$permissions']) && !is_array($input['$permissions'])) { -// throw new StructureException('$permissions must be of type array'); -// } - foreach ($input as $key => $value) { if (!\is_array($value)) { continue; From 2d37031df8d939dee5d01815442c5efaf8f66d9d Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 10:36:23 +0300 Subject: [PATCH 04/10] Use array_key_exists --- src/Database/Document.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index e30be6d36..7626b47b1 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -27,14 +27,22 @@ class Document extends ArrayObject */ public function __construct(array $input = []) { - if (isset($input['$id']) && !\is_string($input['$id'])) { + if (array_key_exists('$id', $input) && !\is_string($input['$id'])) { throw new StructureException('$id must be of type string'); } - if (isset($input['$permissions']) && !is_array($input['$permissions'])) { + if (array_key_exists('$permissions', $input) && !is_array($input['$permissions'])) { throw new StructureException('$permissions must be of type array'); } +// if (isset($input['$id']) && !\is_string($input['$id'])) { +// throw new StructureException('$id must be of type string'); +// } +// +// if (isset($input['$permissions']) && !is_array($input['$permissions'])) { +// throw new StructureException('$permissions must be of type array'); +// } +// foreach ($input as $key => $value) { if (!\is_array($value)) { continue; From 4b9fb2944ea985cd884849e89ce3951510dc2272 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 12:13:02 +0300 Subject: [PATCH 05/10] $skipPermissionsUpdate true --- src/Database/Database.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 96e65a4ed..cc1336625 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4108,7 +4108,7 @@ public function updateDocument(string $collection, string $id, Document $documen fn () => $this->getDocument($collection->getId(), $id, forUpdate: true) )); - $skipPermissionsUpdate = false; + $skipPermissionsUpdate = true; if ($document->offsetExists('$permissions')) { $originalPermissions = $old->getPermissions(); @@ -4941,7 +4941,7 @@ public function createOrUpdateDocumentsWithIncrease( ))); } - $skipPermissionsUpdate = false; + $skipPermissionsUpdate = true; if ($document->offsetExists('$permissions')) { $originalPermissions = $old->getPermissions(); From 15e985b3ee9d43b483145e330887905fa63bd2df Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 12:28:36 +0300 Subject: [PATCH 06/10] Remove comment --- src/Database/Document.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/src/Database/Document.php b/src/Database/Document.php index 7626b47b1..6b82d73cf 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -35,14 +35,6 @@ public function __construct(array $input = []) throw new StructureException('$permissions must be of type array'); } -// if (isset($input['$id']) && !\is_string($input['$id'])) { -// throw new StructureException('$id must be of type string'); -// } -// -// if (isset($input['$permissions']) && !is_array($input['$permissions'])) { -// throw new StructureException('$permissions must be of type array'); -// } -// foreach ($input as $key => $value) { if (!\is_array($value)) { continue; From e46c4ff9d215762ebeaef96fdbb5d918d34554ad Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 13:38:34 +0300 Subject: [PATCH 07/10] $skipPermissionsUpdate true --- tests/e2e/Adapter/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index a57fe2748..06445a09e 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -17,7 +17,7 @@ abstract class Base extends TestCase { - use CollectionTests; + //use CollectionTests; use DocumentTests; use AttributeTests; use IndexTests; From 957a496d89398713464da229eeb36cafa0820151 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 13:39:09 +0300 Subject: [PATCH 08/10] Remove comment --- tests/e2e/Adapter/Base.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Adapter/Base.php b/tests/e2e/Adapter/Base.php index 06445a09e..a57fe2748 100644 --- a/tests/e2e/Adapter/Base.php +++ b/tests/e2e/Adapter/Base.php @@ -17,7 +17,7 @@ abstract class Base extends TestCase { - //use CollectionTests; + use CollectionTests; use DocumentTests; use AttributeTests; use IndexTests; From 5f6c1fb24633141e9ffa4fd7e4b5f6716c4ffd67 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 15:14:34 +0300 Subject: [PATCH 09/10] Add test --- src/Database/Database.php | 2 +- tests/e2e/Adapter/Scopes/DocumentTests.php | 65 +++++++++++++++++++++- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index cc1336625..79ad5c264 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4108,7 +4108,7 @@ public function updateDocument(string $collection, string $id, Document $documen fn () => $this->getDocument($collection->getId(), $id, forUpdate: true) )); - $skipPermissionsUpdate = true; + $skipPermissionsUpdate = false; if ($document->offsetExists('$permissions')) { $originalPermissions = $old->getPermissions(); diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 658ae0569..e0b784105 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -388,7 +388,70 @@ public function testCreateDocumentsWithDifferentAttributes(): void $database->deleteCollection($collection); } - public function testUpsertDocuments(): void + public function testSkipPermissions(): void + { + /** @var Database $database */ + $database = static::getDatabase(); + + if (!$database->getAdapter()->getSupportForUpserts()) { + $this->expectNotToPerformAssertions(); + return; + } + + $database->createCollection(__FUNCTION__); + $database->createAttribute(__FUNCTION__, 'number', Database::VAR_INTEGER, 0, false); + + $data = []; + for ($i = 1; $i <= 10; $i++) { + $data[] = [ + '$id' => "$i", + 'number' => $i, + ]; + } + + $documents = array_map(fn($d) => new Document($d), $data); + + $results = []; + $count = $database->createDocuments(__FUNCTION__, $documents, onNext: function ($doc) use (&$results) { + $results[] = $doc; + }); + + $this->assertEquals($count, \count($results)); + $this->assertEquals(10, \count($results)); + + /** + * Update 1 row + */ + $data[\array_key_last($data)]['number'] = 100; + + /** + * Add 1 row + */ + $data[] = [ + '$id' => "101", + 'number' => 101, + ]; + + $documents = array_map(fn($d) => new Document($d), $data); + + Authorization::disable(); + + $results = []; + $count = $database->createOrUpdateDocuments( + __FUNCTION__, + $documents, + onNext: function ($doc) use (&$results) { + $results[] = $doc; + } + ); + + Authorization::reset(); + + $this->assertEquals(2, \count($results)); + $this->assertEquals(2, $count); + } + + public function testUpsertDocuments(): void { /** @var Database $database */ $database = static::getDatabase(); From f76954bb44ca206dbc80c373a453563e584befb9 Mon Sep 17 00:00:00 2001 From: fogelito Date: Mon, 28 Jul 2025 15:17:15 +0300 Subject: [PATCH 10/10] formatting --- tests/e2e/Adapter/Scopes/DocumentTests.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index e0b784105..8a5133aef 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -409,7 +409,7 @@ public function testSkipPermissions(): void ]; } - $documents = array_map(fn($d) => new Document($d), $data); + $documents = array_map(fn ($d) => new Document($d), $data); $results = []; $count = $database->createDocuments(__FUNCTION__, $documents, onNext: function ($doc) use (&$results) { @@ -432,7 +432,7 @@ public function testSkipPermissions(): void 'number' => 101, ]; - $documents = array_map(fn($d) => new Document($d), $data); + $documents = array_map(fn ($d) => new Document($d), $data); Authorization::disable(); @@ -451,7 +451,7 @@ public function testSkipPermissions(): void $this->assertEquals(2, $count); } - public function testUpsertDocuments(): void + public function testUpsertDocuments(): void { /** @var Database $database */ $database = static::getDatabase();