1<?php
2
3namespace Drupal\Tests\media\Kernel;
4
5use Drupal\media\Entity\Media;
6use Drupal\media\Entity\MediaType;
7use Drupal\media\MediaInterface;
8use Drupal\media\MediaTypeInterface;
9use Drupal\user\Entity\Role;
10use Drupal\user\Entity\User;
11
12/**
13 * Tests creation of media types and media items.
14 *
15 * @group media
16 */
17class MediaCreationTest extends MediaKernelTestBase {
18
19  /**
20   * Tests creating a media type programmatically.
21   */
22  public function testMediaTypeCreation() {
23    $media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type');
24
25    $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testMediaType->id()));
26
27    // Test a media type created from default configuration.
28    $this->container->get('module_installer')->install(['media_test_type']);
29    $test_media_type = $media_type_storage->load('test');
30    $this->assertInstanceOf(MediaTypeInterface::class, $test_media_type);
31    $this->assertSame('Test type', $test_media_type->get('label'), 'Could not assure the correct type name.');
32    $this->assertSame('Test type.', $test_media_type->get('description'), 'Could not assure the correct type description.');
33    $this->assertSame('test', $test_media_type->get('source'), 'Could not assure the correct media source.');
34    // Source field is not set on the media source, but it should never
35    // be created automatically when a config is being imported.
36    $this->assertSame(['source_field' => '', 'test_config_value' => 'Kakec'], $test_media_type->get('source_configuration'), 'Could not assure the correct media source configuration.');
37    $this->assertSame(['metadata_attribute' => 'field_attribute_config_test'], $test_media_type->get('field_map'), 'Could not assure the correct field map.');
38    // Check the Media Type access handler behavior.
39    // We grant access to the 'view label' operation to all users having
40    // permission to 'view media'.
41    $user1 = User::create([
42      'name' => 'username1',
43      'status' => 1,
44    ]);
45    $user1->save();
46    $user2 = User::create([
47      'name' => 'username2',
48      'status' => 1,
49    ]);
50    $user2->save();
51    $role = Role::create([
52      'id' => 'role1',
53      'label' => 'role1',
54    ]);
55    $role->grantPermission('view media')->trustData()->save();
56    $user2->addRole($role->id());
57    $this->assertFalse($test_media_type->access('view label', $user1));
58    $this->assertTrue($test_media_type->access('view label', $user2));
59  }
60
61  /**
62   * Tests creating a media item programmatically.
63   */
64  public function testMediaEntityCreation() {
65    $media = Media::create([
66      'bundle' => $this->testMediaType->id(),
67      'name' => 'Unnamed',
68      'field_media_test' => 'Nation of sheep, ruled by wolves, owned by pigs.',
69    ]);
70    $media->save();
71
72    $this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)));
73
74    $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()));
75    $this->assertSame($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.');
76    $this->assertSame('Unnamed', $media->getName(), 'The media item was not created with the correct name.');
77    $source_field_name = $media->bundle->entity->getSource()->getSourceFieldDefinition($media->bundle->entity)->getName();
78    $this->assertSame('Nation of sheep, ruled by wolves, owned by pigs.', $media->get($source_field_name)->value, 'Source returns incorrect source field value.');
79  }
80
81}
82