function MediaSourceTest::testMetadataMapping
Same name in other branches
- 9 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testMetadataMapping()
- 8.9.x core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testMetadataMapping()
- 10 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testMetadataMapping()
Tests metadata mapping functionality.
File
-
core/
modules/ media/ tests/ src/ Kernel/ MediaSourceTest.php, line 140
Class
- MediaSourceTest
- Tests media source plugins related logic.
Namespace
Drupal\Tests\media\KernelCode
public function testMetadataMapping() : void {
$field_name = 'field_to_map_to';
$attribute_name = 'attribute_to_map';
$storage = FieldStorageConfig::create([
'entity_type' => 'media',
'field_name' => $field_name,
'type' => 'string',
]);
$storage->save();
FieldConfig::create([
'field_storage' => $storage,
'bundle' => $this->testMediaType
->id(),
'label' => 'Field to map to',
])
->save();
// Save the entity without defining the metadata mapping and check that the
// field stays empty.
/** @var \Drupal\media\MediaInterface $media */
$media = Media::create([
'bundle' => $this->testMediaType
->id(),
'field_media_test' => 'some_value',
]);
$media->save();
$this->assertEmpty($media->get($field_name)->value, 'Field stayed empty.');
// Make sure that source plugin returns NULL for non-existing fields.
$this->testMediaType
->setFieldMap([
'not_here_at_all' => $field_name,
])
->save();
$media = Media::create([
'bundle' => $this->testMediaType
->id(),
'field_media_test' => 'some_value',
]);
$media_source = $media->getSource();
$this->assertNull($media_source->getMetadata($media, 'not_here_at_all'), 'NULL is not returned if asking for a value of non-existing metadata.');
$media->save();
$this->assertTrue($media->get($field_name)
->isEmpty(), 'Non-existing metadata attribute was wrongly mapped to the field.');
// Define mapping and make sure that the value was stored in the field.
\Drupal::state()->set('media_source_test_attributes', [
$attribute_name => [
'title' => 'Attribute to map',
'value' => 'Snowball',
],
]);
$this->testMediaType
->setFieldMap([
$attribute_name => $field_name,
])
->save();
$media = Media::create([
'bundle' => $this->testMediaType
->id(),
'field_media_test' => 'some_value',
]);
$media_source = $media->getSource();
$this->assertSame('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
$media->save();
$this->assertSame('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
// Change the metadata attribute value and re-save the entity. Field value
// should stay the same.
\Drupal::state()->set('media_source_test_attributes', [
$attribute_name => [
'title' => 'Attribute to map',
'value' => 'Pinkeye',
],
]);
$this->assertSame('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
$media->save();
$this->assertSame('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
// Now change the value of the source field and make sure that the mapped
// values update too.
$this->assertSame('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
$media->set('field_media_test', 'some_new_value');
$media->save();
$this->assertSame('Pinkeye', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
// Remove the value of the mapped field and make sure that it is re-mapped
// on save.
\Drupal::state()->set('media_source_test_attributes', [
$attribute_name => [
'title' => 'Attribute to map',
'value' => 'Snowball',
],
]);
$media->{$field_name}->value = NULL;
$this->assertSame('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
$media->save();
$this->assertSame('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.