function CommentItemTest::testCommentAuthorName

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Kernel/CommentItemTest.php \Drupal\Tests\comment\Kernel\CommentItemTest::testCommentAuthorName()
  2. 8.9.x core/modules/comment/tests/src/Kernel/CommentItemTest.php \Drupal\Tests\comment\Kernel\CommentItemTest::testCommentAuthorName()
  3. 11.x core/modules/comment/tests/src/Kernel/CommentItemTest.php \Drupal\Tests\comment\Kernel\CommentItemTest::testCommentAuthorName()

Tests comment author name.

File

core/modules/comment/tests/src/Kernel/CommentItemTest.php, line 75

Class

CommentItemTest
Tests the new entity API for the comment field type.

Namespace

Drupal\Tests\comment\Kernel

Code

public function testCommentAuthorName() : void {
  $this->installEntitySchema('comment');
  $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
  $host = EntityTest::create([
    'name' => $this->randomString(),
  ]);
  $host->save();
  // Create some comments.
  $comment = Comment::create([
    'subject' => 'My comment title',
    'uid' => 1,
    'name' => 'entity-test',
    'mail' => 'entity@localhost',
    'entity_type' => 'entity_test',
    'field_name' => 'comment',
    'entity_id' => $host->id(),
    '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',
    'field_name' => 'comment',
    'entity_id' => $host->id(),
    '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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.