Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / media / tests / src / Kernel / MediaCreationTest.php
1 <?php
2
3 namespace Drupal\Tests\media\Kernel;
4
5 use Drupal\media\Entity\Media;
6 use Drupal\media\Entity\MediaType;
7 use Drupal\media\MediaInterface;
8 use Drupal\media\MediaTypeInterface;
9
10 /**
11  * Tests creation of media types and media items.
12  *
13  * @group media
14  */
15 class MediaCreationTest extends MediaKernelTestBase {
16
17   /**
18    * Tests creating a media type programmatically.
19    */
20   public function testMediaTypeCreation() {
21     $media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type');
22
23     $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testMediaType->id()), 'The new media type has not been correctly created in the database.');
24
25     // Test a media type created from default configuration.
26     $this->container->get('module_installer')->install(['media_test_type']);
27     $test_media_type = $media_type_storage->load('test');
28     $this->assertInstanceOf(MediaTypeInterface::class, $test_media_type, 'The media type from default configuration has not been created in the database.');
29     $this->assertEquals('Test type', $test_media_type->get('label'), 'Could not assure the correct type name.');
30     $this->assertEquals('Test type.', $test_media_type->get('description'), 'Could not assure the correct type description.');
31     $this->assertEquals('test', $test_media_type->get('source'), 'Could not assure the correct media source.');
32     // Source field is not set on the media source, but it should never
33     // be created automatically when a config is being imported.
34     $this->assertEquals(['source_field' => '', 'test_config_value' => 'Kakec'], $test_media_type->get('source_configuration'), 'Could not assure the correct media source configuration.');
35     $this->assertEquals(['metadata_attribute' => 'field_attribute_config_test'], $test_media_type->get('field_map'), 'Could not assure the correct field map.');
36   }
37
38   /**
39    * Tests creating a media item programmatically.
40    */
41   public function testMediaEntityCreation() {
42     $media = Media::create([
43       'bundle' => $this->testMediaType->id(),
44       'name' => 'Unnamed',
45       'field_media_test' => 'Nation of sheep, ruled by wolves, owned by pigs.',
46     ]);
47     $media->save();
48
49     $this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)), 'Failed asserting a non-existent media.');
50
51     $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media item has not been created in the database.');
52     $this->assertEquals($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.');
53     $this->assertEquals('Unnamed', $media->getName(), 'The media item was not created with the correct name.');
54     $source_field_name = $media->bundle->entity->getSource()->getSourceFieldDefinition($media->bundle->entity)->getName();
55     $this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->get($source_field_name)->value, 'Source returns incorrect source field value.');
56   }
57
58 }