1<?php 2 3namespace Drupal\Tests\media\FunctionalJavascript; 4 5use Drupal\field\Entity\FieldConfig; 6use Drupal\field\Entity\FieldStorageConfig; 7use Drupal\media\Entity\MediaType; 8 9/** 10 * Base class for media source tests. 11 */ 12abstract class MediaSourceTestBase extends MediaJavascriptTestBase { 13 14 /** 15 * {@inheritdoc} 16 */ 17 protected function setUp() { 18 parent::setUp(); 19 20 // Let's set the canonical flag in the base class of the source tests, 21 // because every source test has to check the output on the view page. 22 \Drupal::configFactory() 23 ->getEditable('media.settings') 24 ->set('standalone_url', TRUE) 25 ->save(TRUE); 26 27 $this->container->get('router.builder')->rebuild(); 28 } 29 30 /** 31 * Creates storage and field instance, attached to a given media type. 32 * 33 * @param string $field_name 34 * The field name. 35 * @param string $field_type 36 * The field type. 37 * @param string $media_type_id 38 * The media type config entity ID. 39 */ 40 protected function createMediaTypeField($field_name, $field_type, $media_type_id) { 41 $storage = FieldStorageConfig::create([ 42 'field_name' => $field_name, 43 'entity_type' => 'media', 44 'type' => $field_type, 45 ]); 46 $storage->save(); 47 48 FieldConfig::create([ 49 'field_storage' => $storage, 50 'bundle' => $media_type_id, 51 ])->save(); 52 53 // Make the field widget visible in the form display. 54 $component = \Drupal::service('plugin.manager.field.widget') 55 ->prepareConfiguration($field_type, []); 56 57 /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */ 58 $display_repository = \Drupal::service('entity_display.repository'); 59 60 $entity_form_display = $display_repository->getFormDisplay('media', $media_type_id, 'default'); 61 $entity_form_display->setComponent($field_name, $component) 62 ->save(); 63 64 // Use the default formatter and settings. 65 $component = \Drupal::service('plugin.manager.field.formatter') 66 ->prepareConfiguration($field_type, []); 67 68 $entity_display = $display_repository->getViewDisplay('media', $media_type_id); 69 $entity_display->setComponent($field_name, $component) 70 ->save(); 71 } 72 73 /** 74 * Create a set of fields in a media type. 75 * 76 * @param array $fields 77 * An associative array where keys are field names and values field types. 78 * @param string $media_type_id 79 * The media type config entity ID. 80 */ 81 protected function createMediaTypeFields(array $fields, $media_type_id) { 82 foreach ($fields as $field_name => $field_type) { 83 $this->createMediaTypeField($field_name, $field_type, $media_type_id); 84 } 85 } 86 87 /** 88 * Hides a widget in the default form display config. 89 * 90 * @param string $field_name 91 * The field name. 92 * @param string $media_type_id 93 * The media type config entity ID. 94 */ 95 protected function hideMediaTypeFieldWidget($field_name, $media_type_id) { 96 97 /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $display_repository */ 98 $display_repository = \Drupal::service('entity_display.repository'); 99 100 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */ 101 $entity_form_display = $display_repository->getFormDisplay('media', $media_type_id, 'default'); 102 if ($entity_form_display->getComponent($field_name)) { 103 $entity_form_display->removeComponent($field_name)->save(); 104 } 105 } 106 107 /** 108 * Test generic media type creation. 109 * 110 * @param string $media_type_id 111 * The media type config entity ID. 112 * @param string $source_id 113 * The media source ID. 114 * @param array $provided_fields 115 * (optional) An array of field machine names this type provides. 116 * @param string $source_label_visibility 117 * (optional) The visibility that the source field label is expected to 118 * have. Defaults to 'visually_hidden'. 119 * 120 * @return \Drupal\media\MediaTypeInterface 121 * The created media type. 122 */ 123 public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = [], $source_label_visibility = 'visually_hidden') { 124 $session = $this->getSession(); 125 $page = $session->getPage(); 126 $assert_session = $this->assertSession(); 127 128 $this->drupalGet('admin/structure/media/add'); 129 $page->fillField('label', $media_type_id); 130 $this->getSession() 131 ->wait(5000, "jQuery('.machine-name-value').text() === '{$media_type_id}'"); 132 133 // Make sure the source is available. 134 $assert_session->fieldExists('Media source'); 135 $assert_session->optionExists('Media source', $source_id); 136 $page->selectFieldOption('Media source', $source_id); 137 $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]'); 138 $this->assertNotEmpty($result); 139 140 // Make sure the provided fields are visible on the form. 141 foreach ($provided_fields as $provided_field) { 142 $result = $assert_session->waitForElementVisible('css', 'select[name="field_map[' . $provided_field . ']"]'); 143 $this->assertNotEmpty($result); 144 } 145 146 // Save the form to create the type. 147 $page->pressButton('Save'); 148 $assert_session->pageTextContains('The media type ' . $media_type_id . ' has been added.'); 149 $this->drupalGet('admin/structure/media'); 150 $assert_session->pageTextContains($media_type_id); 151 152 $media_type = MediaType::load($media_type_id); 153 154 // Assert that the default display of the media type only shows the source 155 // field. 156 $this->drupalGet("/admin/structure/media/manage/$media_type_id/display"); 157 // There should be only one field with editable settings, and it should be 158 // the source field. 159 $assert_session->elementsCount('css', 'input[name$="_settings_edit"]', 1); 160 $source_field_name = $media_type->getSource() 161 ->getSourceFieldDefinition($media_type) 162 ->getName(); 163 $assert_session->buttonExists("{$source_field_name}_settings_edit"); 164 // Ensure the source field label is configured as expected. 165 $assert_session->fieldValueEquals("fields[$source_field_name][label]", $source_label_visibility); 166 167 // Bundle definitions are statically cached in the context of the test, we 168 // need to make sure we have updated information before proceeding with the 169 // actions on the UI. 170 \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); 171 172 return $media_type; 173 } 174 175} 176