Newer
Older
Angie Byron
committed
<?php
/**
* @file
Angie Byron
committed
* Contains \Drupal\editor\Tests\EditorLoadingTest.
Angie Byron
committed
*/
namespace Drupal\editor\Tests;
use Drupal\simpletest\WebTestBase;
/**
* Tests loading of text editors.
*
* @group editor
Angie Byron
committed
*/
class EditorLoadingTest extends WebTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('filter', 'editor', 'editor_test', 'node');
Alex Pott
committed
protected function setUp() {
Angie Byron
committed
parent::setUp();
Angie Byron
committed
// Add text formats.
$filtered_html_format = entity_create('filter_format', array(
Angie Byron
committed
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => array(),
Angie Byron
committed
));
$filtered_html_format->save();
$full_html_format = entity_create('filter_format', array(
Angie Byron
committed
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => array(),
Angie Byron
committed
));
$full_html_format->save();
Angie Byron
committed
// Create node type.
$this->drupalCreateContentType(array(
'type' => 'article',
'name' => 'Article',
));
// Create 3 users, each with access to different text formats:
// - "untrusted": plain_text
// - "normal": plain_text, filtered_html
// - "privileged": plain_text, filtered_html, full_html
$this->untrusted_user = $this->drupalCreateUser(array('create article content', 'edit any article content'));
$this->normal_user = $this->drupalCreateUser(array('create article content', 'edit any article content', 'use text format filtered_html'));
$this->privileged_user = $this->drupalCreateUser(array('create article content', 'edit any article content', 'use text format filtered_html', 'use text format full_html'));
}
/**
* Tests loading of text editors.
*/
Angie Byron
committed
public function testLoading() {
Angie Byron
committed
// Only associate a text editor with the "Full HTML" text format.
$editor = entity_create('editor', array(
'format' => 'full_html',
'editor' => 'unicorn',
));
$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->normal_user);
$this->drupalGet('node/add/article');
list( , $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
Angie Byron
committed
$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.');
Angie Byron
committed
$this->assertTrue(count($format_selector) === 0, 'No text format selector exists on the page because the user only has access to a single format.');
Angie Byron
committed
$this->drupalLogout($this->normal_user);
// The normal user:
Angie Byron
committed
// - has access to 2 text formats (and the fallback format);
Angie Byron
committed
// - does have access to the full_html text format, so: Unicorn text editor.
$this->drupalLogin($this->privileged_user);
$this->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
$expected = array('formats' => array('full_html' => array(
Dries Buytaert
committed
'format' => 'full_html',
Angie Byron
committed
'editor' => 'unicorn',
'editorSettings' => array('ponyModeEnabled' => TRUE),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
Angie Byron
committed
)));
$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.');
Angie Byron
committed
$specific_format_selector = $this->xpath('//select[contains(@class, "filter-list") and contains(@class, "editor") and @data-editor-for="edit-body-0-value"]');
Angie Byron
committed
$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->drupalLogout($this->privileged_user);
// 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->untrusted_user);
$this->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
$expected = array('formats' => array('plain_text' => array(
Dries Buytaert
committed
'format' => 'plain_text',
Angie Byron
committed
'editor' => 'unicorn',
'editorSettings' => array('ponyModeEnabled' => TRUE),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
Angie Byron
committed
)));
$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.');
Angie Byron
committed
$hidden_input = $this->xpath('//input[@type="hidden" and @value="plain_text" and contains(@class, "editor") and @data-editor-for="edit-body-0-value"]');
Angie Byron
committed
$this->assertTrue(count($hidden_input) === 1, 'A single text format hidden input exists on the page and has the "editor" class and a "data-editor-for" attribute with the correct value.');
// Create an "article" node that users the full_html text format, then try
// to let the untrusted user edit it.
$this->drupalCreateNode(array(
'type' => 'article',
Angie Byron
committed
'body' => array(
Alex Pott
committed
array('value' => $this->randomMachineName(32), 'format' => 'full_html')
Angie Byron
committed
),
Angie Byron
committed
));
// The untrusted user tries to edit content that is written in a text format
Alex Pott
committed
// that (s)he is not allowed to use. The editor is still loaded. CKEditor,
// for example, supports being loaded in a disabled state.
Angie Byron
committed
$this->drupalGet('node/1/edit');
list( , $editor_settings_present, $editor_js_present, $body, $format_selector) = $this->getThingsToCheck();
Alex Pott
committed
$this->assertTrue($editor_settings_present, 'Text Editor module settings.');
$this->assertTrue($editor_js_present, 'Text Editor JavaScript.');
Angie Byron
committed
$this->assertTrue(count($body) === 1, 'A body field exists.');
Angie Byron
committed
$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.');
Angie Byron
committed
$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.');
}
protected function getThingsToCheck() {
$settings = $this->getDrupalSettings();
Angie Byron
committed
return array(
// JavaScript settings.
$settings,
// Editor.module's JS settings present.
isset($settings['editor']),
// Editor.module's JS present.
isset($settings['ajaxPageState']['js']['core/modules/editor/js/editor.js']),
// Body field.
Angie Byron
committed
$this->xpath('//textarea[@id="edit-body-0-value"]'),
Angie Byron
committed
// Format selector.
$this->xpath('//select[contains(@class, "filter-list")]'),
);
}
Angie Byron
committed