function CommentItemTest::testCommentItem

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

Tests using entity fields of the comment field type.

File

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

Class

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

Namespace

Drupal\Tests\comment\Kernel

Code

public function testCommentItem() {
    $this->addDefaultCommentField('entity_test', 'entity_test', 'comment');
    // Verify entity creation.
    $entity = EntityTest::create();
    $entity->name->value = $this->randomMachineName();
    $entity->save();
    // Verify entity has been created properly.
    $id = $entity->id();
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('entity_test');
    $storage->resetCache([
        $id,
    ]);
    $entity = $storage->load($id);
    $this->assertInstanceOf(FieldItemListInterface::class, $entity->comment);
    $this->assertInstanceOf(CommentItemInterface::class, $entity->comment[0]);
    // Test sample item generation.
    
    /** @var \Drupal\entity_test\Entity\EntityTest $entity */
    $entity = EntityTest::create();
    $entity->comment
        ->generateSampleItems();
    $this->entityValidateAndSave($entity);
    $this->assertContains($entity->get('comment')->status, [
        CommentItemInterface::HIDDEN,
        CommentItemInterface::CLOSED,
        CommentItemInterface::OPEN,
    ], 'Comment status value in defined range');
    $mainProperty = $entity->comment[0]
        ->mainPropertyName();
    $this->assertEquals('status', $mainProperty);
}

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