3 namespace Drupal\Tests\media_entity\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\media_entity\Entity\Media;
7 use Drupal\media_entity\Entity\MediaBundle;
10 * Tests creation of Media Bundles and Media Entities.
14 class BasicCreationTest extends KernelTestBase {
21 public static $modules = [
33 * The test media bundle.
35 * @var \Drupal\media_entity\MediaBundleInterface
37 protected $testBundle;
42 protected function setUp() {
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']);
51 // Create a test bundle.
52 $id = strtolower($this->randomMachineName());
53 $this->testBundle = MediaBundle::create([
57 'type_configuration' => [],
59 'new_revision' => FALSE,
61 $this->testBundle->save();
66 * Tests creating a media bundle programmatically.
68 public function testMediaBundleCreation() {
69 /** @var \Drupal\media_entity\MediaBundleInterface $bundle_storage */
70 $bundle_storage = $this->container->get('entity_type.manager')->getStorage('media_bundle');
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.');
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.');
87 * Tests creating a media entity programmatically.
89 public function testMediaEntityCreation() {
90 $media = Media::create([
91 'bundle' => $this->testBundle->id(),
96 $media_not_exist = (bool) Media::load(rand(1000, 9999));
97 $this->assertFalse($media_not_exist, 'Failed asserting a non-existent media.');
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.');
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(),
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.');