Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / media_entity / tests / src / Kernel / BasicCreationTest.php
1 <?php
2
3 namespace Drupal\Tests\media_entity\Kernel;
4
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\media_entity\Entity\Media;
7 use Drupal\media_entity\Entity\MediaBundle;
8
9 /**
10  * Tests creation of Media Bundles and Media Entities.
11  *
12  * @group media_entity
13  */
14 class BasicCreationTest extends KernelTestBase {
15
16   /**
17    * Modules to install.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'media_entity',
23     'entity',
24     'image',
25     'user',
26     'field',
27     'system',
28     'file',
29   ];
30
31
32   /**
33    * The test media bundle.
34    *
35    * @var \Drupal\media_entity\MediaBundleInterface
36    */
37   protected $testBundle;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
45     $this->installEntitySchema('user');
46     $this->installEntitySchema('file');
47     $this->installSchema('file', 'file_usage');
48     $this->installEntitySchema('media');
49     $this->installConfig(['field', 'system', 'image', 'file']);
50
51     // Create a test bundle.
52     $id = strtolower($this->randomMachineName());
53     $this->testBundle = MediaBundle::create([
54       'id' => $id,
55       'label' => $id,
56       'type' => 'generic',
57       'type_configuration' => [],
58       'field_map' => [],
59       'new_revision' => FALSE,
60     ]);
61     $this->testBundle->save();
62
63   }
64
65   /**
66    * Tests creating a media bundle programmatically.
67    */
68   public function testMediaBundleCreation() {
69     /** @var \Drupal\media_entity\MediaBundleInterface $bundle_storage */
70     $bundle_storage = $this->container->get('entity_type.manager')->getStorage('media_bundle');
71
72     $bundle_exists = (bool) $bundle_storage->load($this->testBundle->id());
73     $this->assertTrue($bundle_exists, 'The new media bundle has not been correctly created in the database.');
74
75     // Test default bundle created from default configuration.
76     $this->container->get('module_installer')->install(['media_entity_test_bundle']);
77     $test_bundle = $bundle_storage->load('test');
78     $this->assertTrue((bool) $test_bundle, 'The media bundle from default configuration has not been created in the database.');
79     $this->assertEquals($test_bundle->get('label'), 'Test bundle', 'Could not assure the correct bundle label.');
80     $this->assertEquals($test_bundle->get('description'), 'Test bundle.', 'Could not assure the correct bundle description.');
81     $this->assertEquals($test_bundle->get('type'), 'generic', 'Could not assure the correct bundle plugin type.');
82     $this->assertEquals($test_bundle->get('type_configuration'), [], 'Could not assure the correct plugin configuration.');
83     $this->assertEquals($test_bundle->get('field_map'), [], 'Could not assure the correct field map.');
84   }
85
86   /**
87    * Tests creating a media entity programmatically.
88    */
89   public function testMediaEntityCreation() {
90     $media = Media::create([
91       'bundle' => $this->testBundle->id(),
92       'name' => 'Unnamed',
93     ]);
94     $media->save();
95
96     $media_not_exist = (bool) Media::load(rand(1000, 9999));
97     $this->assertFalse($media_not_exist, 'Failed asserting a non-existent media.');
98
99     $media_exists = (bool) Media::load($media->id());
100     $this->assertTrue($media_exists, 'The new media entity has not been created in the database.');
101     $this->assertEquals($media->bundle(), $this->testBundle->id(), 'The media was not created with the correct bundle.');
102     $this->assertEquals($media->label(), 'Unnamed', 'The media was not created with the correct name.');
103
104     // Test the creation of a media without user-defined label and check if a
105     // default name is provided.
106     $media = Media::create([
107       'bundle' => $this->testBundle->id(),
108     ]);
109     $media->save();
110     $expected_name = 'media' . ':' . $this->testBundle->id() . ':' . $media->uuid();
111     $this->assertEquals($media->bundle(), $this->testBundle->id(), 'The media was not created with correct bundle.');
112     $this->assertEquals($media->label(), $expected_name, 'The media was not created with a default name.');
113   }
114
115 }