diff --git a/comment_notify.module b/comment_notify.module index b47bf77fa67b67a3b89d87da383be51787470292..7ed8c07d3f576c180f8634120a31bd9283399b24 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 07247cb04a4f70a6f4118164dbdfba13775b4772..245e1c6e1df019600e228a9b06b29183fc49fdd1 100644 --- a/tests/src/Functional/CommentNotifyUserPreferencesTest.php +++ b/tests/src/Functional/CommentNotifyUserPreferencesTest.php @@ -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() {