function MediaTypeCreationTest::testMediaTypeCreationFormWithDefaultField

Same name and namespace in other branches
  1. 9 core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php \Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest::testMediaTypeCreationFormWithDefaultField()
  2. 8.9.x core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php \Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest::testMediaTypeCreationFormWithDefaultField()
  3. 10 core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php \Drupal\Tests\media\FunctionalJavascript\MediaTypeCreationTest::testMediaTypeCreationFormWithDefaultField()

Tests the media type creation form.

File

core/modules/media/tests/src/FunctionalJavascript/MediaTypeCreationTest.php, line 65

Class

MediaTypeCreationTest
Tests the media type creation.

Namespace

Drupal\Tests\media\FunctionalJavascript

Code

public function testMediaTypeCreationFormWithDefaultField() : void {
    $session = $this->getSession();
    $page = $session->getPage();
    $assert_session = $this->assertSession();
    $label = 'Type with Default Field';
    $mediaTypeMachineName = str_replace(' ', '_', strtolower($label));
    $this->drupalGet('admin/structure/media/add');
    // Select the media source used by our media type. Do this before setting
    // the label or machine name in order to guard against the regression in
    // https://www.drupal.org/project/drupal/issues/2557299.
    $assert_session->fieldExists('Media source');
    $assert_session->optionExists('Media source', 'test');
    $page->selectFieldOption('Media source', 'test');
    $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
    $this->assertNotEmpty($result);
    // Fill in a label to the media type.
    $page->fillField('label', $label);
    // Wait for machine name generation. Default: waitUntilVisible(), does not
    // work properly.
    $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
    $page->pressButton('Save');
    // Check whether the source field was correctly created.
    $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}/fields");
    // Check 2nd column of first data row, to be machine name for field name.
    $assert_session->elementContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[2]', 'field_media_test');
    // Check 3rd column of first data row, to be correct field type.
    $assert_session->elementTextContains('xpath', '(//table[@id="field-overview"]//tr)[2]//td[3]', 'Text (plain)');
    // Check that the source field is correctly assigned to media type.
    $this->drupalGet("admin/structure/media/manage/{$mediaTypeMachineName}");
    $assert_session->pageTextContains('Test source field is used to store the essential information about the media item.');
    // Check that the plugin cannot be changed after it is set on type creation.
    $assert_session->fieldDisabled('Media source');
    $assert_session->pageTextContains('The media source cannot be changed after the media type is created.');
    // Check that the field map options are sorted alphabetically.
    // Source field should not be included.
    $options = $this->xpath('//select[@name="field_map[attribute_1]"]/option');
    $this->assertGreaterThanOrEqual(2, count($options));
    $this->assertSame('- Skip field -', $options[0]->getText());
    $this->assertSame('Name', $options[1]->getText());
    // It should not be possible to map the source field.
    $assert_session->optionNotExists('field_map[attribute_1]', 'Test source');
    // Open up the media add form and verify that the source field is right
    // after the name, and before the vertical tabs.
    $this->drupalGet("/media/add/{$mediaTypeMachineName}");
    // Get the form element, and its HTML representation.
    $form_selector = '#media-' . Html::cleanCssIdentifier($mediaTypeMachineName) . '-add-form';
    $form = $assert_session->elementExists('css', $form_selector);
    $form_html = $form->getOuterHtml();
    // The name field should come before the source field, which should itself
    // come before the vertical tabs.
    $name_field = $assert_session->fieldExists('Name', $form)
        ->getOuterHtml();
    $test_source_field = $assert_session->fieldExists('Test source', $form)
        ->getOuterHtml();
    $vertical_tabs = $assert_session->elementExists('css', '.vertical-tabs', $form)
        ->getOuterHtml();
    $date_field = $assert_session->fieldExists('Date', $form)
        ->getOuterHtml();
    $published_checkbox = $assert_session->fieldExists('Published', $form)
        ->getOuterHtml();
    $this->assertGreaterThan(strpos($form_html, $name_field), strpos($form_html, $test_source_field));
    $this->assertGreaterThan(strpos($form_html, $test_source_field), strpos($form_html, $vertical_tabs));
    // The "Published" checkbox should be the last element.
    $this->assertGreaterThan(strpos($form_html, $date_field), strpos($form_html, $published_checkbox));
    // Check that a new type with the same machine name cannot be created.
    $this->drupalGet('admin/structure/media/add');
    $page->fillField('label', $label);
    $session->wait(5000, "jQuery('.machine-name-value').text() === '{$mediaTypeMachineName}'");
    $page->selectFieldOption('Media source', 'test');
    $assert_session->assertWaitOnAjaxRequest();
    $page->pressButton('Save');
    $assert_session->pageTextContains('The machine-readable name is already in use. It must be unique.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.