View source
<?php
namespace Drupal\Tests\media\FunctionalJavascript;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\media\Entity\MediaType;
abstract class MediaSourceTestBase extends MediaJavascriptTestBase {
protected function setUp() : void {
parent::setUp();
\Drupal::configFactory()
->getEditable('media.settings')
->set('standalone_url', TRUE)
->save(TRUE);
$this->container
->get('router.builder')
->rebuild();
}
protected function createMediaTypeField($field_name, $field_type, $media_type_id) {
$storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'media',
'type' => $field_type,
]);
$storage
->save();
FieldConfig::create([
'field_storage' => $storage,
'bundle' => $media_type_id,
])
->save();
$component = \Drupal::service('plugin.manager.field.widget')
->prepareConfiguration($field_type, []);
$display_repository = \Drupal::service('entity_display.repository');
$entity_form_display = $display_repository
->getFormDisplay('media', $media_type_id, 'default');
$entity_form_display
->setComponent($field_name, $component)
->save();
$component = \Drupal::service('plugin.manager.field.formatter')
->prepareConfiguration($field_type, []);
$entity_display = $display_repository
->getViewDisplay('media', $media_type_id);
$entity_display
->setComponent($field_name, $component)
->save();
}
protected function createMediaTypeFields(array $fields, $media_type_id) {
foreach ($fields as $field_name => $field_type) {
$this
->createMediaTypeField($field_name, $field_type, $media_type_id);
}
}
protected function hideMediaTypeFieldWidget($field_name, $media_type_id) {
$display_repository = \Drupal::service('entity_display.repository');
$entity_form_display = $display_repository
->getFormDisplay('media', $media_type_id, 'default');
if ($entity_form_display
->getComponent($field_name)) {
$entity_form_display
->removeComponent($field_name)
->save();
}
}
public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = [], $source_label_visibility = 'visually_hidden') {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('admin/structure/media/add');
$page
->fillField('label', $media_type_id);
$this
->getSession()
->wait(5000, "jQuery('.machine-name-value').text() === '{$media_type_id}'");
$assert_session
->fieldExists('Media source');
$assert_session
->optionExists('Media source', $source_id);
$page
->selectFieldOption('Media source', $source_id);
$result = $assert_session
->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
$this
->assertNotEmpty($result);
foreach ($provided_fields as $provided_field) {
$result = $assert_session
->waitForElementVisible('css', 'select[name="field_map[' . $provided_field . ']"]');
$this
->assertNotEmpty($result);
}
$page
->pressButton('Save');
$assert_session
->pageTextContains('The media type ' . $media_type_id . ' has been added.');
$this
->drupalGet('admin/structure/media');
$assert_session
->pageTextContains($media_type_id);
$media_type = MediaType::load($media_type_id);
$this
->drupalGet("/admin/structure/media/manage/{$media_type_id}/display");
$assert_session
->elementsCount('css', 'input[name$="_settings_edit"]', 1);
$source_field_name = $media_type
->getSource()
->getSourceFieldDefinition($media_type)
->getName();
$assert_session
->buttonExists("{$source_field_name}_settings_edit");
$assert_session
->fieldValueEquals("fields[{$source_field_name}][label]", $source_label_visibility);
\Drupal::service('entity_type.bundle.info')
->clearCachedBundles();
return $media_type;
}
}