You are here

public function MediaCreationTest::testMediaEntityCreation in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/Kernel/MediaCreationTest.php \Drupal\Tests\media\Kernel\MediaCreationTest::testMediaEntityCreation()

Tests creating a media item programmatically.

File

core/modules/media/tests/src/Kernel/MediaCreationTest.php, line 64

Class

MediaCreationTest
Tests creation of media types and media items.

Namespace

Drupal\Tests\media\Kernel

Code

public function testMediaEntityCreation() {
  $media = Media::create([
    'bundle' => $this->testMediaType
      ->id(),
    'name' => 'Unnamed',
    'field_media_test' => 'Nation of sheep, ruled by wolves, owned by pigs.',
  ]);
  $media
    ->save();
  $this
    ->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)));
  $this
    ->assertInstanceOf(MediaInterface::class, Media::load($media
    ->id()));
  $this
    ->assertSame($this->testMediaType
    ->id(), $media
    ->bundle(), 'The media item was not created with the correct type.');
  $this
    ->assertSame('Unnamed', $media
    ->getName(), 'The media item was not created with the correct name.');
  $source_field_name = $media->bundle->entity
    ->getSource()
    ->getSourceFieldDefinition($media->bundle->entity)
    ->getName();
  $this
    ->assertSame('Nation of sheep, ruled by wolves, owned by pigs.', $media
    ->get($source_field_name)->value, 'Source returns incorrect source field value.');
}