summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorgnuget2019-01-30 05:32:22 (GMT)
committerDavid Valdez2019-01-30 05:32:22 (GMT)
commit91b846c3df740714def86ea088c9894eeb86b3ab (patch)
tree4fa14a062aac133afc0598c28ef61daa091fe08e
parent96ecd28cd0f674291e93102661149520e7b45259 (diff)
Issue #2997418 by wturrell, gnuget: User without Subscribe to comment notifications gets setting in profile
-rw-r--r--comment_notify.module47
-rw-r--r--tests/src/Functional/CommentNotifyUserPreferencesTest.php83
2 files changed, 111 insertions, 19 deletions
diff --git a/comment_notify.module b/comment_notify.module
index b47bf77..7ed8c07 100644
--- a/comment_notify.module
+++ b/comment_notify.module
@@ -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';
}
diff --git a/tests/src/Functional/CommentNotifyUserPreferencesTest.php b/tests/src/Functional/CommentNotifyUserPreferencesTest.php
index 07247cb..245e1c6 100644
--- a/tests/src/Functional/CommentNotifyUserPreferencesTest.php
+++ b/tests/src/Functional/CommentNotifyUserPreferencesTest.php
@@ -33,9 +33,88 @@ class CommentNotifyUserPreferencesTest extends CommentNotifyTestBase {
}
/**
+ * 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() {