diff --git a/comment_notify.inc b/comment_notify.inc index 61f08ecf15d9e7d002900d82e3b68839edb4c1e2..59f5f05e881cfbd9a118411cd4caaad5fd9919b1 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 3d7ea76d4c87773cbd032a5f1017ffb3b6b91db9..eebacd21c3cbc11c9a3ca3ee2d5ac62d8067a1dd 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 dfd7bd22b8544f5628d4717cca509b6cda59518a..833f7fdaedd12c3dff177aeea4326afce3839fcb 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', []); + + } + }