From 82dcbe2734523e94dee0e36dcada7e0a456c4da6 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 3 Aug 2025 16:25:47 +0300 Subject: [PATCH 1/7] Fix permissions encoding --- src/Database/Database.php | 16 ++++++++++++---- tests/e2e/Adapter/Scopes/DocumentTests.php | 5 +++++ tests/e2e/Adapter/Scopes/PermissionTests.php | 14 +++++++++++++- 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index dce4a39b3..7d6aeef1d 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3609,6 +3609,10 @@ public function createDocument(string $collection, Document $document): Document ->setAttribute('$createdAt', ($createdAt === null || !$this->preserveDates) ? $time : $createdAt) ->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt); + if (empty($document->getPermissions())){ + $document->setAttribute('$permissions', []); + } + if ($this->adapter->getSharedTables()) { if ($this->adapter->getTenantPerDocument()) { if ( @@ -3708,6 +3712,10 @@ public function createDocuments( ->setAttribute('$createdAt', ($createdAt === null || !$this->preserveDates) ? $time : $createdAt) ->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt); + if (empty($document->getPermissions())){ + $document->setAttribute('$permissions', []); + } + if ($this->adapter->getSharedTables()) { if ($this->adapter->getTenantPerDocument()) { if ($document->getTenant() === null) { @@ -4421,7 +4429,7 @@ public function updateDocuments( if (empty($batch)) { break; } - +var_dump($updates); $this->withTransaction(function () use ($collection, $updates, &$batch) { foreach ($batch as &$document) { $new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy())); @@ -6334,9 +6342,9 @@ public function encode(Document $collection, Document $document): Document } if ($key === '$permissions') { - if (empty($value)) { - $document->setAttribute('$permissions', []); // set default value - } +// if (empty($value)) { +// $document->setAttribute('$permissions', []); // set default value +// } continue; } diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index dedc53ce0..40c319574 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -449,6 +449,11 @@ public function testSkipPermissions(): void $this->assertEquals(2, \count($results)); $this->assertEquals(2, $count); + + foreach ($results as $result){ + $this->assertArrayHasKey('$permissions', $result); + $this->assertEquals([], $result->getAttribute('$permissions')); + } } public function testUpsertDocuments(): void diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index 433396276..ae16a3518 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -14,7 +14,17 @@ trait PermissionTests { - public function testCreateDocumentsEmptyPermission(): void +// public function testCreateDocumentsEmptyPermission1(): void +// { +// /** @var Database $database */ +// $database = static::getDatabase(); +// +// $database->createCollection(__FUNCTION__); +// +// $document = $database->createDocument(__FUNCTION__, new Document()); +// } + + public function testCreateDocumentsEmptyPermission(): void { /** @var Database $database */ $database = static::getDatabase(); @@ -46,6 +56,8 @@ public function testCreateDocumentsEmptyPermission(): void $this->assertArrayHasKey('$permissions', $result); $this->assertEquals([], $result->getAttribute('$permissions')); } + + $this->assertEquals('shmuel', 'fogel'); } public function testReadPermissionsFailure(): Document From f4551b1f91db9700fabe4a8de29038876c344173 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 3 Aug 2025 17:29:28 +0300 Subject: [PATCH 2/7] UpdateDocument preserbe original --- src/Database/Database.php | 3 +- src/Database/Document.php | 4 +- tests/e2e/Adapter/Scopes/PermissionTests.php | 93 +++++++++++++++++--- 3 files changed, 85 insertions(+), 15 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 7d6aeef1d..c5fa5a605 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -4429,7 +4429,7 @@ public function updateDocuments( if (empty($batch)) { break; } -var_dump($updates); + $this->withTransaction(function () use ($collection, $updates, &$batch) { foreach ($batch as &$document) { $new = new Document(\array_merge($document->getArrayCopy(), $updates->getArrayCopy())); @@ -4453,6 +4453,7 @@ public function updateDocuments( $document = $this->encode($collection, $document); } + $this->adapter->updateDocuments( $collection->getId(), $updates, diff --git a/src/Database/Document.php b/src/Database/Document.php index 6b82d73cf..566a37444 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -278,9 +278,7 @@ public function setAttributes(array $attributes): static */ public function removeAttribute(string $key): static { - if (\array_key_exists($key, (array)$this)) { - unset($this[$key]); - } + unset($this[$key]); /* @phpstan-ignore-next-line */ return $this; diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index ae16a3518..fed9b0c3e 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -14,15 +14,88 @@ trait PermissionTests { -// public function testCreateDocumentsEmptyPermission1(): void -// { -// /** @var Database $database */ -// $database = static::getDatabase(); -// -// $database->createCollection(__FUNCTION__); -// -// $document = $database->createDocument(__FUNCTION__, new Document()); -// } + public function testUnsetPermissions(): void + { + /** @var Database $database */ + $database = static::getDatabase(); + + $database->createCollection(__FUNCTION__); + $this->assertTrue($database->createAttribute( + collection: __FUNCTION__, + id: 'president', + type: Database::VAR_STRING, + size: 255, + required: false + )); + + $permissions = [ + Permission::read(Role::any()), + Permission::create(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ]; + + $documents = []; + + for ($i = 0; $i < 3; $i++) { + $documents[] = new Document([ + '$permissions' => $permissions, + 'president' => 'Donald Trump' + ]); + } + + /** + * @var $results Array + */ + $results = []; + $count = $database->createDocuments(__FUNCTION__, $documents, onNext: function ($doc) use (&$results) { + $results[] = $doc; + }); + + $this->assertEquals(3, $count); + + foreach ($results as $result) { + $this->assertEquals('Donald Trump', $result->getAttribute('president')); + $this->assertEquals($permissions, $result->getPermissions()); + } + + /** + * No permissions passed, Check old is preserved + */ + $updates = new Document([ + 'president' => 'George Washington' + ]); + + /** + * @var $results Array + */ + $results = []; + $modified = $database->updateDocuments( + __FUNCTION__, + $updates, + onNext: function ($doc) use (&$results) { + $results[] = $doc; + } + ); + + $this->assertEquals(3, $modified); + + foreach ($results as $result) { + $this->assertEquals('George Washington', $result->getAttribute('president')); + $this->assertEquals($permissions, $result->getPermissions()); + } + + $documents = $database->find(__FUNCTION__); + + $this->assertEquals(3, count($documents)); + + foreach ($documents as $document) { + $this->assertEquals('George Washington', $document->getAttribute('president')); + $this->assertEquals($permissions, $document->getPermissions()); + } + + $this->assertEquals('shmuel', 'fogel'); + } public function testCreateDocumentsEmptyPermission(): void { @@ -56,8 +129,6 @@ public function testCreateDocumentsEmptyPermission(): void $this->assertArrayHasKey('$permissions', $result); $this->assertEquals([], $result->getAttribute('$permissions')); } - - $this->assertEquals('shmuel', 'fogel'); } public function testReadPermissionsFailure(): Document From a85cec9dcbaa93767b4741e101aaa1af916ebbf8 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 3 Aug 2025 17:34:03 +0300 Subject: [PATCH 3/7] UpdateDocuments Bug --- tests/e2e/Adapter/Scopes/PermissionTests.php | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index fed9b0c3e..be0348885 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -94,6 +94,42 @@ public function testUnsetPermissions(): void $this->assertEquals($permissions, $document->getPermissions()); } + /** + * Unset permissions + */ + $updates = new Document([ + '$permissions' => [], + 'president' => 'Richard Nixon' + ]); + + /** + * @var $results Array + */ + $results = []; + $modified = $database->updateDocuments( + __FUNCTION__, + $updates, + onNext: function ($doc) use (&$results) { + $results[] = $doc; + } + ); + + $this->assertEquals(3, $modified); + + foreach ($results as $result) { + $this->assertEquals('Richard Nixon', $result->getAttribute('president')); + $this->assertEquals([], $result->getPermissions()); + } + + $documents = $database->find(__FUNCTION__); + + $this->assertEquals(3, count($documents)); + + foreach ($documents as $document) { + $this->assertEquals('Richard Nixon', $document->getAttribute('president')); + $this->assertEquals([], $document->getPermissions()); + } + $this->assertEquals('shmuel', 'fogel'); } From b87f053fe2db13b3e9073afd0e060734588bd4ee Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 3 Aug 2025 17:43:07 +0300 Subject: [PATCH 4/7] UpdateDocuments Bug --- tests/e2e/Adapter/Scopes/PermissionTests.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index be0348885..6247c2294 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -129,8 +129,6 @@ public function testUnsetPermissions(): void $this->assertEquals('Richard Nixon', $document->getAttribute('president')); $this->assertEquals([], $document->getPermissions()); } - - $this->assertEquals('shmuel', 'fogel'); } public function testCreateDocumentsEmptyPermission(): void From 6ac96b21c1c191f9091ea4a9b4193f716933b591 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 3 Aug 2025 17:51:25 +0300 Subject: [PATCH 5/7] Remove comments --- src/Database/Database.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index c5fa5a605..9f2674f0a 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -6343,9 +6343,6 @@ public function encode(Document $collection, Document $document): Document } if ($key === '$permissions') { -// if (empty($value)) { -// $document->setAttribute('$permissions', []); // set default value -// } continue; } @@ -6426,6 +6423,10 @@ public function decode(Document $collection, Document $document, array $selectio $filters = $attribute['filters'] ?? []; $value = $document->getAttribute($key); + if ($key === '$permissions') { + continue; + } + if (\is_null($value)) { $value = $document->getAttribute($this->adapter->filter($key)); @@ -6485,6 +6486,10 @@ public function casting(Document $collection, Document $document): Document continue; } + if ($key === '$permissions') { + continue; + } + if ($array) { $value = !is_string($value) ? $value From 9d2c910871f4010b13edf76f908cc3929e06cfec Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 3 Aug 2025 18:03:16 +0300 Subject: [PATCH 6/7] Remove update documents tests to a different PR --- tests/e2e/Adapter/Scopes/PermissionTests.php | 117 ------------------- 1 file changed, 117 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index 6247c2294..40f3705a6 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -14,123 +14,6 @@ trait PermissionTests { - public function testUnsetPermissions(): void - { - /** @var Database $database */ - $database = static::getDatabase(); - - $database->createCollection(__FUNCTION__); - $this->assertTrue($database->createAttribute( - collection: __FUNCTION__, - id: 'president', - type: Database::VAR_STRING, - size: 255, - required: false - )); - - $permissions = [ - Permission::read(Role::any()), - Permission::create(Role::any()), - Permission::update(Role::any()), - Permission::delete(Role::any()), - ]; - - $documents = []; - - for ($i = 0; $i < 3; $i++) { - $documents[] = new Document([ - '$permissions' => $permissions, - 'president' => 'Donald Trump' - ]); - } - - /** - * @var $results Array - */ - $results = []; - $count = $database->createDocuments(__FUNCTION__, $documents, onNext: function ($doc) use (&$results) { - $results[] = $doc; - }); - - $this->assertEquals(3, $count); - - foreach ($results as $result) { - $this->assertEquals('Donald Trump', $result->getAttribute('president')); - $this->assertEquals($permissions, $result->getPermissions()); - } - - /** - * No permissions passed, Check old is preserved - */ - $updates = new Document([ - 'president' => 'George Washington' - ]); - - /** - * @var $results Array - */ - $results = []; - $modified = $database->updateDocuments( - __FUNCTION__, - $updates, - onNext: function ($doc) use (&$results) { - $results[] = $doc; - } - ); - - $this->assertEquals(3, $modified); - - foreach ($results as $result) { - $this->assertEquals('George Washington', $result->getAttribute('president')); - $this->assertEquals($permissions, $result->getPermissions()); - } - - $documents = $database->find(__FUNCTION__); - - $this->assertEquals(3, count($documents)); - - foreach ($documents as $document) { - $this->assertEquals('George Washington', $document->getAttribute('president')); - $this->assertEquals($permissions, $document->getPermissions()); - } - - /** - * Unset permissions - */ - $updates = new Document([ - '$permissions' => [], - 'president' => 'Richard Nixon' - ]); - - /** - * @var $results Array - */ - $results = []; - $modified = $database->updateDocuments( - __FUNCTION__, - $updates, - onNext: function ($doc) use (&$results) { - $results[] = $doc; - } - ); - - $this->assertEquals(3, $modified); - - foreach ($results as $result) { - $this->assertEquals('Richard Nixon', $result->getAttribute('president')); - $this->assertEquals([], $result->getPermissions()); - } - - $documents = $database->find(__FUNCTION__); - - $this->assertEquals(3, count($documents)); - - foreach ($documents as $document) { - $this->assertEquals('Richard Nixon', $document->getAttribute('president')); - $this->assertEquals([], $document->getPermissions()); - } - } - public function testCreateDocumentsEmptyPermission(): void { /** @var Database $database */ From 8735c8fce78d64bb64f134dc2207ea684c9848a0 Mon Sep 17 00:00:00 2001 From: fogelito Date: Sun, 3 Aug 2025 18:04:19 +0300 Subject: [PATCH 7/7] formatting --- src/Database/Database.php | 4 ++-- tests/e2e/Adapter/Scopes/DocumentTests.php | 2 +- tests/e2e/Adapter/Scopes/PermissionTests.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Database/Database.php b/src/Database/Database.php index 9f2674f0a..74c5c6d91 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -3609,7 +3609,7 @@ public function createDocument(string $collection, Document $document): Document ->setAttribute('$createdAt', ($createdAt === null || !$this->preserveDates) ? $time : $createdAt) ->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt); - if (empty($document->getPermissions())){ + if (empty($document->getPermissions())) { $document->setAttribute('$permissions', []); } @@ -3712,7 +3712,7 @@ public function createDocuments( ->setAttribute('$createdAt', ($createdAt === null || !$this->preserveDates) ? $time : $createdAt) ->setAttribute('$updatedAt', ($updatedAt === null || !$this->preserveDates) ? $time : $updatedAt); - if (empty($document->getPermissions())){ + if (empty($document->getPermissions())) { $document->setAttribute('$permissions', []); } diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 40c319574..81ce0fae2 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -450,7 +450,7 @@ public function testSkipPermissions(): void $this->assertEquals(2, \count($results)); $this->assertEquals(2, $count); - foreach ($results as $result){ + foreach ($results as $result) { $this->assertArrayHasKey('$permissions', $result); $this->assertEquals([], $result->getAttribute('$permissions')); } diff --git a/tests/e2e/Adapter/Scopes/PermissionTests.php b/tests/e2e/Adapter/Scopes/PermissionTests.php index 40f3705a6..433396276 100644 --- a/tests/e2e/Adapter/Scopes/PermissionTests.php +++ b/tests/e2e/Adapter/Scopes/PermissionTests.php @@ -14,7 +14,7 @@ trait PermissionTests { - public function testCreateDocumentsEmptyPermission(): void + public function testCreateDocumentsEmptyPermission(): void { /** @var Database $database */ $database = static::getDatabase();