From 62df31425a61d59b7ef9cf251cabf804cd0b7861 Mon Sep 17 00:00:00 2001 From: Stephen Cox Date: Fri, 23 May 2025 11:51:35 +0100 Subject: [PATCH 1/3] Fix regression bug with notification type enqueuing as separate notification #137 --- .../src/WorkflowNotification.php | 4 ++-- .../tests/src/Kernel/WorkflowNotificationTest.php | 13 ++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/modules/localgov_workflows_notifications/src/WorkflowNotification.php b/modules/localgov_workflows_notifications/src/WorkflowNotification.php index 45ec023..afcb922 100644 --- a/modules/localgov_workflows_notifications/src/WorkflowNotification.php +++ b/modules/localgov_workflows_notifications/src/WorkflowNotification.php @@ -42,11 +42,11 @@ public function enqueue(ContentEntityInterface $entity, string $type): void { continue; } - // Ensure the queue contains only one item for per service contact. + // Ensure the queue contains only one item for per service contact and type. $found = FALSE; $claimed_items = []; while ($queue_item = $queue->claimItem(1)) { - if ($queue_item->data->service_contact == $contact->id()) { + if ($queue_item->data->service_contact == $contact->id() && $queue_item->data->type == $type) { // Delete old item and create new one with additional entity. $queue->deleteItem($queue_item); diff --git a/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php b/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php index d79da9b..0277150 100644 --- a/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php +++ b/modules/localgov_workflows_notifications/tests/src/Kernel/WorkflowNotificationTest.php @@ -166,7 +166,7 @@ public function testEnqueueNodes() { $this->notifier->enqueue($node5, 'published'); $this->assertEquals(2, $this->queue->numberOfItems()); - // Enqueue a notification with different type. + // Ensure notifications are not duplicated. $node6 = $this->createNode([ 'type' => 'page', 'title' => $this->randomMachineName(), @@ -175,6 +175,17 @@ public function testEnqueueNodes() { ], ]); $this->notifier->enqueue($node6, 'published'); + $this->assertEquals(2, $this->queue->numberOfItems()); + + // Enqueue a notification with different type. + $node7 = $this->createNode([ + 'type' => 'page', + 'title' => $this->randomMachineName(), + 'localgov_service_contacts' => [ + ['target_id' => $this->serviceContacts['enabled']->id()], + ], + ]); + $this->notifier->enqueue($node7, 'unpublished'); $this->assertEquals(3, $this->queue->numberOfItems()); } From deaeffd4379930d3c73cad7e1bf2e50c760b81ef Mon Sep 17 00:00:00 2001 From: Stephen Cox Date: Fri, 23 May 2025 12:09:37 +0100 Subject: [PATCH 2/3] Coding standards fix --- .../src/WorkflowNotification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/localgov_workflows_notifications/src/WorkflowNotification.php b/modules/localgov_workflows_notifications/src/WorkflowNotification.php index afcb922..d757871 100644 --- a/modules/localgov_workflows_notifications/src/WorkflowNotification.php +++ b/modules/localgov_workflows_notifications/src/WorkflowNotification.php @@ -42,7 +42,7 @@ public function enqueue(ContentEntityInterface $entity, string $type): void { continue; } - // Ensure the queue contains only one item for per service contact and type. + // Aggregate notifications by service contact and type. $found = FALSE; $claimed_items = []; while ($queue_item = $queue->claimItem(1)) { From 8a37cc85110f3b4dcb4280dbde6749caf314e9fd Mon Sep 17 00:00:00 2001 From: Stephen Cox Date: Tue, 27 May 2025 11:22:15 +0100 Subject: [PATCH 3/3] Use identity operator when comparing --- .../src/WorkflowNotification.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/localgov_workflows_notifications/src/WorkflowNotification.php b/modules/localgov_workflows_notifications/src/WorkflowNotification.php index d757871..f3f9624 100644 --- a/modules/localgov_workflows_notifications/src/WorkflowNotification.php +++ b/modules/localgov_workflows_notifications/src/WorkflowNotification.php @@ -46,7 +46,7 @@ public function enqueue(ContentEntityInterface $entity, string $type): void { $found = FALSE; $claimed_items = []; while ($queue_item = $queue->claimItem(1)) { - if ($queue_item->data->service_contact == $contact->id() && $queue_item->data->type == $type) { + if ($queue_item->data->service_contact == $contact->id() && $queue_item->data->type === $type) { // Delete old item and create new one with additional entity. $queue->deleteItem($queue_item);