Commit 91b846c3 authored by gnuget's avatar gnuget Committed by gnuget

Issue #2997418 by wturrell, gnuget: User without Subscribe to comment...

Issue #2997418 by wturrell, gnuget: User without Subscribe to comment notifications gets setting in profile
parent 96ecd28c
......@@ -193,13 +193,6 @@ function comment_notify_form_user_form_alter(&$form, FormStateInterface &$form_s
$user = $form_state->getFormObject()->getEntity();
$notify_settings = $user->id() && comment_notify_get_user_notification_setting($user->id()) ? comment_notify_get_user_notification_setting($user->id()) : comment_notify_get_default_notification_setting();
$form['comment_notify_settings'] = array(
'#type' => 'details',
'#title' => t('Comment follow-up notification settings'),
'#weight' => 4,
'#open' => TRUE,
);
// Only show the node followup UI if the user has permission to create nodes.
$nodes = FALSE;
foreach (node_type_get_names() as $type => $name) {
......@@ -209,6 +202,19 @@ function comment_notify_form_user_form_alter(&$form, FormStateInterface &$form_s
}
}
// If the user cannot create nodes nor has the 'subscribe to comments'
// permission then there is no need to alter the user_form.
if ((!\Drupal::currentUser()->hasPermission('administer nodes') && $nodes === FALSE) && ($user->hasPermission( 'subscribe to comments') === FALSE)) {
return;
}
$form['comment_notify_settings'] = array(
'#type' => 'details',
'#title' => t('Comment follow-up notification settings'),
'#weight' => 4,
'#open' => TRUE,
);
if (\Drupal::currentUser()->hasPermission('administer nodes') || $nodes) {
$form['comment_notify_settings']['node_notify'] = array(
'#type' => 'checkbox',
......@@ -224,16 +230,23 @@ function comment_notify_form_user_form_alter(&$form, FormStateInterface &$form_s
);
}
$available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
$available_options += _comment_notify_options();
$form['comment_notify_settings']['comment_notify'] = array(
'#type' => 'select',
'#title' => t('Receive comment follow-up notification e-mails'),
'#default_value' => isset ($notify_settings->comment_notify) ? $notify_settings->comment_notify : NULL,
'#options' => $available_options,
'#description' => t("Check this box to receive e-mail notification for follow-up comments to comments you posted. You can later disable this on a post-by-post basis... so if you leave this to YES, you can still disable follow-up notifications for comments you don't want follow-up mails anymore - i.e. for very popular posts.")
);
if ($user->hasPermission('subscribe to comments')) {
$available_options[COMMENT_NOTIFY_DISABLED] = t('No notifications');
$available_options += _comment_notify_options();
$form['comment_notify_settings']['comment_notify'] = array(
'#type' => 'select',
'#title' => t('Receive comment follow-up notification e-mails'),
'#default_value' => isset ($notify_settings->comment_notify) ? $notify_settings->comment_notify : NULL,
'#options' => $available_options,
'#description' => t("Check this box to receive e-mail notification for follow-up comments to comments you posted. You can later disable this on a post-by-post basis... so if you leave this to YES, you can still disable follow-up notifications for comments you don't want follow-up mails anymore - i.e. for very popular posts.")
);
}
else {
$form['comment_notify_settings']['comment_notify'] = array(
'#type' => 'hidden',
'#value' => COMMENT_NOTIFY_DISABLED,
);
}
$form['actions']['submit']['#submit'][] = '_comment_notify_submit_user_form';
}
......
......@@ -32,10 +32,89 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
$this->authenticatedUser = $this->drupalCreateUser($this->permissions);
}
/**
* The User's comment notify box should display different options depending
* the permissions of the user.
*/
public function testUserCommentNotifyBox() {
// The user hasn't the subscribe to comments permission nor the 'administer
// nodes' permission, nor has permission to create content, so it shouldn't
// see any Comment notify settings in the profile page.
$this->authenticatedUser = $this->drupalCreateUser(
[
'post comments',
'skip comment approval',
]
);
$this->drupalLogin($this->authenticatedUser);
$this->drupalGet($this->authenticatedUser->toUrl('edit-form')->toString());
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Comment follow-up notification settings')));
$this->drupalLogout();
// The user only has the 'subscribe to comments' permission, he should be
// able to see the Comment Notify settings box but the 'Receive content
// follow-up notification e-mails' checkbox shouldn't appear.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'subscribe to comments',
]);
$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->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertFalse($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// The user only has the 'administer nodes' permission, he should be
// able to see the Comment Notify settings box but the 'Comment follow-up
// notification settings' dropdown shouldn't appear.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'administer nodes',
]);
$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->assertFalse($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// The user only hasn't the 'administer nodes' permission nor the 'subscribe
// to comments' permission but he can create nodes of the type article, so
// he should be able to see the Comment Notify settings box with the
// 'Receive content follow-up notification e-mails' checkbox.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'create article content',
]);
$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->assertFalse($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
// The has all the permissions, so he should be able to see all the Comment
// notify settings.
$this->authenticatedUser = $this->drupalCreateUser([
'post comments',
'skip comment approval',
'create article content',
'subscribe to comments',
]);
$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->assertTrue($this->getSession()->getPage()->hasContent(t('Receive comment follow-up notification e-mails')));
$this->assertTrue($this->getSession()->getPage()->hasContent(t('Receive content follow-up notification e-mails')));
$this->drupalLogout();
}
/**
* 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() {
......
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