function CKEditorLoadingTest::testLoading

Tests loading of CKEditor CSS, JS and JS settings.

File

drupal/core/modules/ckeditor/lib/Drupal/ckeditor/Tests/CKEditorLoadingTest.php, line 75
Definition of \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->untrusted_user);
  $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->normal_user);
  $this
    ->drupalGet('node/add/article');
  list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck();
  $ckeditor_plugin = drupal_container()
    ->get('plugin.manager.editor')
    ->createInstance('ckeditor');
  $editor = entity_load('editor', 'filtered_html');
  $expected = array(
    'formats' => array(
      'filtered_html' => array(
        'editor' => 'ckeditor',
        'editorSettings' => $ckeditor_plugin
          ->getJSSettings($editor),
      ),
    ),
  );
  $this
    ->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
  $this
    ->assertIdentical($expected, $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 contains(@class, "editor") and @data-editor-for="edit-body-und-0-value"]');
  $this
    ->assertTrue(count($specific_format_selector) === 1, 'A single text format selector exists on the page and has the "editor" class and a "data-editor-for" attribute with the correct value.');
  $this
    ->assertTrue(isset($settings['ajaxPageState']['js']['core/modules/ckeditor/js/ckeditor.js']), 'CKEditor glue JS is present.');
  $this
    ->assertTrue(isset($settings['ajaxPageState']['js']['core/misc/ckeditor/ckeditor.js']), 'CKEditor lib JS 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.
  module_enable(array(
    'ckeditor_test',
  ));
  drupal_container()
    ->get('plugin.manager.ckeditor.plugin')
    ->clearCachedDefinitions();
  $editor->settings['toolbar']['buttons'][0][] = 'Llama';
  $editor->settings['plugins']['internal']['link_shortcut'] = 'CTRL+K';
  $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(
        'editor' => 'ckeditor',
        'editorSettings' => $ckeditor_plugin
          ->getJSSettings($editor),
      ),
    ),
  );
  $this
    ->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
  $this
    ->assertIdentical($expected, $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(isset($settings['ajaxPageState']['js']['core/modules/ckeditor/js/ckeditor.js']), 'CKEditor glue JS is present.');
  $this
    ->assertTrue(isset($settings['ajaxPageState']['js']['core/misc/ckeditor/ckeditor.js']), 'CKEditor lib JS is present.');
}