You are here

protected function MediaResourceTestBase::createEntity in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/tests/src/Functional/Rest/MediaResourceTestBase.php \Drupal\Tests\media\Functional\Rest\MediaResourceTestBase::createEntity()

Creates the entity to be tested.

Return value

\Drupal\Core\Entity\EntityInterface The entity to be tested.

Overrides EntityResourceTestBase::createEntity

File

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

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([
      '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();

  // 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;
}