You are here

public function EmbeddedFormWidgetTest::testInsertionAndReselection in Drupal 9

Same name and namespace in other branches
  1. 8 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 97

Class

EmbeddedFormWidgetTest
Tests media widget nested inside another widget.

Namespace

Drupal\Tests\media_library\FunctionalJavascript

Code

public function testInsertionAndReselection($widget) {
  $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.
  $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[0]');
  $assert_session
    ->elementExists('css', '.ui-dialog-buttonpane')
    ->pressButton('Insert selected');
  $this
    ->assertNotNull($assert_session
    ->waitForElementVisible('xpath', $first_item_locator));
}