Skip to content

Commit

Permalink
Merge pull request #29 from Gizra/28-double-sending
Browse files Browse the repository at this point in the history
Only send messages for unique notifiers
  • Loading branch information
jhedstrom authored Sep 27, 2016
2 parents 7a7f05b + 2885753 commit 2fe6ba7
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
7 changes: 0 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}

}
5 changes: 4 additions & 1 deletion src/Subscribers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 2fe6ba7

Please sign in to comment.