summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgnuget2019-02-07 16:57:09 (GMT)
committerDavid Valdez2019-02-07 16:57:09 (GMT)
commita2f7604deb06f79a28455aed7d410eebe390cd15 (patch)
treeda7343a42ca6ca56d9dda1e24674e96fd03dfebb
parentd50f43cf03ed0596ea3af79207a0f719ca58df89 (diff)
Issue #3020673 by gnuget, wturrell: False positive notifications with multiple comment types per node
-rw-r--r--comment_notify.inc7
-rw-r--r--comment_notify.module4
-rw-r--r--tests/src/Functional/CommentNotifyNotificationsTest.php87
3 files changed, 95 insertions, 3 deletions
diff --git a/comment_notify.inc b/comment_notify.inc
index 61f08ec..59f5f05 100644
--- a/comment_notify.inc
+++ b/comment_notify.inc
@@ -212,12 +212,15 @@ function comment_notify_get_notification_type($cid) {
* Get a list of mails which need to be contacted for a node.
*
* @param integer $nid
+ * @param string $comment_type
+ *
* @return \Drupal\comment\CommentInterface[]
* A list of comment entities.
*/
-function comment_notify_get_watchers($nid) {
- $cids = db_query("SELECT c.cid FROM {comment_field_data} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users_field_data} u ON c.uid = u.uid WHERE c.entity_id = :nid AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", array(
+function comment_notify_get_watchers($nid, $comment_type) {
+ $cids = db_query("SELECT c.cid FROM {comment_field_data} c INNER JOIN {comment_notify} cn ON c.cid = cn.cid LEFT JOIN {users_field_data} u ON c.uid = u.uid WHERE c.entity_id = :nid AND c.comment_type = :comment_type AND c.status = :status AND cn.notify <> :notify AND (u.uid = 0 OR u.status = 1)", array(
':nid' => $nid,
+ ':comment_type' => $comment_type,
':status' => CommentInterface::PUBLISHED,
':notify' => COMMENT_NOTIFY_DISABLED,
))->fetchCol();
diff --git a/comment_notify.module b/comment_notify.module
index 3d7ea76..eebacd2 100644
--- a/comment_notify.module
+++ b/comment_notify.module
@@ -331,6 +331,8 @@ function _comment_notify_mailalert(CommentInterface $comment) {
$user = \Drupal::currentUser();
$nid = $comment->getCommentedEntityId();
+ $comment_type = $comment->get('comment_type')->getString();
+
// Check to see if a notification has already been sent for this
// comment so that edits to a comment don't trigger an additional
// notification.
@@ -387,7 +389,7 @@ function _comment_notify_mailalert(CommentInterface $comment) {
$thread = $comment->getThread() ?: '';
// Get the list of commenters to notify.
- $watchers = comment_notify_get_watchers($nid);
+ $watchers = comment_notify_get_watchers($nid, $comment_type);
foreach ($watchers as $alert) {
// If the user is not anonymous, always load the current e-mail address
diff --git a/tests/src/Functional/CommentNotifyNotificationsTest.php b/tests/src/Functional/CommentNotifyNotificationsTest.php
index dfd7bd2..833f7fd 100644
--- a/tests/src/Functional/CommentNotifyNotificationsTest.php
+++ b/tests/src/Functional/CommentNotifyNotificationsTest.php
@@ -2,6 +2,9 @@
namespace Drupal\Tests\comment_notify\Functional;
+use Drupal\comment\Entity\Comment;
+use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
+
/**
* Tests that all the notifications are sent as expected.
*
@@ -57,4 +60,88 @@ class CommentNotifyNotificationsTest extends CommentNotifyTestBase {
}
+ /**
+ * Tests the notifications are sent correctly with multiple comment types.
+ */
+ public function testCommentTypeNotification() {
+ // Add a second comment type.
+ $this->addDefaultCommentField('node', 'article', 'field_comment', CommentItemInterface::OPEN, 'comment_type_2');
+ $user1 = $this->drupalCreateUser($this->permissions);
+ $user2 = $this->drupalCreateUser($this->permissions);
+
+ $node = $this->drupalCreateNode(
+ [
+ 'type' => 'article',
+ 'uid' => $this->adminUser,
+ ]
+ );
+
+ // Comment of the comment type 1.
+ $comment1 = Comment::create([
+ 'comment_type' => 'comment',
+ 'langcode' => 'und',
+ 'entity_id' => $node->id(),
+ 'entity_type' => $node->getEntityTypeId(),
+ 'uid' => $user1->id(),
+ 'subject' => $this->randomMachineName(),
+ 'status' => 1,
+ 'field_name' => 'comment',
+ 'comment_body' => [
+ 'summary' => '',
+ 'value' => $this->randomMachineName(),
+ 'format' => 'basic_html',
+ ],
+ ]);
+ $comment1->save($comment1);
+ $notify_hash = \Drupal::csrfToken()->get('127.0.0.1' . $comment1->id());
+ comment_notify_add_notification($comment1->id(), TRUE, $notify_hash, 1);
+
+ // Comment of the comment type 2.
+ $comment2 = Comment::create([
+ 'comment_type' => 'comment_type_2',
+ 'langcode' => 'und',
+ 'entity_id' => $node->id(),
+ 'entity_type' => $node->getEntityTypeId(),
+ 'uid' => $user2->id(),
+ 'subject' => $this->randomMachineName(),
+ 'status' => 1,
+ 'field_name' => 'field_comment',
+ 'comment_body' => [
+ 'summary' => '',
+ 'value' => $this->randomMachineName(),
+ 'format' => 'basic_html',
+ ],
+ ]);
+ $comment2->save($comment2);
+ $notify_hash = \Drupal::csrfToken()->get('127.0.0.1' . $comment1->id());
+ comment_notify_add_notification($comment2->id(), TRUE, $notify_hash, 1);
+
+ $this->assertEmpty($this->getMails(), 'No notifications has been sent.');
+
+ // User 1 reply user 2 in comment type 2, user 2 should get a notification.
+ $this->drupalLogin($user1);
+ $this->postComment(
+ "/comment/reply/node/{$node->id()}/field_comment/{$comment2->id()}",
+ $this->randomMachineName(),
+ $this->randomMachineName(),
+ ['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_COMMENT]
+ );
+ $this->drupalLogout();
+ $this->assertMail('to', $user2->getEmail(), t('Message was sent to the user2 user.'));
+ $this->container->get('state')->set('system.test_mail_collector', []);
+
+ // User 2 reply user 1 in comment type 1, user 1 should get a notification.
+ $this->drupalLogin($user2);
+ $this->postComment(
+ "/comment/reply/node/{$node->id()}/comment/{$comment1->id()}",
+ $this->randomMachineName(),
+ $this->randomMachineName(),
+ ['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_COMMENT]
+ );
+ $this->drupalLogout();
+ $this->assertMail('to', $user1->getEmail(), t('Message was sent to the user1 user.'));
+ $this->container->get('state')->set('system.test_mail_collector', []);
+
+ }
+
}