function MediaTest::createEntity

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

Overrides ResourceTestBase::createEntity

File

core/modules/jsonapi/tests/src/Functional/MediaTest.php, line 97

Class

MediaTest
JSON:API integration test for the "Media" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function createEntity() {
    if (!MediaType::load('camelids')) {
        // Create a "Camelids" media type.
        $media_type = MediaType::create([
            'name' => 'Camelids',
            'id' => 'camelids',
            'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.',
            'source' => 'file',
        ]);
        $media_type->save();
        // Create the source field.
        $source_field = $media_type->getSource()
            ->createSourceField($media_type);
        $source_field->getFieldStorageDefinition()
            ->save();
        $source_field->save();
        $media_type->set('source_configuration', [
            'source_field' => $source_field->getName(),
        ])
            ->save();
    }
    // Create a file to upload.
    $file = File::create([
        'uri' => 'public://llama.txt',
    ]);
    $file->setPermanent();
    $file->save();
    // @see \Drupal\Tests\jsonapi\Functional\MediaTest::testPostIndividual()
    $post_file = File::create([
        'uri' => 'public://llama2.txt',
    ]);
    $post_file->setPermanent();
    $post_file->save();
    // Create a "Llama" media item.
    $media = Media::create([
        'bundle' => 'camelids',
        'field_media_file' => [
            'target_id' => $file->id(),
        ],
    ]);
    $media->setName('Llama')
        ->setPublished()
        ->setCreatedTime(123456789)
        ->setOwnerId($this->account
        ->id())
        ->setRevisionUserId($this->account
        ->id())
        ->save();
    return $media;
}

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