Commit a2f7604d authored by gnuget's avatar gnuget Committed by gnuget

Issue #3020673 by gnuget, wturrell: False positive notifications with multiple...

Issue #3020673 by gnuget, wturrell: False positive notifications with multiple comment types per node
parent d50f43cf
......@@ -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();
......
......@@ -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
......
......@@ -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', []);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment