From 59026a17dcc013776004cddd126447abb762615c Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Mon, 4 Aug 2025 19:01:48 +0530 Subject: [PATCH 1/5] feat: Enhance database operations with event queues and improve test coverage for bulk operations - Added event queue injections in Bulk Update and Create classes for better event handling. - Modified getUser method in Scope class to allow for user retrieval with an override option. - Expanded RealtimeCustomClientTest to include comprehensive tests for bulk create, update, and delete operations, ensuring proper event emissions for multiple clients. - Improved assertions in tests to validate event data and permissions for created, updated, and deleted documents. --- .../Collections/Documents/Action.php | 46 ++ .../Collections/Documents/Bulk/Delete.php | 18 +- .../Collections/Documents/Bulk/Update.php | 18 +- .../Collections/Documents/Create.php | 16 +- .../Http/Grids/Tables/Rows/Bulk/Delete.php | 4 + .../Http/Grids/Tables/Rows/Bulk/Update.php | 4 + .../Http/Grids/Tables/Rows/Create.php | 3 + tests/e2e/Scopes/Scope.php | 4 +- .../Realtime/RealtimeCustomClientTest.php | 633 +++++++++++++++++- 9 files changed, 739 insertions(+), 7 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 9a1c5f3dad..6766097f81 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Documents; +use Appwrite\Event\Event; use Appwrite\Extend\Exception; use Utopia\Database\Database; use Utopia\Database\Document; @@ -279,4 +280,49 @@ abstract class Action extends UtopiaAction return true; } + + /** + * For triggering different queues for each document for a bulk documents + * @param string $event + * @param Document $database + * @param Document $collection + * @param Document[] $documents + * @param Event $queueForEvents + * @param Event $queueForRealtime + * @param Event $queueForFunctions + * @param Event $queueForWebhooks + * @return void + */ + final protected function triggerQueuesForBulkDocuments(string $event, Document $database, Document $collection, array $documents, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks) + { + foreach ($documents as $document) { + $queueForEvents + ->setEvent($event) + ->setParam('databaseId', $database->getId()) + ->setContext('database', $database) + ->setParam('collectionId', $collection->getId()) + ->setParam('tableId', $collection->getId()) + ->setParam('documentId', $document->getId()) + ->setParam('rowId', $document->getId()) + ->setPayload($document->getArrayCopy()) + ->setContext($this->getCollectionsEventsContext(), $collection); + + $queueForRealtime + ->from($queueForEvents) + ->trigger(); + + $queueForFunctions + ->from($queueForEvents) + ->trigger(); + + $queueForWebhooks + ->from($queueForEvents) + ->trigger(); + } + + $queueForEvents->reset(); + $queueForRealtime->reset(); + $queueForFunctions->reset(); + $queueForWebhooks->reset(); + } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php index 440927d45c..17c524b23c 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Documents\Bulk; +use Appwrite\Event\Event; use Appwrite\Event\StatsUsage; use Appwrite\Extend\Exception; use Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Documents\Action; @@ -73,10 +74,14 @@ class Delete extends Action ->inject('dbForProject') ->inject('queueForStatsUsage') ->inject('plan') + ->inject('queueForEvents') + ->inject('queueForRealtime') + ->inject('queueForFunctions') + ->inject('queueForWebhooks') ->callback($this->action(...)); } - public function action(string $databaseId, string $collectionId, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, array $plan): void + public function action(string $databaseId, string $collectionId, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, array $plan, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks): void { $database = $dbForProject->getDocument('databases', $databaseId); if ($database->isEmpty()) { @@ -134,5 +139,16 @@ class Delete extends Action 'total' => $modified, $this->getSdkGroup() => $documents, ]), $this->getResponseModel()); + + $this->triggerQueuesForBulkDocuments( + 'databases.[databaseId].collections.[collectionId].documents.[documentId].delete', + $database, + $collection, + $documents, + $queueForEvents, + $queueForRealtime, + $queueForFunctions, + $queueForWebhooks + ); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php index 14bd71c5ea..908817f31d 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php @@ -2,6 +2,7 @@ namespace Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Documents\Bulk; +use Appwrite\Event\Event; use Appwrite\Event\StatsUsage; use Appwrite\Extend\Exception; use Appwrite\Platform\Modules\Databases\Http\Databases\Collections\Documents\Action; @@ -77,10 +78,14 @@ class Update extends Action ->inject('dbForProject') ->inject('queueForStatsUsage') ->inject('plan') + ->inject('queueForEvents') + ->inject('queueForRealtime') + ->inject('queueForFunctions') + ->inject('queueForWebhooks') ->callback($this->action(...)); } - public function action(string $databaseId, string $collectionId, string|array $data, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, array $plan): void + public function action(string $databaseId, string $collectionId, string|array $data, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, array $plan, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks): void { $data = \is_string($data) ? \json_decode($data, true) @@ -158,5 +163,16 @@ class Update extends Action 'total' => $modified, $this->getSdkGroup() => $documents ]), $this->getResponseModel()); + + $this->triggerQueuesForBulkDocuments( + 'databases.[databaseId].collections.[collectionId].documents.[documentId].update', + $database, + $collection, + $documents, + $queueForEvents, + $queueForRealtime, + $queueForFunctions, + $queueForWebhooks + ); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index 9e7a31c4be..d961f400eb 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -122,10 +122,12 @@ class Create extends Action ->inject('user') ->inject('queueForEvents') ->inject('queueForStatsUsage') + ->inject('queueForRealtime') + ->inject('queueForFunctions') + ->inject('queueForWebhooks') ->callback($this->action(...)); } - - public function action(string $databaseId, string $documentId, string $collectionId, string|array $data, ?array $permissions, ?array $documents, UtopiaResponse $response, Database $dbForProject, Document $user, Event $queueForEvents, StatsUsage $queueForStatsUsage): void + public function action(string $databaseId, string $documentId, string $collectionId, string|array $data, ?array $permissions, ?array $documents, UtopiaResponse $response, Database $dbForProject, Document $user, Event $queueForEvents, StatsUsage $queueForStatsUsage, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks): void { $data = \is_string($data) ? \json_decode($data, true) @@ -417,6 +419,16 @@ class Create extends Action $this->getSdkGroup() => $documents ]), $this->getBulkResponseModel()); + $this->triggerQueuesForBulkDocuments( + 'databases.[databaseId].collections.[collectionId].documents.[documentId].create', + $database, + $collection, + $documents, + $queueForEvents, + $queueForRealtime, + $queueForFunctions, + $queueForWebhooks + ); return; } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php index 4837d30e46..aab4906d02 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php @@ -60,6 +60,10 @@ class Delete extends DocumentsDelete ->inject('dbForProject') ->inject('queueForStatsUsage') ->inject('plan') + ->inject('queueForEvents') + ->inject('queueForRealtime') + ->inject('queueForFunctions') + ->inject('queueForWebhooks') ->callback($this->action(...)); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php index c72d82d65d..2c373ffbc6 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php @@ -62,6 +62,10 @@ class Update extends DocumentsUpdate ->inject('dbForProject') ->inject('queueForStatsUsage') ->inject('plan') + ->inject('queueForEvents') + ->inject('queueForRealtime') + ->inject('queueForFunctions') + ->inject('queueForWebhooks') ->callback($this->action(...)); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Create.php index 56933b882c..6fadbe1412 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Create.php @@ -101,6 +101,9 @@ class Create extends DocumentCreate ->inject('user') ->inject('queueForEvents') ->inject('queueForStatsUsage') + ->inject('queueForRealtime') + ->inject('queueForFunctions') + ->inject('queueForWebhooks') ->callback($this->action(...)); } } diff --git a/tests/e2e/Scopes/Scope.php b/tests/e2e/Scopes/Scope.php index 2ee0d0198e..a9451cc22b 100644 --- a/tests/e2e/Scopes/Scope.php +++ b/tests/e2e/Scopes/Scope.php @@ -182,9 +182,9 @@ abstract class Scope extends TestCase /** * @return array */ - public function getUser(): array + public function getUser(bool $override = false): array { - if (isset(self::$user[$this->getProject()['$id']])) { + if (!$override && isset(self::$user[$this->getProject()['$id']])) { return self::$user[$this->getProject()['$id']]; } diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index 1844f3bce4..f234209b88 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -819,7 +819,6 @@ class RealtimeCustomClientTest extends Scope ]); $response = json_decode($client->receive(), true); - $this->assertArrayHasKey('type', $response); $this->assertArrayHasKey('data', $response); $this->assertEquals('event', $response['type']); @@ -898,9 +897,641 @@ class RealtimeCustomClientTest extends Scope $this->assertNotEmpty($response['data']['payload']); $this->assertEquals('Bradley Cooper', $response['data']['payload']['name']); + // test bulk create + $documents = $this->client->call(Client::METHOD_POST, "/databases/{$databaseId}/collections/{$actorsId}/documents", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'documents' => [ + [ + '$id' => ID::unique(), + 'name' => 'Robert Downey Jr.', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ], + [ + '$id' => ID::unique(), + 'name' => 'Scarlett Johansson', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ] + ], + ]); + + // Receive first document event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.create", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.create", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.create", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertArrayHasKey('name', $response['data']['payload']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + $this->assertIsArray($response['data']['payload']['$permissions']); + $this->assertContains(Permission::read(Role::any()), $response['data']['payload']['$permissions']); + $this->assertContains(Permission::update(Role::any()), $response['data']['payload']['$permissions']); + $this->assertContains(Permission::delete(Role::any()), $response['data']['payload']['$permissions']); + + // Receive second document event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.create", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.create", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.create", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertArrayHasKey('name', $response['data']['payload']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + $this->assertIsArray($response['data']['payload']['$permissions']); + $this->assertContains(Permission::read(Role::any()), $response['data']['payload']['$permissions']); + $this->assertContains(Permission::update(Role::any()), $response['data']['payload']['$permissions']); + $this->assertContains(Permission::delete(Role::any()), $response['data']['payload']['$permissions']); + + // test bulk update + $response = $this->client->call(Client::METHOD_PATCH, '/databases/' . $databaseId . '/collections/' . $actorsId . '/documents/', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'data' => [ + 'name' => 'Marvel Hero' + ], + ]); + $this->assertEquals(200, $response['headers']['status-code']); + + // Receive first document update event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.update", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.update", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertEquals('Marvel Hero', $response['data']['payload']['name']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + + // Receive second document update event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.update", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.update", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertEquals('Marvel Hero', $response['data']['payload']['name']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + + // Receive third document update event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.update", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.update", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertEquals('Marvel Hero', $response['data']['payload']['name']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + + // Test bulk delete + $response = $this->client->call(Client::METHOD_DELETE, "/databases/{$databaseId}/collections/{$actorsId}/documents", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $this->assertEquals(200, $response['headers']['status-code']); + + // Receive first document delete event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.delete", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertArrayHasKey('name', $response['data']['payload']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + $this->assertIsArray($response['data']['payload']['$permissions']); + + // Receive second document delete event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.delete", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertArrayHasKey('name', $response['data']['payload']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + $this->assertIsArray($response['data']['payload']['$permissions']); + + // Receive third document delete event + $response = json_decode($client->receive(), true); + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('event', $response['type']); + $this->assertNotEmpty($response['data']); + $this->assertArrayHasKey('timestamp', $response['data']); + $this->assertCount(6, $response['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response['data']['payload']['$id']}.delete", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.*.collections.*", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response['data']['events']); + $this->assertContains("databases.*", $response['data']['events']); + $this->assertNotEmpty($response['data']['payload']); + $this->assertIsArray($response['data']['payload']); + $this->assertArrayHasKey('$id', $response['data']['payload']); + $this->assertArrayHasKey('name', $response['data']['payload']); + $this->assertArrayHasKey('$permissions', $response['data']['payload']); + $this->assertIsArray($response['data']['payload']['$permissions']); + $client->close(); } + public function testChannelDatabaseBulkOperationMultipleClient() + { + // user with api key will do operations and other valid users + $user1 = $this->getUser(true); + $user1Id = $user1['$id']; + $session = $user1['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client1 = $this->getWebsocket(['documents', 'collections'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_' . $projectId . '=' . $session + ]); + + $response = json_decode($client1->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + + $user2 = $this->getUser(override:true); + $user2Id = $user2['$id']; + $session = $user2['session'] ?? ''; + $projectId = $this->getProject()['$id']; + + $client2 = $this->getWebsocket(['documents', 'collections'], [ + 'origin' => 'http://localhost', + 'cookie' => 'a_session_' . $projectId . '=' . $session + ]); + + $response = json_decode($client2->receive(), true); + + $this->assertArrayHasKey('type', $response); + $this->assertArrayHasKey('data', $response); + $this->assertEquals('connected', $response['type']); + $this->assertNotEmpty($response['data']); + + + /** + * Test Database Create + */ + $database = $this->client->call(Client::METHOD_POST, '/databases', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'databaseId' => ID::unique(), + 'name' => 'Actors DB', + ]); + + $databaseId = $database['body']['$id']; + + /** + * Test Collection Create + */ + $actors = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'collectionId' => ID::unique(), + 'name' => 'Actors', + 'permissions' => [ + Permission::create(Role::user($this->getUser()['$id'])), + ], + 'documentSecurity' => true, + ]); + + $actorsId = $actors['body']['$id']; + + $name = $this->client->call(Client::METHOD_POST, '/databases/' . $databaseId . '/collections/' . $actorsId . '/attributes/string', array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'key' => 'name', + 'size' => 256, + 'required' => true, + ]); + + $this->assertEquals(202, $name['headers']['status-code']); + $this->assertEquals('name', $name['body']['key']); + $this->assertEquals('string', $name['body']['type']); + $this->assertEquals(256, $name['body']['size']); + $this->assertTrue($name['body']['required']); + + sleep(2); + + // create + $this->client->call(Client::METHOD_POST, "/databases/{$databaseId}/collections/{$actorsId}/documents", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'documents' => [ + [ + '$id' => ID::unique(), + 'name' => 'Any', + '$permissions' => [ + Permission::read(Role::any()), + Permission::update(Role::any()), + Permission::delete(Role::any()), + ], + ], + [ + '$id' => ID::unique(), + 'name' => 'Users', + '$permissions' => [ + Permission::read(Role::users()), + Permission::update(Role::users()), + Permission::delete(Role::users()), + ], + ], + [ + '$id' => ID::unique(), + 'name' => 'User1', + '$permissions' => [ + Permission::read(Role::user($user1Id)), + ], + ], + [ + '$id' => ID::unique(), + 'name' => 'User2', + '$permissions' => [ + Permission::read(Role::user($user2Id)), + ], + ], + [ + '$id' => ID::unique(), + 'name' => 'User2-1', + '$permissions' => [ + Permission::read(Role::user($user2Id)), + ], + ] + ], + ]); + + // Receive and assert for client1 - should receive 3 individual document events + for ($i = 0; $i < 3; $i++) { + $response1 = json_decode($client1->receive(), true); + $this->assertArrayHasKey('type', $response1); + $this->assertArrayHasKey('data', $response1); + $this->assertEquals('event', $response1['type']); + $this->assertNotEmpty($response1['data']); + $this->assertArrayHasKey('timestamp', $response1['data']); + $this->assertCount(6, $response1['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response1['data']['payload']['$id']}.create", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.create", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.create", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.*.collections.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response1['data']['events']); + $this->assertContains("databases.*", $response1['data']['events']); + $this->assertNotEmpty($response1['data']['payload']); + $this->assertIsArray($response1['data']['payload']); + $this->assertArrayHasKey('$id', $response1['data']['payload']); + $this->assertArrayHasKey('name', $response1['data']['payload']); + $this->assertArrayHasKey('$permissions', $response1['data']['payload']); + $this->assertIsArray($response1['data']['payload']['$permissions']); + } + + // Receive and assert for client2 - should receive 4 individual document events + for ($i = 0; $i < 4; $i++) { + $response2 = json_decode($client2->receive(), true); + $this->assertArrayHasKey('type', $response2); + $this->assertArrayHasKey('data', $response2); + $this->assertEquals('event', $response2['type']); + $this->assertNotEmpty($response2['data']); + $this->assertArrayHasKey('timestamp', $response2['data']); + $this->assertCount(6, $response2['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response2['data']['payload']['$id']}.create", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.create", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.create", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.*.collections.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.create", $response2['data']['events']); + $this->assertContains("databases.*", $response2['data']['events']); + $this->assertNotEmpty($response2['data']['payload']); + $this->assertIsArray($response2['data']['payload']); + $this->assertArrayHasKey('$id', $response2['data']['payload']); + $this->assertArrayHasKey('name', $response2['data']['payload']); + $this->assertArrayHasKey('$permissions', $response2['data']['payload']); + $this->assertIsArray($response2['data']['payload']['$permissions']); + } + + + // Perform bulk update + $response = $this->client->call(Client::METHOD_PATCH, "/databases/{$databaseId}/collections/{$actorsId}/documents/", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'data' => [ + 'name' => 'Marvel Hero' + ], + ]); + + $this->assertEquals(200, $response['headers']['status-code']); + + // Receive and assert for client1 - should receive 3 individual document update events + for ($i = 0; $i < 3; $i++) { + $response1 = json_decode($client1->receive(), true); + $this->assertArrayHasKey('type', $response1); + $this->assertArrayHasKey('data', $response1); + $this->assertEquals('event', $response1['type']); + $this->assertNotEmpty($response1['data']); + $this->assertArrayHasKey('timestamp', $response1['data']); + $this->assertCount(6, $response1['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response1['data']['payload']['$id']}.update", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.*.collections.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.*", $response1['data']['events']); + $this->assertNotEmpty($response1['data']['payload']); + $this->assertIsArray($response1['data']['payload']); + $this->assertArrayHasKey('$id', $response1['data']['payload']); + $this->assertEquals('Marvel Hero', $response1['data']['payload']['name']); + $this->assertArrayHasKey('$permissions', $response1['data']['payload']); + } + + // Receive and assert for client2 - should receive 4 individual document update events + for ($i = 0; $i < 4; $i++) { + $response2 = json_decode($client2->receive(), true); + $this->assertArrayHasKey('type', $response2); + $this->assertArrayHasKey('data', $response2); + $this->assertEquals('event', $response2['type']); + $this->assertNotEmpty($response2['data']); + $this->assertArrayHasKey('timestamp', $response2['data']); + $this->assertCount(6, $response2['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response2['data']['payload']['$id']}.update", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.*.collections.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.*", $response2['data']['events']); + $this->assertNotEmpty($response2['data']['payload']); + $this->assertIsArray($response2['data']['payload']); + $this->assertArrayHasKey('$id', $response2['data']['payload']); + $this->assertEquals('Marvel Hero', $response2['data']['payload']['name']); + $this->assertArrayHasKey('$permissions', $response2['data']['payload']); + } + + // Perform bulk delete + $response = $this->client->call(Client::METHOD_DELETE, "/databases/{$databaseId}/collections/{$actorsId}/documents", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ])); + + $this->assertEquals(200, $response['headers']['status-code']); + + // Receive and assert for client1 - should receive 3 individual document delete events + for ($i = 0; $i < 3; $i++) { + $response1 = json_decode($client1->receive(), true); + $this->assertArrayHasKey('type', $response1); + $this->assertArrayHasKey('data', $response1); + $this->assertEquals('event', $response1['type']); + $this->assertNotEmpty($response1['data']); + $this->assertArrayHasKey('timestamp', $response1['data']); + $this->assertCount(6, $response1['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response1['data']['payload']['$id']}.delete", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.delete", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.delete", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.*.collections.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response1['data']['events']); + $this->assertContains("databases.*", $response1['data']['events']); + $this->assertNotEmpty($response1['data']['payload']); + $this->assertIsArray($response1['data']['payload']); + $this->assertArrayHasKey('$id', $response1['data']['payload']); + $this->assertArrayHasKey('name', $response1['data']['payload']); + $this->assertArrayHasKey('$permissions', $response1['data']['payload']); + $this->assertIsArray($response1['data']['payload']['$permissions']); + } + + // Receive and assert for client2 - should receive 4 individual document delete events + for ($i = 0; $i < 4; $i++) { + $response2 = json_decode($client2->receive(), true); + $this->assertArrayHasKey('type', $response2); + $this->assertArrayHasKey('data', $response2); + $this->assertEquals('event', $response2['type']); + $this->assertNotEmpty($response2['data']); + $this->assertArrayHasKey('timestamp', $response2['data']); + $this->assertCount(6, $response2['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response2['data']['payload']['$id']}.delete", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.delete", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.delete", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.*.collections.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.delete", $response2['data']['events']); + $this->assertContains("databases.*", $response2['data']['events']); + $this->assertNotEmpty($response2['data']['payload']); + $this->assertIsArray($response2['data']['payload']); + $this->assertArrayHasKey('$id', $response2['data']['payload']); + $this->assertArrayHasKey('name', $response2['data']['payload']); + $this->assertArrayHasKey('$permissions', $response2['data']['payload']); + $this->assertIsArray($response2['data']['payload']['$permissions']); + } + + $client1->close(); + $client2->close(); + } + public function testChannelDatabaseCollectionPermissions() { $user = $this->getUser(); From 7334efbf3a769ebe2e753e17339845bfb1cfa925 Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 5 Aug 2025 11:52:58 +0530 Subject: [PATCH 2/5] feat: Initialize event and payload properties in Event class and optimize bulk document queue triggering --- src/Appwrite/Event/Event.php | 2 ++ .../Databases/Collections/Documents/Action.php | 16 +++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/Appwrite/Event/Event.php b/src/Appwrite/Event/Event.php index 2c38811022..2557bf3f26 100644 --- a/src/Appwrite/Event/Event.php +++ b/src/Appwrite/Event/Event.php @@ -383,6 +383,8 @@ class Event { $this->params = []; $this->sensitive = []; + $this->event = ''; + $this->payload = []; return $this; } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 6766097f81..8b33e64ba8 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -295,17 +295,19 @@ abstract class Action extends UtopiaAction */ final protected function triggerQueuesForBulkDocuments(string $event, Document $database, Document $collection, array $documents, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks) { + $queueForEvents + ->setEvent($event) + ->setParam('databaseId', $database->getId()) + ->setContext('database', $database) + ->setParam('collectionId', $collection->getId()) + ->setParam('tableId', $collection->getId()) + ->setContext($this->getCollectionsEventsContext(), $collection); + foreach ($documents as $document) { $queueForEvents - ->setEvent($event) - ->setParam('databaseId', $database->getId()) - ->setContext('database', $database) - ->setParam('collectionId', $collection->getId()) - ->setParam('tableId', $collection->getId()) ->setParam('documentId', $document->getId()) ->setParam('rowId', $document->getId()) - ->setPayload($document->getArrayCopy()) - ->setContext($this->getCollectionsEventsContext(), $collection); + ->setPayload($document->getArrayCopy()); $queueForRealtime ->from($queueForEvents) From a144d41020f3584aa237fd282cb5f4b9383f975a Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 5 Aug 2025 11:53:17 +0530 Subject: [PATCH 3/5] feat: Enhance RealtimeCustomClientTest with permission updates and timeout assertions for clients --- .../Realtime/RealtimeCustomClientTest.php | 140 ++++++++++++++++-- 1 file changed, 128 insertions(+), 12 deletions(-) diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index f234209b88..bc842be74e 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -3,6 +3,7 @@ namespace Tests\E2E\Services\Realtime; use CURLFile; +use Exception; use Tests\E2E\Client; use Tests\E2E\Scopes\ProjectCustom; use Tests\E2E\Scopes\Scope; @@ -12,6 +13,7 @@ use Utopia\Database\Helpers\ID; use Utopia\Database\Helpers\Permission; use Utopia\Database\Helpers\Role; use WebSocket\ConnectionException; +use WebSocket\TimeoutException; class RealtimeCustomClientTest extends Scope { @@ -996,7 +998,12 @@ class RealtimeCustomClientTest extends Scope 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'data' => [ - 'name' => 'Marvel Hero' + 'name' => 'Marvel Hero', + '$permissions' => [ + Permission::read(Role::user($this->getUser()['$id'])), + Permission::update(Role::user($this->getUser()['$id'])), + Permission::delete(Role::user($this->getUser()['$id'])), + ] ], ]); $this->assertEquals(200, $response['headers']['status-code']); @@ -1313,7 +1320,7 @@ class RealtimeCustomClientTest extends Scope ], [ '$id' => ID::unique(), - 'name' => 'User2-1', + 'name' => 'User2', '$permissions' => [ Permission::read(Role::user($user2Id)), ], @@ -1384,21 +1391,26 @@ class RealtimeCustomClientTest extends Scope } - // Perform bulk update + // Perform bulk update(making it only accessible by user1) $response = $this->client->call(Client::METHOD_PATCH, "/databases/{$databaseId}/collections/{$actorsId}/documents/", array_merge([ 'content-type' => 'application/json', 'x-appwrite-project' => $this->getProject()['$id'], 'x-appwrite-key' => $this->getProject()['apiKey'] ]), [ 'data' => [ - 'name' => 'Marvel Hero' + 'name' => 'Marvel Hero', + '$permissions' => [ + Permission::read(Role::user($user1Id)), + Permission::update(Role::user($user1Id)), + Permission::delete(Role::user($user1Id)), + ] ], ]); $this->assertEquals(200, $response['headers']['status-code']); - // Receive and assert for client1 - should receive 3 individual document update events - for ($i = 0; $i < 3; $i++) { + // Receive and assert for client1 + for ($i = 0; $i < 5; $i++) { $response1 = json_decode($client1->receive(), true); $this->assertArrayHasKey('type', $response1); $this->assertArrayHasKey('data', $response1); @@ -1427,8 +1439,112 @@ class RealtimeCustomClientTest extends Scope $this->assertArrayHasKey('$permissions', $response1['data']['payload']); } - // Receive and assert for client2 - should receive 4 individual document update events - for ($i = 0; $i < 4; $i++) { + // client2 shouldn't receive any event and lead to timeout + try { + json_decode($client2->receive(), true); + $this->fail('Expected TimeoutException was not thrown.'); + } catch (Exception $e) { + $this->assertInstanceOf(TimeoutException::class, $e); + } + + // Perform bulk update(making it only accessible by user2) + $response = $this->client->call(Client::METHOD_PATCH, "/databases/{$databaseId}/collections/{$actorsId}/documents/", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'data' => [ + 'name' => 'Marvel Hero', + '$permissions' => [ + Permission::read(Role::user($user2Id)), + Permission::update(Role::user($user2Id)), + Permission::delete(Role::user($user2Id)), + ] + ], + ]); + + // Receive and assert for client2 + for ($i = 0; $i < 5; $i++) { + $response2 = json_decode($client2->receive(), true); + $this->assertArrayHasKey('type', $response2); + $this->assertArrayHasKey('data', $response2); + $this->assertEquals('event', $response2['type']); + $this->assertNotEmpty($response2['data']); + $this->assertArrayHasKey('timestamp', $response2['data']); + $this->assertCount(6, $response2['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response2['data']['payload']['$id']}.update", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.*.collections.*", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response2['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response2['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response2['data']['events']); + $this->assertContains("databases.*", $response2['data']['events']); + $this->assertNotEmpty($response2['data']['payload']); + $this->assertIsArray($response2['data']['payload']); + $this->assertArrayHasKey('$id', $response2['data']['payload']); + $this->assertEquals('Marvel Hero', $response2['data']['payload']['name']); + $this->assertArrayHasKey('$permissions', $response2['data']['payload']); + } + + // client1 shouldn't receive any event and lead to timeout + try { + json_decode($client1->receive(), true); + $this->fail('Expected TimeoutException was not thrown.'); + } catch (Exception $e) { + $this->assertInstanceOf(TimeoutException::class, $e); + } + + // Updating the permission for both the users + $response = $this->client->call(Client::METHOD_PATCH, "/databases/{$databaseId}/collections/{$actorsId}/documents/", array_merge([ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'] + ]), [ + 'data' => [ + 'name' => 'Marvel Hero', + '$permissions' => [ + Permission::read(Role::users()), + Permission::update(Role::users()), + Permission::delete(Role::users()), + ] + ], + ]); + // both user1 and user2 should receive the event + for ($i = 0; $i < 5; $i++) { + $response1 = json_decode($client1->receive(), true); + $this->assertArrayHasKey('type', $response1); + $this->assertArrayHasKey('data', $response1); + $this->assertEquals('event', $response1['type']); + $this->assertNotEmpty($response1['data']); + $this->assertArrayHasKey('timestamp', $response1['data']); + $this->assertCount(6, $response1['data']['channels']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.{$response1['data']['payload']['$id']}.update", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}.documents.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.*.collections.*", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*", $response1['data']['events']); + $this->assertContains("databases.*.collections.{$actorsId}", $response1['data']['events']); + $this->assertContains("databases.{$databaseId}.collections.*.documents.*.update", $response1['data']['events']); + $this->assertContains("databases.*", $response1['data']['events']); + $this->assertNotEmpty($response1['data']['payload']); + $this->assertIsArray($response1['data']['payload']); + $this->assertArrayHasKey('$id', $response1['data']['payload']); + $this->assertEquals('Marvel Hero', $response1['data']['payload']['name']); + $this->assertArrayHasKey('$permissions', $response1['data']['payload']); + $response2 = json_decode($client2->receive(), true); $this->assertArrayHasKey('type', $response2); $this->assertArrayHasKey('data', $response2); @@ -1466,8 +1582,8 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals(200, $response['headers']['status-code']); - // Receive and assert for client1 - should receive 3 individual document delete events - for ($i = 0; $i < 3; $i++) { + // Receive and assert for client1 + for ($i = 0; $i < 5; $i++) { $response1 = json_decode($client1->receive(), true); $this->assertArrayHasKey('type', $response1); $this->assertArrayHasKey('data', $response1); @@ -1497,8 +1613,8 @@ class RealtimeCustomClientTest extends Scope $this->assertIsArray($response1['data']['payload']['$permissions']); } - // Receive and assert for client2 - should receive 4 individual document delete events - for ($i = 0; $i < 4; $i++) { + // Receive and assert for client2 + for ($i = 0; $i < 5; $i++) { $response2 = json_decode($client2->receive(), true); $this->assertArrayHasKey('type', $response2); $this->assertArrayHasKey('data', $response2); From 5baa2ad113a5ef14f5cdee4b429de6cab72ec42f Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 5 Aug 2025 13:29:38 +0530 Subject: [PATCH 4/5] refactor: Rename triggerQueuesForBulkDocuments to triggerBulk for consistency across document actions --- .../Http/Databases/Collections/Documents/Action.php | 12 ++++++++++-- .../Databases/Collections/Documents/Bulk/Delete.php | 6 +++--- .../Databases/Collections/Documents/Bulk/Update.php | 6 +++--- .../Http/Databases/Collections/Documents/Create.php | 2 +- .../Databases/Http/Grids/Tables/Rows/Bulk/Delete.php | 2 +- .../Databases/Http/Grids/Tables/Rows/Bulk/Update.php | 2 +- tests/e2e/Scopes/Scope.php | 4 ++-- 7 files changed, 21 insertions(+), 13 deletions(-) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php index 8b33e64ba8..7e9aafad75 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Action.php @@ -293,8 +293,16 @@ abstract class Action extends UtopiaAction * @param Event $queueForWebhooks * @return void */ - final protected function triggerQueuesForBulkDocuments(string $event, Document $database, Document $collection, array $documents, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks) - { + protected function triggerBulk( + string $event, + Document $database, + Document $collection, + array $documents, + Event $queueForEvents, + Event $queueForRealtime, + Event $queueForFunctions, + Event $queueForWebhooks + ): void { $queueForEvents ->setEvent($event) ->setParam('databaseId', $database->getId()) diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php index 17c524b23c..f44e54f2b4 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Delete.php @@ -73,15 +73,15 @@ class Delete extends Action ->inject('response') ->inject('dbForProject') ->inject('queueForStatsUsage') - ->inject('plan') ->inject('queueForEvents') ->inject('queueForRealtime') ->inject('queueForFunctions') ->inject('queueForWebhooks') + ->inject('plan') ->callback($this->action(...)); } - public function action(string $databaseId, string $collectionId, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, array $plan, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks): void + public function action(string $databaseId, string $collectionId, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks, array $plan): void { $database = $dbForProject->getDocument('databases', $databaseId); if ($database->isEmpty()) { @@ -140,7 +140,7 @@ class Delete extends Action $this->getSdkGroup() => $documents, ]), $this->getResponseModel()); - $this->triggerQueuesForBulkDocuments( + $this->triggerBulk( 'databases.[databaseId].collections.[collectionId].documents.[documentId].delete', $database, $collection, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php index 908817f31d..82b39ef178 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Bulk/Update.php @@ -77,15 +77,15 @@ class Update extends Action ->inject('response') ->inject('dbForProject') ->inject('queueForStatsUsage') - ->inject('plan') ->inject('queueForEvents') ->inject('queueForRealtime') ->inject('queueForFunctions') ->inject('queueForWebhooks') + ->inject('plan') ->callback($this->action(...)); } - public function action(string $databaseId, string $collectionId, string|array $data, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, array $plan, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks): void + public function action(string $databaseId, string $collectionId, string|array $data, array $queries, UtopiaResponse $response, Database $dbForProject, StatsUsage $queueForStatsUsage, Event $queueForEvents, Event $queueForRealtime, Event $queueForFunctions, Event $queueForWebhooks, array $plan): void { $data = \is_string($data) ? \json_decode($data, true) @@ -164,7 +164,7 @@ class Update extends Action $this->getSdkGroup() => $documents ]), $this->getResponseModel()); - $this->triggerQueuesForBulkDocuments( + $this->triggerBulk( 'databases.[databaseId].collections.[collectionId].documents.[documentId].update', $database, $collection, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php index d961f400eb..0691249943 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Databases/Collections/Documents/Create.php @@ -419,7 +419,7 @@ class Create extends Action $this->getSdkGroup() => $documents ]), $this->getBulkResponseModel()); - $this->triggerQueuesForBulkDocuments( + $this->triggerBulk( 'databases.[databaseId].collections.[collectionId].documents.[documentId].create', $database, $collection, diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php index aab4906d02..9a594245b3 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Delete.php @@ -59,11 +59,11 @@ class Delete extends DocumentsDelete ->inject('response') ->inject('dbForProject') ->inject('queueForStatsUsage') - ->inject('plan') ->inject('queueForEvents') ->inject('queueForRealtime') ->inject('queueForFunctions') ->inject('queueForWebhooks') + ->inject('plan') ->callback($this->action(...)); } } diff --git a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php index 2c373ffbc6..d33e8b0fb6 100644 --- a/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php +++ b/src/Appwrite/Platform/Modules/Databases/Http/Grids/Tables/Rows/Bulk/Update.php @@ -61,11 +61,11 @@ class Update extends DocumentsUpdate ->inject('response') ->inject('dbForProject') ->inject('queueForStatsUsage') - ->inject('plan') ->inject('queueForEvents') ->inject('queueForRealtime') ->inject('queueForFunctions') ->inject('queueForWebhooks') + ->inject('plan') ->callback($this->action(...)); } } diff --git a/tests/e2e/Scopes/Scope.php b/tests/e2e/Scopes/Scope.php index a9451cc22b..c072fdca35 100644 --- a/tests/e2e/Scopes/Scope.php +++ b/tests/e2e/Scopes/Scope.php @@ -182,9 +182,9 @@ abstract class Scope extends TestCase /** * @return array */ - public function getUser(bool $override = false): array + public function getUser(bool $fresh = false): array { - if (!$override && isset(self::$user[$this->getProject()['$id']])) { + if (!$fresh && isset(self::$user[$this->getProject()['$id']])) { return self::$user[$this->getProject()['$id']]; } From 697ac3447bf9a7b5ca84bf65d996d6fac791705c Mon Sep 17 00:00:00 2001 From: ArnabChatterjee20k Date: Tue, 5 Aug 2025 13:37:24 +0530 Subject: [PATCH 5/5] fix: Update getUser method call in RealtimeCustomClientTest for clarity --- tests/e2e/Services/Realtime/RealtimeCustomClientTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php index bc842be74e..60f58bd8ee 100644 --- a/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php +++ b/tests/e2e/Services/Realtime/RealtimeCustomClientTest.php @@ -1211,7 +1211,7 @@ class RealtimeCustomClientTest extends Scope $this->assertEquals('connected', $response['type']); $this->assertNotEmpty($response['data']); - $user2 = $this->getUser(override:true); + $user2 = $this->getUser(true); $user2Id = $user2['$id']; $session = $user2['session'] ?? ''; $projectId = $this->getProject()['$id'];