You are here

public function MediaTest::testDialogAccess in Drupal 10

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

Tests the EditorMediaDialog's form elements' #access logic.

File

core/modules/ckeditor/tests/src/FunctionalJavascript/MediaTest.php, line 496

Class

MediaTest
@coversDefaultClass \Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalMedia @group ckeditor

Namespace

Drupal\Tests\ckeditor\FunctionalJavascript

Code

public function testDialogAccess() {
  $page = $this
    ->getSession()
    ->getPage();
  $assert_session = $this
    ->assertSession();
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assignNameToCkeditorIframe();
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');

  // Enable `filter_html` without "alt", "data-align" or "data-caption"
  // attributes added to the drupal-media tag.
  $allowed_html = "<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type='1 A I'> <li> <dl> <dt> <dd> <h2 id='jump-*'> <h3 id> <h4 id> <h5 id> <h6 id> <drupal-media data-entity-type data-entity-uuid data-view-mode>";
  $filter_format = FilterFormat::load('test_format');
  $filter_format
    ->setFilterConfig('filter_html', [
    'status' => TRUE,
    'settings' => [
      'allowed_html' => $allowed_html,
    ],
  ])
    ->save();

  // Test the validation of attributes in the dialog.  If the alt,
  // data-caption, and data-align attributes are not set on the drupal-media
  // tag, the respective fields shouldn't display in the dialog.
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assignNameToCkeditorIframe();
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'drupal-media', 2000));

  // Test `aria-label` attribute appears on the widget wrapper.
  $assert_session
    ->elementExists('css', '.cke_widget_drupalmedia[aria-label="Screaming hairy armadillo"]');
  $page
    ->pressButton('Edit media');
  $this
    ->waitForMetadataDialog();
  $assert_session
    ->fieldNotExists('attributes[alt]');
  $assert_session
    ->fieldNotExists('attributes[align]');
  $assert_session
    ->fieldNotExists('hasCaption');
  $assert_session
    ->pageTextContains('There is nothing to configure for this media.');

  // The edit link for the format shouldn't appear unless the user has
  // permission to edit the text format.
  $assert_session
    ->pageTextNotContains('Edit the text format Test format to modify the attributes that can be overridden.');
  $page
    ->pressButton('Close');
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');

  // Now test the same thing with a user who has access to edit text formats.
  // An extra message containing a link to edit the text format should
  // appear.
  Role::load(RoleInterface::AUTHENTICATED_ID)
    ->grantPermission('administer filters')
    ->save();
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assignNameToCkeditorIframe();
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'drupal-media', 2000));
  $page
    ->pressButton('Edit media');
  $this
    ->waitForMetadataDialog();
  $assert_session
    ->fieldNotExists('attributes[alt]');
  $assert_session
    ->fieldNotExists('attributes[align]');
  $assert_session
    ->fieldNotExists('hasCaption');
  $assert_session
    ->pageTextContains('There is nothing to configure for this media. Edit the text format Test format to modify the attributes that can be overridden.');
  $assert_session
    ->linkExists('Edit the text format Test format');
  $page
    ->pressButton('Close');
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');

  // Now test that adding the attributes to the allowed HTML will allow
  // the fields to display in the dialog.
  $allowed_html = str_replace('<drupal-media data-entity-type data-entity-uuid data-view-mode>', '<drupal-media alt data-align data-caption data-entity-type data-entity-uuid data-view-mode>', $allowed_html);
  $filter_format
    ->setFilterConfig('filter_html', [
    'status' => TRUE,
    'settings' => [
      'allowed_html' => $allowed_html,
    ],
  ])
    ->save();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'drupal-media', 2000));
  $page
    ->pressButton('Edit media');
  $this
    ->waitForMetadataDialog();
  $assert_session
    ->fieldExists('attributes[alt]');
  $assert_session
    ->fieldExists('attributes[data-align]');
  $assert_session
    ->fieldExists('hasCaption');
  $page
    ->pressButton('Close');
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');

  // Test that setting the media image field to not display alt field also
  // disables it in the dialog.
  FieldConfig::loadByName('media', 'image', 'field_media_image')
    ->setSetting('alt_field', FALSE)
    ->save();

  // @todo This manual cache clearing should not be necessary, fix in
  // https://www.drupal.org/project/drupal/issues/3076544
  $this->container
    ->get('cache.discovery')
    ->delete('entity_bundle_field_definitions:media:image:en');

  // Wait for preview.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'drupal-media', 2000));
  $page
    ->pressButton('Edit media');
  $this
    ->waitForMetadataDialog();
  $assert_session
    ->fieldNotExists('attributes[alt]');
  $assert_session
    ->fieldExists('attributes[data-align]');
  $assert_session
    ->fieldExists('hasCaption');
  $page
    ->pressButton('Close');
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');

  // Test that enabling the alt field on the media image field restores
  // the field in the dialog.
  FieldConfig::loadByName('media', 'image', 'field_media_image')
    ->setSetting('alt_field', TRUE)
    ->save();

  // @todo This manual cache clearing should not be necessary, fix in
  // https://www.drupal.org/project/drupal/issues/3076544
  $this->container
    ->get('cache.discovery')
    ->delete('entity_bundle_field_definitions:media:image:en');

  // Wait for preview.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'drupal-media', 2000));
  $page
    ->pressButton('Edit media');
  $this
    ->waitForMetadataDialog();
  $assert_session
    ->fieldExists('attributes[alt]');
  $assert_session
    ->fieldExists('attributes[data-align]');
  $assert_session
    ->fieldExists('hasCaption');
  $page
    ->pressButton('Close');
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');

  // Test that disabling `filter_caption` and `filter_align` disables the
  // respective fields in the dialog.
  $filter_format
    ->setFilterConfig('filter_caption', [
    'status' => FALSE,
  ])
    ->setFilterConfig('filter_align', [
    'status' => FALSE,
  ])
    ->save();

  // Wait for preview.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'drupal-media', 2000));
  $page
    ->pressButton('Edit media');
  $this
    ->waitForMetadataDialog();
  $assert_session
    ->fieldNotExists('attributes[data-align]');
  $assert_session
    ->fieldNotExists('hasCaption');

  // The alt field should be unaffected.
  $assert_session
    ->fieldExists('attributes[alt]');
  $page
    ->pressButton('Close');
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');

  // Test that enabling the two filters restores the fields in the dialog.
  $filter_format
    ->setFilterConfig('filter_caption', [
    'status' => TRUE,
  ])
    ->setFilterConfig('filter_align', [
    'status' => TRUE,
  ])
    ->save();

  // Wait for preview.
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'drupal-media', 2000));
  $page
    ->pressButton('Edit media');
  $this
    ->waitForMetadataDialog();
  $assert_session
    ->fieldExists('attributes[data-align]');
  $assert_session
    ->fieldExists('hasCaption');
  $assert_session
    ->pageTextNotContains('There is nothing to configure for this media. Edit the text format Test format to modify the attributes that can be overridden.');

  // The alt field should be unaffected.
  $assert_session
    ->fieldExists('attributes[alt]');
}