function MediaMappingsConstraintValidatorTest::testMediaMappingSource

Same name and namespace in other branches
  1. 11.x core/modules/media/tests/src/Kernel/MediaMappingsConstraintValidatorTest.php \Drupal\Tests\media\Kernel\MediaMappingsConstraintValidatorTest::testMediaMappingSource()

@covers ::validate

File

core/modules/media/tests/src/Kernel/MediaMappingsConstraintValidatorTest.php, line 38

Class

MediaMappingsConstraintValidatorTest
@coversDefaultClass \Drupal\media\Plugin\Validation\Constraint\MediaMappingsConstraintValidator[[api-linebreak]]

Namespace

Drupal\Tests\media\Kernel

Code

public function testMediaMappingSource() : void {
  $media_type = $this->createMediaType('image', [
    'id' => 'test',
  ]);
  $source_field_name = $media_type->getSource()
    ->getSourceFieldDefinition($media_type)
    ->getName();
  $field_map = $media_type->getFieldMap();
  $field_map[File::METADATA_ATTRIBUTE_MIME] = $source_field_name;
  $media_type->setFieldMap($field_map);
  $media_type->save();
  $typed_data = $this->container
    ->get('typed_data_manager');
  $definition = $typed_data->createDataDefinition('entity:media_type');
  $violations = $typed_data->create($definition, $media_type)
    ->validate();
  assert($violations instanceof ConstraintViolationListInterface);
  $this->assertCount(1, $violations);
  $this->assertEquals('It is not possible to map the source field ' . $source_field_name . ' of a media type.', $violations[0]->getMessage());
}

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