View source
<?php
namespace Drupal\Tests\ckeditor\Functional;
use Drupal\editor\Entity\Editor;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class CKEditorLoadingTest extends BrowserTestBase {
protected static $modules = [
'filter',
'editor',
'ckeditor',
'node',
];
protected $defaultTheme = 'stark';
protected $untrustedUser;
protected $normalUser;
protected function setUp() : void {
parent::setUp();
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [],
]);
$filtered_html_format
->save();
$editor = Editor::create([
'format' => 'filtered_html',
'editor' => 'ckeditor',
]);
$editor
->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->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',
'use text format full_html',
]);
}
public function testLoading() {
$this
->drupalLogin($this->untrustedUser);
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present) = $this
->getThingsToCheck();
$this
->assertFalse($editor_settings_present, 'No Text Editor module settings.');
$this
->assertFalse($editor_js_present, 'No Text Editor JavaScript.');
$this
->assertSession()
->fieldExists('edit-body-0-value');
$this
->assertSession()
->elementNotExists('css', 'select.js-filter-list');
$this
->assertSession()
->elementNotExists('xpath', '//input[@type="hidden" and contains(@class, "editor")]');
$this
->assertSession()
->responseNotContains($this
->getModulePath('ckeditor') . '/js/ckeditor.js');
$this
->drupalGet('user');
$this
->assertSession()
->responseNotContains($this
->getModulePath('ckeditor') . '/js/ckeditor.js');
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present) = $this
->getThingsToCheck();
$ckeditor_plugin = $this->container
->get('plugin.manager.editor')
->createInstance('ckeditor');
$editor = Editor::load('filtered_html');
$expected = [
'formats' => [
'filtered_html' => [
'format' => 'filtered_html',
'editor' => 'ckeditor',
'editorSettings' => $ckeditor_plugin
->getJSSettings($editor),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
],
],
];
$this
->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this
->assertEquals($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
->assertSession()
->fieldExists('edit-body-0-value');
$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
->assertContains('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries']), 'CKEditor glue library is present.');
\Drupal::service('module_installer')
->install([
'ckeditor_test',
]);
$this->container
->get('plugin.manager.ckeditor.plugin')
->clearCachedDefinitions();
$editor_settings = $editor
->getSettings();
$editor_settings['toolbar']['rows'][0][0]['items'][] = 'Llama';
$editor
->setSettings($editor_settings);
$editor
->save();
$this
->drupalGet('node/add/article');
list($settings, $editor_settings_present, $editor_js_present) = $this
->getThingsToCheck();
$expected = [
'formats' => [
'filtered_html' => [
'format' => 'filtered_html',
'editor' => 'ckeditor',
'editorSettings' => $ckeditor_plugin
->getJSSettings($editor),
'editorSupportsContentFiltering' => TRUE,
'isXssSafe' => FALSE,
],
],
];
$this
->assertTrue($editor_settings_present, "Text Editor module's JavaScript settings are on the page.");
$this
->assertEquals($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
->assertContains('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries']), 'CKEditor glue library is present.');
$settings = $this
->getDrupalSettings();
$expected = $settings['ckeditor']['timestamp'];
$this
->assertSame($expected, \Drupal::state()
->get('system.css_js_query_string'), "CKEditor scripts cache-busting string is correct before flushing all caches.");
$this
->resetAll();
$this
->assertSame($expected, \Drupal::state()
->get('system.css_js_query_string'), "CKEditor scripts cache-busting string is correct after flushing all caches.");
}
public function testLoadingWithoutInternalButtons() {
$filtered_html_editor = Editor::load('filtered_html');
$settings = $filtered_html_editor
->getSettings();
$settings['toolbar']['rows'] = [
0 => [
0 => [
'name' => 'Links',
'items' => [
'DrupalLink',
'DrupalUnlink',
],
],
],
];
$filtered_html_editor
->setSettings($settings)
->save();
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('node/add/article');
$editor_settings = $this
->getDrupalSettings()['editor']['formats']['filtered_html']['editorSettings'];
$this
->assertTrue(isset($editor_settings['customConfig']));
$this
->assertTrue(isset($editor_settings['entities']));
$this
->assertTrue(isset($editor_settings['allowedContent']));
$this
->assertTrue(isset($editor_settings['disallowedContent']));
}
protected function getThingsToCheck() {
$settings = $this
->getDrupalSettings();
return [
$settings,
isset($settings['editor']),
isset($settings['ajaxPageState']['libraries']) && in_array('ckeditor/drupal.ckeditor', explode(',', $settings['ajaxPageState']['libraries'])),
];
}
}