You are here

public function MediaTest::testErrorMessages in Drupal 10

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

Tests that failed media embed preview requests inform the end user.

File

core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php, line 397

Class

MediaTest
@coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Media @group ckeditor5 @internal

Namespace

Drupal\Tests\ckeditor5\FunctionalJavascript

Code

public function testErrorMessages() {

  // This test currently frequently causes the SQLite database to lock, so
  // skip the test on SQLite until the issue can be resolved.
  // @todo https://www.drupal.org/project/drupal/issues/3273626
  if (Database::getConnection()
    ->driver() === 'sqlite') {
    $this
      ->markTestSkipped('Test frequently causes a locked database on SQLite');
  }

  // Assert that a request to the `media.filter.preview` route that does not
  // result in a 200 response (due to server error or network error) is
  // handled in the JavaScript by displaying the expected error message.
  // @see core/modules/media/js/media_embed_ckeditor.theme.js
  // @see js/ckeditor5_plugins/drupalMedia/src/drupalmediaediting.js
  $this->container
    ->get('state')
    ->set('test_media_filter_controller_throw_error', TRUE);
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $assert_session = $this
    ->assertSession();
  $assert_session
    ->waitForElementVisible('css', '.ck-widget.drupal-media');
  $this
    ->assertEmpty($assert_session
    ->waitForElementVisible('css', 'img[src*="image-test.png"]', 1000));
  $assert_session
    ->elementNotExists('css', '.ck-widget.drupal-media .media');
  $this
    ->assertNotEmpty($assert_session
    ->waitForText('An error occurred while trying to preview the media. Please save your work and reload this page.'));

  // Now assert that the error doesn't appear when the override to force an
  // error is removed.
  $this->container
    ->get('state')
    ->set('test_media_filter_controller_throw_error', FALSE);
  $this
    ->getSession()
    ->reload();
  $this
    ->waitForEditor();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'img[src*="image-test.png"]'));

  // There's a second kind of error message that comes from the back end
  // that happens when the media uuid can't be converted to a media preview.
  // In this case, the error will appear in a the themeable
  // media-embed-error.html template.  We have a hook altering the css
  // classes to test the twig template is working properly and picking up our
  // extra class.
  // @see \Drupal\media\Plugin\Filter\MediaEmbed::renderMissingMediaIndicator()
  // @see core/modules/media/templates/media-embed-error.html.twig
  // @see media_test_embed_preprocess_media_embed_error()
  $original_value = $this->host->body->value;
  $this->host->body->value = str_replace($this->media
    ->uuid(), 'invalid_uuid', $original_value);
  $this->host
    ->save();
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('css', '.ck-widget.drupal-media .this-error-message-is-themeable'));

  // Test when using the classy theme, an additional class is added in
  // classy/templates/content/media-embed-error.html.twig.
  $this
    ->assertTrue($this->container
    ->get('theme_installer')
    ->install([
    'classy',
  ]));
  $this
    ->config('system.theme')
    ->set('default', 'classy')
    ->save();
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('css', '.ck-widget.drupal-media .this-error-message-is-themeable.media-embed-error--missing-source'));

  // @todo Uncomment this in https://www.drupal.org/project/ckeditor5/issues/3194084.
  // @codingStandardsIgnoreLine

  //$assert_session->responseContains('classy/css/components/media-embed-error.css');

  // Test that restoring a valid UUID results in the media embed preview
  // displaying.
  $this->host->body->value = $original_value;
  $this->host
    ->save();
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'img[src*="image-test.png"]'));
  $assert_session
    ->elementNotExists('css', '.ck-widget.drupal-media .this-error-message-is-themeable');
}