appwrite/tests/e2e/Services/GraphQL/MessagingTest.php

1330 lines
52 KiB
PHP
Raw Normal View History

<?php
namespace Tests\E2E\Services\GraphQL;
use Tests\E2E\Client;
use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Scopes\Scope;
use Tests\E2E\Scopes\SideServer;
use Utopia\Database\Helpers\ID;
2023-10-30 20:35:50 +00:00
use Utopia\DSN\DSN;
2024-04-01 11:02:47 +00:00
use Utopia\System\System;
class MessagingTest extends Scope
{
use ProjectCustom;
use SideServer;
use Base;
private static array $cachedProviders = [];
private static array $cachedTopic = [];
private static array $cachedSubscriber = [];
private static array $cachedEmail = [];
private static array $cachedSms = [];
private static array $cachedPush = [];
protected function setupProviders(): array
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedProviders[$key])) {
return self::$cachedProviders[$key];
}
$providersParams = [
'Sendgrid' => [
'providerId' => ID::unique(),
'name' => 'Sengrid1',
'apiKey' => 'my-apikey',
2023-12-19 18:15:20 +00:00
'fromName' => 'Sender Name',
'fromEmail' => 'sender-email@my-domain.com',
],
2025-10-24 08:49:59 +00:00
'Resend' => [
'providerId' => ID::unique(),
'name' => 'Resend1',
'apiKey' => 'my-apikey',
'fromName' => 'Sender Name',
'fromEmail' => 'sender-email@my-domain.com',
],
'Mailgun' => [
'providerId' => ID::unique(),
'name' => 'Mailgun1',
'apiKey' => 'my-apikey',
'domain' => 'my-domain',
2023-12-19 18:15:20 +00:00
'fromName' => 'Sender Name',
'fromEmail' => 'sender-email@my-domain.com',
'isEuRegion' => false,
],
'Twilio' => [
'providerId' => ID::unique(),
'name' => 'Twilio1',
'accountSid' => 'my-accountSid',
'authToken' => 'my-authToken',
2023-10-23 19:35:46 +00:00
'from' => '+123456789',
],
'Telesign' => [
'providerId' => ID::unique(),
'name' => 'Telesign1',
2024-02-13 02:05:15 +00:00
'customerId' => 'my-username',
'apiKey' => 'my-password',
2023-10-23 19:35:46 +00:00
'from' => '+123456789',
],
'Textmagic' => [
'providerId' => ID::unique(),
'name' => 'Textmagic1',
'username' => 'my-username',
'apiKey' => 'my-apikey',
2023-10-23 19:35:46 +00:00
'from' => '+123456789',
],
'Msg91' => [
'providerId' => ID::unique(),
'name' => 'Ms91-1',
'senderId' => 'my-senderid',
'authKey' => 'my-authkey',
2024-03-13 15:36:16 +00:00
'templateId' => '123456'
],
'Vonage' => [
'providerId' => ID::unique(),
'name' => 'Vonage1',
'apiKey' => 'my-apikey',
'apiSecret' => 'my-apisecret',
2023-10-23 19:35:46 +00:00
'from' => '+123456789',
],
2024-02-21 13:26:17 +00:00
'Fcm' => [
'providerId' => ID::unique(),
'name' => 'FCM1',
2023-12-19 18:15:20 +00:00
'serviceAccountJSON' => [
'type' => 'service_account',
2024-01-09 00:41:55 +00:00
"project_id" => "test-project",
"private_key_id" => "test-private-key-id",
"private_key" => "test-private-key",
2023-12-19 18:15:20 +00:00
]
],
2024-02-21 13:26:17 +00:00
'Apns' => [
'providerId' => ID::unique(),
'name' => 'APNS1',
'authKey' => 'my-authkey',
'authKeyId' => 'my-authkeyid',
'teamId' => 'my-teamid',
'bundleId' => 'my-bundleid',
],
];
$providers = [];
foreach (\array_keys($providersParams) as $providerKey) {
$query = $this->getQuery('create_' . \strtolower($providerKey) . '_provider');
$graphQLPayload = [
'query' => $query,
'variables' => $providersParams[$providerKey],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
2024-01-09 00:41:55 +00:00
$providers[] = $response['body']['data']['messagingCreate' . $providerKey . 'Provider'];
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($providersParams[$providerKey]['name'], $response['body']['data']['messagingCreate' . $providerKey . 'Provider']['name']);
}
2026-03-31 16:34:37 +00:00
self::$cachedProviders[$key] = $providers;
return $providers;
}
protected function setupUpdatedProviders(): array
{
$key = $this->getProject()['$id'] . '_updated';
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedProviders[$key])) {
return self::$cachedProviders[$key];
}
$providers = $this->setupProviders();
$providersParams = [
'Sendgrid' => [
2023-10-06 13:53:46 +00:00
'providerId' => $providers[0]['_id'],
'name' => 'Sengrid2',
'apiKey' => 'my-apikey',
],
2025-10-24 08:49:59 +00:00
'Resend' => [
2023-10-06 13:53:46 +00:00
'providerId' => $providers[1]['_id'],
2025-10-24 08:49:59 +00:00
'name' => 'Resend2',
'apiKey' => 'my-apikey',
],
'Mailgun' => [
'providerId' => $providers[2]['_id'],
'name' => 'Mailgun2',
'apiKey' => 'my-apikey',
'domain' => 'my-domain',
],
'Twilio' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[3]['_id'],
'name' => 'Twilio2',
'accountSid' => 'my-accountSid',
'authToken' => 'my-authToken',
],
'Telesign' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[4]['_id'],
'name' => 'Telesign2',
2024-02-13 02:20:10 +00:00
'customerId' => 'my-username',
'apiKey' => 'my-password',
],
'Textmagic' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[5]['_id'],
'name' => 'Textmagic2',
'username' => 'my-username',
'apiKey' => 'my-apikey',
],
'Msg91' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[6]['_id'],
'name' => 'Ms91-2',
'senderId' => 'my-senderid',
'authKey' => 'my-authkey',
2024-03-13 15:36:16 +00:00
'templateId' => '123456',
],
'Vonage' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[7]['_id'],
'name' => 'Vonage2',
'apiKey' => 'my-apikey',
'apiSecret' => 'my-apisecret',
],
2024-02-21 13:26:17 +00:00
'Fcm' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[8]['_id'],
'name' => 'FCM2',
2023-12-19 18:15:20 +00:00
'serviceAccountJSON' => [
'type' => 'service_account',
2024-01-09 00:41:55 +00:00
'project_id' => 'test-project',
'private_key_id' => 'test-project-id',
'private_key' => "test-private-key",
2023-12-19 18:15:20 +00:00
]
],
2024-02-21 13:26:17 +00:00
'Apns' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[9]['_id'],
'name' => 'APNS2',
'authKey' => 'my-authkey',
'authKeyId' => 'my-authkeyid',
'teamId' => 'my-teamid',
'bundleId' => 'my-bundleid',
],
];
foreach (\array_keys($providersParams) as $index => $providerKey) {
$query = $this->getQuery('update_' . \strtolower($providerKey) . '_provider');
2024-02-21 13:26:17 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => $providersParams[$providerKey],
];
2024-02-21 13:26:17 +00:00
$response = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $graphQLPayload);
2024-02-21 13:26:17 +00:00
$providers[$index] = $response['body']['data']['messagingUpdate' . $providerKey . 'Provider'];
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($providersParams[$providerKey]['name'], $response['body']['data']['messagingUpdate' . $providerKey . 'Provider']['name']);
}
$response = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], [
'query' => $this->getQuery('update_mailgun_provider'),
'variables' => [
2025-10-24 08:49:59 +00:00
'providerId' => $providers[2]['_id'],
'name' => 'Mailgun2',
'apiKey' => 'my-apikey',
'domain' => 'my-domain',
'isEuRegion' => true,
'enabled' => false,
]
]);
2025-10-24 08:49:59 +00:00
$providers[2] = $response['body']['data']['messagingUpdateMailgunProvider'];
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('Mailgun2', $response['body']['data']['messagingUpdateMailgunProvider']['name']);
$this->assertEquals(false, $response['body']['data']['messagingUpdateMailgunProvider']['enabled']);
2026-03-31 16:34:37 +00:00
self::$cachedProviders[$key] = $providers;
return $providers;
}
protected function setupTopic(): array
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedTopic[$key])) {
return self::$cachedTopic[$key];
}
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_TOPIC);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'topicId' => ID::unique(),
'name' => 'topic1',
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('topic1', $response['body']['data']['messagingCreateTopic']['name']);
2026-03-31 16:34:37 +00:00
self::$cachedTopic[$key] = $response['body']['data']['messagingCreateTopic'];
return self::$cachedTopic[$key];
}
protected function setupUpdatedTopic(): string
{
$key = $this->getProject()['$id'] . '_updated';
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedTopic[$key])) {
return self::$cachedTopic[$key];
}
$topic = $this->setupTopic();
$topicId = $topic['_id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_TOPIC);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'topicId' => $topicId,
'name' => 'topic2',
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('topic2', $response['body']['data']['messagingUpdateTopic']['name']);
2026-03-31 16:34:37 +00:00
self::$cachedTopic[$key] = $topicId;
return $topicId;
}
protected function setupSubscriber(): array
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedSubscriber[$key])) {
return self::$cachedSubscriber[$key];
}
$topic = $this->setupTopic();
$topicId = $topic['_id'];
$userId = $this->getUser()['$id'];
2023-10-26 14:14:06 +00:00
$providerParam = [
'sendgrid' => [
'providerId' => ID::unique(),
'name' => 'Sengrid1',
'apiKey' => 'my-apikey',
2023-12-19 18:15:20 +00:00
'fromName' => 'Sender',
'fromEmail' => 'sender-email@my-domain.com',
2023-10-26 14:14:06 +00:00
]
];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_SENDGRID_PROVIDER);
2023-10-26 14:14:06 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => $providerParam['sendgrid'],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$providerId = $response['body']['data']['messagingCreateSendgridProvider']['_id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_USER_TARGET);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'targetId' => ID::unique(),
2023-11-14 12:44:07 +00:00
'providerType' => 'email',
2023-10-17 17:23:26 +00:00
'userId' => $userId,
2023-10-26 14:14:06 +00:00
'providerId' => $providerId,
2023-11-21 09:41:09 +00:00
'identifier' => 'random-email@mail.org',
2023-10-17 17:23:26 +00:00
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($userId, $response['body']['data']['usersCreateTarget']['userId']);
2023-11-21 09:41:09 +00:00
$this->assertEquals('random-email@mail.org', $response['body']['data']['usersCreateTarget']['identifier']);
$targetId = $response['body']['data']['usersCreateTarget']['_id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_SUBSCRIBER);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'subscriberId' => ID::unique(),
'topicId' => $topicId,
'targetId' => $targetId,
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($response['body']['data']['messagingCreateSubscriber']['topicId'], $topicId);
$this->assertEquals($response['body']['data']['messagingCreateSubscriber']['targetId'], $targetId);
2023-11-23 17:44:02 +00:00
$this->assertEquals($response['body']['data']['messagingCreateSubscriber']['target']['userId'], $userId);
2026-03-31 16:34:37 +00:00
self::$cachedSubscriber[$key] = $response['body']['data']['messagingCreateSubscriber'];
return self::$cachedSubscriber[$key];
}
protected function setupEmail(): array
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedEmail[$key])) {
return self::$cachedEmail[$key];
}
2024-04-01 11:02:47 +00:00
if (empty(System::getEnv('_APP_MESSAGE_EMAIL_TEST_DSN'))) {
$this->markTestSkipped('Email DSN not provided');
}
2024-04-01 11:02:47 +00:00
$emailDSN = new DSN(System::getEnv('_APP_MESSAGE_EMAIL_TEST_DSN'));
2023-10-30 20:35:50 +00:00
$to = $emailDSN->getParam('to');
2023-12-19 18:15:20 +00:00
$fromName = $emailDSN->getParam('fromName');
$fromEmail = $emailDSN->getParam('fromEmail');
2023-10-30 20:35:50 +00:00
$isEuRegion = $emailDSN->getParam('isEuRegion');
$apiKey = $emailDSN->getPassword();
$domain = $emailDSN->getUser();
if (empty($to) || empty($from) || empty($apiKey) || empty($domain) || empty($isEuRegion)) {
$this->markTestSkipped('Email provider not configured');
}
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_MAILGUN_PROVIDER);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'providerId' => ID::unique(),
'name' => 'Mailgun1',
'apiKey' => $apiKey,
'domain' => $domain,
2023-12-19 18:15:20 +00:00
'fromName' => $fromName,
'fromEmail' => $fromEmail,
2023-10-26 14:14:06 +00:00
'isEuRegion' => filter_var($isEuRegion, FILTER_VALIDATE_BOOLEAN),
2023-10-17 17:23:26 +00:00
],
];
$provider = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $provider['headers']['status-code']);
$providerId = $provider['body']['data']['messagingCreateMailgunProvider']['_id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_TOPIC);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'topicId' => ID::unique(),
'name' => 'topic1',
],
];
$topic = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $topic['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_USER);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
2023-10-19 07:59:48 +00:00
'userId' => ID::unique(),
'email' => 'random1-mail@mail.org',
2023-10-17 17:23:26 +00:00
'password' => 'password',
'name' => 'Messaging User',
]
];
$user = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $user['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_USER_TARGET);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'targetId' => ID::unique(),
2023-11-14 12:44:07 +00:00
'providerType' => 'email',
2023-10-17 17:23:26 +00:00
'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId,
'identifier' => $to,
],
];
$target = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $target['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_SUBSCRIBER);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'subscriberId' => ID::unique(),
'topicId' => $topic['body']['data']['messagingCreateTopic']['_id'],
'targetId' => $target['body']['data']['usersCreateTarget']['_id'],
],
];
$subscriber = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $subscriber['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_EMAIL);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
'messageId' => ID::unique(),
2023-10-30 18:07:57 +00:00
'topics' => [$topic['body']['data']['messagingCreateTopic']['_id']],
2023-10-17 17:23:26 +00:00
'subject' => 'Khali beats Undertaker',
'content' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
],
];
$email = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $email['headers']['status-code']);
2026-02-24 01:00:07 +00:00
$emailMessageId = $email['body']['data']['messagingCreateEmail']['_id'];
$this->assertEventually(function () use ($emailMessageId) {
$response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $emailMessageId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertContains($response['body']['status'], ['sent', 'failed']);
}, 30000, 500);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_MESSAGE);
$graphQLPayload = [
2023-10-17 17:23:26 +00:00
'query' => $query,
'variables' => [
2026-02-24 01:00:07 +00:00
'messageId' => $emailMessageId,
2023-10-17 17:23:26 +00:00
],
];
$message = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-30 18:07:57 +00:00
$this->assertEquals(1, $message['body']['data']['messagingGetMessage']['deliveredTotal']);
$this->assertEquals(0, \count($message['body']['data']['messagingGetMessage']['deliveryErrors']));
2023-10-19 07:59:48 +00:00
2026-03-31 16:34:37 +00:00
self::$cachedEmail[$key] = $message['body']['data']['messagingGetMessage'];
return self::$cachedEmail[$key];
2023-10-19 07:59:48 +00:00
}
protected function setupSms(): array
2023-10-19 07:59:48 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedSms[$key])) {
return self::$cachedSms[$key];
}
2023-10-19 07:59:48 +00:00
2024-04-01 11:02:47 +00:00
if (empty(System::getEnv('_APP_MESSAGE_SMS_TEST_DSN'))) {
$this->markTestSkipped('SMS DSN not provided');
}
2024-04-01 11:02:47 +00:00
$smsDSN = new DSN(System::getEnv('_APP_MESSAGE_SMS_TEST_DSN'));
2023-10-30 20:35:50 +00:00
$to = $smsDSN->getParam('to');
$from = $smsDSN->getParam('from');
$authKey = $smsDSN->getPassword();
$senderId = $smsDSN->getUser();
2023-10-19 07:59:48 +00:00
if (empty($to) || empty($from) || empty($senderId) || empty($authKey)) {
$this->markTestSkipped('SMS provider not configured');
}
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_MSG91_PROVIDER);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'providerId' => ID::unique(),
'name' => 'Msg91-1',
'senderId' => $senderId,
'authKey' => $authKey,
'from' => $from,
],
];
$provider = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $provider['headers']['status-code']);
$providerId = $provider['body']['data']['messagingCreateMsg91Provider']['_id'];
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_TOPIC);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'topicId' => ID::unique(),
'name' => 'topic1',
],
];
$topic = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $topic['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_USER);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'userId' => ID::unique(),
'email' => 'random3-email@mail.org',
'password' => 'password',
'name' => 'Messaging User',
]
];
$user = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $user['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_USER_TARGET);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'targetId' => ID::unique(),
2023-11-14 12:44:07 +00:00
'providerType' => 'sms',
2023-10-19 07:59:48 +00:00
'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId,
'identifier' => $to,
],
];
$target = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $target['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_SUBSCRIBER);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'subscriberId' => ID::unique(),
'topicId' => $topic['body']['data']['messagingCreateTopic']['_id'],
'targetId' => $target['body']['data']['usersCreateTarget']['_id'],
],
];
$subscriber = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $subscriber['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_SMS);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => ID::unique(),
2023-10-30 18:07:57 +00:00
'topics' => [$topic['body']['data']['messagingCreateTopic']['_id']],
2023-10-19 07:59:48 +00:00
'content' => '454665',
],
];
$sms = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $sms['headers']['status-code']);
2026-02-24 01:00:07 +00:00
$smsMessageId = $sms['body']['data']['messagingCreateSMS']['_id'];
$this->assertEventually(function () use ($smsMessageId) {
$response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $smsMessageId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertContains($response['body']['status'], ['sent', 'failed']);
}, 30000, 500);
2023-10-19 07:59:48 +00:00
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_MESSAGE);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2026-02-24 01:00:07 +00:00
'messageId' => $smsMessageId,
2023-10-19 07:59:48 +00:00
],
];
$message = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-30 18:07:57 +00:00
$this->assertEquals(1, $message['body']['data']['messagingGetMessage']['deliveredTotal']);
2023-10-19 07:59:48 +00:00
$this->assertEquals(0, \count($message['body']['data']['messagingGetMessage']['deliveryErrors']));
2026-03-31 16:34:37 +00:00
self::$cachedSms[$key] = $message['body']['data']['messagingGetMessage'];
return self::$cachedSms[$key];
2023-10-19 07:59:48 +00:00
}
protected function setupPush(): array
2023-10-19 07:59:48 +00:00
{
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
if (!empty(self::$cachedPush[$key])) {
return self::$cachedPush[$key];
}
if (empty(System::getEnv('_APP_MESSAGE_PUSH_TEST_DSN'))) {
$this->markTestSkipped('Push DSN empty');
}
$pushDSN = new DSN(System::getEnv('_APP_MESSAGE_PUSH_TEST_DSN'));
$to = $pushDSN->getParam('to');
$serviceAccountJSON = $pushDSN->getParam('serviceAccountJSON');
if (empty($to) || empty($serviceAccountJSON)) {
$this->markTestSkipped('Push provider not configured');
}
$query = $this->getQuery(self::CREATE_FCM_PROVIDER);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'providerId' => ID::unique(),
'name' => 'FCM1',
2023-12-19 18:15:20 +00:00
'serviceAccountJSON' => [
'type' => 'service_account',
2024-01-09 00:41:55 +00:00
"project_id" => "test-project",
"private_key_id" => "test-private-key-id",
"private_key" => "test-private-key",
2023-12-19 18:15:20 +00:00
]
2023-10-19 07:59:48 +00:00
],
];
$provider = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $provider['headers']['status-code']);
2024-02-21 13:26:17 +00:00
$providerId = $provider['body']['data']['messagingCreateFcmProvider']['_id'];
2023-10-19 07:59:48 +00:00
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_TOPIC);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'topicId' => ID::unique(),
'name' => 'topic1',
],
];
$topic = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $topic['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_USER);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'userId' => ID::unique(),
'email' => 'random5-mail@mail.org',
'password' => 'password',
'name' => 'Messaging User',
]
];
$user = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $user['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_USER_TARGET);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'targetId' => ID::unique(),
2023-11-14 12:44:07 +00:00
'providerType' => 'push',
2023-10-19 07:59:48 +00:00
'userId' => $user['body']['data']['usersCreate']['_id'],
'providerId' => $providerId,
'identifier' => $to,
],
];
$target = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $target['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_SUBSCRIBER);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'subscriberId' => ID::unique(),
'topicId' => $topic['body']['data']['messagingCreateTopic']['_id'],
'targetId' => $target['body']['data']['usersCreateTarget']['_id'],
],
];
$subscriber = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $subscriber['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_PUSH_NOTIFICATION);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => ID::unique(),
2023-10-30 18:07:57 +00:00
'topics' => [$topic['body']['data']['messagingCreateTopic']['_id']],
2023-10-19 07:59:48 +00:00
'title' => 'Push Notification Title',
'body' => 'Push Notifiaction Body',
],
];
$push = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $push['headers']['status-code']);
2026-02-24 01:00:07 +00:00
$pushMessageId = $push['body']['data']['messagingCreatePushNotification']['_id'];
$this->assertEventually(function () use ($pushMessageId) {
$response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $pushMessageId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertContains($response['body']['status'], ['sent', 'failed']);
}, 30000, 500);
2023-10-19 07:59:48 +00:00
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_MESSAGE);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2026-02-24 01:00:07 +00:00
'messageId' => $pushMessageId,
2023-10-19 07:59:48 +00:00
],
];
$message = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-30 18:07:57 +00:00
$this->assertEquals(1, $message['body']['data']['messagingGetMessage']['deliveredTotal']);
2023-10-19 07:59:48 +00:00
$this->assertEquals(0, \count($message['body']['data']['messagingGetMessage']['deliveryErrors']));
2026-03-31 16:34:37 +00:00
self::$cachedPush[$key] = $message['body']['data']['messagingGetMessage'];
return self::$cachedPush[$key];
}
public function testCreateProviders(): void
{
$providers = $this->setupProviders();
$this->assertCount(10, $providers);
}
public function testUpdateProviders(): void
{
$providers = $this->setupUpdatedProviders();
$this->assertEquals('Sengrid2', $providers[0]['name']);
}
public function testListProviders()
{
$providers = $this->setupUpdatedProviders();
$query = $this->getQuery(self::LIST_PROVIDERS);
$graphQLPayload = [
'query' => $query,
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(\count($providers), \count($response['body']['data']['messagingListProviders']['providers']));
}
public function testGetProvider()
{
$providers = $this->setupUpdatedProviders();
$query = $this->getQuery(self::GET_PROVIDER);
$graphQLPayload = [
'query' => $query,
'variables' => [
'providerId' => $providers[0]['_id'],
]
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($providers[0]['name'], $response['body']['data']['messagingGetProvider']['name']);
}
public function testDeleteProvider()
{
$providers = $this->setupUpdatedProviders();
foreach ($providers as $provider) {
$query = $this->getQuery(self::DELETE_PROVIDER);
$graphQLPayload = [
'query' => $query,
'variables' => [
'providerId' => $provider['_id'],
]
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
], $graphQLPayload);
$this->assertEquals(204, $response['headers']['status-code']);
}
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedProviders[$key] = [];
self::$cachedProviders[$key . '_updated'] = [];
}
public function testCreateTopic(): void
{
$topic = $this->setupTopic();
$this->assertEquals('topic1', $topic['name']);
}
public function testUpdateTopic(): void
{
$topicId = $this->setupUpdatedTopic();
$this->assertNotEmpty($topicId);
}
public function testListTopics()
{
$this->setupTopic();
$query = $this->getQuery(self::LIST_TOPICS);
$graphQLPayload = [
'query' => $query,
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals(1, \count($response['body']['data']['messagingListTopics']['topics']));
}
public function testGetTopic()
{
$topicId = $this->setupUpdatedTopic();
$query = $this->getQuery(self::GET_TOPIC);
$graphQLPayload = [
'query' => $query,
'variables' => [
'topicId' => $topicId,
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals('topic2', $response['body']['data']['messagingGetTopic']['name']);
}
public function testCreateSubscriber(): void
{
$subscriber = $this->setupSubscriber();
$this->assertNotEmpty($subscriber['_id']);
}
public function testListSubscribers()
{
$subscriber = $this->setupSubscriber();
$query = $this->getQuery(self::LIST_SUBSCRIBERS);
$graphQLPayload = [
'query' => $query,
'variables' => [
'topicId' => $subscriber['topicId'],
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($subscriber['topicId'], $response['body']['data']['messagingListSubscribers']['subscribers'][0]['topicId']);
$this->assertEquals($subscriber['targetId'], $response['body']['data']['messagingListSubscribers']['subscribers'][0]['targetId']);
$this->assertEquals($subscriber['target']['userId'], $response['body']['data']['messagingListSubscribers']['subscribers'][0]['target']['userId']);
$this->assertEquals(1, \count($response['body']['data']['messagingListSubscribers']['subscribers']));
}
public function testGetSubscriber()
{
$subscriber = $this->setupSubscriber();
$topicId = $subscriber['topicId'];
$subscriberId = $subscriber['_id'];
$query = $this->getQuery(self::GET_SUBSCRIBER);
$graphQLPayload = [
'query' => $query,
'variables' => [
'topicId' => $topicId,
'subscriberId' => $subscriberId,
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
$this->assertEquals($subscriberId, $response['body']['data']['messagingGetSubscriber']['_id']);
$this->assertEquals($topicId, $response['body']['data']['messagingGetSubscriber']['topicId']);
$this->assertEquals($subscriber['targetId'], $response['body']['data']['messagingGetSubscriber']['targetId']);
$this->assertEquals($subscriber['target']['userId'], $response['body']['data']['messagingGetSubscriber']['target']['userId']);
}
public function testDeleteSubscriber()
{
$subscriber = $this->setupSubscriber();
$topicId = $subscriber['topicId'];
$subscriberId = $subscriber['_id'];
$query = $this->getQuery(self::DELETE_SUBSCRIBER);
$graphQLPayload = [
'query' => $query,
'variables' => [
'topicId' => $topicId,
'subscriberId' => $subscriberId,
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
], $this->getHeaders()), $graphQLPayload);
$this->assertEquals(200, $response['headers']['status-code']);
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedSubscriber[$key] = [];
}
public function testDeleteTopic()
{
$topicId = $this->setupUpdatedTopic();
$query = $this->getQuery(self::DELETE_TOPIC);
$graphQLPayload = [
'query' => $query,
'variables' => [
'topicId' => $topicId,
],
];
$response = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(204, $response['headers']['status-code']);
// Clear cache after deletion
$key = $this->getProject()['$id'];
2026-03-31 16:34:37 +00:00
self::$cachedTopic[$key] = [];
self::$cachedTopic[$key . '_updated'] = [];
}
public function testSendEmail(): void
{
$email = $this->setupEmail();
$this->assertEquals(1, $email['deliveredTotal']);
}
public function testUpdateEmail()
{
$email = $this->setupEmail();
$query = $this->getQuery(self::CREATE_EMAIL);
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => ID::unique(),
'status' => 'draft',
'topics' => [$email['topics'][0]],
'subject' => 'Khali beats Undertaker',
'content' => 'https://www.youtube.com/watch?v=dQw4w9WgXcQ',
],
];
$email = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $email['headers']['status-code']);
$query = $this->getQuery(self::UPDATE_EMAIL);
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => $email['body']['data']['messagingCreateEmail']['_id'],
'status' => 'processing',
],
];
$email = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $email['headers']['status-code']);
2026-02-24 01:00:07 +00:00
$updatedEmailId = $email['body']['data']['messagingUpdateEmail']['_id'];
$this->assertEventually(function () use ($updatedEmailId) {
$response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $updatedEmailId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertContains($response['body']['status'], ['sent', 'failed']);
}, 30000, 500);
$query = $this->getQuery(self::GET_MESSAGE);
$graphQLPayload = [
'query' => $query,
'variables' => [
2026-02-24 01:00:07 +00:00
'messageId' => $updatedEmailId,
],
];
$message = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(1, $message['body']['data']['messagingGetMessage']['deliveredTotal']);
$this->assertEquals(0, \count($message['body']['data']['messagingGetMessage']['deliveryErrors']));
}
public function testSendSMS(): void
{
$sms = $this->setupSms();
$this->assertEquals(1, $sms['deliveredTotal']);
2023-10-19 07:59:48 +00:00
}
public function testUpdateSMS()
2023-10-19 07:59:48 +00:00
{
$sms = $this->setupSms();
$query = $this->getQuery(self::CREATE_SMS);
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => ID::unique(),
'status' => 'draft',
'topics' => [$sms['topics'][0]],
'content' => '345463',
],
];
$sms = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $sms['headers']['status-code']);
$query = $this->getQuery(self::UPDATE_SMS);
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => $sms['body']['data']['messagingCreateSMS']['_id'],
'status' => 'processing',
],
];
$sms = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $sms['headers']['status-code']);
2026-02-24 01:00:07 +00:00
$updatedSmsId = $sms['body']['data']['messagingUpdateSMS']['_id'];
$this->assertEventually(function () use ($updatedSmsId) {
$response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $updatedSmsId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertContains($response['body']['status'], ['sent', 'failed']);
}, 30000, 500);
$query = $this->getQuery(self::GET_MESSAGE);
$graphQLPayload = [
'query' => $query,
'variables' => [
2026-02-24 01:00:07 +00:00
'messageId' => $updatedSmsId,
],
];
$message = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $message['headers']['status-code']);
$this->assertEquals(1, $message['body']['data']['messagingGetMessage']['deliveredTotal']);
$this->assertEquals(0, \count($message['body']['data']['messagingGetMessage']['deliveryErrors']));
}
public function testSendPushNotification(): void
{
$push = $this->setupPush();
$this->assertEquals(1, $push['deliveredTotal']);
}
public function testUpdatePushNotification()
{
$push = $this->setupPush();
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::CREATE_PUSH_NOTIFICATION);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => ID::unique(),
'status' => 'draft',
2023-11-14 19:54:55 +00:00
'topics' => [$push['topics'][0]],
2023-10-19 07:59:48 +00:00
'title' => 'Push Notification Title',
'body' => 'Push Notifiaction Body',
],
];
$push = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $push['headers']['status-code']);
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::UPDATE_PUSH_NOTIFICATION);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
'messageId' => $push['body']['data']['messagingCreatePushNotification']['_id'],
'status' => 'processing',
],
];
$push = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $push['headers']['status-code']);
2026-02-24 01:00:07 +00:00
$updatedPushId = $push['body']['data']['messagingUpdatePushNotification']['_id'];
$this->assertEventually(function () use ($updatedPushId) {
$response = $this->client->call(Client::METHOD_GET, '/messaging/messages/' . $updatedPushId, array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]));
$this->assertContains($response['body']['status'], ['sent', 'failed']);
}, 30000, 500);
2023-10-19 07:59:48 +00:00
2025-08-19 11:03:18 +00:00
$query = $this->getQuery(self::GET_MESSAGE);
2023-10-19 07:59:48 +00:00
$graphQLPayload = [
'query' => $query,
'variables' => [
2026-02-24 01:00:07 +00:00
'messageId' => $updatedPushId,
2023-10-19 07:59:48 +00:00
],
];
$message = $this->client->call(Client::METHOD_POST, '/graphql', \array_merge([
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
]), $graphQLPayload);
$this->assertEquals(200, $message['headers']['status-code']);
2023-10-30 18:07:57 +00:00
$this->assertEquals(1, $message['body']['data']['messagingGetMessage']['deliveredTotal']);
2023-10-19 07:59:48 +00:00
$this->assertEquals(0, \count($message['body']['data']['messagingGetMessage']['deliveryErrors']));
}
}