View source
<?php
namespace Drupal\Tests\media_entity\Kernel;
use Drupal\Core\Language\Language;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\media_entity\Entity\Media;
use Drupal\media_entity\Entity\MediaBundle;
class TokensTest extends EntityKernelTestBase {
public static $modules = [
'media_entity',
'path',
'file',
'image',
'entity',
'datetime',
'language',
];
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installSchema('file', 'file_usage');
$this
->installEntitySchema('media');
$this
->installConfig([
'language',
'datetime',
'field',
'system',
]);
}
public function testMediaEntityTokens() {
$bundle_name = $this
->randomMachineName();
MediaBundle::create([
'id' => $bundle_name,
'label' => $bundle_name,
'type' => 'generic',
'type_configuration' => [],
'field_map' => [],
'status' => 1,
'new_revision' => FALSE,
])
->save();
$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.');
}
}