annotate core/modules/media/tests/src/Kernel/MediaSourceTest.php @ 9:1fc0ff908d1f

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