From 8f9643b598a193d6d3979db95d5decd59736e28e Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 21 May 2026 16:58:05 +0100 Subject: [PATCH 1/4] Add Email Templates migration --- composer.json | 2 +- composer.lock | 16 +-- .../Utopia/Response/Model/MigrationReport.php | 6 + .../Services/Migrations/MigrationsBase.php | 104 ++++++++++++++++++ 4 files changed, 119 insertions(+), 9 deletions(-) diff --git a/composer.json b/composer.json index e16437bc99..30d8eedc73 100644 --- a/composer.json +++ b/composer.json @@ -75,7 +75,7 @@ "utopia-php/lock": "0.2.*", "utopia-php/logger": "0.8.*", "utopia-php/messaging": "0.22.*", - "utopia-php/migration": "dev-add-custom-domains-migration as 1.12.0", + "utopia-php/migration": "dev-add-email-templates-migration as 1.12.0", "utopia-php/platform": "1.0.0-rc2", "utopia-php/pools": "1.*", "utopia-php/span": "1.1.*", diff --git a/composer.lock b/composer.lock index f089b0af28..0d112275d8 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "4d93c3da3ef7aa9fe898f609a6524431", + "content-hash": "575aa56a776213cb0c24063ec029441b", "packages": [ { "name": "adhocore/jwt", @@ -4675,16 +4675,16 @@ }, { "name": "utopia-php/migration", - "version": "dev-add-custom-domains-migration", + "version": "dev-add-email-templates-migration", "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "cc7d68aaadf2b37adcce7735b9e2f068cc78caf9" + "reference": "9813afb2d7e0ddf8f00002fa30c0318d697e8d97" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/cc7d68aaadf2b37adcce7735b9e2f068cc78caf9", - "reference": "cc7d68aaadf2b37adcce7735b9e2f068cc78caf9", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/9813afb2d7e0ddf8f00002fa30c0318d697e8d97", + "reference": "9813afb2d7e0ddf8f00002fa30c0318d697e8d97", "shasum": "" }, "require": { @@ -4741,10 +4741,10 @@ "utopia" ], "support": { - "source": "https://github.com/utopia-php/migration/tree/add-custom-domains-migration", + "source": "https://github.com/utopia-php/migration/tree/add-email-templates-migration", "issues": "https://github.com/utopia-php/migration/issues" }, - "time": "2026-05-21T13:40:46+00:00" + "time": "2026-05-21T15:55:26+00:00" }, { "name": "utopia-php/mongo", @@ -8593,7 +8593,7 @@ }, { "package": "utopia-php/migration", - "version": "dev-add-custom-domains-migration", + "version": "dev-add-email-templates-migration", "alias": "1.12.0", "alias_normalized": "1.12.0.0" } diff --git a/src/Appwrite/Utopia/Response/Model/MigrationReport.php b/src/Appwrite/Utopia/Response/Model/MigrationReport.php index a0f83f2b9f..1b475595d1 100644 --- a/src/Appwrite/Utopia/Response/Model/MigrationReport.php +++ b/src/Appwrite/Utopia/Response/Model/MigrationReport.php @@ -119,6 +119,12 @@ class MigrationReport extends Model 'default' => 0, 'example' => 5, ]) + ->addRule(Resource::TYPE_EMAIL_TEMPLATE, [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Number of custom email templates to be migrated (one per templateId × locale pair).', + 'default' => 0, + 'example' => 7, + ]) ->addRule(Resource::TYPE_SITE, [ 'type' => self::TYPE_INTEGER, 'description' => 'Number of sites to be migrated.', diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 548c532884..95967587ac 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -3114,6 +3114,110 @@ trait MigrationsBase $this->client->call(Client::METHOD_DELETE, '/proxy/rules/' . $sourceRule['$id'], $sourceHeaders); } + public function testAppwriteMigrationEmailTemplate(): void + { + $consoleHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => 'console', + 'origin' => 'http://localhost', + 'cookie' => 'a_session_console=' . $this->getRoot()['session'], + ]; + + $sourceProjectId = $this->getProject()['$id']; + $destinationProjectId = $this->getDestinationProject()['$id']; + + // The source SDK path requires custom SMTP enabled before a template can be saved. + $this->client->call(Client::METHOD_PATCH, '/projects/' . $sourceProjectId . '/smtp', $consoleHeaders, [ + 'enabled' => true, + 'senderName' => 'Test Sender', + 'senderEmail' => 'sender@example.com', + 'host' => 'smtp.example.com', + 'port' => 587, + 'username' => 'test', + 'password' => 'test', + 'secure' => 'tls', + ]); + + $templateId = 'verification'; + $locale = 'en'; + $subject = 'Verify your account ' . ID::unique(); + $message = '

Hello {{user}}, verify your account at {{redirect}}

'; + + $update = $this->client->call( + Client::METHOD_PATCH, + '/projects/' . $sourceProjectId . '/templates/email/' . $templateId . '/' . $locale, + $consoleHeaders, + [ + 'subject' => $subject, + 'message' => $message, + 'senderName' => 'Template Sender', + 'senderEmail' => 'tpl-sender@example.com', + 'replyToEmail' => 'reply@example.com', + 'replyToName' => 'Reply Team', + ] + ); + $this->assertEquals(200, $update['headers']['status-code']); + + $result = $this->performMigrationSync([ + 'resources' => [ + Resource::TYPE_EMAIL_TEMPLATE, + ], + 'endpoint' => $this->webEndpoint, + 'projectId' => $sourceProjectId, + 'apiKey' => $this->getProject()['apiKey'], + ]); + + $this->assertEquals('completed', $result['status']); + $this->assertEquals([Resource::TYPE_EMAIL_TEMPLATE], $result['resources']); + $this->assertArrayHasKey(Resource::TYPE_EMAIL_TEMPLATE, $result['statusCounters']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_EMAIL_TEMPLATE]['error']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_EMAIL_TEMPLATE]['pending']); + $this->assertGreaterThanOrEqual(1, $result['statusCounters'][Resource::TYPE_EMAIL_TEMPLATE]['success']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_EMAIL_TEMPLATE]['processing']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_EMAIL_TEMPLATE]['warning']); + + // Read-back via the SDK requires the destination to have SMTP enabled too. + $this->client->call(Client::METHOD_PATCH, '/projects/' . $destinationProjectId . '/smtp', $consoleHeaders, [ + 'enabled' => true, + 'senderName' => 'Dest Sender', + 'senderEmail' => 'dest@example.com', + 'host' => 'smtp.example.com', + 'port' => 587, + 'username' => 'test', + 'password' => 'test', + 'secure' => 'tls', + ]); + + $fetched = $this->client->call( + Client::METHOD_GET, + '/projects/' . $destinationProjectId . '/templates/email/' . $templateId . '/' . $locale, + $consoleHeaders + ); + $this->assertEquals(200, $fetched['headers']['status-code']); + $this->assertSame($subject, $fetched['body']['subject']); + $this->assertSame($message, $fetched['body']['message']); + $this->assertSame('Template Sender', $fetched['body']['senderName']); + $this->assertSame('tpl-sender@example.com', $fetched['body']['senderEmail']); + $this->assertSame('reply@example.com', $fetched['body']['replyToEmail']); + $this->assertSame('Reply Team', $fetched['body']['replyToName']); + + // Reset both projects so the test is idempotent. + $smtpReset = [ + 'enabled' => false, + 'senderName' => '', + 'senderEmail' => '', + 'replyToName' => '', + 'replyToEmail' => '', + 'host' => '', + 'port' => 0, + 'username' => '', + 'password' => '', + 'secure' => '', + ]; + $this->client->call(Client::METHOD_PATCH, '/projects/' . $sourceProjectId . '/smtp', $consoleHeaders, $smtpReset); + $this->client->call(Client::METHOD_PATCH, '/projects/' . $destinationProjectId . '/smtp', $consoleHeaders, $smtpReset); + } + /** * Import documents from a CSV file. */ From 03c7c24422e951475f40f72e072b94e2208f244b Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 21 May 2026 17:18:03 +0100 Subject: [PATCH 2/4] Bump migration pin + use maildev for E2E SMTP enable --- composer.lock | 8 +++---- .../Services/Migrations/MigrationsBase.php | 24 +++++++++---------- 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/composer.lock b/composer.lock index 0d112275d8..bbbf124068 100644 --- a/composer.lock +++ b/composer.lock @@ -4679,12 +4679,12 @@ "source": { "type": "git", "url": "https://github.com/utopia-php/migration.git", - "reference": "9813afb2d7e0ddf8f00002fa30c0318d697e8d97" + "reference": "edcb0aa3285b44616e570e5fe092aaac97deb686" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/utopia-php/migration/zipball/9813afb2d7e0ddf8f00002fa30c0318d697e8d97", - "reference": "9813afb2d7e0ddf8f00002fa30c0318d697e8d97", + "url": "https://api.github.com/repos/utopia-php/migration/zipball/edcb0aa3285b44616e570e5fe092aaac97deb686", + "reference": "edcb0aa3285b44616e570e5fe092aaac97deb686", "shasum": "" }, "require": { @@ -4744,7 +4744,7 @@ "source": "https://github.com/utopia-php/migration/tree/add-email-templates-migration", "issues": "https://github.com/utopia-php/migration/issues" }, - "time": "2026-05-21T15:55:26+00:00" + "time": "2026-05-21T16:14:58+00:00" }, { "name": "utopia-php/mongo", diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 95967587ac..bc30a21490 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -3126,17 +3126,20 @@ trait MigrationsBase $sourceProjectId = $this->getProject()['$id']; $destinationProjectId = $this->getDestinationProject()['$id']; - // The source SDK path requires custom SMTP enabled before a template can be saved. - $this->client->call(Client::METHOD_PATCH, '/projects/' . $sourceProjectId . '/smtp', $consoleHeaders, [ + // The source SDK path requires custom SMTP enabled before a template can be + // saved — and the enable call validates the SMTP connection. `maildev` is the + // dev mailcatcher in the test cluster's docker-compose; it accepts unauthenticated + // connections on port 1025, so it's the only host that lets us pass validation. + $smtpHost = 'maildev'; + $smtpPort = 1025; + $smtpUpdate = $this->client->call(Client::METHOD_PATCH, '/projects/' . $sourceProjectId . '/smtp', $consoleHeaders, [ 'enabled' => true, 'senderName' => 'Test Sender', 'senderEmail' => 'sender@example.com', - 'host' => 'smtp.example.com', - 'port' => 587, - 'username' => 'test', - 'password' => 'test', - 'secure' => 'tls', + 'host' => $smtpHost, + 'port' => $smtpPort, ]); + $this->assertEquals(200, $smtpUpdate['headers']['status-code'], 'SMTP enable on source failed: ' . \json_encode($smtpUpdate['body'])); $templateId = 'verification'; $locale = 'en'; @@ -3181,11 +3184,8 @@ trait MigrationsBase 'enabled' => true, 'senderName' => 'Dest Sender', 'senderEmail' => 'dest@example.com', - 'host' => 'smtp.example.com', - 'port' => 587, - 'username' => 'test', - 'password' => 'test', - 'secure' => 'tls', + 'host' => $smtpHost, + 'port' => $smtpPort, ]); $fetched = $this->client->call( From 3b30bb6894bd66a33a17c2ebbb2ef72738294ed1 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 28 May 2026 16:51:27 +0100 Subject: [PATCH 3/4] Migrate email-templates E2E to project-scoped endpoints (matches SMTP test pattern) --- .../Services/Migrations/MigrationsBase.php | 50 +++++++++---------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index ca03716551..328a3acc95 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -3268,18 +3268,25 @@ trait MigrationsBase $sourceProjectId = $this->getProject()['$id']; $destinationProjectId = $this->getDestinationProject()['$id']; + $sourceAdminHeaders = \array_merge($consoleHeaders, [ + 'x-appwrite-project' => $sourceProjectId, + 'x-appwrite-mode' => 'admin', + ]); + $destinationAdminHeaders = \array_merge($consoleHeaders, [ + 'x-appwrite-project' => $destinationProjectId, + 'x-appwrite-mode' => 'admin', + ]); + // The source SDK path requires custom SMTP enabled before a template can be // saved — and the enable call validates the SMTP connection. `maildev` is the // dev mailcatcher in the test cluster's docker-compose; it accepts unauthenticated // connections on port 1025, so it's the only host that lets us pass validation. - $smtpHost = 'maildev'; - $smtpPort = 1025; - $smtpUpdate = $this->client->call(Client::METHOD_PATCH, '/projects/' . $sourceProjectId . '/smtp', $consoleHeaders, [ + $smtpUpdate = $this->client->call(Client::METHOD_PATCH, '/project/smtp', $sourceAdminHeaders, [ 'enabled' => true, 'senderName' => 'Test Sender', 'senderEmail' => 'sender@example.com', - 'host' => $smtpHost, - 'port' => $smtpPort, + 'host' => 'maildev', + 'port' => 1025, ]); $this->assertEquals(200, $smtpUpdate['headers']['status-code'], 'SMTP enable on source failed: ' . \json_encode($smtpUpdate['body'])); @@ -3290,9 +3297,11 @@ trait MigrationsBase $update = $this->client->call( Client::METHOD_PATCH, - '/projects/' . $sourceProjectId . '/templates/email/' . $templateId . '/' . $locale, - $consoleHeaders, + '/project/templates/email', + $sourceAdminHeaders, [ + 'templateId' => $templateId, + 'locale' => $locale, 'subject' => $subject, 'message' => $message, 'senderName' => 'Template Sender', @@ -3322,18 +3331,19 @@ trait MigrationsBase $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_PROJECT_EMAIL_TEMPLATE]['warning']); // Read-back via the SDK requires the destination to have SMTP enabled too. - $this->client->call(Client::METHOD_PATCH, '/projects/' . $destinationProjectId . '/smtp', $consoleHeaders, [ + $this->client->call(Client::METHOD_PATCH, '/project/smtp', $destinationAdminHeaders, [ 'enabled' => true, 'senderName' => 'Dest Sender', 'senderEmail' => 'dest@example.com', - 'host' => $smtpHost, - 'port' => $smtpPort, + 'host' => 'maildev', + 'port' => 1025, ]); $fetched = $this->client->call( Client::METHOD_GET, - '/projects/' . $destinationProjectId . '/templates/email/' . $templateId . '/' . $locale, - $consoleHeaders + '/project/templates/email/' . $templateId, + $destinationAdminHeaders, + ['locale' => $locale] ); $this->assertEquals(200, $fetched['headers']['status-code']); $this->assertSame($subject, $fetched['body']['subject']); @@ -3344,20 +3354,8 @@ trait MigrationsBase $this->assertSame('Reply Team', $fetched['body']['replyToName']); // Reset both projects so the test is idempotent. - $smtpReset = [ - 'enabled' => false, - 'senderName' => '', - 'senderEmail' => '', - 'replyToName' => '', - 'replyToEmail' => '', - 'host' => '', - 'port' => 0, - 'username' => '', - 'password' => '', - 'secure' => '', - ]; - $this->client->call(Client::METHOD_PATCH, '/projects/' . $sourceProjectId . '/smtp', $consoleHeaders, $smtpReset); - $this->client->call(Client::METHOD_PATCH, '/projects/' . $destinationProjectId . '/smtp', $consoleHeaders, $smtpReset); + $this->client->call(Client::METHOD_PATCH, '/project/smtp', $sourceAdminHeaders, ['enabled' => false]); + $this->client->call(Client::METHOD_PATCH, '/project/smtp', $destinationAdminHeaders, ['enabled' => false]); } /** From a0eb40bb6b53fc9322d63e7922532cfe89b9a7c6 Mon Sep 17 00:00:00 2001 From: Prem Palanisamy Date: Thu, 28 May 2026 16:57:43 +0100 Subject: [PATCH 4/4] Use API key headers in email-templates E2E (drop admin/console-session pattern) --- .../Services/Migrations/MigrationsBase.php | 33 ++++++++----------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 328a3acc95..1668100fb8 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -3258,30 +3258,25 @@ trait MigrationsBase public function testAppwriteMigrationEmailTemplate(): void { - $consoleHeaders = [ - 'content-type' => 'application/json', - 'x-appwrite-project' => 'console', - 'origin' => 'http://localhost', - 'cookie' => 'a_session_console=' . $this->getRoot()['session'], - ]; - $sourceProjectId = $this->getProject()['$id']; $destinationProjectId = $this->getDestinationProject()['$id']; - $sourceAdminHeaders = \array_merge($consoleHeaders, [ + $sourceKeyHeaders = [ + 'content-type' => 'application/json', 'x-appwrite-project' => $sourceProjectId, - 'x-appwrite-mode' => 'admin', - ]); - $destinationAdminHeaders = \array_merge($consoleHeaders, [ + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + $destinationKeyHeaders = [ + 'content-type' => 'application/json', 'x-appwrite-project' => $destinationProjectId, - 'x-appwrite-mode' => 'admin', - ]); + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; // The source SDK path requires custom SMTP enabled before a template can be // saved — and the enable call validates the SMTP connection. `maildev` is the // dev mailcatcher in the test cluster's docker-compose; it accepts unauthenticated // connections on port 1025, so it's the only host that lets us pass validation. - $smtpUpdate = $this->client->call(Client::METHOD_PATCH, '/project/smtp', $sourceAdminHeaders, [ + $smtpUpdate = $this->client->call(Client::METHOD_PATCH, '/project/smtp', $sourceKeyHeaders, [ 'enabled' => true, 'senderName' => 'Test Sender', 'senderEmail' => 'sender@example.com', @@ -3298,7 +3293,7 @@ trait MigrationsBase $update = $this->client->call( Client::METHOD_PATCH, '/project/templates/email', - $sourceAdminHeaders, + $sourceKeyHeaders, [ 'templateId' => $templateId, 'locale' => $locale, @@ -3331,7 +3326,7 @@ trait MigrationsBase $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_PROJECT_EMAIL_TEMPLATE]['warning']); // Read-back via the SDK requires the destination to have SMTP enabled too. - $this->client->call(Client::METHOD_PATCH, '/project/smtp', $destinationAdminHeaders, [ + $this->client->call(Client::METHOD_PATCH, '/project/smtp', $destinationKeyHeaders, [ 'enabled' => true, 'senderName' => 'Dest Sender', 'senderEmail' => 'dest@example.com', @@ -3342,7 +3337,7 @@ trait MigrationsBase $fetched = $this->client->call( Client::METHOD_GET, '/project/templates/email/' . $templateId, - $destinationAdminHeaders, + $destinationKeyHeaders, ['locale' => $locale] ); $this->assertEquals(200, $fetched['headers']['status-code']); @@ -3354,8 +3349,8 @@ trait MigrationsBase $this->assertSame('Reply Team', $fetched['body']['replyToName']); // Reset both projects so the test is idempotent. - $this->client->call(Client::METHOD_PATCH, '/project/smtp', $sourceAdminHeaders, ['enabled' => false]); - $this->client->call(Client::METHOD_PATCH, '/project/smtp', $destinationAdminHeaders, ['enabled' => false]); + $this->client->call(Client::METHOD_PATCH, '/project/smtp', $sourceKeyHeaders, ['enabled' => false]); + $this->client->call(Client::METHOD_PATCH, '/project/smtp', $destinationKeyHeaders, ['enabled' => false]); } /**