Mercurial > hg > isophonics-drupal-site
comparison core/modules/media/tests/src/Kernel/MediaSourceTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\media\Kernel; | |
4 | |
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; | |
10 | |
11 /** | |
12 * Tests media source plugins related logic. | |
13 * | |
14 * @group media | |
15 */ | |
16 class MediaSourceTest extends MediaKernelTestBase { | |
17 | |
18 /** | |
19 * Tests default media name functionality. | |
20 */ | |
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.'); | |
30 $media->save(); | |
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.'); | |
33 | |
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(), | |
39 'name' => $name, | |
40 ]); | |
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.'); | |
44 $media->save(); | |
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.'); | |
47 | |
48 // Change the default name attribute and see if it is used to set the name. | |
49 $name = 'Old Major'; | |
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.'); | |
57 $media->save(); | |
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.'); | |
60 } | |
61 | |
62 /** | |
63 * Tests metadata mapping functionality. | |
64 */ | |
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, | |
71 'type' => 'string', | |
72 ]); | |
73 $storage->save(); | |
74 | |
75 FieldConfig::create([ | |
76 'field_storage' => $storage, | |
77 'bundle' => $this->testMediaType->id(), | |
78 'label' => 'Field to map to', | |
79 ])->save(); | |
80 | |
81 // Save the entity without defining the metadata mapping and check that the | |
82 // field stays empty. | |
83 /** @var \Drupal\media\MediaInterface $media */ | |
84 $media = Media::create([ | |
85 'bundle' => $this->testMediaType->id(), | |
86 'field_media_test' => 'some_value', | |
87 ]); | |
88 $media->save(); | |
89 $this->assertEmpty($media->get($field_name)->value, 'Field stayed empty.'); | |
90 | |
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', | |
96 ]); | |
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.'); | |
99 $media->save(); | |
100 $this->assertTrue($media->get($field_name)->isEmpty(), 'Non-existing metadata attribute was wrongly mapped to the field.'); | |
101 | |
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'], | |
105 ]); | |
106 $this->testMediaType->setFieldMap([$attribute_name => $field_name])->save(); | |
107 $media = Media::create([ | |
108 'bundle' => $this->testMediaType->id(), | |
109 'field_media_test' => 'some_value', | |
110 ]); | |
111 $media_source = $media->getSource(); | |
112 $this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.'); | |
113 $media->save(); | |
114 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); | |
115 | |
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'], | |
120 ]); | |
121 $this->assertEquals('Pinkeye', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.'); | |
122 $media->save(); | |
123 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); | |
124 | |
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'); | |
129 $media->save(); | |
130 $this->assertEquals('Pinkeye', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); | |
131 | |
132 // Remove the value of the mapped field and make sure that it is re-mapped | |
133 // on save. | |
134 \Drupal::state()->set('media_source_test_attributes', [ | |
135 $attribute_name => ['title' => 'Attribute to map', 'value' => 'Snowball'], | |
136 ]); | |
137 $media->{$field_name}->value = NULL; | |
138 $this->assertEquals('Snowball', $media_source->getMetadata($media, $attribute_name), 'Value of the metadata attribute is not correct.'); | |
139 $media->save(); | |
140 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); | |
141 } | |
142 | |
143 /** | |
144 * Tests the thumbnail functionality. | |
145 */ | |
146 public function testThumbnail() { | |
147 file_put_contents('public://thumbnail1.jpg', ''); | |
148 file_put_contents('public://thumbnail2.jpg', ''); | |
149 | |
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'], | |
153 ]); | |
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', | |
159 ]); | |
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.'); | |
162 $media->save(); | |
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.'); | |
166 | |
167 // Now change the metadata attribute and make sure that the thumbnail stays | |
168 // the same. | |
169 \Drupal::state()->set('media_source_test_attributes', [ | |
170 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail2.jpg'], | |
171 ]); | |
172 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); | |
173 $media->save(); | |
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.'); | |
177 | |
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.'); | |
181 $media->save(); | |
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.'); | |
185 | |
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'], | |
190 ]); | |
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.'); | |
193 $media->save(); | |
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.'); | |
197 | |
198 // Change the thumbnail metadata attribute and make sure that the thumbnail | |
199 // is set correctly. | |
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'], | |
203 ]); | |
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', | |
209 ]); | |
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.'); | |
213 $media->save(); | |
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.'); | |
217 | |
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'], | |
223 ]); | |
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', | |
229 ]); | |
230 $this->assertEquals('public://thumbnail1.jpg', $media->getSource()->getMetadata($media, 'thumbnail_uri'), 'Value of the metadata attribute is not correct.'); | |
231 $media->save(); | |
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.'); | |
235 | |
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.'); | |
242 | |
243 $item = $queue->claimItem(); | |
244 $this->assertEquals($media->id(), $item->data['id'], 'Queue item that was created does not belong to the correct entity.'); | |
245 | |
246 $queue_worker->processItem($item->data); | |
247 $queue->deleteItem($item); | |
248 $this->assertEquals(0, $queue->numberOfItems(), 'Item was not removed from the queue.'); | |
249 | |
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.'); | |
254 | |
255 // Set alt and title metadata attributes and make sure they are used for the | |
256 // thumbnail. | |
257 \Drupal::state()->set('media_source_test_definition', [ | |
258 'thumbnail_alt_metadata_attribute' => 'alt', | |
259 'thumbnail_title_metadata_attribute' => 'title', | |
260 ]); | |
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.'], | |
264 ]); | |
265 $media = Media::create([ | |
266 'bundle' => $this->testMediaType->id(), | |
267 'name' => 'Boxer', | |
268 'field_media_test' => 'some_value', | |
269 ]); | |
270 $media->save(); | |
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.'); | |
274 } | |
275 | |
276 /** | |
277 * Tests the media entity constraints functionality. | |
278 */ | |
279 public function testConstraints() { | |
280 // Test entity constraints. | |
281 \Drupal::state()->set('media_source_test_entity_constraints', [ | |
282 'MediaTestConstraint' => [], | |
283 ]); | |
284 | |
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', | |
292 ]); | |
293 | |
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.'); | |
299 | |
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.'); | |
304 | |
305 // Save and make sure it succeeded. | |
306 $this->assertEmpty($media->id(), 'Entity ID was found.'); | |
307 $media->save(); | |
308 $this->assertNotEmpty($media->id(), 'Entity ID was not found.'); | |
309 $this->assertSame($media->getName(), 'I love Drupal!'); | |
310 | |
311 // Test source field constraints. | |
312 \Drupal::state()->set('media_source_test_field_constraints', [ | |
313 'MediaTestConstraint' => [], | |
314 ]); | |
315 \Drupal::state()->set('media_source_test_entity_constraints', []); | |
316 | |
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', | |
324 ]); | |
325 | |
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.'); | |
331 | |
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.'); | |
336 | |
337 // Save and make sure it succeeded. | |
338 $this->assertEmpty($media->id(), 'Entity ID was found.'); | |
339 $media->save(); | |
340 $this->assertNotEmpty($media->id(), 'Entity ID was not found.'); | |
341 } | |
342 | |
343 /** | |
344 * Tests logic related to the automated source field creation. | |
345 */ | |
346 public function testSourceFieldCreation() { | |
347 /** @var \Drupal\media\MediaTypeInterface $type */ | |
348 $type = MediaType::create([ | |
349 'id' => 'test_type', | |
350 'label' => 'Test type', | |
351 'source' => 'test', | |
352 ]); | |
353 | |
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(); | |
358 | |
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.'); | |
365 | |
366 // Test field. | |
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.'); | |
373 | |
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. | |
379 $type->save(); | |
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.'); | |
384 | |
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', | |
390 ]); | |
391 $field = $type->getSource()->createSourceField($type); | |
392 $field_storage = $field->getFieldStorageDefinition(); | |
393 | |
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.'); | |
400 | |
401 // Test field. | |
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.'); | |
408 } | |
409 | |
410 /** | |
411 * Tests configuration form submit handler on the base media source plugin. | |
412 */ | |
413 public function testSourceConfigurationSubmit() { | |
414 /** @var \Drupal\media\MediaSourceManager $manager */ | |
415 $manager = $this->container->get('plugin.manager.media.source'); | |
416 $form = []; | |
417 $form_state = new FormState(); | |
418 $form_state->setValues(['test_config_value' => 'Somewhere over the rainbow.']); | |
419 | |
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.'); | |
425 | |
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.'); | |
431 | |
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.'); | |
438 | |
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.'); | |
443 } | |
444 | |
445 } |