function MediaTest::testPreviewUsesDefaultThemeAndIsClientCacheable

Same name in this branch
  1. 9 core/modules/ckeditor5/tests/src/FunctionalJavascript/MediaTest.php \Drupal\Tests\ckeditor5\FunctionalJavascript\MediaTest::testPreviewUsesDefaultThemeAndIsClientCacheable()

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

File

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

Class

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

Namespace

Drupal\Tests\ckeditor\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', 'stable9')
        ->set('admin', 'starterkit_theme')
        ->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('stable9', $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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.