function MediaSourceTest::testSave
Same name in other branches
- 8.9.x core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testSave()
- 10 core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testSave()
- 11.x core/modules/media/tests/src/Kernel/MediaSourceTest.php \Drupal\Tests\media\Kernel\MediaSourceTest::testSave()
Tests that metadata is correctly mapped irrespective of how media is saved.
File
-
core/
modules/ media/ tests/ src/ Kernel/ MediaSourceTest.php, line 21
Class
- MediaSourceTest
- Tests media source plugins related logic.
Namespace
Drupal\Tests\media\KernelCode
public function testSave() {
$field_storage = FieldStorageConfig::create([
'entity_type' => 'media',
'field_name' => 'field_to_map_to',
'type' => 'string',
]);
$field_storage->save();
FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $this->testMediaType
->id(),
'label' => 'Field to map to',
])
->save();
// Set an arbitrary metadata value to be mapped.
$this->container
->get('state')
->set('media_source_test_attributes', [
'attribute_to_map' => [
'title' => 'Attribute to map',
'value' => 'Snowball',
],
'thumbnail_uri' => [
'value' => 'public://TheSisko.png',
],
]);
$this->testMediaType
->setFieldMap([
'attribute_to_map' => 'field_to_map_to',
])
->save();
/** @var \Drupal\Core\Entity\EntityStorageInterface $storage */
$storage = $this->container
->get('entity_type.manager')
->getStorage('media');
/** @var \Drupal\media\MediaInterface $a */
$a = $storage->create([
'bundle' => $this->testMediaType
->id(),
]);
/** @var \Drupal\media\MediaInterface $b */
$b = $storage->create([
'bundle' => $this->testMediaType
->id(),
]);
// Set a random source value on both items.
$a->set($a->getSource()
->getSourceFieldDefinition($a->bundle->entity)
->getName(), $this->randomString());
$b->set($b->getSource()
->getSourceFieldDefinition($b->bundle->entity)
->getName(), $this->randomString());
$a->save();
$storage->save($b);
// Assert that the default name was mapped into the name field for both
// media items.
$this->assertFalse($a->get('name')
->isEmpty());
$this->assertFalse($b->get('name')
->isEmpty());
// Assert that arbitrary metadata was mapped correctly.
$this->assertFalse($a->get('field_to_map_to')
->isEmpty());
$this->assertFalse($b->get('field_to_map_to')
->isEmpty());
// Assert that the thumbnail was mapped correctly from the source.
$this->assertSame('public://TheSisko.png', $a->thumbnail->entity
->getFileUri());
$this->assertSame('public://TheSisko.png', $b->thumbnail->entity
->getFileUri());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.