You are here

public function BasicCreationTest::testMediaEntityCreation in Media entity 8

Tests creating a media entity programmatically.

File

tests/src/Kernel/BasicCreationTest.php, line 89

Class

BasicCreationTest
Tests creation of Media Bundles and Media Entities.

Namespace

Drupal\Tests\media_entity\Kernel

Code

public function testMediaEntityCreation() {
  $media = Media::create([
    'bundle' => $this->testBundle
      ->id(),
    'name' => 'Unnamed',
  ]);
  $media
    ->save();
  $media_not_exist = (bool) Media::load(rand(1000, 9999));
  $this
    ->assertFalse($media_not_exist, 'Failed asserting a non-existent media.');
  $media_exists = (bool) Media::load($media
    ->id());
  $this
    ->assertTrue($media_exists, 'The new media entity has not been created in the database.');
  $this
    ->assertEquals($media
    ->bundle(), $this->testBundle
    ->id(), 'The media was not created with the correct bundle.');
  $this
    ->assertEquals($media
    ->label(), 'Unnamed', 'The media was not created with the correct name.');

  // Test the creation of a media without user-defined label and check if a
  // default name is provided.
  $media = Media::create([
    'bundle' => $this->testBundle
      ->id(),
  ]);
  $media
    ->save();
  $expected_name = 'media' . ':' . $this->testBundle
    ->id() . ':' . $media
    ->uuid();
  $this
    ->assertEquals($media
    ->bundle(), $this->testBundle
    ->id(), 'The media was not created with correct bundle.');
  $this
    ->assertEquals($media
    ->label(), $expected_name, 'The media was not created with a default name.');
}