function MediaCreationTest::testMediaTypeCreation

Same name and namespace in other branches
  1. 11.x core/modules/media/tests/src/Kernel/MediaCreationTest.php \Drupal\Tests\media\Kernel\MediaCreationTest::testMediaTypeCreation()
  2. 9 core/modules/media/tests/src/Kernel/MediaCreationTest.php \Drupal\Tests\media\Kernel\MediaCreationTest::testMediaTypeCreation()
  3. 8.9.x core/modules/media/tests/src/Kernel/MediaCreationTest.php \Drupal\Tests\media\Kernel\MediaCreationTest::testMediaTypeCreation()
  4. main core/modules/media/tests/src/Kernel/MediaCreationTest.php \Drupal\Tests\media\Kernel\MediaCreationTest::testMediaTypeCreation()

Tests creating a media type programmatically.

File

core/modules/media/tests/src/Kernel/MediaCreationTest.php, line 24

Class

MediaCreationTest
Tests creation of media types and media items.

Namespace

Drupal\Tests\media\Kernel

Code

public function testMediaTypeCreation() : void {
  $media_type_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('media_type');
  $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testMediaType
    ->id()));
  // Test a media type created from default configuration.
  $this->container
    ->get('module_installer')
    ->install([
    'media_test_type',
  ]);
  $test_media_type = $media_type_storage->load('test');
  $this->assertInstanceOf(MediaTypeInterface::class, $test_media_type);
  $this->assertSame('Test type', $test_media_type->get('label'), 'Could not assure the correct type name.');
  $this->assertSame('Test type.', $test_media_type->get('description'), 'Could not assure the correct type description.');
  $this->assertSame('test', $test_media_type->get('source'), 'Could not assure the correct media source.');
  // Source field is not set on the media source, but it should never
  // be created automatically when a config is being imported.
  $this->assertSame([
    'source_field' => '',
    'test_config_value' => 'Foo',
  ], $test_media_type->get('source_configuration'), 'Could not assure the correct media source configuration.');
  $this->assertSame([
    'metadata_attribute' => 'field_attribute_config_test',
  ], $test_media_type->get('field_map'), 'Could not assure the correct field map.');
  // Check the Media Type access handler behavior.
  // We grant access to the 'view label' operation to all users having
  // permission to 'view media'.
  $user1 = User::create([
    'name' => 'username1',
    'status' => 1,
  ]);
  $user1->save();
  $user2 = User::create([
    'name' => 'username2',
    'status' => 1,
  ]);
  $user2->save();
  $role = Role::create([
    'id' => 'role1',
    'label' => 'role1',
  ]);
  $role->grantPermission('view media')
    ->trustData()
    ->save();
  $user2->addRole($role->id());
  $this->assertFalse($test_media_type->access('view label', $user1));
  $this->assertTrue($test_media_type->access('view label', $user2));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.