diff --git a/core/modules/comment/src/Entity/Comment.php b/core/modules/comment/src/Entity/Comment.php index 9d40cb33e9..b99d912f3d 100644 --- a/core/modules/comment/src/Entity/Comment.php +++ b/core/modules/comment/src/Entity/Comment.php @@ -147,20 +147,20 @@ public function preSave(EntityStorageInterface $storage) { } while (!\Drupal::lock()->acquire($lock_name)); $this->threadLock = $lock_name; } - // 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); - } $this->setThread($thread); if (!$this->getHostname()) { // Ensure a client host from the current request. $this->setHostname(\Drupal::request()->getClientIP()); } } + // 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/tests/src/Kernel/CommentItemTest.php b/core/modules/comment/tests/src/Kernel/CommentItemTest.php index 7b9a263c2d..1a7fc655d5 100644 --- a/core/modules/comment/tests/src/Kernel/CommentItemTest.php +++ b/core/modules/comment/tests/src/Kernel/CommentItemTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\comment\Kernel; +use Drupal\comment\Entity\Comment; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Tests\CommentTestTrait; use Drupal\Core\Field\FieldItemListInterface; @@ -64,4 +65,52 @@ public function testCommentItem() { $this->assertEqual('status', $mainProperty); } + /** + * Tests comment author name. + */ + public function testCommentAuthorName() { + $this->installEntitySchema('comment'); + + // Create some comments. + $comment = Comment::create([ + 'subject' => 'My comment title', + 'uid' => 1, + 'name' => 'entity-test', + 'mail' => 'entity@localhost', + 'entity_type' => 'entity_test', + 'comment_type' => 'entity_test', + 'status' => 1, + ]); + $comment->save(); + + // The entity fields for name and mail have no meaning if the user is not + // Anonymous. + $this->assertNull($comment->name->value); + $this->assertNull($comment->mail->value); + + $comment_anonymous = Comment::create([ + 'subject' => 'Anonymous comment title', + 'uid' => 0, + 'name' => 'barry', + 'mail' => 'test@example.com', + 'homepage' => 'https://example.com', + 'entity_type' => 'entity_test', + 'comment_type' => 'entity_test', + 'status' => 1, + ]); + $comment_anonymous->save(); + + // The entity fields for name and mail have retained their values when + // comment belongs to an anonymous user. + $this->assertNotNull($comment_anonymous->name->value); + $this->assertNotNull($comment_anonymous->mail->value); + + $comment_anonymous->setOwnerId(1) + ->save(); + // The entity fields for name and mail have no meaning if the user is not + // Anonymous. + $this->assertNull($comment_anonymous->name->value); + $this->assertNull($comment_anonymous->mail->value); + } + } diff --git a/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php b/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php index 068bcdb883..e1b3c94336 100644 --- a/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php +++ b/core/modules/comment/tests/src/Kernel/Views/CommentUserNameTest.php @@ -72,18 +72,12 @@ protected function setUp($import_test_views = TRUE) { 'subject' => 'My comment title', 'uid' => $this->adminUser->id(), 'name' => $this->adminUser->label(), - 'mail' => 'test@example.com', 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'status' => 1, ]); $comment->save(); - // The entity fields for name and mail have no meaning if the user is not - // Anonymous. - $this->assertNull($comment->name->value); - $this->assertNull($comment->mail->value); - $comment_anonymous = Comment::create([ 'subject' => 'Anonymous comment title', 'uid' => 0, @@ -96,11 +90,6 @@ protected function setUp($import_test_views = TRUE) { 'status' => 1, ]); $comment_anonymous->save(); - - // The entity fields for name and mail have retained their values when - // comment belongs to an anonymous user. - $this->assertNotNull($comment_anonymous->name->value); - $this->assertNotNull($comment_anonymous->mail->value); } /**