diff --git a/core/modules/comment/src/CommentForm.php b/core/modules/comment/src/CommentForm.php index a9d742bb90bb37ab48a8562fcba0bd84e52d8e81..8766e27e19f639b11306da85e4503c46385fa5b6 100644 --- a/core/modules/comment/src/CommentForm.php +++ b/core/modules/comment/src/CommentForm.php @@ -169,7 +169,7 @@ public function form(array $form, FormStateInterface $form_state) { '#maxlength' => 64, '#size' => 30, '#description' => $this->t('The content of this field is kept private and will not be shown publicly.'), - '#access' => $is_admin || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), + '#access' => ($comment->getOwner()->isAnonymous() && $is_admin) || ($this->currentUser->isAnonymous() && $anonymous_contact != COMMENT_ANONYMOUS_MAYNOT_CONTACT), ); $form['author']['homepage'] = array( diff --git a/core/modules/comment/src/Tests/CommentAdminTest.php b/core/modules/comment/src/Tests/CommentAdminTest.php index 3414ab6db1b37f85f14c714dd2de1768b320dbf0..4fd0c21582ce98bb4b35d9af4a1e6d1fd8467900 100644 --- a/core/modules/comment/src/Tests/CommentAdminTest.php +++ b/core/modules/comment/src/Tests/CommentAdminTest.php @@ -168,4 +168,45 @@ public function testCommentAdmin() { // Rest from here on in is field_ui. } + /** + * Tests editing a comment as an admin. + */ + public function testEditComment() { + // Enable anonymous user comments. + user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array( + 'access comments', + 'post comments', + 'skip comment approval', + )); + + // Login as a web user. + $this->drupalLogin($this->webUser); + // Post a comment. + $comment = $this->postComment($this->node, $this->randomMachineName()); + + $this->drupalLogout(); + + // Post anonymous comment. + $this->drupalLogin($this->adminUser); + $this->setCommentAnonymous('2'); // Ensure that we need email id before posting comment. + $this->drupalLogout(); + + // Post comment with contact info (required). + $author_name = $this->randomMachineName(); + $author_mail = $this->randomMachineName() . '@example.com'; + $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), array('name' => $author_name, 'mail' => $author_mail)); + + // Login as an admin user. + $this->drupalLogin($this->adminUser); + + // Make sure the comment field is not visible when + // the comment was posted by an authenticated user. + $this->drupalGet('comment/' . $comment->id() . '/edit'); + $this->assertNoFieldById('edit-mail', $comment->getAuthorEmail()); + + // Make sure the comment field is visible when + // the comment was posted by an anonymous user. + $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit'); + $this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail()); + } }