X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fmedia%2Ftests%2Fsrc%2FTraits%2FMediaTypeCreationTrait.php;fp=web%2Fcore%2Fmodules%2Fmedia%2Ftests%2Fsrc%2FTraits%2FMediaTypeCreationTrait.php;h=84375072fc7d4ed0d2e480b2cff05d1bde5cfb25;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php b/web/core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php new file mode 100644 index 000000000..84375072f --- /dev/null +++ b/web/core/modules/media/tests/src/Traits/MediaTypeCreationTrait.php @@ -0,0 +1,76 @@ + $this->randomMachineName(), + 'label' => $this->randomString(), + 'source' => $source_plugin_id, + ]; + + /** @var \Drupal\media\MediaTypeInterface $media_type */ + $media_type = MediaType::create($values); + $this->assertSame(SAVED_NEW, $media_type->save()); + + $source = $media_type->getSource(); + $source_field = $source->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(); + + $source_configuration = $source->getConfiguration(); + $source_configuration['source_field'] = $source_field->getName(); + $source->setConfiguration($source_configuration); + + $this->assertSame(SAVED_UPDATED, $media_type->save()); + + // Add the source field to the form display for the media type. + $form_display = entity_get_form_display('media', $media_type->id(), 'default'); + $source->prepareFormDisplay($media_type, $form_display); + $form_display->save(); + + return $media_type; + } + +}