diff --git a/comment_notify.inc b/comment_notify.inc
index 3ad0f35c791ebb5594c49c4ff74474d76b80e8e9..105f8e66a7e5b12bb55fbb418618376f5560f5fc 100644
--- a/comment_notify.inc
+++ b/comment_notify.inc
@@ -298,17 +298,22 @@ function comment_notify_mark_comment_as_notified($comment) {
* TRUE if the comment was unsubscribed correctly.
*/
function comment_notify_unsubscribe_by_email($mail) {
- $update_query = db_update('comment_notify');
- $update_query->fields(['notify' => 0]);
-
$comment_query = \Drupal::entityQuery('comment');
-
if ($user = user_load_by_mail($mail)) {
$comment_query->condition('uid', $user->id());
}
else {
$comment_query->condition('mail', $mail);
}
+
+ $comments = $comment_query->execute();
+
+ if (empty($comments)) {
+ return FALSE;
+ }
+
+ $update_query = db_update('comment_notify');
+ $update_query->fields(['notify' => 0]);
$update_query->condition('cid', $comment_query->execute(), 'IN');
return (bool) $update_query->execute();
diff --git a/src/Form/CommentNotifySettings.php b/src/Form/CommentNotifySettings.php
index 7bc130721ccbac927b8c92c7dfd6f8caf423af7f..cd811e585cdc3f03867333adc83b7cbc5e2b27a8 100644
--- a/src/Form/CommentNotifySettings.php
+++ b/src/Form/CommentNotifySettings.php
@@ -8,6 +8,7 @@ use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
+use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Render\Markup;
use Drupal\field\Entity\FieldConfig;
use Drupal\Core\Config\ConfigFactoryInterface;
@@ -55,7 +56,8 @@ class CommentNotifySettings extends ConfigFormBase {
return new static(
$container->get('config.factory'),
$container->get('entity_field.manager'),
- $container->get('module_handler')
+ $container->get('module_handler'),
+ $container->get('messenger')
);
}
@@ -69,10 +71,11 @@ class CommentNotifySettings extends ConfigFormBase {
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
*/
- public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManager $field_manager, ModuleHandlerInterface $module_handler) {
+ public function __construct(ConfigFactoryInterface $config_factory, EntityFieldManager $field_manager, ModuleHandlerInterface $module_handler, MessengerInterface $messenger) {
parent::__construct($config_factory);
$this->fieldManager = $field_manager;
$this->moduleHandler = $module_handler;
+ $this->messenger = $messenger;
}
/**
@@ -125,7 +128,7 @@ class CommentNotifySettings extends ConfigFormBase {
}
if (!empty($anonymous_problems)) {
- drupal_set_message($this->t('Anonymous commenters have the permission to subscribe to comments but they need to be allowed to:
@anonymous_problems', ['@anonymous_problems' => $anonymous_problems]), 'warning', FALSE);
+ $this->messenger->addWarning($this->t('Anonymous commenters have the permission to subscribe to comments but they need to be allowed to:
@anonymous_problems', ['@anonymous_problems' => $anonymous_problems]));
}
}
diff --git a/src/Form/CommentNotifyUnsubscribe.php b/src/Form/CommentNotifyUnsubscribe.php
index 473d9e9d9d79b0d97a9558386836219fabb43748..90c387581da07e1017710a29ed5491224901c5f1 100644
--- a/src/Form/CommentNotifyUnsubscribe.php
+++ b/src/Form/CommentNotifyUnsubscribe.php
@@ -4,12 +4,40 @@ namespace Drupal\comment_notify\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
+use Drupal\Core\Messenger\MessengerInterface;
+use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Unsubscribe form for Comment Notify.
*/
class CommentNotifyUnsubscribe extends FormBase {
+ /**
+ * Messenger.
+ *
+ * @var \Drupal\Core\Messenger\MessengerInterface
+ */
+ protected $messenger;
+
+ /**
+ * {@inheritdoc}
+ */
+ public static function create(ContainerInterface $container) {
+ return new static(
+ $container->get('messenger')
+ );
+ }
+
+ /**
+ * CommentNotifyUnsubscribe constructor.
+ *
+ * @param \Drupal\Core\Messenger\MessengerInterface $messenger
+ * Messenger service.
+ */
+ public function __construct(MessengerInterface $messenger) {
+ $this->messenger = $messenger;
+ }
+
/**
* {@inheritdoc}
*/
@@ -45,10 +73,10 @@ class CommentNotifyUnsubscribe extends FormBase {
$comments = comment_notify_unsubscribe_by_email($email);
// Update the admin about the state of the subscription.
if ($comments == 0) {
- drupal_set_message($this->t("There were no active comment notifications for that email."));
+ $this->messenger->addWarning($this->t("There were no active comment notifications for that email."));
}
else {
- drupal_set_message($this->formatPlural($comments, "Email unsubscribed from 1 comment notification.", "Email unsubscribed from @count comment notifications."));
+ $this->messenger->addStatus($this->t("Email unsubscribed from all the comment notifications."));
}
}
diff --git a/tests/src/Functional/CommentNotifyConfigPageTest.php b/tests/src/Functional/CommentNotifyConfigPageTest.php
index 31ab413451ae1a77ed3ed8bba715fdc1fe1d805b..0fdeb3f796ffa0aacc67423f8ccef2590647e10c 100644
--- a/tests/src/Functional/CommentNotifyConfigPageTest.php
+++ b/tests/src/Functional/CommentNotifyConfigPageTest.php
@@ -187,4 +187,115 @@ class CommentNotifyConfigPageTest extends CommentNotifyTestBase {
$this->assertSession()->linkByHrefExists('/admin/structure/types/manage/article/fields/node.article.comment');
}
+ /**
+ * Tests the Unsubscribe page.
+ */
+ public function testUnsubscribePage() {
+ /** @var \Drupal\node\Entity\Node $node */
+ $node = $this->drupalCreateNode(['type' => 'article']);
+
+ // 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',
+ ]
+ );
+
+ // Try to unsubscribe an email which haven't notifications.
+ $this->drupalLogin($this->adminUser);
+ $this->drupalGet("admin/config/people/comment_notify/unsubscribe");
+ $this->getSession()->getPage()->fillField('Email to unsubscribe', $this->getRandomEmailAddress());
+ $this->submitForm([], 'Unsubscribe this e-mail');
+ $this->assertSession()->responseContains('There were no active comment notifications for that email.');
+ $this->drupalLogout();
+
+ // Unsubscribe an email that belongs to an anonymous user.
+ $anonymous_mail = $this->getRandomEmailAddress();
+ $comment = $this->postComment(
+ $node->toUrl()->toString(),
+ $this->randomMachineName(),
+ $this->randomMachineName(),
+ ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
+ ['name' => $this->randomMachineName(), 'mail' => $anonymous_mail]
+ );
+ $result = comment_notify_get_notification_type($comment['id']);
+ $this->assertEquals($result, COMMENT_NOTIFY_ENTITY, 'The notification was added as expected');
+
+ $this->drupalLogin($this->adminUser);
+ $this->drupalGet("admin/config/people/comment_notify/unsubscribe");
+ $this->getSession()->getPage()->fillField('Email to unsubscribe', $anonymous_mail);
+ $this->submitForm([], 'Unsubscribe this e-mail');
+ $this->assertSession()->responseContains('Email unsubscribed from all the comment notifications.');
+ $this->drupalLogout();
+ $result = comment_notify_get_notification_type($comment['id']);
+ $this->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');
+
+
+ // Unsubscribe an email that have several notifications.
+ $anonymous_mail2 = $this->getRandomEmailAddress();
+ $comment1 = $this->postComment(
+ $node->toUrl()->toString(),
+ $this->randomMachineName(),
+ $this->randomMachineName(),
+ ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_ENTITY],
+ ['name' => $this->randomMachineName(), 'mail' => $anonymous_mail2]
+ );
+ $result_comment1 = comment_notify_get_notification_type($comment1['id']);
+ $this->assertEquals($result_comment1, COMMENT_NOTIFY_ENTITY, 'The notification was added as expected');
+ $comment2 = $this->postComment(
+ $node->toUrl()->toString(),
+ $this->randomMachineName(),
+ $this->randomMachineName(),
+ ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_COMMENT],
+ ['name' => $this->randomMachineName(), 'mail' => $anonymous_mail2]
+ );
+ $result_comment2 = comment_notify_get_notification_type($comment2['id']);
+ $this->assertEquals($result_comment2, COMMENT_NOTIFY_COMMENT, 'The notification was added as expected');
+
+ $this->drupalLogin($this->adminUser);
+ $this->drupalGet("admin/config/people/comment_notify/unsubscribe");
+ $this->getSession()->getPage()->fillField('Email to unsubscribe', $anonymous_mail2);
+ $this->submitForm([], 'Unsubscribe this e-mail');
+ $this->assertSession()->responseContains('Email unsubscribed from all the comment notifications.');
+ $this->drupalLogout();
+
+ $result_comment1 = comment_notify_get_notification_type($comment1['id']);
+ $result_comment2 = comment_notify_get_notification_type($comment2['id']);
+ $this->assertEquals($result_comment1, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');
+ $this->assertEquals($result_comment2, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');
+
+ // Unsubscribe an email of a registered user.
+ $user = $this->drupalCreateUser([
+ 'access comments',
+ 'access content',
+ 'edit own comments',
+ 'post comments',
+ 'skip comment approval',
+ 'subscribe to comments',
+ ]);
+ $this->drupalLogin($user);
+ $comment = $this->postComment(
+ $node->toUrl()->toString(),
+ $this->randomMachineName(),
+ $this->randomMachineName(),
+ ['notify' => TRUE, 'notify_type' => COMMENT_NOTIFY_COMMENT]
+ );
+ $this->drupalLogout();
+
+ $this->drupalLogin($this->adminUser);
+ $this->drupalGet("admin/config/people/comment_notify/unsubscribe");
+ $this->getSession()->getPage()->fillField('Email to unsubscribe', $user->getEmail());
+ $this->submitForm([], 'Unsubscribe this e-mail');
+ $this->assertSession()->responseContains('Email unsubscribed from all the comment notifications.');
+ $this->drupalLogout();
+ $result = comment_notify_get_notification_type($comment['id']);
+ $this->assertEquals($result, COMMENT_NOTIFY_DISABLED, 'The mail was unsubscribed as expected');
+ }
+
+
}