From c864eadfb921045d6e7efb9074706e9114be43c0 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 15 Aug 2025 14:48:12 +0530 Subject: [PATCH 1/3] Enhance permission handling in SQL adapter and add tests for document creation with varied attributes --- src/Database/Adapter/SQL.php | 12 ++++---- tests/e2e/Adapter/Scopes/DocumentTests.php | 32 ++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/Database/Adapter/SQL.php b/src/Database/Adapter/SQL.php index 5eeb793b9..07113d3d3 100644 --- a/src/Database/Adapter/SQL.php +++ b/src/Database/Adapter/SQL.php @@ -1864,6 +1864,7 @@ public function createDocuments(string $collection, array $documents): array $batchKeys = []; $bindValues = []; $permissions = []; + $bindValuesPermissions = []; foreach ($documents as $index => $document) { $attributes = $document->getAttributes(); @@ -1902,6 +1903,10 @@ public function createDocuments(string $collection, array $documents): array $permission = \str_replace('"', '', $permission); $permission = "('{$type}', '{$permission}', :_uid_{$index} {$tenantBind})"; $permissions[] = $permission; + $bindValuesPermissions[":_uid_{$index}"] = $document->getId(); + if ($this->sharedTables) { + $bindValuesPermissions[":_tenant_{$index}"] = $document->getTenant(); + } } } } @@ -1930,11 +1935,8 @@ public function createDocuments(string $collection, array $documents): array $stmtPermissions = $this->getPDO()->prepare($sqlPermissions); - foreach ($documents as $index => $document) { - $stmtPermissions->bindValue(":_uid_{$index}", $document->getId()); - if ($this->sharedTables) { - $stmtPermissions->bindValue(":_tenant_{$index}", $document->getTenant()); - } + foreach ($bindValuesPermissions as $key => $value) { + $stmtPermissions->bindValue($key, $value, $this->getPDOType($value)); } $this->execute($stmtPermissions); diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 188872a3b..27ae5a96c 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -369,6 +369,38 @@ public function testCreateDocuments(): void $this->assertIsInt($document->getAttribute('bigint')); $this->assertEquals(9223372036854775807, $document->getAttribute('bigint')); } + + // with different set of attributes + $colName = "docs_with_diff"; + $database->createCollection($colName); + $database->createAttribute($colName, 'key', Database::VAR_STRING, 50, true); + $database->createAttribute($colName, 'value', Database::VAR_STRING, 50, false); + $permissions = [Permission::read(Role::any()), Permission::write(Role::any()),Permission::update(Role::any())]; + $docs = [ + new Document([ + '$id' => 'doc1', + 'key' => 'doc1', + ]), + new Document([ + '$id' => 'doc2', + 'key' => 'doc2', + ]), + new Document([ + '$id' => 'doc3', + '$permissions' => $permissions, + 'key' => 'doc3', + 'value' => 'test' + ]), + ]; + $this->assertEquals(3, $database->createDocuments($colName, $docs)); + // we should get only one document as read permission provided to the last document only + $addedDocs = $database->find($colName); + $this->assertCount(1, $addedDocs); + $doc = $addedDocs[0]; + $this->assertEquals('doc3', $doc->getId()); + $this->assertNotEmpty($doc->getPermissions()); + $this->assertCount(3, $doc->getPermissions()); + $database->deleteCollection($colName); } public function testCreateDocumentsWithAutoIncrement(): void From 1f0ec3949b0a4917e0702d105a59dfd560080093 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 15 Aug 2025 18:19:12 +0530 Subject: [PATCH 2/3] added testcases for mismatching for update and create bulk --- tests/e2e/Adapter/Scopes/DocumentTests.php | 89 ++++++++++++++-------- 1 file changed, 57 insertions(+), 32 deletions(-) diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 7bb4fbf8a..0a008d228 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -369,38 +369,6 @@ public function testCreateDocuments(): void $this->assertIsInt($document->getAttribute('bigint')); $this->assertEquals(9223372036854775807, $document->getAttribute('bigint')); } - - // with different set of attributes - $colName = "docs_with_diff"; - $database->createCollection($colName); - $database->createAttribute($colName, 'key', Database::VAR_STRING, 50, true); - $database->createAttribute($colName, 'value', Database::VAR_STRING, 50, false); - $permissions = [Permission::read(Role::any()), Permission::write(Role::any()),Permission::update(Role::any())]; - $docs = [ - new Document([ - '$id' => 'doc1', - 'key' => 'doc1', - ]), - new Document([ - '$id' => 'doc2', - 'key' => 'doc2', - ]), - new Document([ - '$id' => 'doc3', - '$permissions' => $permissions, - 'key' => 'doc3', - 'value' => 'test' - ]), - ]; - $this->assertEquals(3, $database->createDocuments($colName, $docs)); - // we should get only one document as read permission provided to the last document only - $addedDocs = $database->find($colName); - $this->assertCount(1, $addedDocs); - $doc = $addedDocs[0]; - $this->assertEquals('doc3', $doc->getId()); - $this->assertNotEmpty($doc->getPermissions()); - $this->assertCount(3, $doc->getPermissions()); - $database->deleteCollection($colName); } public function testCreateDocumentsWithAutoIncrement(): void @@ -5878,4 +5846,61 @@ public function testUpdateDocumentsCount(): void $database->deleteCollection($collectionName); } + + public function testCreateUpdateDocumentsMismatch() + { + /** @var Database $database */ + $database = static::getDatabase(); + + // with different set of attributes + $colName = "docs_with_diff"; + $database->createCollection($colName); + $database->createAttribute($colName, 'key', Database::VAR_STRING, 50, true); + $database->createAttribute($colName, 'value', Database::VAR_STRING, 50, false, 'value'); + $permissions = [Permission::read(Role::any()), Permission::write(Role::any()),Permission::update(Role::any())]; + $docs = [ + new Document([ + '$id' => 'doc1', + 'key' => 'doc1', + ]), + new Document([ + '$id' => 'doc2', + 'key' => 'doc2', + 'value' => 'test', + ]), + new Document([ + '$id' => 'doc3', + '$permissions' => $permissions, + 'key' => 'doc3' + ]), + ]; + $this->assertEquals(3, $database->createDocuments($colName, $docs)); + // we should get only one document as read permission provided to the last document only + $addedDocs = $database->find($colName); + $this->assertCount(1, $addedDocs); + $doc = $addedDocs[0]; + $this->assertEquals('doc3', $doc->getId()); + $this->assertNotEmpty($doc->getPermissions()); + $this->assertCount(3, $doc->getPermissions()); + + $database->createDocument($colName, new Document([ + '$id' => 'doc4', + '$permissions' => $permissions, + 'key' => 'doc4' + ])); + + $this->assertEquals(2, $database->updateDocuments($colName, new Document(['key' => 'new doc']))); + $doc = $database->getDocument($colName, 'doc4'); + $this->assertEquals('doc4', $doc->getId()); + $this->assertEquals('value', $doc->getAttribute('value')); + + $addedDocs = $database->find($colName); + $this->assertCount(2, $addedDocs); + foreach ($addedDocs as $doc) { + $this->assertNotEmpty($doc->getPermissions()); + $this->assertCount(3, $doc->getPermissions()); + $this->assertEquals('value', $doc->getAttribute('value')); + } + $database->deleteCollection($colName); + } } From 7c7af88a234476032f411dc086b42e93dfb5b9d0 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Fri, 15 Aug 2025 18:21:25 +0530 Subject: [PATCH 3/3] linting --- tests/e2e/Adapter/Scopes/DocumentTests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Adapter/Scopes/DocumentTests.php b/tests/e2e/Adapter/Scopes/DocumentTests.php index 0a008d228..bf7f2a905 100644 --- a/tests/e2e/Adapter/Scopes/DocumentTests.php +++ b/tests/e2e/Adapter/Scopes/DocumentTests.php @@ -5847,7 +5847,7 @@ public function testUpdateDocumentsCount(): void $database->deleteCollection($collectionName); } - public function testCreateUpdateDocumentsMismatch() + public function testCreateUpdateDocumentsMismatch(): void { /** @var Database $database */ $database = static::getDatabase();