View source
<?php
namespace Drupal\Tests\editor\Functional;
use Drupal\editor\Entity\Editor;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class EditorLoadingTest extends BrowserTestBase {
public static $modules = [
'filter',
'editor',
'editor_test',
'node',
];
protected $defaultTheme = 'stark';
protected $untrustedUser;
protected $normalUser;
protected $privilegedUser;
protected function setUp() {
parent::setUp();
\Drupal::state()
->set('editor_test_give_me_a_trex_thanks', TRUE);
\Drupal::service('plugin.manager.editor')
->clearCachedDefinitions();
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [],
]);
$filtered_html_format
->save();
$full_html_format = FilterFormat::create([
'format' => 'full_html',
'name' => 'Full HTML',
'weight' => 1,
'filters' => [],
]);
$full_html_format
->save();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$this
->drupalCreateContentType([
'type' => 'page',
'name' => 'Page',
]);
$body = FieldConfig::loadByName('node', 'page', 'body');
$body
->delete();
FieldStorageConfig::create([
'field_name' => 'field_text',
'entity_type' => 'node',
'type' => 'text',
])
->save();
FieldConfig::create([
'field_name' => 'field_text',
'entity_type' => 'node',
'label' => 'Textfield',
'bundle' => 'page',
])
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'page')
->setComponent('field_text')
->save();
$this->untrustedUser = $this
->drupalCreateUser([
'create article content',
'edit any article content',
]);
$this->normalUser = $this
->drupalCreateUser([
'create article content',
'edit any article content',
'use text format filtered_html',
]);
$this->privilegedUser = $this
->drupalCreateUser([
'create article content',
'edit any article content',
'create page content',
'edit any page content',
'use text format filtered_html',
'use text format full_html',
]);
}
public function testLoading() {
$editor = Editor::create([
'format' => 'full_html',
'editor' => 'unicorn',
'image_upload' => [
'status' => FALSE,
'scheme' => 'public',
'directory' => 'inline-images',
'max_size' => '',
'max_dimensions' => [
'width' => '',
'height' => '',
],
],
]);
$editor
->save();
$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
->assertCount(1, $body, 'A body field exists.');
$this
->assertCount(0, $format_selector, 'No text format selector exists on the page because the user only has access to a single format.');
$this
->drupalLogout($this->normalUser);
$this
->drupalLogin($this->privilegedUser);
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
->getThingsToCheck('body');
$expected = [
'formats' => [
'full_html' => [
'format' => 'full_html',
'editor' => 'unicorn',
'editorSettings' => [
'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
->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
->drupalGet('editor/dialog/image/full_html');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalLogout($this->privilegedUser);
$editor = Editor::create([
'format' => 'plain_text',
'editor' => 'unicorn',
]);
$editor
->save();
$this
->drupalLogin($this->untrustedUser);
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body, $format_selector) = $this
->getThingsToCheck('body');
$expected = [
'formats' => [
'plain_text' => [
'format' => 'plain_text',
'editor' => 'unicorn',
'editorSettings' => [
'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
->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 @value="plain_text" and @data-editor-for="edit-body-0-value"]');
$this
->assertCount(1, $hidden_input, 'A single text format hidden input exists on the page and has a "data-editor-for" attribute with the correct value.');
$this
->drupalCreateNode([
'type' => 'article',
'body' => [
[
'value' => $this
->randomMachineName(32),
'format' => 'full_html',
],
],
]);
$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
->assertCount(1, $body, '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
->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.');
}
public function testSupportedElementTypes() {
$editor = Editor::create([
'format' => 'full_html',
'editor' => 'unicorn',
'image_upload' => [
'status' => FALSE,
'scheme' => 'public',
'directory' => 'inline-images',
'max_size' => '',
'max_dimensions' => [
'width' => '',
'height' => '',
],
],
]);
$editor
->save();
$this
->drupalCreateNode([
'type' => 'page',
'field_text' => [
[
'value' => $this
->randomMachineName(32),
'format' => 'full_html',
],
],
]);
$this
->drupalLogin($this->privilegedUser);
$this
->drupalGet('node/1/edit');
list(, $editor_settings_present, $editor_js_present, $field, $format_selector) = $this
->getThingsToCheck('field-text', 'input');
$this
->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this
->assertTrue($editor_js_present, 'Text Editor JavaScript is present.');
$this
->assertCount(1, $field, 'A text 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 contains(@class, "editor") and @data-editor-for="edit-field-text-0-value"]');
$this
->assertCount(1, $specific_format_selector, 'A single text format selector exists on the page and has the "editor" class and a "data-editor-for" attribute with the correct value.');
$editor
->delete();
Editor::create([
'format' => 'full_html',
'editor' => 'trex',
])
->save();
$this
->drupalGet('node/1/edit');
list(, $editor_settings_present, $editor_js_present, $field, $format_selector) = $this
->getThingsToCheck('field-text', 'input');
$this
->assertFalse($editor_settings_present, "Text Editor module's JavaScript settings are not on the page.");
$this
->assertFalse($editor_js_present, 'Text Editor JavaScript is not present.');
$this
->assertCount(1, $field, 'A text 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 contains(@class, "editor") and @data-editor-for="edit-field-text-0-value"]');
$this
->assertCount(0, $specific_format_selector, 'No text format selector exists on the page with the "editor" class and a "data-editor-for" attribute with the expected value.');
}
protected function getThingsToCheck($field_name, $type = 'textarea') {
$settings = $this
->getDrupalSettings();
return [
$settings,
isset($settings['editor']),
strpos($this
->getSession()
->getPage()
->getContent(), drupal_get_path('module', 'editor') . '/js/editor.js') !== FALSE,
$this
->xpath('//' . $type . '[@id="edit-' . $field_name . '-0-value"]'),
$this
->xpath('//select[contains(@class, "filter-list")]'),
];
}
}