diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index c64dafe..f698df2 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -144,6 +144,8 @@ public function preSave(EntityStorageInterface $storage) { } // The entity fields for name and mail have no meaning if the user is not // Anonymous. Set them to NULL to make it clearer that they are not used. + // For anonymous users see \Drupal\comment\CommentForm::form() for mail, and + // \Drupal\comment\CommentForm::buildEntity() for name setting. if (!$this->getOwner()->isAnonymous()) { $this->set('name', NULL); $this->set('mail', NULL); diff --git a/core/modules/comment/src/Tests/CommentInterfaceTest.php b/core/modules/comment/src/Tests/CommentInterfaceTest.php index 884e299..36d01d6 100644 --- a/core/modules/comment/src/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/src/Tests/CommentInterfaceTest.php @@ -249,7 +249,7 @@ public function testCommentEdit() { $this->drupalGet('comment/' . $comment->id() . '/edit'); $new_comment = $this->postComment(NULL, $new_comment_text, $new_subject_text, $user->getAccountName() . ' (' . $user->id() . ')'); $this->assertNull($new_comment->get('name')->value, 'Comment author name stored in database is still NULL after edit.'); - $this->assertNull($new_comment->get('mail')->value, 'Comment author ename stored in database is still NULL after edit.'); + $this->assertNull($new_comment->get('mail')->value, 'Comment author name stored in database is still NULL after edit.'); $this->assertTrue($new_comment->getAuthorName() == $user->getAccountName(), 'Comment author name from API not changed by edit.'); $this->assertTrue($new_comment->getOwnerId() == $user->id(), 'Comment owner not changed by edit.'); @@ -258,6 +258,83 @@ public function testCommentEdit() { $this->drupalLogout(); } + + // Now create an anonymous comment and test editing it as an admin user. + // Do this for the different valid scenarios of + $contact_valid_options = array( + array( + 'level' => COMMENT_ANONYMOUS_MAYNOT_CONTACT, + 'provide_email' => FALSE, + 'label' => 'COMMENT_ANONYMOUS_MAYNOT_CONTACT', + ), + array( + 'level' => COMMENT_ANONYMOUS_MAY_CONTACT, + 'provide_email' => FALSE, + 'label' => 'COMMENT_ANONYMOUS_MAY_CONTACT - no email', + ), + array( + 'level' => COMMENT_ANONYMOUS_MAY_CONTACT, + 'provide_email' => TRUE, + 'label' => 'COMMENT_ANONYMOUS_MAY_CONTACT - email', + ), + array( + 'level' => COMMENT_ANONYMOUS_MUST_CONTACT, + 'provide_email' => TRUE, + 'label' => 'COMMENT_ANONYMOUS_MUST_CONTACT', + ), + ); + foreach ($contact_valid_options as $option_set) { + $provide_email = $option_set['provide_email']; + $level = $option_set['level']; + $label = array('@level' => $option_set['label']); + + $subject_text = $this->randomMachineName(); + $comment_text = $this->randomMachineName(); + $new_comment_text = $this->randomMachineName(); + $new_subject_text = $this->randomMachineName(); + $anon_name = $this->randomMachineName(); + $anon_email = 'anon@example.com'; + $anon_contact_info = array( + 'name' => $anon_name, + ); + if ($provide_email) { + $anon_contact_info['mail'] = $anon_email; + } + else { + $anon_email = ''; + } + + $this->setCommentAnonymous($level); + user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, array( + 'access content', + 'access comments', + 'post comments', + 'skip comment approval' + )); + + $anonymous_comment = $this->postComment($this->node, $comment_text, $subject_text, $anon_contact_info); + $this->assertTrue($this->commentExists($anonymous_comment), new FormattableMarkup('Comment found (@level).', $label)); + + if (isset($anonymous_comment)) { + $this->assertEqual($anonymous_comment->getOwnerId(), 0, 'Anonymous comment has owner id of zero.'); + $this->assertEqual($anonymous_comment->get('name')->value, $anon_name, 'Anon comment author in database is the value submitted.'); + $this->assertEqual($anonymous_comment->getAuthorName(), $anon_name, 'Anon author name from API is the value stored.'); + $email_assert_message = new FormattableMarkup('Anon comment email in database is the value submitted (with settings @level).', $label); + $this->assertEqual($anonymous_comment->get('mail')->value, $anon_email, $email_assert_message); + + $this->drupalLogin($this->adminUser); + // Test editing the comment to make sure the user information stays intact. + $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit'); + $new_anon_comment = $this->postComment(NULL, $new_comment_text, $new_subject_text); + $this->assertEqual($new_anon_comment->getOwnerId(), 0, 'Edited comment has owner id of zero.'); + $this->assertEqual($new_anon_comment->get('name')->value, $anon_name, 'Edited comment author in database is unchanged.'); + $this->assertEqual($new_anon_comment->getAuthorName(), $anon_name, 'Anon author name from API is unchanged.'); + + $this->assertEqual($anonymous_comment->id(), $new_anon_comment->id(), 'Edited comment id is the same as the original.'); + + $this->drupalLogout(); + } + } } /**