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 {
protected static $modules = [
'filter',
'editor',
'editor_test',
'node',
];
protected $defaultTheme = 'stark';
protected $untrustedUser;
protected $normalUser;
protected $privilegedUser;
protected function setUp() : void {
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) = $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
->assertSession()
->elementNotExists('css', 'select.js-filter-list');
$this
->drupalLogout($this->normalUser);
$this
->drupalLogin($this->privilegedUser);
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present, $body) = $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
->assertSame($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
->assertSession()
->elementsCount('css', 'select.js-filter-list', 1);
$select = $this
->assertSession()
->elementExists('css', 'select.js-filter-list');
$this
->assertSame('edit-body-0-value', $select
->getAttribute('data-editor-for'));
$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) = $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
->assertSame($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
->assertSession()
->elementNotExists('css', 'select.js-filter-list');
$hidden_input = $this
->assertSession()
->hiddenFieldExists('body[0][format]');
$this
->assertSame('plain_text', $hidden_input
->getValue());
$this
->assertSame('edit-body-0-value', $hidden_input
->getAttribute('data-editor-for'));
$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) = $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
->assertSession()
->fieldDisabled("edit-body-0-value");
$this
->assertSession()
->fieldValueEquals("edit-body-0-value", 'This field has been disabled because you do not have sufficient permissions to edit it.');
$this
->assertSession()
->elementNotExists('css', 'select.js-filter-list');
$this
->assertSession()
->elementNotExists('xpath', '//input[@type="hidden" and contains(@class, "editor")]');
}
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) = $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
->assertSession()
->elementsCount('css', 'select.js-filter-list', 1);
$select = $this
->assertSession()
->elementExists('css', 'select.js-filter-list');
$this
->assertStringContainsString('editor', $select
->getAttribute('class'));
$this
->assertSame('edit-field-text-0-value', $select
->getAttribute('data-editor-for'));
$editor
->delete();
Editor::create([
'format' => 'full_html',
'editor' => 'trex',
])
->save();
$this
->drupalGet('node/1/edit');
list(, $editor_settings_present, $editor_js_present, $field) = $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
->assertSession()
->elementsCount('css', 'select.js-filter-list', 1);
$select = $this
->assertSession()
->elementExists('css', 'select.js-filter-list');
$this
->assertStringNotContainsString('editor', $select
->getAttribute('class'));
$this
->assertNotSame('edit-field-text-0-value', $select
->getAttribute('data-editor-for'));
}
protected function getThingsToCheck($field_name, $type = 'textarea') {
$settings = $this
->getDrupalSettings();
return [
$settings,
isset($settings['editor']),
strpos($this
->getSession()
->getPage()
->getContent(), $this
->getModulePath('editor') . '/js/editor.js') !== FALSE,
$this
->xpath('//' . $type . '[@id="edit-' . $field_name . '-0-value"]'),
];
}
}