You are here

function CKEditorLoadingTest::testLoading in Zircon Profile 8

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

Tests loading of CKEditor CSS, JS and JS settings.

File

core/modules/ckeditor/src/Tests/CKEditorLoadingTest.php, line 80
Contains \Drupal\ckeditor\Tests\CKEditorLoadingTest.

Class

CKEditorLoadingTest
Tests loading of CKEditor.

Namespace

Drupal\ckeditor\Tests

Code

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
    ->assertTrue(count($body) === 1, 'A body field exists.');
  $this
    ->assertTrue(count($format_selector) === 0, 'No text format selector exists on the page.');
  $hidden_input = $this
    ->xpath('//input[@type="hidden" and contains(@class, "editor")]');
  $this
    ->assertTrue(count($hidden_input) === 0, '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 = entity_load('editor', 'filtered_html');
  $expected = array(
    'formats' => array(
      'filtered_html' => array(
        '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
    ->assertTrue(count($body) === 1, 'A body field exists.');
  $this
    ->assertTrue(count($format_selector) === 1, '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
    ->assertTrue(count($specific_format_selector) === 1, 'A single text format selector exists on the page and has a "data-editor-for" attribute with the correct value.');
  $this
    ->assertTrue(in_array('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(array(
    '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 = array(
    'formats' => array(
      'filtered_html' => array(
        '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
    ->assertTrue(in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])), 'CKEditor glue library is present.');
}