You are here

public function EditorLoadingTest::testLoading in Zircon Profile 8

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

Tests loading of text editors.

File

core/modules/editor/src/Tests/EditorLoadingTest.php, line 114
Contains \Drupal\editor\Tests\EditorLoadingTest.

Class

EditorLoadingTest
Tests loading of text editors.

Namespace

Drupal\editor\Tests

Code

public function testLoading() {

  // Only associate a text editor with the "Full HTML" text format.
  $editor = entity_create('editor', array(
    'format' => 'full_html',
    'editor' => 'unicorn',
    'image_upload' => array(
      'status' => FALSE,
      'scheme' => file_default_scheme(),
      'directory' => 'inline-images',
      'max_size' => '',
      'max_dimensions' => array(
        'width' => '',
        'height' => '',
      ),
    ),
  ));
  $editor
    ->save();

  // The normal user:
  // - has access to 2 text formats;
  // - doesn't have access to the full_html text format, so: no text editor.
  $this
    ->drupalLogin($this->normalUser);
  $this
    ->drupalGet('node/add/article');
  list(, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck('body');
  $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 because the user only has access to a single format.');
  $this
    ->drupalLogout($this->normalUser);

  // The privileged user:
  // - has access to 2 text formats (and the fallback format);
  // - does have access to the full_html text format, so: Unicorn text editor.
  $this
    ->drupalLogin($this->privilegedUser);
  $this
    ->drupalGet('node/add/article');
  list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck('body');
  $expected = array(
    'formats' => array(
      'full_html' => array(
        'format' => 'full_html',
        'editor' => 'unicorn',
        'editorSettings' => array(
          'ponyModeEnabled' => TRUE,
        ),
        'editorSupportsContentFiltering' => TRUE,
        'isXssSafe' => FALSE,
      ),
    ),
  );
  $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 @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.');

  // Load the editor image dialog form and make sure it does not fatal.
  $this
    ->drupalGet('editor/dialog/image/full_html');
  $this
    ->assertResponse(200);
  $this
    ->drupalLogout($this->privilegedUser);

  // Also associate a text editor with the "Plain Text" text format.
  $editor = entity_create('editor', array(
    'format' => 'plain_text',
    'editor' => 'unicorn',
  ));
  $editor
    ->save();

  // The untrusted user:
  // - has access to 1 text format (plain_text);
  // - has access to the plain_text text format, so: Unicorn text editor.
  $this
    ->drupalLogin($this->untrustedUser);
  $this
    ->drupalGet('node/add/article');
  list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck('body');
  $expected = array(
    'formats' => array(
      'plain_text' => array(
        'format' => 'plain_text',
        'editor' => 'unicorn',
        'editorSettings' => array(
          'ponyModeEnabled' => TRUE,
        ),
        'editorSupportsContentFiltering' => TRUE,
        'isXssSafe' => FALSE,
      ),
    ),
  );
  $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) === 0, 'No text format selector exists on the page.');
  $hidden_input = $this
    ->xpath('//input[@type="hidden" and @value="plain_text" and @data-editor-for="edit-body-0-value"]');
  $this
    ->assertTrue(count($hidden_input) === 1, 'A single text format hidden input exists on the page and has a "data-editor-for" attribute with the correct value.');

  // Create an "article" node that uses the full_html text format, then try
  // to let the untrusted user edit it.
  $this
    ->drupalCreateNode(array(
    'type' => 'article',
    'body' => array(
      array(
        'value' => $this
          ->randomMachineName(32),
        'format' => 'full_html',
      ),
    ),
  ));

  // The untrusted user tries to edit content that is written in a text format
  // that (s)he is not allowed to use. The editor is still loaded. CKEditor,
  // for example, supports being loaded in a disabled state.
  $this
    ->drupalGet('node/1/edit');
  list(, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
    ->getThingsToCheck('body');
  $this
    ->assertTrue($editor_settings_present, 'Text Editor module settings.');
  $this
    ->assertTrue($editor_js_present, 'Text Editor JavaScript.');
  $this
    ->assertTrue(count($body) === 1, 'A body field exists.');
  $this
    ->assertFieldByXPath('//textarea[@id="edit-body-0-value" and @disabled="disabled"]', t('This field has been disabled because you do not have sufficient permissions to edit it.'), 'Text format access denied message found.');
  $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.');
}