function MediaResourceTestBase::createEntity

Same name in other branches
  1. 9 core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::createEntity()
  2. 8.9.x core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::createEntity()
  3. 11.x core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::createEntity()

Overrides EntityResourceTestBase::createEntity

File

core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php, line 98

Class

MediaResourceTestBase

Namespace

Drupal\Tests\media\Functional\Rest

Code

protected function createEntity() {
    if (!MediaType::load('camelids')) {
        // Create a "Camelids" media type.
        $media_type = MediaType::create([
            'label' => '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();
    // Create a "Llama" media item.
    $media = Media::create([
        'bundle' => 'camelids',
        'field_media_file' => [
            'target_id' => $file->id(),
        ],
    ]);
    $media->setName('Llama')
        ->setPublished()
        ->setCreatedTime(123456789)
        ->setOwnerId(static::$auth ? $this->account
        ->id() : 0)
        ->setRevisionUserId(static::$auth ? $this->account
        ->id() : 0)
        ->save();
    return $media;
}

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