3 namespace Drupal\Tests\media\Kernel;
5 use Drupal\Core\Form\FormState;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\media\Entity\Media;
9 use Drupal\media\Entity\MediaType;
12 * Tests media source plugins related logic.
16 class MediaSourceTest extends MediaKernelTestBase {
19 * Tests default media name functionality.
21 public function testDefaultName() {
22 // Make sure that the default name is set if not provided by the user.
23 /** @var \Drupal\media\MediaInterface $media */
24 $media = Media::create(['bundle' => $this->testMediaType->id()]);
25 $media_source = $media->getSource();
26 $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
27 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
28 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not used correctly by getName().');
29 $this->assertEquals($media->getName(), $media->label(), 'Default name and label are not the same.');
31 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media->getName(), 'Default name was not saved correctly.');
32 $this->assertEquals($media->getName(), $media->label(), 'The label changed during save.');
34 // Make sure that the user-supplied name is used.
35 /** @var \Drupal\media\MediaInterface $media */
36 $name = 'User-supplied name';
37 $media = Media::create([
38 'bundle' => $this->testMediaType->id(),
41 $media_source = $media->getSource();
42 $this->assertEquals('default_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Default metadata attribute is not used for the default name.');
43 $this->assertEquals('media:' . $media->bundle() . ':' . $media->uuid(), $media_source->getMetadata($media, 'default_name'), 'Value of the default name metadata attribute does not look correct.');
45 $this->assertEquals($name, $media->getName(), 'User-supplied name was not set correctly.');
46 $this->assertEquals($media->getName(), $media->label(), 'The user-supplied name does not match the label.');
48 // Change the default name attribute and see if it is used to set the name.
50 \Drupal::state()->set('media_source_test_attributes', ['alternative_name' => ['title' => 'Alternative name', 'value' => $name]]);
51 \Drupal::state()->set('media_source_test_definition', ['default_name_metadata_attribute' => 'alternative_name']);
52 /** @var \Drupal\media\MediaInterface $media */
53 $media = Media::create(['bundle' => $this->testMediaType->id()]);
54 $media_source = $media->getSource();
55 $this->assertEquals('alternative_name', $media_source->getPluginDefinition()['default_name_metadata_attribute'], 'Correct metadata attribute is not used for the default name.');
56 $this->assertEquals($name, $media_source->getMetadata($media, 'alternative_name'), 'Value of the default name metadata attribute does not look correct.');
58 $this->assertEquals($name, $media->getName(), 'Default name was not set correctly.');
59 $this->assertEquals($media->getName(), $media->label(), 'The default name does not match the label.');
63 * Tests metadata mapping functionality.
65 public function testMetadataMapping() {
66 $field_name = 'field_to_map_to';
67 $attribute_name = 'attribute_to_map';
68 $storage = FieldStorageConfig::create([
69 'entity_type' => 'media',
70 'field_name' => $field_name,
76 'field_storage' => $storage,
77 'bundle' => $this->testMediaType->id(),
78 'label' => 'Field to map to',
81 // Save the entity without defining the metadata mapping and check that the
83 /** @var \Drupal\media\MediaInterface $media */
84 $media = Media::create([
85 'bundle' => $this->testMediaType->id(),
86 'field_media_test' => 'some_value',
89 $this->assertEmpty($media->get($field_name)->value, 'Field stayed empty.');
91 // Make sure that source plugin returns NULL for non-existing fields.
92 $this->testMediaType->setFieldMap(['not_here_at_all' => $field_name])->save();
93 $media = Media::create([
94 'bundle' => $this->testMediaType->id(),
95 'field_media_test' => 'some_value',
97 $media_source = $media->getSource();
98 $this->assertNull($media_source->getMetadata($media, 'not_here_at_all'), 'NULL is not returned if asking for a value of non-existing metadata.');
100 $this->assertTrue($media->get($field_name)->isEmpty(), 'Non-existing metadata attribute was wrongly mapped to the field.');
102 // Define mapping and make sure that the value was stored in the field.
103 \Drupal::state()->set('media_source_test_attributes', [
104 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'],
106 $this->testMediaType->setFieldMap([$attribute_name => $field_name])->save();
107 $media = Media::create([
108 'bundle' => $this->testMediaType->id(),
109 'field_media_test' => 'some_value',
111 $media_source = $media->getSource();
112 $this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
114 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
116 // Change the metadata attribute value and re-save the entity. Field value
117 // should stay the same.
118 \Drupal::state()->set('media_source_test_attributes', [
119 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Pinkeye'],
121 $this->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
123 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
125 // Now change the value of the source field and make sure that the mapped
126 // values update too.
127 $this->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
128 $media->set('field_media_test', 'some_new_value');
130 $this->assertEquals('Pinkeye', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
132 // Remove the value of the mapped field and make sure that it is re-mapped
134 \Drupal::state()->set('media_source_test_attributes', [
135 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'],
137 $media->{$field_name}->value = NULL;
138 $this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.');
140 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.');
144 * Tests the thumbnail functionality.
146 public function testThumbnail() {
147 file_put_contents('public://thumbnail1.jpg', '');
148 file_put_contents('public://thumbnail2.jpg', '');
150 // Save a media entity and make sure thumbnail was added.
151 \Drupal::state()->set('media_source_test_attributes', [
152 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'],
154 /** @var \Drupal\media\MediaInterface $media */
155 $media = Media::create([
156 'bundle' => $this->testMediaType->id(),
157 'name' => 'Mr. Jones',
158 'field_media_test' => 'some_value',
160 $media_source = $media->getSource();
161 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
163 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media entity.');
164 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
165 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
167 // Now change the metadata attribute and make sure that the thumbnail stays
169 \Drupal::state()->set('media_source_test_attributes', [
170 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail2.jpg'],
172 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
174 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not preserved.');
175 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
176 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
178 // Remove the thumbnail and make sure that it is auto-updated on save.
179 $media->thumbnail->target_id = NULL;
180 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
182 $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.');
183 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
184 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
186 // Change the metadata attribute again, change the source field value too
187 // and make sure that the thumbnail updates.
188 \Drupal::state()->set('media_source_test_attributes', [
189 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'],
191 $media->field_media_test->value = 'some_new_value';
192 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
194 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.');
195 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
196 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
198 // Change the thumbnail metadata attribute and make sure that the thumbnail
200 \Drupal::state()->set('media_source_test_attributes', [
201 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
202 'alternative_thumbnail_uri' => ['title' => 'Should be used', 'value' => 'public://thumbnail2.jpg'],
204 \Drupal::state()->set('media_source_test_definition', ['thumbnail_uri_metadata_attribute' => 'alternative_thumbnail_uri']);
205 $media = Media::create([
206 'bundle' => $this->testMediaType->id(),
207 'name' => 'Mr. Jones',
208 'field_media_test' => 'some_value',
210 $media_source = $media->getSource();
211 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
212 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'alternative_thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.');
214 $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'Correct metadata attribute was not used for the thumbnail.');
215 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
216 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
218 // Enable queued thumbnails and make sure that the entity gets the default
219 // thumbnail initially.
220 \Drupal::state()->set('media_source_test_definition', []);
221 \Drupal::state()->set('media_source_test_attributes', [
222 'thumbnail_uri' => ['title' => 'Should not be used', 'value' => 'public://thumbnail1.jpg'],
224 $this->testMediaType->setQueueThumbnailDownloadsStatus(TRUE)->save();
225 $media = Media::create([
226 'bundle' => $this->testMediaType->id(),
227 'name' => 'Mr. Jones',
228 'field_media_test' => 'some_value',
230 $this->assertEquals('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.');
232 $this->assertEquals('public://media-icons/generic/generic.png', $media->thumbnail->entity->getFileUri(), 'Default thumbnail was not set initially.');
233 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
234 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
236 // Process the queue item and make sure that the thumbnail was updated too.
237 $queue_name = 'media_entity_thumbnail';
238 /** @var \Drupal\Core\Queue\QueueWorkerInterface $queue_worker */
239 $queue_worker = \Drupal::service('plugin.manager.queue_worker')->createInstance($queue_name);
240 $queue = \Drupal::queue($queue_name);
241 $this->assertEquals(1, $queue->numberOfItems(), 'Item was not added to the queue.');
243 $item = $queue->claimItem();
244 $this->assertEquals($media->id(), $item->data['id'], 'Queue item that was created does not belong to the correct entity.');
246 $queue_worker->processItem($item->data);
247 $queue->deleteItem($item);
248 $this->assertEquals(0, $queue->numberOfItems(), 'Item was not removed from the queue.');
250 $media = Media::load($media->id());
251 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not updated by the queue.');
252 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
253 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
255 // Set alt and title metadata attributes and make sure they are used for the
257 \Drupal::state()->set('media_source_test_definition', [
258 'thumbnail_alt_metadata_attribute' => 'alt',
259 'thumbnail_title_metadata_attribute' => 'title',
261 \Drupal::state()->set('media_source_test_attributes', [
262 'alt' => ['title' => 'Alt text', 'value' => 'This will be alt.'],
263 'title' => ['title' => 'Title text', 'value' => 'This will be title.'],
265 $media = Media::create([
266 'bundle' => $this->testMediaType->id(),
268 'field_media_test' => 'some_value',
271 $this->assertEquals('Boxer', $media->getName(), 'Correct name was not set on the media entity.');
272 $this->assertEquals('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.');
273 $this->assertEquals('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.');
277 * Tests the media entity constraints functionality.
279 public function testConstraints() {
280 // Test entity constraints.
281 \Drupal::state()->set('media_source_test_entity_constraints', [
282 'MediaTestConstraint' => [],
285 // Create a media item media that uses a source plugin with constraints and
286 // make sure the constraints works as expected when validating.
287 /** @var \Drupal\media\MediaInterface $media */
288 $media = Media::create([
289 'bundle' => $this->testConstraintsMediaType->id(),
290 'name' => 'I do not like Drupal',
291 'field_media_test_constraints' => 'Not checked',
294 // Validate the entity and make sure violation is reported.
295 /** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
296 $violations = $media->validate();
297 $this->assertCount(1, $violations, 'Expected number of validations not found.');
298 $this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.');
300 // Fix the violation and make sure it is not reported anymore.
301 $media->setName('I love Drupal!');
302 $violations = $media->validate();
303 $this->assertCount(0, $violations, 'Expected number of validations not found.');
305 // Save and make sure it succeeded.
306 $this->assertEmpty($media->id(), 'Entity ID was found.');
308 $this->assertNotEmpty($media->id(), 'Entity ID was not found.');
309 $this->assertSame($media->getName(), 'I love Drupal!');
311 // Test source field constraints.
312 \Drupal::state()->set('media_source_test_field_constraints', [
313 'MediaTestConstraint' => [],
315 \Drupal::state()->set('media_source_test_entity_constraints', []);
317 // Create media that uses source with constraints and make sure it can't
318 // be saved without validating them.
319 /** @var \Drupal\media\MediaInterface $media */
320 $media = Media::create([
321 'bundle' => $this->testConstraintsMediaType->id(),
322 'name' => 'Not checked',
323 'field_media_test_constraints' => 'I do not like Drupal',
326 // Validate the entity and make sure violation is reported.
327 /** @var \Drupal\Core\Entity\EntityConstraintViolationListInterface $violations */
328 $violations = $media->validate();
329 $this->assertCount(1, $violations, 'Expected number of validations not found.');
330 $this->assertEquals('Inappropriate text.', $violations->get(0)->getMessage(), 'Incorrect constraint validation message found.');
332 // Fix the violation and make sure it is not reported anymore.
333 $media->set('field_media_test_constraints', 'I love Drupal!');
334 $violations = $media->validate();
335 $this->assertCount(0, $violations, 'Expected number of validations not found.');
337 // Save and make sure it succeeded.
338 $this->assertEmpty($media->id(), 'Entity ID was found.');
340 $this->assertNotEmpty($media->id(), 'Entity ID was not found.');
344 * Tests logic related to the automated source field creation.
346 public function testSourceFieldCreation() {
347 /** @var \Drupal\media\MediaTypeInterface $type */
348 $type = MediaType::create([
350 'label' => 'Test type',
354 /** @var \Drupal\field\Entity\FieldConfig $field */
355 $field = $type->getSource()->createSourceField($type);
356 /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
357 $field_storage = $field->getFieldStorageDefinition();
359 // Test field storage.
360 $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
361 $this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.');
362 $this->assertEquals('string', $field_storage->getType(), 'Field is not of correct type.');
363 $this->assertEquals('field_media_test_1', $field_storage->getName(), 'Incorrect field name is used.');
364 $this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
367 $this->assertTrue($field->isNew(), 'Field is saved automatically.');
368 $this->assertEquals('field_media_test_1', $field->getName(), 'Incorrect field name is used.');
369 $this->assertEquals('string', $field->getType(), 'Field is of incorrect type.');
370 $this->assertTrue($field->isRequired(), 'Field is not required.');
371 $this->assertEquals('Test source', $field->label(), 'Incorrect label is used.');
372 $this->assertEquals('test_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
374 // Fields should be automatically saved only when creating the media type
375 // using the media type creation form. Make sure that they are not saved
376 // when creating a media type programmatically.
377 // Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest is testing
378 // form part of the functionality.
380 $storage = FieldStorageConfig::load('media.field_media_test_1');
381 $this->assertNull($storage, 'Field storage was not saved.');
382 $field = FieldConfig::load('media.test_type.field_media_test_1');
383 $this->assertNull($field, 'Field storage was not saved.');
385 // Test the plugin with a different default source field type.
386 $type = MediaType::create([
387 'id' => 'test_constraints_type',
388 'label' => 'Test type with constraints',
389 'source' => 'test_constraints',
391 $field = $type->getSource()->createSourceField($type);
392 $field_storage = $field->getFieldStorageDefinition();
394 // Test field storage.
395 $this->assertTrue($field_storage->isNew(), 'Field storage is saved automatically.');
396 $this->assertFalse($field_storage->isLocked(), 'Field storage is not locked.');
397 $this->assertEquals('string_long', $field_storage->getType(), 'Field is of incorrect type.');
398 $this->assertEquals('field_media_test_constraints_1', $field_storage->getName(), 'Incorrect field name is used.');
399 $this->assertEquals('media', $field_storage->getTargetEntityTypeId(), 'Field is not targeting media entities.');
402 $this->assertTrue($field->isNew(), 'Field is saved automatically.');
403 $this->assertEquals('field_media_test_constraints_1', $field->getName(), 'Incorrect field name is used.');
404 $this->assertEquals('string_long', $field->getType(), 'Field is of incorrect type.');
405 $this->assertTrue($field->isRequired(), 'Field is not required.');
406 $this->assertEquals('Test source with constraints', $field->label(), 'Incorrect label is used.');
407 $this->assertEquals('test_constraints_type', $field->getTargetBundle(), 'Field is not targeting correct bundle.');
411 * Tests configuration form submit handler on the base media source plugin.
413 public function testSourceConfigurationSubmit() {
414 /** @var \Drupal\media\MediaSourceManager $manager */
415 $manager = $this->container->get('plugin.manager.media.source');
417 $form_state = new FormState();
418 $form_state->setValues(['test_config_value' => 'Somewhere over the rainbow.']);
420 /** @var \Drupal\media\MediaSourceInterface $source */
421 $source = $manager->createInstance('test', []);
422 $source->submitConfigurationForm($form, $form_state);
423 $expected = ['test_config_value' => 'Somewhere over the rainbow.', 'source_field' => 'field_media_test_1'];
424 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
426 // Try to save a NULL value.
427 $form_state->setValue('test_config_value', NULL);
428 $source->submitConfigurationForm($form, $form_state);
429 $expected['test_config_value'] = NULL;
430 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
432 // Make sure that the config keys are determined correctly even if the
433 // existing value is NULL.
434 $form_state->setValue('test_config_value', 'Somewhere over the rainbow.');
435 $source->submitConfigurationForm($form, $form_state);
436 $expected['test_config_value'] = 'Somewhere over the rainbow.';
437 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');
439 // Make sure that a non-relevant value will be skipped.
440 $form_state->setValue('not_relevant', 'Should not be saved in the plugin.');
441 $source->submitConfigurationForm($form, $form_state);
442 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.');