You are here

public function TokensTest::testMediaEntityTokens in Media entity 8

Tests some of the tokens provided by media_entity.

File

tests/src/Kernel/TokensTest.php, line 46

Class

TokensTest
Tests token handling.

Namespace

Drupal\Tests\media_entity\Kernel

Code

public function testMediaEntityTokens() {

  // Create a generic media bundle.
  $bundle_name = $this
    ->randomMachineName();
  MediaBundle::create([
    'id' => $bundle_name,
    'label' => $bundle_name,
    'type' => 'generic',
    'type_configuration' => [],
    'field_map' => [],
    'status' => 1,
    'new_revision' => FALSE,
  ])
    ->save();

  // Create a media entity.
  $media = Media::create([
    'name' => $this
      ->randomMachineName(),
    'bundle' => $bundle_name,
    'uid' => '1',
    'langcode' => Language::LANGCODE_DEFAULT,
    'status' => Media::PUBLISHED,
  ]);
  $media
    ->save();
  $token_service = $this->container
    ->get('token');
  $replaced_value = $token_service
    ->replace('[media:name]', [
    'media' => $media,
  ]);
  $this
    ->assertEquals($media
    ->label(), $replaced_value, 'Token replacement for the media label was sucessful.');
}