From f9bc4861b5b6e03c6dc82d256b96b054ae4f34b4 Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Tue, 27 Sep 2016 09:35:31 -0700 Subject: [PATCH 1/2] Only send messages for unique notifiers - Fixes #28 --- .../MessageSubscribeEmailSubscribersTest.php | 50 +++++++++++++++++++ src/Subscribers.php | 5 +- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/message_subscribe_email/tests/src/Kernel/MessageSubscribeEmailSubscribersTest.php b/message_subscribe_email/tests/src/Kernel/MessageSubscribeEmailSubscribersTest.php index 56ecdb4..a2bba3a 100644 --- a/message_subscribe_email/tests/src/Kernel/MessageSubscribeEmailSubscribersTest.php +++ b/message_subscribe_email/tests/src/Kernel/MessageSubscribeEmailSubscribersTest.php @@ -75,4 +75,54 @@ public function testGetSubscribers() { $this->assertEquals('message_notify_' . $this->messageTemplate->id(), $mails[0]['id']); } + /** + * Tests behavior with the default notifiers in place. + */ + public function testWithDefaultNotifiers() { + $this->config('message_subscribe.settings') + // Override default notifiers. + ->set('default_notifiers', ['email']) + ->save(); + + $message = Message::create(['template' => $this->messageTemplate->id()]); + + $node = $this->nodes[1]; + $user1 = $this->users[1]; + $user2 = $this->users[2]; + + $uids = $this->messageSubscribers->getSubscribers($node, $message); + + // Assert subscribers data. + $expected_uids = [ + $user1->id() => [ + 'notifiers' => [ + 'email' => 'email', + ], + 'flags' => [ + 'subscribe_node', + ], + ], + $user2->id() => [ + 'notifiers' => [ + 'email' => 'email', + ], + 'flags' => [ + 'subscribe_node', + ], + ], + ]; + + $this->assertEquals($expected_uids, $uids, 'All expected subscribers were fetched.'); + + $subscribe_options = [ + 'uids' => $uids, + ]; + $this->messageSubscribers->sendMessage($node, $message, [], $subscribe_options); + + // Assert sent emails. + $mails = $this->getMails(); + $this->assertEquals(2, count($mails), 'Both users were sent an email.'); + $this->assertEquals('message_notify_' . $this->messageTemplate->id(), $mails[0]['id']); + } + } diff --git a/src/Subscribers.php b/src/Subscribers.php index fa3d0c5..d638a61 100644 --- a/src/Subscribers.php +++ b/src/Subscribers.php @@ -191,7 +191,7 @@ public function sendMessage(EntityInterface $entity, MessageInterface $message, $values += ['notifiers' => []]; // Send the message using the required notifiers. - foreach ($values['notifiers'] as $notifier_name) { + foreach (array_unique($values['notifiers']) as $notifier_name) { $options = !empty($notify_options[$notifier_name]) ? $notify_options[$notifier_name] : []; $options += [ 'save on fail' => FALSE, @@ -386,6 +386,9 @@ protected function addDefaultNotifiers(array &$uids) { if (empty($notifiers)) { return; } + // Use notifier names as keys to avoid potential duplication of notifiers + // by other modules' hooks. + $notifiers = array_combine($notifiers, $notifiers); foreach (array_keys($uids) as $uid) { $uids[$uid]['notifiers'] += $notifiers; } From 288575347b7f14d7c2d8d6616ae9cd64fbab50c9 Mon Sep 17 00:00:00 2001 From: Jonathan Hedstrom Date: Tue, 27 Sep 2016 10:55:42 -0700 Subject: [PATCH 2/2] Removes now-committed Flag patch --- .travis.yml | 7 ------- 1 file changed, 7 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4ef72a5..9e68280 100644 --- a/.travis.yml +++ b/.travis.yml @@ -51,13 +51,6 @@ before_script: - travis_retry git clone --branch 8.x-1.x --depth 1 https://github.com/Gizra/message_notify.git - cd .. - # Patch flag. - # @todo Remove this once https://www.drupal.org/node/2802653 lands. - - cd modules/flag - - curl https://www.drupal.org/files/issues/2802653-10.patch > patch.txt - - git apply patch.txt - - cd - - # Install Composer dependencies on 8.1.x and above. - test ${DRUPAL_CORE} == "8.0.x" || composer self-update && composer install