From fd2b111e0c4de061c94d291bd0b7af71ce3697f6 Mon Sep 17 00:00:00 2001 From: Kai Dederichs Date: Wed, 27 Dec 2023 23:28:01 +0100 Subject: [PATCH] When external_id is set, don't use recipient and use it in place of include_subscription_ids --- .../Bridge/OneSignal/OneSignalTransport.php | 12 +++++++- .../Tests/OneSignalTransportTest.php | 30 ++++++++++++++++++- 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php b/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php index c9502dce1f083..697382d7af20f 100644 --- a/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/OneSignal/OneSignalTransport.php @@ -81,7 +81,17 @@ protected function doSend(MessageInterface $message): SentMessage $options = $opts ? $opts->toArray() : []; $options['app_id'] = $this->appId; - $options['include_player_ids'] = [$recipientId]; + if (isset($options['external_id'])) { + $options['include_aliases'] = [ + 'external_id' => [ + $options['external_id'] + ] + ]; + $options['target_channel'] = 'push'; + unset($options['external_id']); + } else { + $options['include_subscription_ids'] = [$recipientId]; // include_player_ids has been deprecated + } if (!isset($options['headings'])) { $options['headings'] = ['en' => $message->getSubject()]; diff --git a/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php b/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php index f0d88e7383b65..52258726e110c 100644 --- a/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php +++ b/src/Symfony/Component/Notifier/Bridge/OneSignal/Tests/OneSignalTransportTest.php @@ -145,7 +145,7 @@ public function testSend() ->method('getContent') ->willReturn(json_encode(['id' => 'b98881cc-1e94-4366-bbd9-db8f3429292b', 'recipients' => 1, 'external_id' => null])); - $expectedBody = json_encode(['app_id' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246', 'headings' => ['en' => 'Hello'], 'contents' => ['en' => 'World'], 'include_player_ids' => ['ea345989-d273-4f21-a33b-0c006efc5edb']]); + $expectedBody = json_encode(['app_id' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246', 'headings' => ['en' => 'Hello'], 'contents' => ['en' => 'World'], 'include_subscription_ids' => ['ea345989-d273-4f21-a33b-0c006efc5edb']]); $client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface { $this->assertJsonStringEqualsJsonString($expectedBody, $options['body']); @@ -159,4 +159,32 @@ public function testSend() $this->assertSame('b98881cc-1e94-4366-bbd9-db8f3429292b', $sentMessage->getMessageId()); } + + public function testSendExternalIds() + { + $response = $this->createMock(ResponseInterface::class); + $response->expects($this->exactly(2)) + ->method('getStatusCode') + ->willReturn(200); + $response->expects($this->once()) + ->method('getContent') + ->willReturn(json_encode(['id' => 'b98881cc-1e94-4366-bbd9-db8f3429292b', 'recipients' => 1, 'external_id' => null])); + + $expectedBody = json_encode(['app_id' => '9fb175f0-0b32-4e99-ae97-bd228b9eb246', 'headings' => ['en' => 'Hello'], 'contents' => ['en' => 'World'], 'include_aliases' => ['external_id' => ['external_id']], 'target_channel' => 'push']); + + $client = new MockHttpClient(function (string $method, string $url, array $options = []) use ($response, $expectedBody): ResponseInterface { + $this->assertJsonStringEqualsJsonString($expectedBody, $options['body']); + + return $response; + }); + + $transport = self::createTransport($client, 'ea345989-d273-4f21-a33b-0c006efc5edb'); + + $options = new OneSignalOptions(); + $options->externalId('external_id'); + + $sentMessage = $transport->send(new PushMessage('Hello', 'World', $options)); + + $this->assertSame('b98881cc-1e94-4366-bbd9-db8f3429292b', $sentMessage->getMessageId()); + } }