summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgnuget2019-02-27 22:16:13 (GMT)
committerDavid Valdez2019-02-27 22:16:13 (GMT)
commit56a37b069065a428b0f0c1391b3faf3a2a3d6e80 (patch)
treed5c37df718b6b376fca8461d86bb61000c6f2c15
parentb2a8e8e09c0a47cca4ccbdd0e48ba7a46964900a (diff)
Issue #3036258 by gnuget: Fatal error when a non-existent mail is unsubscribed
-rw-r--r--comment_notify.inc13
-rw-r--r--src/Form/CommentNotifySettings.php9
-rw-r--r--src/Form/CommentNotifyUnsubscribe.php32
-rw-r--r--tests/src/Functional/CommentNotifyConfigPageTest.php111
4 files changed, 156 insertions, 9 deletions
diff --git a/comment_notify.inc b/comment_notify.inc
index 3ad0f35..105f8e6 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 7bc1307..cd811e5 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:<br/>@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:<br/>@anonymous_problems', ['@anonymous_problems' => $anonymous_problems]));
}
}
diff --git a/src/Form/CommentNotifyUnsubscribe.php b/src/Form/CommentNotifyUnsubscribe.php
index 473d9e9..90c3875 100644
--- a/src/Form/CommentNotifyUnsubscribe.php
+++ b/src/Form/CommentNotifyUnsubscribe.php
@@ -4,6 +4,8 @@ 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.
@@ -11,6 +13,32 @@ use Drupal\Core\Form\FormStateInterface;
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}
*/
public function getFormId() {
@@ -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 31ab413..0fdeb3f 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');
+ }
+
+
}