You are here

public function MediaLibraryTest::testButton in Drupal 10

Same name in this branch
  1. 10 core/modules/ckeditor/tests/src/FunctionalJavascript/MediaLibraryTest.php \Drupal\Tests\ckeditor\FunctionalJavascript\MediaLibraryTest::testButton()
  2. 10 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaLibraryTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaLibraryTest::testButton()

Tests using drupalMedia button to embed media into CKEditor 5.

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaLibraryTest.php, line 151

Class

MediaLibraryTest
@coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\MediaLibrary @group ckeditor5 @internal

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testButton() {
  $media_preview_selector = '.ck-content .ck-widget.drupal-media .media';
  $this
    ->drupalGet('/node/add/blog');
  $this
    ->waitForEditor();
  $this
    ->pressEditorButton('Insert Drupal Media');
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', '#drupal-modal #media-library-content'));

  // Ensure that the tab order is correct.
  $tabs = $page
    ->findAll('css', '.media-library-menu__link');
  $expected_tab_order = [
    'Show Image media (selected)',
    'Show Arrakis media',
  ];
  foreach ($tabs as $key => $tab) {
    $this
      ->assertSame($expected_tab_order[$key], $tab
      ->getText());
  }
  $assert_session
    ->pageTextContains('0 of 1 item selected');
  $assert_session
    ->elementExists('css', '.js-media-library-item')
    ->click();
  $assert_session
    ->pageTextContains('1 of 1 item selected');
  $assert_session
    ->elementExists('css', '.ui-dialog-buttonpane')
    ->pressButton('Insert selected');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', $media_preview_selector, 1000));
  $xpath = new \DOMXPath($this
    ->getEditorDataAsDom());
  $drupal_media = $xpath
    ->query('//drupal-media')[0];
  $expected_attributes = [
    'data-entity-type' => 'media',
    'data-entity-uuid' => $this->media
      ->uuid(),
  ];
  foreach ($expected_attributes as $name => $expected) {
    $this
      ->assertSame($expected, $drupal_media
      ->getAttribute($name));
  }
  $this
    ->assertEditorButtonEnabled('Undo');
  $this
    ->pressEditorButton('Undo');
  $this
    ->assertEmpty($assert_session
    ->waitForElementVisible('css', $media_preview_selector, 1000));
  $this
    ->assertEditorButtonDisabled('Undo');
  $this
    ->pressEditorButton('Redo');
  $this
    ->assertEditorButtonEnabled('Undo');

  // Ensure that data-align attribute is set by default when media is inserted
  // while filter_align is enabled.
  FilterFormat::load('test_format')
    ->setFilterConfig('filter_align', [
    'status' => TRUE,
  ])
    ->save();
  $this
    ->drupalGet('/node/add/blog');
  $this
    ->waitForEditor();
  $this
    ->pressEditorButton('Insert Drupal Media');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', '#drupal-modal #media-library-content'));
  $assert_session
    ->elementExists('css', '.js-media-library-item')
    ->click();
  $assert_session
    ->elementExists('css', '.ui-dialog-buttonpane')
    ->pressButton('Insert selected');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', $media_preview_selector, 1000));
  $xpath = new \DOMXPath($this
    ->getEditorDataAsDom());
  $drupal_media = $xpath
    ->query('//drupal-media')[0];
  $expected_attributes = [
    'data-entity-type' => 'media',
    'data-entity-uuid' => $this->media
      ->uuid(),
    'data-align' => 'center',
  ];
  foreach ($expected_attributes as $name => $expected) {
    $this
      ->assertSame($expected, $drupal_media
      ->getAttribute($name));
  }
}