You are here

public function MediaSourceTestBase::doTestCreateMediaType in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php \Drupal\Tests\media\FunctionalJavascript\MediaSourceTestBase::doTestCreateMediaType()

Test generic media type creation.

Parameters

string $media_type_id: The media type config entity ID.

string $source_id: The media source ID.

array $provided_fields: (optional) An array of field machine names this type provides.

string $source_label_visibility: (optional) The visibility that the source field label is expected to have. Defaults to 'visually_hidden'.

Return value

\Drupal\media\MediaTypeInterface The created media type.

5 calls to MediaSourceTestBase::doTestCreateMediaType()
MediaSourceAudioVideoTest::testAudioTypeCreation in core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php
Check the Audio source functionality.
MediaSourceAudioVideoTest::testVideoTypeCreation in core/modules/media/tests/src/FunctionalJavascript/MediaSourceAudioVideoTest.php
Check the Video source functionality.
MediaSourceFileTest::testMediaFileSource in core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php
Tests the file media source.
MediaSourceImageTest::testMediaImageSource in core/modules/media/tests/src/FunctionalJavascript/MediaSourceImageTest.php
Tests the image media source.
MediaSourceOEmbedVideoTest::testMediaOEmbedVideoSource in core/modules/media/tests/src/FunctionalJavascript/MediaSourceOEmbedVideoTest.php
Tests the oembed media source.

File

core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php, line 123

Class

MediaSourceTestBase
Base class for media source tests.

Namespace

Drupal\Tests\media\FunctionalJavascript

Code

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}'");

  // Make sure the source is available.
  $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);

  // Make sure the provided fields are visible on the form.
  foreach ($provided_fields as $provided_field) {
    $result = $assert_session
      ->waitForElementVisible('css', 'select[name="field_map[' . $provided_field . ']"]');
    $this
      ->assertNotEmpty($result);
  }

  // Save the form to create the type.
  $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);

  // Assert that the default display of the media type only shows the source
  // field.
  $this
    ->drupalGet("/admin/structure/media/manage/{$media_type_id}/display");

  // There should be only one field with editable settings, and it should be
  // the source field.
  $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");

  // Ensure the source field label is configured as expected.
  $assert_session
    ->fieldValueEquals("fields[{$source_field_name}][label]", $source_label_visibility);

  // Bundle definitions are statically cached in the context of the test, we
  // need to make sure we have updated information before proceeding with the
  // actions on the UI.
  \Drupal::service('entity_type.bundle.info')
    ->clearCachedBundles();
  return $media_type;
}