Commit 96ecd28c authored by gnuget's avatar gnuget Committed by gnuget

Issue #3028241 by gnuget: Comment follow-up notification settings in the user...

Issue #3028241 by gnuget: Comment follow-up notification settings in the user page aren't being saved
parent 6748381c
......@@ -246,7 +246,7 @@ function _comment_notify_submit_user_form(array &$form, FormStateInterface $form
/** @var \Drupal\user\UserInterface $user */
$user = $form_state->getFormObject()->getEntity();
if (!$user->isAnonymous() && is_integer($form_state->getValue('node_notify')) && is_integer($form_state->getValue('comment_notify'))) {
if (!$user->isAnonymous() && is_numeric($form_state->getValue('node_notify')) && is_numeric($form_state->getValue('comment_notify'))) {
// Save the values to {comment_notify_user_settings}.
comment_notify_set_user_notification_setting($user->id(), $form_state->getValue('node_notify'), $form_state->getValue('comment_notify'));
}
......
......@@ -2,10 +2,7 @@
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.
......@@ -17,18 +14,6 @@ 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,
......@@ -40,7 +25,6 @@ class CommentNotifyAnonymousTest extends CommentNotifyTestBase {
'subscribe to comments',
]
);
$this->drupalLogout();
}
/**
......
......@@ -9,25 +9,11 @@ namespace Drupal\Tests\comment_notify\Functional;
*/
class CommentNotifyConfigPageTest extends CommentNotifyTestBase {
/**
* Test that the config page is working.
*/
protected function setUp() {
parent::setUp();
$this->drupalLogin($this->adminUser);
// Enable comment notify on this node.
$this->drupalCreateContentType([
'type' => 'article',
]);
$this->addDefaultCommentField('node', 'article');
}
/**
* Test to all the options are saved correctly.
*/
public function testConfigPage() {
$this->drupalLogin($this->adminUser);
$this->drupalGet("admin/config/people/comment_notify");
// Test the default values are working.
......@@ -138,6 +124,7 @@ class CommentNotifyConfigPageTest extends CommentNotifyTestBase {
$this->drupalGet("admin/config/people/comment_notify");
$field = $this->getSession()->getPage()->findField('Default mail text for sending out the notifications to node authors');
$this->assertTrue($field->getValue() == 'Hello');
$this->drupalLogout();
}
......
......@@ -3,8 +3,9 @@
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\comment\CommentInterface;
use Drupal\Core\Test\AssertMailTrait;
use Drupal\node\NodeInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\BrowserTestBase;
/**
......@@ -48,6 +49,16 @@ abstract class CommentNotifyTestBase extends BrowserTestBase {
'administer comments',
]
);
// 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();
}
/**
......
<?php
namespace Drupal\Tests\comment_notify\Functional;
use Drupal\Core\Session\AccountInterface;
/**
* Tests the Comment Notify users preferences.
*
* @group comment_notify
*/
class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
/**
* Authenticated User.
*
* @var \Drupal\user\Entity\User $authenticatedUser
*/
protected $authenticatedUser;
/**
* Permissions required by the module.
*/
protected $permissions = [
'post comments',
'skip comment approval',
'subscribe to comments',
];
protected function setUp() {
parent::setUp();
$this->authenticatedUser = $this->drupalCreateUser($this->permissions);
}
/**
* Tests the Comment follow-up notification settings.
*
* @todo Write a test that make sure that the "Comment follow-up" notification settings box doesn't show up if the user hasn't the 'subscribe to comments' permission.
*/
public function testUserCommentPreferences() {
// Tests that the settings are present in the user profile.
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->drupalLogout();
}
public function testsCommentFollowUpsNotifications() {
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
// Test the "No notifications" option.
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_DISABLED);
$this->getSession()->getPage()->pressButton(t('Save'));
$node = $this->drupalCreateNode(['type' => 'article']);
$this->drupalGet($node->toUrl()->toString());
$this->assertTrue($this->getSession()->getPage()->hasUncheckedField('Notify me when new comments are posted'));
// Test the "All comments" option.
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_NODE);
$this->getSession()->getPage()->pressButton(t('Save'));
$this->drupalGet($node->toUrl()->toString());
$this->assertTrue($this->getSession()->getPage()->hascheckedField('Notify me when new comments are posted'));
$this->assertTrue($this->getSession()->getPage()->hascheckedField('All comments'));
// Tests the "Replies to my comments" option.
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->getSession()->getPage()->selectFieldOption('comment_notify', COMMENT_NOTIFY_COMMENT);
$this->getSession()->getPage()->pressButton(t('Save'));
$this->drupalGet($node->toUrl()->toString());
$this->assertTrue($this->getSession()->getPage()->hascheckedField('Notify me when new comments are posted'));
$this->assertTrue($this->getSession()->getPage()->hascheckedField('Replies to my comment'));
$this->drupalLogout();
}
/**
* Tests the Content follow-up notification settings.
*/
public function testUserNodePreferences() {
// Tests that the option not present in the user profile unless the user has
// the 'administer nodes' permission.
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// Tests that the option is present in the user profile if the user has the
// 'administer nodes' permission.
$permissions = array_merge($this->permissions,['administer nodes']);
$this->authenticatedUser = $this->createUser($permissions);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->getSession()->getPage()->checkField('Receive content follow-up notification e-mails');
$this->getSession()->getPage()->pressButton(t('Save'));
$node_notify_preference = comment_notify_get_user_node_notify_preference($this->authenticatedUser->id());
$this->assertEquals(COMMENT_NOTIFY_NODE, $node_notify_preference);
$this->drupalLogout();
// Tests that the notification is sent when the content created by the user
// receives a new comment.
$node = $this->drupalCreateNode(
[
'type' => 'article',
'uid' => $this->authenticatedUser,
]
);
// Write a comment as anonymous user.
user_role_grant_permissions(
AccountInterface::ANONYMOUS_ROLE,
[
'access comments',
'access content',
'post comments',
'skip comment approval',
'subscribe to comments',
]
);
$this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE]
);
// Test that the notification was sent.
$this->assertMail('to', $this->authenticatedUser->getEmail(), t('Message was sent to the user.'));
// Tests that the notification is not sent when the user hasn't selected
// the content notifications setting.
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->getSession()->getPage()->uncheckField('Receive content follow-up notification e-mails');
$this->getSession()->getPage()->pressButton(t('Save'));
$this->assertTrue($this->getSession()->getPage()->hasUncheckedField('Receive content follow-up notification e-mails'));
$this->drupalLogout();
drupal_static_reset('comment_notify_get_user_notification_setting');
$node_notify_preference = comment_notify_get_user_node_notify_preference($this->authenticatedUser->id());
$this->assertEquals(COMMENT_NOTIFY_DISABLED, $node_notify_preference);
$this->container->get('state')->set('system.test_mail_collector', []);
$node = $this->drupalCreateNode(
[
'type' => 'article',
'uid' => $this->authenticatedUser,
]
);
$this->postComment(
$node->toUrl()->toString(),
$this->randomMachineName(),
$this->randomMachineName(),
['notify' => FALSE, 'notify_type' => COMMENT_NOTIFY_NODE]
);
$captured_emails = $this->container->get('state')->get('system.test_mail_collector');
$this->assertEmpty($captured_emails, 'No notifications has been sent.');
}
}
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