randomMachineName()); } else { $id = $values['bundle']; } $values += [ 'id' => $id, 'label' => $id, 'source' => $source, 'source_configuration' => [], 'field_map' => [], 'new_revision' => FALSE, ]; $media_type = MediaType::create($values); $status = $media_type->save(); // @todo Rename to assertSame() when #1945040 is done. // @see https://www.drupal.org/node/1945040 $this->assertIdentical(SAVED_NEW, $status, 'Media type was created successfully.'); // Ensure that the source field exists. $source = $media_type->getSource(); $source_field = $source->getSourceFieldDefinition($media_type); if (!$source_field) { $source_field = $source->createSourceField($media_type); /** @var \Drupal\field\FieldStorageConfigInterface $storage */ $storage = $source_field->getFieldStorageDefinition(); $storage->setLocked(TRUE)->save(); $source_field->save(); $media_type ->set('source_configuration', [ 'source_field' => $source_field->getName(), ]) ->save(); } return $media_type; } }