function CommentTest::createEntity

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/CommentTest.php \Drupal\Tests\jsonapi\Functional\CommentTest::createEntity()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/CommentTest.php \Drupal\Tests\jsonapi\Functional\CommentTest::createEntity()
  3. 10 core/modules/jsonapi/tests/src/Functional/CommentTest.php \Drupal\Tests\jsonapi\Functional\CommentTest::createEntity()

Overrides ResourceTestBase::createEntity

File

core/modules/jsonapi/tests/src/Functional/CommentTest.php, line 107

Class

CommentTest
JSON:API integration test for the "Comment" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function createEntity() {
    // Create a "bar" bundle for the "entity_test" entity type and create.
    $bundle = 'bar';
    entity_test_create_bundle($bundle, NULL, 'entity_test');
    // Create a comment field on this bundle.
    $this->addDefaultCommentField('entity_test', 'bar', 'comment');
    // Create a "Camelids" test entity that the comment will be assigned to.
    $this->commentedEntity = EntityTest::create([
        'name' => 'Camelids',
        'type' => 'bar',
        'comment' => CommentItemInterface::OPEN,
    ]);
    $this->commentedEntity
        ->save();
    // Create a "Llama" comment.
    $comment = Comment::create([
        'comment_body' => [
            'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
            'format' => 'plain_text',
        ],
        'entity_id' => $this->commentedEntity
            ->id(),
        'entity_type' => 'entity_test',
        'field_name' => 'comment',
    ]);
    $comment->setSubject('Llama')
        ->setOwnerId($this->account
        ->id())
        ->setPublished()
        ->setCreatedTime(123456789)
        ->setChangedTime(123456789);
    $comment->save();
    return $comment;
}

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