3 namespace Drupal\Tests\media\Functional;
5 use Drupal\media\Entity\MediaType;
8 * Trait with helpers for Media functional tests.
10 trait MediaFunctionalTestCreateMediaTypeTrait {
13 * Creates a media type.
15 * @param array $values
16 * The media type values.
17 * @param string $source
18 * (optional) The media source plugin that is responsible for additional
19 * logic related to this media type. Defaults to 'test'.
21 * @return \Drupal\media\MediaTypeInterface
22 * A newly created media type.
24 protected function createMediaType(array $values = [], $source = 'test') {
25 if (empty($values['bundle'])) {
26 $id = strtolower($this->randomMachineName());
29 $id = $values['bundle'];
35 'source_configuration' => [],
37 'new_revision' => FALSE,
40 $media_type = MediaType::create($values);
41 $status = $media_type->save();
43 // @todo Rename to assertSame() when #1945040 is done.
44 // @see https://www.drupal.org/node/1945040
45 $this->assertIdentical(SAVED_NEW, $status, 'Media type was created successfully.');
47 // Ensure that the source field exists.
48 $source = $media_type->getSource();
49 $source_field = $source->getSourceFieldDefinition($media_type);
51 $source_field = $source->createSourceField($media_type);
52 /** @var \Drupal\field\FieldStorageConfigInterface $storage */
53 $storage = $source_field->getFieldStorageDefinition();
55 $source_field->save();
58 ->set('source_configuration', [
59 'source_field' => $source_field->getName(),