Chris@0: container->get('entity_type.manager')->getStorage('media_type'); Chris@0: Chris@0: $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testMediaType->id()), 'The new media type has not been correctly created in the database.'); Chris@0: Chris@0: // Test a media type created from default configuration. Chris@0: $this->container->get('module_installer')->install(['media_test_type']); Chris@0: $test_media_type = $media_type_storage->load('test'); Chris@0: $this->assertInstanceOf(MediaTypeInterface::class, $test_media_type, 'The media type from default configuration has not been created in the database.'); Chris@17: $this->assertSame('Test type', $test_media_type->get('label'), 'Could not assure the correct type name.'); Chris@17: $this->assertSame('Test type.', $test_media_type->get('description'), 'Could not assure the correct type description.'); Chris@17: $this->assertSame('test', $test_media_type->get('source'), 'Could not assure the correct media source.'); Chris@0: // Source field is not set on the media source, but it should never Chris@0: // be created automatically when a config is being imported. Chris@17: $this->assertSame(['source_field' => '', 'test_config_value' => 'Kakec'], $test_media_type->get('source_configuration'), 'Could not assure the correct media source configuration.'); Chris@17: $this->assertSame(['metadata_attribute' => 'field_attribute_config_test'], $test_media_type->get('field_map'), 'Could not assure the correct field map.'); Chris@14: // Check the Media Type access handler behavior. Chris@14: // We grant access to the 'view label' operation to all users having Chris@14: // permission to 'view media'. Chris@14: $user1 = User::create([ Chris@14: 'name' => 'username1', Chris@14: 'status' => 1, Chris@14: ]); Chris@14: $user1->save(); Chris@14: $user2 = User::create([ Chris@14: 'name' => 'username2', Chris@14: 'status' => 1, Chris@14: ]); Chris@14: $user2->save(); Chris@14: $role = Role::create([ Chris@14: 'id' => 'role1', Chris@14: 'label' => 'role1', Chris@14: ]); Chris@14: $role->grantPermission('view media')->trustData()->save(); Chris@14: $user2->addRole($role->id()); Chris@14: $this->assertFalse($test_media_type->access('view label', $user1)); Chris@14: $this->assertTrue($test_media_type->access('view label', $user2)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests creating a media item programmatically. Chris@0: */ Chris@0: public function testMediaEntityCreation() { Chris@0: $media = Media::create([ Chris@0: 'bundle' => $this->testMediaType->id(), Chris@0: 'name' => 'Unnamed', Chris@0: 'field_media_test' => 'Nation of sheep, ruled by wolves, owned by pigs.', Chris@0: ]); Chris@0: $media->save(); Chris@0: Chris@0: $this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)), 'Failed asserting a non-existent media.'); Chris@0: Chris@0: $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media item has not been created in the database.'); Chris@17: $this->assertSame($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.'); Chris@17: $this->assertSame('Unnamed', $media->getName(), 'The media item was not created with the correct name.'); Chris@0: $source_field_name = $media->bundle->entity->getSource()->getSourceFieldDefinition($media->bundle->entity)->getName(); Chris@17: $this->assertSame('Nation of sheep, ruled by wolves, owned by pigs.', $media->get($source_field_name)->value, 'Source returns incorrect source field value.'); Chris@0: } Chris@0: Chris@0: }