d87b9ec24f7533c5155e0225b9a8883c67cef79b
[yaffs-website] / web / modules / contrib / media_entity / tests / src / Kernel / TokensTest.php
1 <?php
2
3 namespace Drupal\Tests\media_entity\Kernel;
4
5 use Drupal\Core\Language\Language;
6 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
7 use Drupal\media_entity\Entity\Media;
8 use Drupal\media_entity\Entity\MediaBundle;
9
10 /**
11  * Tests token handling.
12  *
13  * @group media_entity
14  */
15 class TokensTest extends EntityKernelTestBase {
16
17   /**
18    * Modules to install.
19    *
20    * @var array
21    */
22   public static $modules = [
23     'media_entity',
24     'path',
25     'file',
26     'image',
27     'entity',
28     'datetime',
29     'language',
30   ];
31
32   /**
33    * {@inheritdoc}
34    */
35   protected function setUp() {
36     parent::setUp();
37     $this->installEntitySchema('file');
38     $this->installSchema('file', 'file_usage');
39     $this->installEntitySchema('media');
40     $this->installConfig(['language', 'datetime', 'field', 'system']);
41   }
42
43   /**
44    * Tests some of the tokens provided by media_entity.
45    */
46   public function testMediaEntityTokens() {
47     // Create a generic media bundle.
48     $bundle_name = $this->randomMachineName();
49
50     MediaBundle::create([
51       'id' => $bundle_name,
52       'label' => $bundle_name,
53       'type' => 'generic',
54       'type_configuration' => [],
55       'field_map' => [],
56       'status' => 1,
57       'new_revision' => FALSE,
58     ])->save();
59
60     // Create a media entity.
61     $media = Media::create([
62       'name' => $this->randomMachineName(),
63       'bundle' => $bundle_name,
64       'uid' => '1',
65       'langcode' => Language::LANGCODE_DEFAULT,
66       'status' => Media::PUBLISHED,
67     ]);
68     $media->save();
69
70     $token_service = $this->container->get('token');
71
72     $replaced_value = $token_service->replace('[media:name]', ['media' => $media]);
73     $this->assertEquals($media->label(), $replaced_value, 'Token replacement for the media label was sucessful.');
74
75   }
76
77 }