Commit 707b9646 authored by gnuget's avatar gnuget Committed by greggles

Issue #2879914 by gnuget, kmonahan: Notifications not sent when user chooses...

Issue #2879914 by gnuget, kmonahan: Notifications not sent when user chooses "Replies to my comment"
parent 01751b11
......@@ -360,9 +360,11 @@ function _comment_notify_mailalert(CommentInterface $comment) {
// from his or her user account instead of trusting $comment->mail.
$recipient_user = $alert->getOwner();
$mail = !empty($recipient_user->getEmail()) ? $recipient_user->getEmail() : $alert->getAuthorEmail();
// Trim the trailing / off the thread, if present.
$alert_thread = rtrim((string) $alert->getThread(), '/');
$relevant_thread = Unicode::substr($thread, 0, Unicode::strlen($alert->getThread()) -1);
if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread . '/', $alert->getThread()) != 0) {
$relevant_thread = Unicode::substr($thread, 0, Unicode::strlen($alert_thread));
if ($alert->notify == COMMENT_NOTIFY_COMMENT && strcmp($relevant_thread, $alert_thread) != 0) {
continue;
}
......
<?php
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\comment\CommentInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\NodeInterface;
/**
* Comment notifications tests as anonymous user.
*
* @group comment_notify
*/
class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
// Enable comment notify on this node and allow anonymous information in
// comments.
$this->drupalCreateContentType([
'type' => 'article',
]);
$this->addDefaultCommentField('node', 'article');
$comment_field = FieldConfig::loadByName('node', 'article', 'comment');
$comment_field->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
$comment_field->save();
// Allow anonymous users to post comments and get notifications.
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'access comments',
'access content',
'post comments',
'skip comment approval',
'subscribe to comments',
]
);
$this->drupalLogout();
}
/**
* Tests that the mail must be required if an anonymous user wants
* to receive notifications.
*/
public function testMail() {
/** @var \Drupal\node\Entity\Node $node */
$node = $this->drupalCreateNode(['type' => 'article']);
$subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE];
$this->drupalGet($node->toUrl());
$this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe);
$this->assertTrue($this->getSession()->getPage()->hasContent(t('If you want to subscribe to comments you must supply a valid e-mail address.')));
}
/**
* Tests the "All comments" notification option used by an anonymous user.
*/
public function testAnonymousAllCommentsTest() {
/** @var \Drupal\node\Entity\Node $node */
$node = $this->drupalCreateNode(['type' => 'article']);
$subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE];
$contact = ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()];
$comment = $this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
$subscribe,
$contact
);
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($comment['id']);
$this->assertEquals($result, $subscribe['notify_type'], 'All Comments option was saved properly.');
// Tests that the user receives the email if a new comment is posted.
$this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$this->assertMail('to', $contact['mail'], t('Message was sent to the anonymous user.'));
// Test the unsubscribe link.
$mails = $this->getMails();
preg_match("/\/comment_notify\/disable\/.+/", $mails[0]['body'], $output);
$this->drupalGet($output[0]);
$this->assertTrue($this->getSession()->getPage()->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
// Confirm that the notification has been disabled.
$result = comment_notify_get_notification_type($comment['id']);
$this->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The notification has been disabled');
// Tests that the user stopped receiving notifications.
$this->container->get('state')->set('system.test_mail_collector', []);
$this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
$this->assertEmpty($captured_emails, 'No notifications has been sent.');
}
/**
* Tests the "Replies to my comment" option used by anonymous user.
*/
public function testAnonymousRepliesTest() {
// Create a node with comments allowed.
$node = $this->drupalCreateNode(['type' => 'article']);
$subscribe = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_COMMENT];
$contact = ['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()];
$comment = $this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe, $contact);
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($comment['id']);
$this->assertEquals($result, $subscribe['notify_type'], 'Replies to my comment option was saved properly.');
// Tests that the user doesn't receive a mail if a new comment is posted.
$this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
$this->assertEmpty($captured_emails, 'No notifications has been sent.');
// Tests that the user receives a mail if a reply has been posted.
$this->postComment(
"/comment/reply/node/{$node->id()}/comment/{$comment['id']}",
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$this->assertMail('to', $contact['mail'], t('Message was sent to the anonymous user.'));
// Test the unsubscribe link.
$mails = $this->getMails();
preg_match("/\/comment_notify\/disable\/.+/", $mails[0]['body'], $output);
$this->drupalGet($output[0]);
$this->assertTrue($this->getSession()->getPage()->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
// Confirm that the notification has been disabled.
$result = comment_notify_get_notification_type($comment['id']);
$this->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The notification has been disabled');
// Tests that the user stopped receiving notifications.
$this->container->get('state')->set('system.test_mail_collector', []);
$this->postComment(
"/comment/reply/node/{$node->id()}/comment/{$comment['id']}",
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE],
['name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress()]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
$this->assertEmpty($captured_emails, 'No notifications has been sent.');
// @TODO Move this test to the settings form.
// \Drupal::configFactory()->getEditable('comment_notify.settings')->set('available_alerts', [1 => FALSE, 2 => TRUE])->save();
// $subscribe_0 = ['notify' => TRUE];
// $contact_0 = ['mail' => $this->getRandomEmailAddress()];
// $anonymous_comment_0 = $this->postComment($node->toUrl()->toString(), $this->randomMachineName(), $this->randomMachineName(), $subscribe_0, $contact_0);
//
// // Confirm that the notification is saved.
// $result = comment_notify_get_notification_type($anonymous_comment_0['id']);
// $this->assertEquals($result, 2, 'Notify selection option 0 is saved properly.');
}
}
<?php
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\comment\CommentInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\node\NodeInterface;
/**
* Comment notify general tests.
*
* @group comment_notify
*/
class CommentNotifyTest extends CommentNotifyTestBase {
/**
* Test various behaviors for anonymous users.
*/
public function testCommentNotifyAnonymousUserFunctionalTest() {
$this->drupalLogin($this->adminUser);
// Enable comment notify on this node and allow anonymous information in
// comments.
$this->drupalCreateContentType([
'type' => 'article',
]);
$this->addDefaultCommentField('node', 'article');
$comment_field = FieldConfig::loadByName('node', 'article', 'comment');
$comment_field->setSetting('anonymous', CommentInterface::ANONYMOUS_MAY_CONTACT);
$comment_field->save();
// Create a node with comments allowed.
$node = $this->drupalCreateNode(array('type' => 'article', 'promote' => NodeInterface::PROMOTED));
// Allow anonymous users to post comments and get notifications.
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'access comments',
'access content',
'post comments',
'skip comment approval',
'subscribe to comments',
]
);
$this->drupalLogout();
// Notify type 1 - All comments on the node.
// First confirm that we properly require an e-mail address.
$subscribe_1 = ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_NODE];
$this->drupalGet($node->toUrl());
$this->postCommentNotifyComment($node, $this->randomMachineName(), $this->randomMachineName(), $subscribe_1);
$this->assertTrue($this->getSession()->getPage()->hasContent(t('If you want to subscribe to comments you must supply a valid e-mail address.')));
// Try again with an e-mail address.
$contact_1 = array('name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress());
$anonymous_comment_1 = $this->postCommentNotifyComment($node, $this->randomMachineName(), $this->randomMachineName(), $subscribe_1, $contact_1);
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($anonymous_comment_1['id']);
$this->assertEquals($result, $subscribe_1['notify_type'], 'Notify selection option 1 is saved properly.');
// Notify type 2 - replies to a comment.
$subscribe_2 = array('notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_COMMENT);
$contact_2 = array('name' => $this->randomMachineName(), 'mail' => $this->getRandomEmailAddress());
$anonymous_comment_2 = $this->postCommentNotifyComment($node, $this->randomMachineName(), $this->randomMachineName(), $subscribe_2, $contact_2);
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($anonymous_comment_2['id']);
$this->assertEquals($result, $subscribe_2['notify_type'], 'Notify selection option 2 is saved properly.');
// Confirm that the original subscriber with all comments on this node got
// their mail.
$this->assertMail('to', $contact_1['mail'], t('Message was sent to the proper anonymous user.'));
// Test the unsubscribe link.
$mails = $this->getMails();
preg_match("/\/comment_notify\/disable\/.+/", $mails[0]['body'], $output);
$this->drupalGet($output[0]);
$this->assertTrue($this->getSession()->getPage()->hasContent("Your comment follow-up notification for this post was disabled. Thanks."));
// Notify type 0 (i.e. only one mode is enabled).
\Drupal::configFactory()->getEditable('comment_notify.settings')->set('available_alerts', [1 => FALSE, 2 => TRUE])->save();
$subscribe_0 = array('notify' => TRUE);
$contact_0 = array('mail' => $this->getRandomEmailAddress());
$anonymous_comment_0 = $this->postCommentNotifyComment($node, $this->randomMachineName(), $this->randomMachineName(), $subscribe_0, $contact_0);
// Confirm that the notification is saved.
$result = comment_notify_get_notification_type($anonymous_comment_0['id']);
$this->assertEquals($result, 2, 'Notify selection option 0 is saved properly.');
}
}
......@@ -68,7 +68,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
* @return array|bool
* return an array with the comment or false if the post comment fails.
*/
protected function postCommentNotifyComment(NodeInterface $node, $subject, $comment, $notify, $contact = NULL) {
protected function postComment($url, $subject, $comment, $notify, $contact = NULL) {
$edit = [];
$edit['subject[0][value]'] = $subject;
$edit['comment_body[0][value]'] = $comment;
......@@ -81,7 +81,7 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
$edit += $contact;
}
$this->drupalPostForm($node->toUrl()->toString(), $edit, t('Save'));
$this->drupalPostForm($url, $edit, t('Save'));
$match = array();
// Get comment ID.
......
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