summaryrefslogtreecommitdiffstats
path: root/core/modules/media/tests/src/Kernel
diff options
context:
space:
mode:
Diffstat (limited to 'core/modules/media/tests/src/Kernel')
-rw-r--r--core/modules/media/tests/src/Kernel/MediaCreationTest.php58
-rw-r--r--core/modules/media/tests/src/Kernel/MediaKernelTestBase.php91
-rw-r--r--core/modules/media/tests/src/Kernel/MediaSourceTest.php439
-rw-r--r--core/modules/media/tests/src/Kernel/MediaTranslationTest.php103
4 files changed, 691 insertions, 0 deletions
diff --git a/core/modules/media/tests/src/Kernel/MediaCreationTest.php b/core/modules/media/tests/src/Kernel/MediaCreationTest.php
new file mode 100644
index 0000000..1234703
--- /dev/null
+++ b/core/modules/media/tests/src/Kernel/MediaCreationTest.php
@@ -0,0 +1,58 @@
+<?php
+
+namespace Drupal\Tests\media\Kernel;
+
+use Drupal\media\Entity\Media;
+use Drupal\media\Entity\MediaType;
+use Drupal\media\MediaInterface;
+use Drupal\media\MediaTypeInterface;
+
+/**
+ * Tests creation of media types and media items.
+ *
+ * @group media
+ */
+class MediaCreationTest extends MediaKernelTestBase {
+
+ /**
+ * Tests creating a media type programmatically.
+ */
+ public function testMediaTypeCreation() {
+ $media_type_storage = $this->container->get('entity_type.manager')->getStorage('media_type');
+
+ $this->assertInstanceOf(MediaTypeInterface::class, MediaType::load($this->testMediaType->id()), 'The new media type has not been correctly created in the database.');
+
+ // Test a media type created from default configuration.
+ $this->container->get('module_installer')->install(['media_test_type']);
+ $test_media_type = $media_type_storage->load('test');
+ $this->assertInstanceOf(MediaTypeInterface::class, $test_media_type, 'The media type from default configuration has not been created in the database.');
+ $this->assertEquals('Test type', $test_media_type->get('label'), 'Could not assure the correct type name.');
+ $this->assertEquals('Test type.', $test_media_type->get('description'), 'Could not assure the correct type description.');
+ $this->assertEquals('test', $test_media_type->get('source'), 'Could not assure the correct media source.');
+ // Source field is not set on the media source, but it should never
+ // be created automatically when a config is being imported.
+ $this->assertEquals(['source_field' => '', 'test_config_value' => 'Kakec'], $test_media_type->get('source_configuration'), 'Could not assure the correct media source configuration.');
+ $this->assertEquals(['metadata_attribute' => 'field_attribute_config_test'], $test_media_type->get('field_map'), 'Could not assure the correct field map.');
+ }
+
+ /**
+ * Tests creating a media item programmatically.
+ */
+ public function testMediaEntityCreation() {
+ $media = Media::create([
+ 'bundle' => $this->testMediaType->id(),
+ 'name' => 'Unnamed',
+ 'field_media_test' => 'Nation of sheep, ruled by wolves, owned by pigs.',
+ ]);
+ $media->save();
+
+ $this->assertNotInstanceOf(MediaInterface::class, Media::load(rand(1000, 9999)), 'Failed asserting a non-existent media.');
+
+ $this->assertInstanceOf(MediaInterface::class, Media::load($media->id()), 'The new media item has not been created in the database.');
+ $this->assertEquals($this->testMediaType->id(), $media->bundle(), 'The media item was not created with the correct type.');
+ $this->assertEquals('Unnamed', $media->label(), 'The media item was not created with the correct name.');
+ $source_field_name = $media->bundle->entity->getSource()->getSourceFieldDefinition($media->bundle->entity)->getName();
+ $this->assertEquals('Nation of sheep, ruled by wolves, owned by pigs.', $media->get($source_field_name)->value, 'Source returns incorrect source field value.');
+ }
+
+}
diff --git a/core/modules/media/tests/src/Kernel/MediaKernelTestBase.php b/core/modules/media/tests/src/Kernel/MediaKernelTestBase.php
new file mode 100644
index 0000000..0daec87
--- /dev/null
+++ b/core/modules/media/tests/src/Kernel/MediaKernelTestBase.php
@@ -0,0 +1,91 @@
+<?php
+
+namespace Drupal\Tests\media\Kernel;
+
+use Drupal\KernelTests\KernelTestBase;
+use Drupal\media\Entity\MediaType;
+
+/**
+ * Base class for Media kernel tests.
+ */
+abstract class MediaKernelTestBase extends KernelTestBase {
+
+ /**
+ * Modules to install.
+ *
+ * @var array
+ */
+ public static $modules = [
+ 'media',
+ 'media_test_source',
+ 'image',
+ 'user',
+ 'field',
+ 'system',
+ 'file',
+ ];
+
+ /**
+ * The test media type.
+ *
+ * @var \Drupal\media\MediaTypeInterface
+ */
+ protected $testMediaType;
+
+ /**
+ * The test media type with constraints.
+ *
+ * @var \Drupal\media\MediaTypeInterface
+ */
+ protected $testConstraintsMediaType;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUp() {
+ parent::setUp();
+
+ $this->installEntitySchema('user');
+ $this->installEntitySchema('file');
+ $this->installSchema('file', 'file_usage');
+ $this->installEntitySchema('media');
+ $this->installConfig(['field', 'system', 'image', 'file', 'media']);
+
+ // Create a test media type.
+ $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());
+ $media_type = MediaType::create([
+ 'id' => $id,
+ 'label' => $id,
+ 'source' => $media_source_name,
+ 'new_revision' => FALSE,
+ ]);
+ $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();
+ $media_type->set('source_configuration', [
+ 'source_field' => $source_field->getName(),
+ ])->save();
+ return $media_type;
+ }
+
+}
diff --git a/core/modules/media/tests/src/Kernel/MediaSourceTest.php b/core/modules/media/tests/src/Kernel/MediaSourceTest.php
new file mode 100644
index 0000000..c236914
--- /dev/null
+++ b/core/modules/media/tests/src/Kernel/MediaSourceTest.php
@@ -0,0 +1,439 @@
+<?php
+
+namespace Drupal\Tests\media\Kernel;
+
+use Drupal\Core\Form\FormState;
+use Drupal\field\Entity\FieldConfig;
+use Drupal\field\Entity\FieldStorageConfig;
+use Drupal\media\Entity\Media;
+use Drupal\media\Entity\MediaType;
+
+/**
+ * Tests media source plugins related logic.
+ *
+ * @group media
+ */
+class MediaSourceTest extends MediaKernelTestBase {
+
+ /**
+ * Tests default media name functionality.
+ */
+ public function testDefaultName() {
+ // Make sure that the default name is set if not provided by the user.
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = Media::create(['bundle' => $this->testMediaType->id()]);
+ $media_source = $media->getSource();
+ $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
+ $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
+ $media->save();
+ $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->label(), 'Default name was not set correctly.');
+
+ // Make sure that the user-supplied name is used.
+ /** @var \Drupal\media\MediaInterface $media */
+ $name = 'User-supplied name';
+ $media = Media::create([
+ 'bundle' => $this->testMediaType->id(),
+ 'name' => $name,
+ ]);
+ $media_source = $media->getSource();
+ $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
+ $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
+ $media->save();
+ $this->assertEquals($name, $media->label(), 'User-supplied name was not set correctly.');
+
+ // Change the default name attribute and see if it is used to set the name.
+ $name = 'Old Major';
+ \Drupal::state()->set('media_source_test_attributes', ['alternative_name' => ['title' => 'Alternative name', 'value' => $name]]);
+ \Drupal::state()->set('media_source_test_definition', ['default_name_metadata_attribute' => 'alternative_name']);
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = Media::create(['bundle' => $this->testMediaType->id()]);
+ $media_source = $media->getSource();
+ $this->assertEquals('alternative_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.');
+ $this->assertEquals($name, $media_source->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.');
+ $media->save();
+ $this->assertEquals($name, $media->label(), 'Default name was not set correctly.');
+ }
+
+ /**
+ * Tests metadata mapping functionality.
+ */
+ public function testMetadataMapping() {
+ $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->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('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->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('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->assertEquals('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->assertEquals('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->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
+ }
+
+ /**
+ * Tests the thumbnail functionality.
+ */
+ public function testThumbnail() {
+ file_put_contents('public://thumbnail1.jpg', '');
+ file_put_contents('public://thumbnail2.jpg', '');
+
+ // Save a media entity and make sure thumbnail was added.
+ \Drupal::state()->set('media_source_test_attributes', [
+ 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'],
+ ]);
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = Media::create([
+ 'bundle' => $this->testMediaType->id(),
+ 'name' => 'Mr. Jones',
+ 'field_media_test' => 'some_value',
+ ]);
+ $media_source = $media->getSource();
+ $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media entity.');
+ $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+
+ // Now change the metadata attribute and make sure that the thumbnail stays
+ // the same.
+ \Drupal::state()->set('media_source_test_attributes', [
+ 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail2.jpg'],
+ ]);
+ $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not preserved.');
+ $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+
+ // Remove the thumbnail and make sure that it is auto-updated on save.
+ $media->thumbnail->target_id = NULL;
+ $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.');
+ $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+
+ // Change the metadata attribute again, change the source field value too
+ // and make sure that the thumbnail updates.
+ \Drupal::state()->set('media_source_test_attributes', [
+ 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'],
+ ]);
+ $media->field_media_test->value = 'some_new_value';
+ $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.');
+ $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+
+ // Change the thumbnail metadata attribute and make sure that the thumbnail
+ // is set correctly.
+ \Drupal::state()->set('media_source_test_attributes', [
+ 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
+ 'alternative_thumbnail_uri' => ['title' => 'Should be used', 'value' => 'public://thumbnail2.jpg'],
+ ]);
+ \Drupal::state()->set('media_source_test_definition', ['thumbnail_uri_metadata_attribute' => 'alternative_thumbnail_uri']);
+ $media = Media::create([
+ 'bundle' => $this->testMediaType->id(),
+ 'name' => 'Mr. Jones',
+ 'field_media_test' => 'some_value',
+ ]);
+ $media_source = $media->getSource();
+ $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
+ $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'Correct metadata attribute was not used for the thumbnail.');
+ $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+
+ // Enable queued thumbnails and make sure that the entity gets the default
+ // thumbnail initially.
+ \Drupal::state()->set('media_source_test_definition', []);
+ \Drupal::state()->set('media_source_test_attributes', [
+ 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
+ ]);
+ $this->testMediaType->setQueueThumbnailDownloadsStatus(TRUE)->save();
+ $media = Media::create([
+ 'bundle' => $this->testMediaType->id(),
+ 'name' => 'Mr. Jones',
+ 'field_media_test' => 'some_value',
+ ]);
+ $this->assertEquals('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
+ $media->save();
+ $this->assertEquals('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.');
+ $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+
+ // Process the queue item and make sure that the thumbnail was updated too.
+ $queue_name = 'media_entity_thumbnail';
+ /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
+ $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
+ $queue = \Drupal::queue($queue_name);
+ $this->assertEquals(1, $queue->numberOfItems(), 'Item was not added to the queue.');
+
+ $item = $queue->claimItem();
+ $this->assertEquals($media->id(), $item->data['id'], 'Queue item that was created does not belong to the correct entity.');
+
+ $queue_worker->processItem($item->data);
+ $queue->deleteItem($item);
+ $this->assertEquals(0, $queue->numberOfItems(), 'Item was not removed from the queue.');
+
+ $media = Media::load($media->id());
+ $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.');
+ $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+
+ // Set alt and title metadata attributes and make sure they are used for the
+ // thumbnail.
+ \Drupal::state()->set('media_source_test_definition', [
+ 'thumbnail_alt_metadata_attribute' => 'alt',
+ 'thumbnail_title_metadata_attribute' => 'title',
+ ]);
+ \Drupal::state()->set('media_source_test_attributes', [
+ 'alt' => ['title' => 'Alt text', 'value' => 'This will be alt.'],
+ 'title' => ['title' => 'Title text', 'value' => 'This will be title.'],
+ ]);
+ $media = Media::create([
+ 'bundle' => $this->testMediaType->id(),
+ 'name' => 'Boxer',
+ 'field_media_test' => 'some_value',
+ ]);
+ $media->save();
+ $this->assertEquals('Boxer', $media->label(), 'Correct name was not set on the media entity.');
+ $this->assertEquals('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
+ $this->assertEquals('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
+ }
+
+ /**
+ * Tests the media entity constraints functionality.
+ */
+ public function testConstraints() {
+ // Test entity constraints.
+ \Drupal::state()->set('media_source_test_entity_constraints', [
+ 'MediaTestConstraint' => [],
+ ]);
+
+ // Create a media item media that uses a source plugin with constraints and
+ // make sure the constraints works as expected when validating.
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = Media::create([
+ 'bundle' => $this->testConstraintsMediaType->id(),
+ 'name' => 'I do not like Drupal',
+ 'field_media_test_constraints' => 'Not checked',
+ ]);
+
+ // Validate the entity and make sure violation is reported.
+ /** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
+ $violations = $media->validate();
+ $this->assertCount(1, $violations, 'Expected number of validations not found.');
+ $this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.');
+
+ // Fix the violation and make sure it is not reported anymore.
+ $media->set('name', 'I love Drupal!');
+ $violations = $media->validate();
+ $this->assertCount(0, $violations, 'Expected number of validations not found.');
+
+ // Save and make sure it succeeded.
+ $this->assertEmpty($media->id(), 'Entity ID was found.');
+ $media->save();
+ $this->assertNotEmpty($media->id(), 'Entity ID was not found.');
+
+ // Test source field constraints.
+ \Drupal::state()->set('media_source_test_field_constraints', [
+ 'MediaTestConstraint' => [],
+ ]);
+ \Drupal::state()->set('media_source_test_entity_constraints', []);
+
+ // Create media that uses source with constraints and make sure it can't
+ // be saved without validating them.
+ /** @var \Drupal\media\MediaInterface $media */
+ $media = Media::create([
+ 'bundle' => $this->testConstraintsMediaType->id(),
+ 'name' => 'Not checked',
+ 'field_media_test_constraints' => 'I do not like Drupal',
+ ]);
+
+ // Validate the entity and make sure violation is reported.
+ /** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
+ $violations = $media->validate();
+ $this->assertCount(1, $violations, 'Expected number of validations not found.');
+ $this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.');
+
+ // Fix the violation and make sure it is not reported anymore.
+ $media->set('field_media_test_constraints', 'I love Drupal!');
+ $violations = $media->validate();
+ $this->assertCount(0, $violations, 'Expected number of validations not found.');
+
+ // Save and make sure it succeeded.
+ $this->assertEmpty($media->id(), 'Entity ID was found.');
+ $media->save();
+ $this->assertNotEmpty($media->id(), 'Entity ID was not found.');
+ }
+
+ /**
+ * Tests logic related to the automated source field creation.
+ */
+ public function testSourceFieldCreation() {
+ /** @var \Drupal\media\MediaTypeInterface $type */
+ $type = MediaType::create([
+ 'id' => 'test_type',
+ 'label' => 'Test type',
+ 'source' => 'test',
+ ]);
+
+ /** @var \Drupal\field\Entity\FieldConfig $field */
+ $field = $type->getSource()->createSourceField($type);
+ /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
+ $field_storage = $field->getFieldStorageDefinition();
+
+ // Test field storage.
+ $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
+ $this->assertTrue($field_storage->isLocked(), 'Field storage is not locked.');
+ $this->assertEquals('string', $field_storage->getType(), 'Field is not of correct type.');
+ $this->assertEquals('field_media_test_1', $field_storage->getName(), 'Incorrect field name is used.');
+ $this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
+
+ // Test field.
+ $this->assertTrue($field->isNew(), 'Field is saved automatically.');
+ $this->assertEquals('field_media_test_1', $field->getName(), 'Incorrect field name is used.');
+ $this->assertEquals('string', $field->getType(), 'Field is of incorrect type.');
+ $this->assertTrue($field->isRequired(), 'Field is not required.');
+ $this->assertEquals('Test source', $field->label(), 'Incorrect label is used.');
+ $this->assertEquals('test_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
+
+ // Fields should be automatically saved only when creating the media type
+ // using the media type creation form. Make sure that they are not saved
+ // when creating a media type programmatically.
+ // Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest is testing
+ // form part of the functionality.
+ $type->save();
+ $storage = FieldStorageConfig::load('media.field_media_test_1');
+ $this->assertNull($storage, 'Field storage was not saved.');
+ $field = FieldConfig::load('media.test_type.field_media_test_1');
+ $this->assertNull($field, 'Field storage was not saved.');
+
+ // Test the plugin with a different default source field type.
+ $type = MediaType::create([
+ 'id' => 'test_constraints_type',
+ 'label' => 'Test type with constraints',
+ 'source' => 'test_constraints',
+ ]);
+ $field = $type->getSource()->createSourceField($type);
+ $field_storage = $field->getFieldStorageDefinition();
+
+ // Test field storage.
+ $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
+ $this->assertTrue($field_storage->isLocked(), 'Field storage is not locked.');
+ $this->assertEquals('string_long', $field_storage->getType(), 'Field is of incorrect type.');
+ $this->assertEquals('field_media_test_constraints_1', $field_storage->getName(), 'Incorrect field name is used.');
+ $this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
+
+ // Test field.
+ $this->assertTrue($field->isNew(), 'Field is saved automatically.');
+ $this->assertEquals('field_media_test_constraints_1', $field->getName(), 'Incorrect field name is used.');
+ $this->assertEquals('string_long', $field->getType(), 'Field is of incorrect type.');
+ $this->assertTrue($field->isRequired(), 'Field is not required.');
+ $this->assertEquals('Test source with constraints', $field->label(), 'Incorrect label is used.');
+ $this->assertEquals('test_constraints_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
+ }
+
+ /**
+ * Tests configuration form submit handler on the base media source plugin.
+ */
+ public function testSourceConfigurationSubmit() {
+ /** @var \Drupal\media\MediaSourceManager $manager */
+ $manager = $this->container->get('plugin.manager.media.source');
+ $form = [];
+ $form_state = new FormState();
+ $form_state->setValues(['test_config_value' => 'Somewhere over the rainbow.']);
+
+ /** @var \Drupal\media\MediaSourceInterface $source */
+ $source = $manager->createInstance('test', []);
+ $source->submitConfigurationForm($form, $form_state);
+ $expected = ['test_config_value' => 'Somewhere over the rainbow.', 'source_field' => 'field_media_test_1'];
+ $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
+
+ // Try to save a NULL value.
+ $form_state->setValue('test_config_value', NULL);
+ $source->submitConfigurationForm($form, $form_state);
+ $expected['test_config_value'] = NULL;
+ $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
+
+ // Make sure that the config keys are determined correctly even if the
+ // existing value is NULL.
+ $form_state->setValue('test_config_value', 'Somewhere over the rainbow.');
+ $source->submitConfigurationForm($form, $form_state);
+ $expected['test_config_value'] = 'Somewhere over the rainbow.';
+ $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
+
+ // Make sure that a non-relevant value will be skipped.
+ $form_state->setValue('not_relevant', 'Should not be saved in the plugin.');
+ $source->submitConfigurationForm($form, $form_state);
+ $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
+ }
+
+}
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 0000000..009e7c4
--- /dev/null
+++ b/core/modules/media/tests/src/Kernel/MediaTranslationTest.php
@@ -0,0 +1,103 @@
+<?php
+
+namespace Drupal\Tests\media\Kernel;
+
+use Drupal\Component\Render\FormattableMarkup;
+use Drupal\language\Entity\ConfigurableLanguage;
+
+/**
+ * Tests multilanguage fields logic.
+ *
+ * @group media
+ */
+class MediaTranslationTest extends MediaKernelTestBase {
+
+ /**
+ * Modules to install.
+ *
+ * @var array
+ */
+ public static $modules = ['language'];
+
+ /**
+ * The test media translation type.
+ *
+ * @var \Drupal\media\MediaTypeInterface
+ */
+ protected $testTranslationMediaType;
+
+ /**
+ * {@inheritdoc}
+ */
+ protected function setUp() {
+ parent::setUp();
+
+ $this->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]));
+ $this->assertEquals('public://' . $langcode . '.png', $media_translation->get('thumbnail')->entity->getFileUri(), new FormattableMarkup('%language translation thumbnail value is not correct.', ['%language' => $langcode]));
+ $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->getSource()->getMetadata($media_translation, 'test_thumbnail_alt'), new FormattableMarkup('%language translation thumbnail alt metadata attribute is not correct.', ['%language' => $langcode]));
+ $this->assertEquals('Test Thumbnail ' . $langcode, $media_translation->get('thumbnail')->alt, new FormattableMarkup('%language translation thumbnail alt value is not correct.', ['%language' => $langcode]));
+ }
+ }
+
+}