From e7ea585ba1c3d6628598da05b6d937c24a6b90e3 Mon Sep 17 00:00:00 2001 From: Brian Stoop Date: Thu, 5 Dec 2024 11:18:19 +0100 Subject: [PATCH] General: Allow broadcast to be sent with empty endpoints --- .../Vortex/PushNotificationDispatcher.php | 10 +++-- ...PushNotificationDispatcherDispatchTest.php | 37 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/Lunr/Vortex/PushNotificationDispatcher.php b/src/Lunr/Vortex/PushNotificationDispatcher.php index 671f6af..b4e389d 100644 --- a/src/Lunr/Vortex/PushNotificationDispatcher.php +++ b/src/Lunr/Vortex/PushNotificationDispatcher.php @@ -87,7 +87,7 @@ public function dispatch(array $endpoints, array $payloads): void foreach ($payloads as $platform => $platform_payloads) { - if (!isset($grouped_endpoints[$platform])) + if (!isset($grouped_endpoints[$platform]) && array_filter($platform_payloads, fn($payload) => $payload->is_broadcast()) === []) { continue; } @@ -104,18 +104,20 @@ public function dispatch(array $endpoints, array $payloads): void foreach ($platform_payloads as $payload_type => $payload) { - if (!isset($grouped_endpoints[$platform][$payload_type])) + if (!isset($grouped_endpoints[$platform][$payload_type]) && $payload->is_broadcast() === FALSE) { continue; } + $endpoints = $grouped_endpoints[$platform][$payload_type] ?? []; + if ($this->dispatchers[$platform] instanceof PushNotificationMultiDispatcherInterface) { - $this->dispatch_multiple($platform, $grouped_endpoints[$platform][$payload_type], $payload); + $this->dispatch_multiple($platform, $endpoints, $payload); } else { - $this->dispatch_single($platform, $grouped_endpoints[$platform][$payload_type], $payload); + $this->dispatch_single($platform, $endpoints, $payload); } } } diff --git a/src/Lunr/Vortex/Tests/PushNotificationDispatcherDispatchTest.php b/src/Lunr/Vortex/Tests/PushNotificationDispatcherDispatchTest.php index 87b9005..7df91b3 100644 --- a/src/Lunr/Vortex/Tests/PushNotificationDispatcherDispatchTest.php +++ b/src/Lunr/Vortex/Tests/PushNotificationDispatcherDispatchTest.php @@ -928,6 +928,43 @@ public function testDispatchMultiCastWithDeferredResponse(): void $this->assertPropertySame('statuses', $expected_statuses); } + /** + * Test dispatch send correct broadcast payload. + * + * @covers Lunr\Vortex\PushNotificationDispatcher::dispatch + */ + public function testDispatchSendsCorrectBroadcastPayload(): void + { + $dispatchers = [ + 'apns' => $this->apns, + 'fcm' => $this->fcm, + 'email' => $this->email, + ]; + $this->set_reflection_property_value('dispatchers', $dispatchers); + + $data_payload = $this->getMockBuilder(FCMPayload::class) + ->disableOriginalConstructor() + ->getMock(); + + $payloads = [ + 'fcm' => [ 'data' => $data_payload ], + ]; + + $data_payload->expects($this->exactly(2)) + ->method('is_broadcast') + ->willReturn(TRUE); + + $this->fcm->expects($this->once()) + ->method('push') + ->with($data_payload, []) + ->willReturn($this->fcm_response); + + $this->fcm_response->expects($this->never()) + ->method('get_status'); + + $this->class->dispatch([], $payloads); + } + } ?>