diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 8d065ee..dd6ca13 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -1368,11 +1368,11 @@ function comment_preview(CommentInterface $comment) { $account = $user; } - if ($account->id()) { + if (!empty($account) && $account->isAuthenticated()) { $comment->uid->target_id = $account->id(); $comment->name->value = check_plain($account->getUsername()); } - elseif (empty($comment->name->value)) { + else { $comment->name->value = \Drupal::config('user.settings')->get('anonymous'); } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php index 324e21e..b0311cb 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentInterfaceTest.php @@ -52,7 +52,15 @@ function testCommentInterface() { $comment = $this->postComment($this->node, $comment_text, $subject_text, TRUE); $this->assertTrue($this->commentExists($comment), 'Comment found.'); + // Comment as anonymous with preview required. + $this->drupalLogout(); + user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access content', 'access comments', 'post comments', 'skip comment approval')); + $anonymous_comment = $this->postComment($this->node, $this->randomName(), $this->randomName(), TRUE); + $this->assertTrue($this->commentExists($anonymous_comment), 'Comment found.'); + $anonymous_comment->delete(); + // Check comment display. + $this->drupalLogin($this->web_user); $this->drupalGet('node/' . $this->node->id()); $this->assertText($subject_text, 'Individual comment subject found.'); $this->assertText($comment_text, 'Individual comment body found.');