View source
<?php
namespace Drupal\Tests\ckeditor\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\editor\Entity\Editor;
use Drupal\filter\FilterFormatInterface;
use Drupal\filter\Entity\FilterFormat;
use Drupal\Tests\BrowserTestBase;
class CKEditorAdminTest extends BrowserTestBase {
protected static $modules = [
'filter',
'editor',
'ckeditor',
];
protected $defaultTheme = 'stark';
protected $adminUser;
protected function setUp() : void {
parent::setUp();
$filtered_html_format = FilterFormat::create([
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'weight' => 0,
'filters' => [],
]);
$filtered_html_format
->save();
$this->adminUser = $this
->drupalCreateUser([
'administer filters',
]);
}
public function testExistingFormat() {
$ckeditor = $this->container
->get('plugin.manager.editor')
->createInstance('ckeditor');
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$editor = Editor::load('filtered_html');
$this
->assertNull($editor, 'No Editor config entity exists yet.');
$select = $this
->assertSession()
->selectExists('editor[editor]');
$this
->assertFalse($select
->hasAttribute('disabled'));
$options = $select
->findAll('css', 'option');
$this
->assertCount(2, $options);
$this
->assertSame('None', $options[0]
->getText());
$this
->assertSame('CKEditor', $options[1]
->getText());
$this
->assertTrue($options[0]
->isSelected());
$edit = [
'editor[editor]' => 'ckeditor',
];
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->pageTextContains('You must configure the selected text editor.');
$expected_default_settings = [
'toolbar' => [
'rows' => [
[
[
'name' => 'Formatting',
'items' => [
'Bold',
'Italic',
],
],
[
'name' => 'Links',
'items' => [
'DrupalLink',
'DrupalUnlink',
],
],
[
'name' => 'Lists',
'items' => [
'BulletedList',
'NumberedList',
],
],
[
'name' => 'Media',
'items' => [
'Blockquote',
'DrupalImage',
],
],
[
'name' => 'Tools',
'items' => [
'Source',
],
],
],
],
],
'plugins' => [
'language' => [
'language_list' => 'un',
],
],
];
$this
->assertEquals($expected_default_settings, $ckeditor
->getDefaultSettings());
$this
->submitForm($edit, 'editor_configure');
$editor = Editor::load('filtered_html');
$this
->assertNull($editor, 'No Editor config entity exists yet.');
$ckeditor_settings_toolbar = [
'#theme' => 'ckeditor_settings_toolbar',
'#editor' => Editor::create([
'editor' => 'ckeditor',
]),
'#plugins' => $this->container
->get('plugin.manager.ckeditor.plugin')
->getButtons(),
];
$settings = $this
->getDrupalSettings();
$expected = $settings['ckeditor']['toolbarAdmin'];
$this
->assertEquals($expected, $this->container
->get('renderer')
->renderPlain($ckeditor_settings_toolbar), 'CKEditor toolbar settings are rendered as part of drupalSettings.');
$expected_buttons_value = json_encode($expected_default_settings['toolbar']['rows']);
$this
->assertSession()
->fieldValueEquals('editor[settings][toolbar][button_groups]', $expected_buttons_value);
$this
->assertSession()
->fieldValueEquals('editor[settings][plugins][stylescombo][styles]', '');
$this
->submitForm($edit, 'Save configuration');
$expected_settings = $expected_default_settings;
$expected_settings['plugins']['stylescombo']['styles'] = '';
$editor = Editor::load('filtered_html');
$this
->assertInstanceOf(Editor::class, $editor);
$this
->assertEquals($expected_settings, $editor
->getSettings(), 'The Editor config entity has the correct settings.');
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$edit = [
'editor[settings][plugins][stylescombo][styles]' => "h1.title|Title\np.callout|Callout\n\n",
];
$this
->submitForm($edit, 'Save configuration');
$expected_settings['plugins']['stylescombo']['styles'] = "h1.title|Title\np.callout|Callout\n\n";
$editor = Editor::load('filtered_html');
$this
->assertInstanceOf(Editor::class, $editor);
$this
->assertEquals($expected_settings, $editor
->getSettings(), 'The Editor config entity has the correct settings.');
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$expected_settings['toolbar']['rows'][0][] = [
'name' => 'Action history',
'items' => [
'Undo',
'|',
'Redo',
'JustifyCenter',
],
];
$edit = [
'editor[settings][toolbar][button_groups]' => json_encode($expected_settings['toolbar']['rows']),
];
$this
->submitForm($edit, 'Save configuration');
$editor = Editor::load('filtered_html');
$this
->assertInstanceOf(Editor::class, $editor);
$this
->assertEquals($expected_settings, $editor
->getSettings(), 'The Editor config entity has the correct settings.');
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$json_encode = function ($html) {
return trim(Json::encode($html), '"');
};
$this
->assertSession()
->responseContains($json_encode('<li data-drupal-ckeditor-button-name="-" class="ckeditor-button-separator ckeditor-multiple-button" data-drupal-ckeditor-type="separator"><a href="#" role="button" aria-label="Button separator" class="ckeditor-separator"></a></li>'));
$this
->assertSession()
->responseContains($json_encode('<li data-drupal-ckeditor-button-name="Format" class="ckeditor-button"><a href="#" role="button" aria-label="Format"><span class="ckeditor-button-dropdown">Format<span class="ckeditor-button-arrow"></span></span></a></li>'));
$this
->assertSession()
->responseContains($json_encode('<li data-drupal-ckeditor-button-name="Styles" class="ckeditor-button"><a href="#" role="button" aria-label="Styles"><span class="ckeditor-button-dropdown">Styles<span class="ckeditor-button-arrow"></span></span></a></li>'));
$this
->assertSession()
->responseContains($json_encode('<li data-drupal-ckeditor-button-name="Strike" class="ckeditor-button"><a href="#" class="cke-icon-only cke_ltr" role="button" title="strike" aria-label="strike"><span class="cke_button_icon cke_button__strike_icon">strike</span></a></li>'));
\Drupal::service('module_installer')
->install([
'ckeditor_test',
]);
$this
->resetAll();
$this->container
->get('plugin.manager.ckeditor.plugin')
->clearCachedDefinitions();
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$this
->assertSession()
->checkboxNotChecked('editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]');
$editor = Editor::load('filtered_html');
$this
->assertInstanceOf(Editor::class, $editor);
$this
->assertEquals($expected_settings, $editor
->getSettings(), 'The Editor config entity has the correct settings.');
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$edit = [
'editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]' => '1',
];
$this
->submitForm($edit, 'Save configuration');
$this
->drupalGet('admin/config/content/formats/manage/filtered_html');
$this
->assertSession()
->checkboxChecked('editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]');
$expected_settings['plugins']['llama_contextual_and_button']['ultra_llama_mode'] = TRUE;
$editor = Editor::load('filtered_html');
$this
->assertInstanceOf(Editor::class, $editor);
$this
->assertEquals($expected_settings, $editor
->getSettings());
$this
->drupalGet('admin/config/content/formats/add');
$edit = [
'format' => 'filtered_html',
'name' => 'Filtered HTML',
'editor[editor]' => 'ckeditor',
];
$this
->submitForm($edit, 'editor_configure');
$this
->submitForm($edit, 'Save configuration');
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->pageTextContains('The machine-readable name is already in use. It must be unique.');
}
public function testNewFormat() {
$this
->drupalLogin($this->adminUser);
$this
->drupalGet('admin/config/content/formats/add');
$select = $this
->assertSession()
->selectExists('editor[editor]');
$this
->assertFalse($select
->hasAttribute('disabled'));
$options = $select
->findAll('css', 'option');
$this
->assertCount(2, $options);
$this
->assertSame('None', $options[0]
->getText());
$this
->assertSame('CKEditor', $options[1]
->getText());
$this
->assertTrue($options[0]
->isSelected());
$edit = [
'name' => 'My amazing text format',
'format' => 'amazing_format',
'editor[editor]' => 'ckeditor',
];
$this
->submitForm($edit, 'editor_configure');
$filter_format = FilterFormat::load('amazing_format');
$this
->assertNull($filter_format, 'No FilterFormat config entity exists yet.');
$editor = Editor::load('amazing_format');
$this
->assertNull($editor, 'No Editor config entity exists yet.');
$ckeditor = $this->container
->get('plugin.manager.editor')
->createInstance('ckeditor');
$default_settings = $ckeditor
->getDefaultSettings();
$expected_buttons_value = json_encode($default_settings['toolbar']['rows']);
$this
->assertSession()
->fieldValueEquals('editor[settings][toolbar][button_groups]', $expected_buttons_value);
$settings = $this
->getDrupalSettings();
$expected = $settings['ckeditor']['toolbarAdmin'];
$this
->assertStringContainsString('<li data-drupal-ckeditor-button-name="Bold" class="ckeditor-button"><a href="#" class="cke-icon-only cke_ltr" role="button" title="bold" aria-label="bold"><span class="cke_button_icon cke_button__bold_icon">bold</span></a></li>', $expected);
$this
->assertSession()
->fieldValueEquals('editor[settings][plugins][stylescombo][styles]', '');
$this
->submitForm($edit, 'Save configuration');
$filter_format = FilterFormat::load('amazing_format');
$this
->assertInstanceOf(FilterFormatInterface::class, $filter_format);
$expected_settings = $default_settings;
$expected_settings['plugins']['stylescombo']['styles'] = '';
$editor = Editor::load('amazing_format');
$this
->assertInstanceOf(Editor::class, $editor);
$this
->assertEquals($expected_settings, $editor
->getSettings(), 'The Editor config entity has the correct settings.');
}
}