You are here

public function MediaImageTest::testPreviewUsesDefaultThemeAndIsClientCacheable in Entity Embed 8

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

File

tests/src/FunctionalJavascript/MediaImageTest.php, line 705

Class

MediaImageTest
Test Media Image specific functionality.

Namespace

Drupal\Tests\entity_embed\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();
  $this->container
    ->get('router.builder')
    ->rebuild();

  // 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 entity_embed_test_entity_view_alter()
  $this->host->body->value = '<drupal-entity data-caption="baz" data-embed-button="test_media_entity_embed" data-entity-embed-display="entity_reference:entity_reference_entity_view" data-entity-embed-display-settings="full" data-entity-type="media" data-entity-uuid="' . $this->media
    ->uuid() . '"></drupal-entity>';
  $this->host
    ->save();
  $this
    ->drupalGet($this->host
    ->toUrl('edit-form'));
  $this
    ->waitForEditor();
  $this
    ->assignNameToCkeditorIframe();
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');
  $this
    ->assertSession()
    ->waitForElementVisible('css', 'img[src*="image-test.png"]');
  $element = $this
    ->assertSession()
    ->elementExists('css', '[data-entity-embed-test-active-theme]');
  $this
    ->assertSame('stable', $element
    ->getAttribute('data-entity-embed-test-active-theme'));

  // Assert that the first preview request transferred data 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(0, $this
    ->getLastPreviewRequestTransferSize());
  $this
    ->pressEditorButton('source');
  $this
    ->assertSession()
    ->waitForElement('css', 'textarea.cke_source');
  $this
    ->pressEditorButton('source');
  $this
    ->assignNameToCkeditorIframe();
  $this
    ->getSession()
    ->switchToIFrame('ckeditor');
  $this
    ->assertSession()
    ->waitForElementVisible('css', 'img[src*="image-test.png"]');
  $this
    ->assertSame(0, $this
    ->getLastPreviewRequestTransferSize());
}