3 namespace Drupal\Tests\media\FunctionalJavascript;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\media\Entity\MediaType;
10 * Base class for media source tests.
12 abstract class MediaSourceTestBase extends MediaJavascriptTestBase {
15 * Creates storage and field instance, attached to a given media type.
17 * @param string $field_name
19 * @param string $field_type
21 * @param string $media_type_id
22 * The media type config entity ID.
24 protected function createMediaTypeField($field_name, $field_type, $media_type_id) {
25 $storage = FieldStorageConfig::create([
26 'field_name' => $field_name,
27 'entity_type' => 'media',
28 'type' => $field_type,
33 'field_storage' => $storage,
34 'bundle' => $media_type_id,
37 // Make the field widget visible in the form display.
38 $component = \Drupal::service('plugin.manager.field.widget')
39 ->prepareConfiguration($field_type, []);
41 // @todo Replace entity_get_form_display() when #2367933 is done.
42 // https://www.drupal.org/node/2872159.
43 $entity_form_display = entity_get_form_display('media', $media_type_id, 'default');
44 $entity_form_display->setComponent($field_name, $component)
47 // Use the default formatter and settings.
48 $component = \Drupal::service('plugin.manager.field.formatter')
49 ->prepareConfiguration($field_type, []);
51 // @todo Replace entity_get_display() when #2367933 is done.
52 // https://www.drupal.org/node/2872159.
53 $entity_display = entity_get_display('media', $media_type_id, 'default');
54 $entity_display->setComponent($field_name, $component)
59 * Create a set of fields in a media type.
61 * @param array $fields
62 * An associative array where keys are field names and values field types.
63 * @param string $media_type_id
64 * The media type config entity ID.
66 protected function createMediaTypeFields(array $fields, $media_type_id) {
67 foreach ($fields as $field_name => $field_type) {
68 $this->createMediaTypeField($field_name, $field_type, $media_type_id);
73 * Hides a widget in the default form display config.
75 * @param string $field_name
77 * @param string $media_type_id
78 * The media type config entity ID.
80 protected function hideMediaTypeFieldWidget($field_name, $media_type_id) {
81 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
82 $entity_form_display = entity_get_form_display('media', $media_type_id, 'default');
83 if ($entity_form_display->getComponent($field_name)) {
84 $entity_form_display->removeComponent($field_name)->save();
89 * Test generic media type creation.
91 * @param string $media_type_id
92 * The media type config entity ID.
93 * @param string $source_id
94 * The media source ID.
95 * @param array $provided_fields
96 * (optional) An array of field machine names this type provides.
98 * @return \Drupal\media\MediaTypeInterface
99 * The created media type.
101 public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = []) {
102 $session = $this->getSession();
103 $page = $session->getPage();
104 $assert_session = $this->assertSession();
106 $this->drupalGet('admin/structure/media/add');
107 $page->fillField('label', $media_type_id);
109 ->wait(5000, "jQuery('.machine-name-value').text() === '{$media_type_id}'");
111 // Make sure the source is available.
112 $assert_session->fieldExists('Media source');
113 $assert_session->optionExists('Media source', $source_id);
114 $page->selectFieldOption('Media source', $source_id);
115 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
116 $this->assertNotEmpty($result);
118 // Make sure the provided fields are visible on the form.
119 foreach ($provided_fields as $provided_field) {
120 $result = $assert_session->waitForElementVisible('css', 'select[name="field_map[' . $provided_field . ']"]');
121 $this->assertNotEmpty($result);
124 // Save the form to create the type.
125 $page->pressButton('Save');
126 $assert_session->pageTextContains('The media type ' . $media_type_id . ' has been added.');
127 $this->drupalGet('admin/structure/media');
128 $assert_session->pageTextContains($media_type_id);
130 // Bundle definitions are statically cached in the context of the test, we
131 // need to make sure we have updated information before proceeding with the
132 // actions on the UI.
133 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
135 return MediaType::load($media_type_id);