You are here

public function CKEditorIntegrationTest::testPreviewUsesDefaultThemeAndIsClientCacheable in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php \Drupal\Tests\media\FunctionalJavascript\CKEditorIntegrationTest::testPreviewUsesDefaultThemeAndIsClientCacheable()

The CKEditor Widget must load a preview generated using the default theme.

File

core/modules/media/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php, line 266

Class

CKEditorIntegrationTest
@coversDefaultClass \Drupal\media\Plugin\CKEditorPlugin\DrupalMedia @group media

Namespace

Drupal\Tests\media\FunctionalJavascript

Code

public function testPreviewUsesDefaultThemeAndIsClientCacheable() {

  // Make the node edit form use the admin theme, like on most Drupal sites.
  $this
    ->config('node.settings')
    ->set('use_admin_theme', TRUE)
    ->save();

  // Allow the test user to view the admin theme.
  $this->adminUser
    ->addRole($this
    ->drupalCreateRole([
    'view the administration theme',
  ]));
  $this->adminUser
    ->save();

  // Configure a different default and admin theme, like on most Drupal sites.
  $this
    ->config('system.theme')
    ->set('default', 'stable')
    ->set('admin', 'classy')
    ->save();

  // Assert that when looking at an embedded entity in the CKEditor Widget,
  // the preview is generated using the default theme, not the admin theme.
  // @see media_test_embed_entity_view_alter()
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assignNameToCkeditorIframe();
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');
  $assert_session = $this
    ->assertSession();
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'img[src*="image-test.png"]'));
  $element = $assert_session
    ->elementExists('css', '[data-media-embed-test-active-theme]');
  $this
    ->assertSame('stable', $element
    ->getAttribute('data-media-embed-test-active-theme'));

  // Assert that the first preview request transferred >500 B over the wire.
  // Then toggle source mode on and off. This causes the CKEditor widget to be
  // destroyed and then reconstructed. Assert that during this reconstruction,
  // a second request is sent. This second request should have transferred 0
  // bytes: the browser should have cached the response, thus resulting in a
  // much better user experience.
  $this
    ->assertGreaterThan(500, $this
    ->getLastPreviewRequestTransferSize());
  $this
    ->pressEditorButton('source');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElement('css', 'textarea.cke_source'));
  $this
    ->pressEditorButton('source');
  $this
    ->assignNameToCkeditorIframe();
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');
  $this
    ->assertNotEmpty($assert_session
    ->waitForElementVisible('css', 'img[src*="image-test.png"]'));
  $this
    ->assertSame(0, $this
    ->getLastPreviewRequestTransferSize());
}