Commit 56a37b06 authored by gnuget's avatar gnuget Committed by gnuget

Issue #3036258 by gnuget: Fatal error when a non-existent mail is unsubscribed

parent b2a8e8e0
......@@ -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();
......
......@@ -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]));
}
}
......
......@@ -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."));
}
}
......
......@@ -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');
}
}
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