function EmbeddedFormWidgetTest::testInsertionAndReselection

Same name in other branches
  1. 9 core/modules/media_library/tests/src/FunctionalJavascript/EmbeddedFormWidgetTest.php \Drupal\Tests\media_library\FunctionalJavascript\EmbeddedFormWidgetTest::testInsertionAndReselection()
  2. 8.9.x core/modules/media_library/tests/src/FunctionalJavascript/EmbeddedFormWidgetTest.php \Drupal\Tests\media_library\FunctionalJavascript\EmbeddedFormWidgetTest::testInsertionAndReselection()
  3. 11.x core/modules/media_library/tests/src/FunctionalJavascript/EmbeddedFormWidgetTest.php \Drupal\Tests\media_library\FunctionalJavascript\EmbeddedFormWidgetTest::testInsertionAndReselection()

Tests media inside another widget that validates too enthusiastically.

@dataProvider insertionReselectionProvider

File

core/modules/media_library/tests/src/FunctionalJavascript/EmbeddedFormWidgetTest.php, line 100

Class

EmbeddedFormWidgetTest
Tests media widget nested inside another widget.

Namespace

Drupal\Tests\media_library\FunctionalJavascript

Code

public function testInsertionAndReselection($widget) : void {
    $this->container
        ->get('entity_display.repository')
        ->getFormDisplay('node', 'basic_page')
        ->setComponent('media_image_field', [
        'type' => $widget,
        'region' => 'content',
        'settings' => [
            'media_types' => [
                'type_three',
            ],
        ],
    ])
        ->save();
    $page = $this->getSession()
        ->getPage();
    $assert_session = $this->assertSession();
    foreach ($this->getTestFiles('image') as $image) {
        $extension = pathinfo($image->filename, PATHINFO_EXTENSION);
        if ($extension === 'jpg') {
            $jpg_image = $image;
            break;
        }
    }
    $this->drupalGet('node/add/basic_page');
    $wrapper = $assert_session->elementExists('css', '#media_image_field-media-library-wrapper');
    $wrapper->pressButton('Add media');
    $this->assertNotNull($assert_session->waitForText('Add or select media'));
    $page->attachFileToField('Add file', $this->container
        ->get('file_system')
        ->realpath($jpg_image->uri));
    $this->assertNotNull($assert_session->waitForText('Alternative text'));
    $page->fillField('Alternative text', $this->randomString());
    $assert_session->elementExists('css', '.ui-dialog-buttonpane')
        ->pressButton('Save and insert');
    $first_item_locator = "(//div[@data-drupal-selector='edit-media-image-field-selection-0'])[1]";
    $this->assertNotNull($first_item = $assert_session->waitForElementVisible('xpath', $first_item_locator));
    $first_item->pressButton('Remove');
    $assert_session->waitForElementRemoved('xpath', $first_item_locator);
    $page->waitFor(10, function () use ($wrapper) {
        return $wrapper->hasButton('Add media');
    });
    // Test reinserting the same selection.
    $media_items = Media::loadMultiple();
    $added_media = array_pop($media_items);
    $added_media_id = $added_media->id();
    $wrapper->pressButton('Add media');
    $this->assertNotNull($assert_session->waitForText('Add or select media'));
    $assert_session->elementExists('xpath', "(//div[contains(@class, 'media-library-item')])[1]")
        ->click();
    $assert_session->checkboxChecked("media_library_select_form[{$added_media_id}]");
    $assert_session->elementExists('css', '.ui-dialog-buttonpane')
        ->pressButton('Insert selected');
    $this->assertNotNull($assert_session->waitForElementVisible('xpath', $first_item_locator));
}

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