Mercurial > hg > isophonics-drupal-site
comparison core/modules/media/tests/src/Kernel/MediaSourceTest.php @ 14:1fec387a4317
Update Drupal core to 8.5.2 via Composer
author | Chris Cannam |
---|---|
date | Mon, 23 Apr 2018 09:46:53 +0100 |
parents | 4c8ae668cc8c |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
13:5fb285c0d0e3 | 14:1fec387a4317 |
---|---|
139 $media->save(); | 139 $media->save(); |
140 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); | 140 $this->assertEquals('Snowball', $media->get($field_name)->value, 'Metadata attribute was not mapped to the field.'); |
141 } | 141 } |
142 | 142 |
143 /** | 143 /** |
144 * Tests the getSourceFieldValue() method. | |
145 */ | |
146 public function testGetSourceFieldValue() { | |
147 /** @var \Drupal\media\MediaInterface $media */ | |
148 $media = Media::create([ | |
149 'bundle' => $this->testMediaType->id(), | |
150 'field_media_test' => 'some_value', | |
151 ]); | |
152 $media->save(); | |
153 $media_source = $media->getSource(); | |
154 $this->assertSame('some_value', $media_source->getSourceFieldValue($media)); | |
155 } | |
156 | |
157 /** | |
144 * Tests the thumbnail functionality. | 158 * Tests the thumbnail functionality. |
145 */ | 159 */ |
146 public function testThumbnail() { | 160 public function testThumbnail() { |
147 file_put_contents('public://thumbnail1.jpg', ''); | 161 file_put_contents('public://thumbnail1.jpg', ''); |
148 file_put_contents('public://thumbnail2.jpg', ''); | 162 file_put_contents('public://thumbnail2.jpg', ''); |
149 | 163 |
150 // Save a media entity and make sure thumbnail was added. | 164 // Save a media item and make sure thumbnail was added. |
151 \Drupal::state()->set('media_source_test_attributes', [ | 165 \Drupal::state()->set('media_source_test_attributes', [ |
152 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'], | 166 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'], |
153 ]); | 167 ]); |
154 /** @var \Drupal\media\MediaInterface $media */ | 168 /** @var \Drupal\media\MediaInterface $media */ |
155 $media = Media::create([ | 169 $media = Media::create([ |
158 'field_media_test' => 'some_value', | 172 'field_media_test' => 'some_value', |
159 ]); | 173 ]); |
160 $media_source = $media->getSource(); | 174 $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.'); | 175 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); |
162 $media->save(); | 176 $media->save(); |
163 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media entity.'); | 177 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'Thumbnail was not added to the media item.'); |
164 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); | 178 $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.'); | 179 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); |
166 | 180 |
167 // Now change the metadata attribute and make sure that the thumbnail stays | 181 // Now change the metadata attribute and make sure that the thumbnail stays |
168 // the same. | 182 // the same. |
177 | 191 |
178 // Remove the thumbnail and make sure that it is auto-updated on save. | 192 // Remove the thumbnail and make sure that it is auto-updated on save. |
179 $media->thumbnail->target_id = NULL; | 193 $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.'); | 194 $this->assertEquals('public://thumbnail2.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); |
181 $media->save(); | 195 $media->save(); |
182 $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.'); | 196 $this->assertEquals('public://thumbnail2.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.'); |
183 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); | 197 $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.'); | 198 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); |
185 | 199 |
186 // Change the metadata attribute again, change the source field value too | 200 // Change the metadata attribute again, change the source field value too |
187 // and make sure that the thumbnail updates. | 201 // and make sure that the thumbnail updates. |
189 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'], | 203 'thumbnail_uri' => ['title' => 'Thumbnail', 'value' => 'public://thumbnail1.jpg'], |
190 ]); | 204 ]); |
191 $media->field_media_test->value = 'some_new_value'; | 205 $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.'); | 206 $this->assertEquals('public://thumbnail1.jpg', $media_source->getMetadata($media, 'thumbnail_uri'), 'Value of the thumbnail metadata attribute is not correct.'); |
193 $media->save(); | 207 $media->save(); |
194 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media entity.'); | 208 $this->assertEquals('public://thumbnail1.jpg', $media->thumbnail->entity->getFileUri(), 'New thumbnail was not added to the media item.'); |
195 $this->assertEquals('Mr. Jones', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); | 209 $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.'); | 210 $this->assertEquals('Thumbnail', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); |
197 | 211 |
198 // Change the thumbnail metadata attribute and make sure that the thumbnail | 212 // Change the thumbnail metadata attribute and make sure that the thumbnail |
199 // is set correctly. | 213 // is set correctly. |
266 'bundle' => $this->testMediaType->id(), | 280 'bundle' => $this->testMediaType->id(), |
267 'name' => 'Boxer', | 281 'name' => 'Boxer', |
268 'field_media_test' => 'some_value', | 282 'field_media_test' => 'some_value', |
269 ]); | 283 ]); |
270 $media->save(); | 284 $media->save(); |
271 $this->assertEquals('Boxer', $media->getName(), 'Correct name was not set on the media entity.'); | 285 $this->assertEquals('Boxer', $media->getName(), 'Correct name was not set on the media item.'); |
272 $this->assertEquals('This will be title.', $media->thumbnail->title, 'Title text was not set on the thumbnail.'); | 286 $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.'); | 287 $this->assertEquals('This will be alt.', $media->thumbnail->alt, 'Alt text was not set on the thumbnail.'); |
274 } | 288 } |
275 | 289 |
276 /** | 290 /** |
277 * Tests the media entity constraints functionality. | 291 * Tests the media item constraints functionality. |
278 */ | 292 */ |
279 public function testConstraints() { | 293 public function testConstraints() { |
280 // Test entity constraints. | 294 // Test entity constraints. |
281 \Drupal::state()->set('media_source_test_entity_constraints', [ | 295 \Drupal::state()->set('media_source_test_entity_constraints', [ |
282 'MediaTestConstraint' => [], | 296 'MediaTestConstraint' => [], |
440 $form_state->setValue('not_relevant', 'Should not be saved in the plugin.'); | 454 $form_state->setValue('not_relevant', 'Should not be saved in the plugin.'); |
441 $source->submitConfigurationForm($form, $form_state); | 455 $source->submitConfigurationForm($form, $form_state); |
442 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.'); | 456 $this->assertEquals($expected, $source->getConfiguration(), 'Submitted values were saved correctly.'); |
443 } | 457 } |
444 | 458 |
459 /** | |
460 * Tests different display options for the source field. | |
461 */ | |
462 public function testDifferentSourceFieldDisplays() { | |
463 $id = 'test_different_displays'; | |
464 $field_name = 'field_media_different_display'; | |
465 | |
466 $this->createMediaTypeViaForm($id, $field_name); | |
467 | |
468 // Source field not in displays. | |
469 $display = entity_get_display('media', $id, 'default'); | |
470 $components = $display->getComponents(); | |
471 $this->assertArrayHasKey($field_name, $components); | |
472 $this->assertSame('entity_reference_entity_id', $components[$field_name]['type']); | |
473 | |
474 $display = entity_get_form_display('media', $id, 'default'); | |
475 $components = $display->getComponents(); | |
476 $this->assertArrayHasKey($field_name, $components); | |
477 $this->assertSame('entity_reference_autocomplete_tags', $components[$field_name]['type']); | |
478 } | |
479 | |
480 /** | |
481 * Tests hidden source field in media type. | |
482 */ | |
483 public function testHiddenSourceField() { | |
484 $id = 'test_hidden_source_field'; | |
485 $field_name = 'field_media_hidden'; | |
486 | |
487 $this->createMediaTypeViaForm($id, $field_name); | |
488 | |
489 // Source field not in displays. | |
490 $display = entity_get_display('media', $id, 'default'); | |
491 $this->assertArrayNotHasKey($field_name, $display->getComponents()); | |
492 | |
493 $display = entity_get_form_display('media', $id, 'default'); | |
494 $this->assertArrayNotHasKey($field_name, $display->getComponents()); | |
495 } | |
496 | |
497 /** | |
498 * Creates a media type via form submit. | |
499 * | |
500 * @param string $source_plugin_id | |
501 * Source plugin ID. | |
502 * @param string $field_name | |
503 * Source field name. | |
504 */ | |
505 protected function createMediaTypeViaForm($source_plugin_id, $field_name) { | |
506 /** @var \Drupal\media\MediaTypeInterface $type */ | |
507 $type = MediaType::create(['source' => $source_plugin_id]); | |
508 | |
509 $form = $this->container->get('entity_type.manager') | |
510 ->getFormObject('media_type', 'add') | |
511 ->setEntity($type); | |
512 | |
513 $form_state = new FormState(); | |
514 $form_state->setValues([ | |
515 'label' => 'Test type', | |
516 'id' => $source_plugin_id, | |
517 'op' => t('Save'), | |
518 ]); | |
519 | |
520 /** @var \Drupal\Core\Entity\EntityFieldManagerInterface $field_manager */ | |
521 $field_manager = \Drupal::service('entity_field.manager'); | |
522 | |
523 // Source field not created yet. | |
524 $fields = $field_manager->getFieldDefinitions('media', $source_plugin_id); | |
525 $this->assertArrayNotHasKey($field_name, $fields); | |
526 | |
527 \Drupal::formBuilder()->submitForm($form, $form_state); | |
528 | |
529 // Source field exists now. | |
530 $fields = $field_manager->getFieldDefinitions('media', $source_plugin_id); | |
531 $this->assertArrayHasKey($field_name, $fields); | |
532 } | |
533 | |
445 } | 534 } |