diff --git a/core/modules/media/src/Entity/Media.php b/core/modules/media/src/Entity/Media.php index 047922fdca..e8dc880f8d 100644 --- a/core/modules/media/src/Entity/Media.php +++ b/core/modules/media/src/Entity/Media.php @@ -189,13 +189,15 @@ protected function updateThumbnail($from_queue = FALSE) { } /** - * Update the queued thumbnail for the media item. + * Updates the queued thumbnail for the media item. * * @return \Drupal\media\MediaInterface * The updated media item. * * @internal - * @todo If the need arises in contrib, consider making this a public API, by adding an interface that extends MediaInterface. + * + * @todo If the need arises in contrib, consider making this a public API, + * by adding an interface that extends MediaInterface. */ public function updateQueuedThumbnail() { $this->updateThumbnail(TRUE); diff --git a/core/modules/media/tests/modules/media_test_source/config/schema/media_test_source.schema.yml b/core/modules/media/tests/modules/media_test_source/config/schema/media_test_source.schema.yml index 25d6bfbcdf..089aeac35f 100644 --- a/core/modules/media/tests/modules/media_test_source/config/schema/media_test_source.schema.yml +++ b/core/modules/media/tests/modules/media_test_source/config/schema/media_test_source.schema.yml @@ -6,6 +6,10 @@ media.source.test: type: string label: 'Test config value' +media.source.test_translation: + type: media.source.test + label: 'Test media source with translations' + media.source.test_constraints: type: media.source.test label: 'Test media source with constraints configuration' diff --git a/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestTranslation.php b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestTranslation.php new file mode 100644 index 0000000000..2416ad38b9 --- /dev/null +++ b/core/modules/media/tests/modules/media_test_source/src/Plugin/media/Source/TestTranslation.php @@ -0,0 +1,30 @@ +language()->getId() . '.png'; + } + + return parent::getMetadata($media, $attribute_name); + } + +} diff --git a/core/modules/media/tests/src/Kernel/BasicCreationTest.php b/core/modules/media/tests/src/Kernel/MediaCreationTest.php similarity index 98% rename from core/modules/media/tests/src/Kernel/BasicCreationTest.php rename to core/modules/media/tests/src/Kernel/MediaCreationTest.php index 8748caca73..123470341b 100644 --- a/core/modules/media/tests/src/Kernel/BasicCreationTest.php +++ b/core/modules/media/tests/src/Kernel/MediaCreationTest.php @@ -12,7 +12,7 @@ * * @group media */ -class BasicCreationTest extends MediaKernelTestBase { +class MediaCreationTest extends MediaKernelTestBase { /** * Tests creating a media type programmatically. diff --git a/core/modules/media/tests/src/Kernel/MediaKernelTestBase.php b/core/modules/media/tests/src/Kernel/MediaKernelTestBase.php index eb644c0abd..0daec876af 100644 --- a/core/modules/media/tests/src/Kernel/MediaKernelTestBase.php +++ b/core/modules/media/tests/src/Kernel/MediaKernelTestBase.php @@ -52,40 +52,40 @@ protected function setUp() { $this->installConfig(['field', 'system', 'image', 'file', 'media']); // Create a test media type. - $id = strtolower($this->randomMachineName()); - $this->testMediaType = MediaType::create([ - 'id' => $id, - 'label' => $id, - 'source' => 'test', - 'new_revision' => FALSE, - ]); - $this->testMediaType->save(); - $source_field = $this->testMediaType->getSource()->createSourceField($this->testMediaType); - $source_field->getFieldStorageDefinition()->save(); - $source_field->save(); - $this->testMediaType->set('source_configuration', [ - 'source_field' => $source_field->getName(), - ])->save(); - + $this->testMediaType = $this->createMediaType('test'); // Create a test media type with constraints. + $this->testConstraintsMediaType = $this->createMediaType('test_constraints'); + } + + /** + * Create a media type for a source plugin. + * + * @param string $media_source_name + * The name of the media source. + * + * @return \Drupal\media\MediaTypeInterface + * A media type. + */ + protected function createMediaType($media_source_name) { $id = strtolower($this->randomMachineName()); - $this->testConstraintsMediaType = MediaType::create([ + $media_type = MediaType::create([ 'id' => $id, 'label' => $id, - 'source' => 'test_constraints', + 'source' => $media_source_name, 'new_revision' => FALSE, ]); - $this->testConstraintsMediaType->save(); - $source_field = $this->testConstraintsMediaType->getSource()->createSourceField($this->testConstraintsMediaType); + $media_type->save(); + $source_field = $media_type->getSource()->createSourceField($media_type); // The media type form creates a source field if it does not exist yet. The // same must be done in a kernel test, since it does not use that form. // @see \Drupal\media\MediaTypeForm::save() $source_field->getFieldStorageDefinition()->save(); // The source field storage has been created, now the field can be saved. $source_field->save(); - $this->testConstraintsMediaType->set('source_configuration', [ + $media_type->set('source_configuration', [ 'source_field' => $source_field->getName(), ])->save(); + return $media_type; } } diff --git a/core/modules/media/tests/src/Kernel/TokensTest.php b/core/modules/media/tests/src/Kernel/MediaTokensTest.php similarity index 96% rename from core/modules/media/tests/src/Kernel/TokensTest.php rename to core/modules/media/tests/src/Kernel/MediaTokensTest.php index 9dea30dad9..a7222c0074 100644 --- a/core/modules/media/tests/src/Kernel/TokensTest.php +++ b/core/modules/media/tests/src/Kernel/MediaTokensTest.php @@ -12,7 +12,7 @@ * * @group media */ -class TokensTest extends EntityKernelTestBase { +class MediaTokensTest extends EntityKernelTestBase { /** * {@inheritdoc} diff --git a/core/modules/media/tests/src/Kernel/MediaTranslationTest.php b/core/modules/media/tests/src/Kernel/MediaTranslationTest.php new file mode 100644 index 0000000000..5d1a400338 --- /dev/null +++ b/core/modules/media/tests/src/Kernel/MediaTranslationTest.php @@ -0,0 +1,100 @@ +installConfig(['language']); + + // Create a test media type for translations. + $this->testTranslationMediaType = $this->createMediaType('test_translation'); + + for ($i = 0; $i < 3; ++$i) { + $language_id = 'l' . $i; + ConfigurableLanguage::create([ + 'id' => $language_id, + 'label' => $this->randomString(), + ])->save(); + file_put_contents('public://' . $language_id . '.png', ''); + } + } + + /** + * Test translatable fields storage/retrieval. + */ + public function testTranslatableFieldSaveLoad() { + /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */ + $entity_type = $this->container->get('entity_type.manager')->getDefinition('media'); + $this->assertTrue($entity_type->isTranslatable(), 'Media is translatable.'); + + // Prepare the field translations. + $source_field_definition = $this->testTranslationMediaType->getSource()->getSourceFieldDefinition($this->testTranslationMediaType); + $source_field_storage = $source_field_definition->getFieldStorageDefinition(); + /** @var \Drupal\Core\Entity\Sql\SqlContentEntityStorage $media_storage */ + $media_storage = $this->container->get('entity_type.manager')->getStorage('media'); + /** @var \Drupal\media\Entity\Media $media */ + $media = $media_storage->create([ + 'bundle' => $this->testTranslationMediaType->id(), + 'name' => 'Unnamed', + ]); + + $field_translations = []; + $available_langcodes = array_keys($this->container->get('language_manager')->getLanguages()); + $media->set('langcode', reset($available_langcodes)); + foreach ($available_langcodes as $langcode) { + $values = []; + for ($i = 0; $i < $source_field_storage->getCardinality(); $i++) { + $values[$i]['value'] = $this->randomString(); + } + $field_translations[$langcode] = $values; + $translation = $media->hasTranslation($langcode) ? $media->getTranslation($langcode) : $media->addTranslation($langcode); + $translation->{$source_field_definition->getName()}->setValue($field_translations[$langcode]); + } + + // Save and reload the field translations. + $media->save(); + $media_storage->resetCache(); + $media = $media_storage->load($media->id()); + + // Check if the correct source field values were saved/loaded. + foreach ($field_translations as $langcode => $items) { + /** @var \Drupal\media\MediaInterface $media_translation */ + $media_translation = $media->getTranslation($langcode); + $result = TRUE; + foreach ($items as $delta => $item) { + $result = $result && $item['value'] == $media_translation->{$source_field_definition->getName()}[$delta]->value; + } + $this->assertTrue($result, new FormattableMarkup('%language translation field value not correct.', ['%language' => $langcode])); + $this->assertEquals('public://' . $langcode . '.png', $media_translation->getSource()->getMetadata($media_translation, 'thumbnail_uri'), new FormattableMarkup('%language translation thumbnail metadata attribute is not correct.', ['%language' => $langcode])); + } + } + +}