You are here

public function CKEditorLoadingTest::testLoading in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/ckeditor/tests/src/Functional/CKEditorLoadingTest.php \Drupal\Tests\ckeditor\Functional\CKEditorLoadingTest::testLoading()

Tests loading of CKEditor CSS, JS and JS settings.

File

core/modules/ckeditor/tests/src/Functional/CKEditorLoadingTest.php, line 90

Class

CKEditorLoadingTest
Tests loading of CKEditor.

Namespace

Drupal\Tests\ckeditor\Functional

Code

public function testLoading() {

  // The untrusted user:
  // - has access to 1 text format (plain_text);
  // - doesn't have access to the filtered_html text format, so: no text editor.
  $this
    ->drupalLogin($this->untrustedUser);
  $this
    ->drupalGet('node/add/article');
  list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck();
  $this
    ->assertFalse($editor_settings_present, 'No Text Editor module settings.');
  $this
    ->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
  $this
    ->assertCount(1, $body, 'A body field exists.');
  $this
    ->assertCount(0, $format_selector, 'No text format selector exists on the page.');
  $hidden_input = $this
    ->xpath('//input[@type="hidden" and contains(@class, "editor")]');
  $this
    ->assertCount(0, $hidden_input, 'A single text format hidden input does not exist on the page.');
  $this
    ->assertNoRaw(drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js', 'CKEditor glue JS is absent.');

  // On pages where there would never be a text editor, CKEditor JS is absent.
  $this
    ->drupalGet('user');
  $this
    ->assertNoRaw(drupal_get_path('module', 'ckeditor') . '/js/ckeditor.js', 'CKEditor glue JS is absent.');

  // The normal user:
  // - has access to 2 text formats;
  // - does have access to the filtered_html text format, so: CKEditor.
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('node/add/article');
  list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck();
  $ckeditor_plugin = $this->container
    ->get('plugin.manager.editor')
    ->createInstance('ckeditor');
  $editor = Editor::load('filtered_html');
  $expected = [
    'formats' => [
      'filtered_html' => [
        'format' => 'filtered_html',
        'editor' => 'ckeditor',
        'editorSettings' => $this
          ->castSafeStrings($ckeditor_plugin
          ->getJSSettings($editor)),
        'editorSupportsContentFiltering' => TRUE,
        'isXssSafe' => FALSE,
      ],
    ],
  ];
  $this
    ->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
  $this
    ->assertIdentical($expected, $this
    ->castSafeStrings($settings['editor']), "Text Editor module's JavaScript settings on the page are correct.");
  $this
    ->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
  $this
    ->assertCount(1, $body, 'A body field exists.');
  $this
    ->assertCount(1, $format_selector, 'A single text format selector exists on the page.');
  $specific_format_selector = $this
    ->xpath('//select[contains(@class, "filter-list") and @data-editor-for="edit-body-0-value"]');
  $this
    ->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
  $this
    ->assertContains('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries']), 'CKEditor glue library is present.');

  // Enable the ckeditor_test module, customize configuration. In this case,
  // there is additional CSS and JS to be loaded.
  // NOTE: the tests in CKEditorTest already ensure that changing the
  // configuration also results in modified CKEditor configuration, so we
  // don't test that here.
  \Drupal::service('module_installer')
    ->install([
    'ckeditor_test',
  ]);
  $this->container
    ->get('plugin.manager.ckeditor.plugin')
    ->clearCachedDefinitions();
  $editor_settings = $editor
    ->getSettings();
  $editor_settings['toolbar']['rows'][0][0]['items'][] = 'Llama';
  $editor
    ->setSettings($editor_settings);
  $editor
    ->save();
  $this
    ->drupalGet('node/add/article');
  list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck();
  $expected = [
    'formats' => [
      'filtered_html' => [
        'format' => 'filtered_html',
        'editor' => 'ckeditor',
        'editorSettings' => $this
          ->castSafeStrings($ckeditor_plugin
          ->getJSSettings($editor)),
        'editorSupportsContentFiltering' => TRUE,
        'isXssSafe' => FALSE,
      ],
    ],
  ];
  $this
    ->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
  $this
    ->assertIdentical($expected, $this
    ->castSafeStrings($settings['editor']), "Text Editor module's JavaScript settings on the page are correct.");
  $this
    ->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
  $this
    ->assertContains('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries']), 'CKEditor glue library is present.');

  // Assert that CKEditor uses Drupal's cache-busting query string by
  // comparing the setting sent with the page with the current query string.
  $settings = $this
    ->getDrupalSettings();
  $expected = $settings['ckeditor']['timestamp'];
  $this
    ->assertIdentical($expected, \Drupal::state()
    ->get('system.css_js_query_string'), "CKEditor scripts cache-busting string is correct before flushing all caches.");

  // Flush all caches then make sure that $settings['ckeditor']['timestamp']
  // still matches.
  drupal_flush_all_caches();
  $this
    ->assertIdentical($expected, \Drupal::state()
    ->get('system.css_js_query_string'), "CKEditor scripts cache-busting string is correct after flushing all caches.");
}