From 11ac481e096940594ef1fa2f81c593b9bfb81429 Mon Sep 17 00:00:00 2001 From: bnjmnm <benm@umich.edu> Date: Thu, 12 May 2022 14:00:29 -0400 Subject: [PATCH] Issue #3259593 by hooroomoo, Dom., Wim Leers, lauriii: Alignment being available as separate buttons AND in dropdown is confusing --- .../modules/ckeditor5/ckeditor5.ckeditor5.yml | 49 +------ .../ckeditor5/ckeditor5.post_update.php | 53 ++++++++ .../config/schema/ckeditor5.schema.yml | 21 +++ .../src/Plugin/CKEditor4To5Upgrade/Core.php | 45 ++++++- .../src/Plugin/CKEditor5Plugin/Alignment.php | 125 ++++++++++++++++++ .../fixtures/update/ckeditor5-3259593.php | 54 ++++++++ .../update/editor.editor.test_format.yml | 22 +++ .../update/filter.format.test_format.yml | 17 +++ .../Update/CKEditor5UpdateAlignmentTest.php | 62 +++++++++ .../src/Kernel/CKEditor5PluginManagerTest.php | 4 +- .../src/Kernel/ConfigurablePluginTest.php | 8 ++ .../src/Kernel/SmartDefaultSettingsTest.php | 17 ++- .../tests/src/Kernel/ValidatorsTest.php | 2 +- .../src/Kernel/WildcardHtmlSupportTest.php | 11 +- .../tests/src/Unit/AlignmentPluginTest.php | 117 ++++++++++++++++ 15 files changed, 543 insertions(+), 64 deletions(-) create mode 100644 core/modules/ckeditor5/ckeditor5.post_update.php create mode 100644 core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php create mode 100644 core/modules/ckeditor5/tests/fixtures/update/ckeditor5-3259593.php create mode 100644 core/modules/ckeditor5/tests/fixtures/update/editor.editor.test_format.yml create mode 100644 core/modules/ckeditor5/tests/fixtures/update/filter.format.test_format.yml create mode 100644 core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateAlignmentTest.php create mode 100644 core/modules/ckeditor5/tests/src/Unit/AlignmentPluginTest.php diff --git a/core/modules/ckeditor5/ckeditor5.ckeditor5.yml b/core/modules/ckeditor5/ckeditor5.ckeditor5.yml index 09d5f8ec5976..3a457be0a184 100644 --- a/core/modules/ckeditor5/ckeditor5.ckeditor5.yml +++ b/core/modules/ckeditor5/ckeditor5.ckeditor5.yml @@ -391,7 +391,7 @@ ckeditor5_horizontalLine: - <hr> ckeditor5_alignment: - ckeditor5: &alignment_ckeditor5_section + ckeditor5: plugins: [alignment.Alignment] config: # @see core/modules/system/css/components/align.module.css @@ -405,60 +405,17 @@ ckeditor5_alignment: className: text-align-right - name: justify className: text-align-justify - drupal: &alignment_drupal_section + drupal: label: Alignment library: core/ckeditor5.alignment admin_library: ckeditor5/admin.alignment + class: Drupal\ckeditor5\Plugin\CKEditor5Plugin\Alignment toolbar_items: alignment: label: Text alignment elements: - <$text-container class="text-align-left text-align-center text-align-right text-align-justify"> -ckeditor5_alignment.left: - ckeditor5: *alignment_ckeditor5_section - drupal: - label: Align left - toolbar_items: - "alignment:left": - label: Align left - elements: - - <$text-container class="text-align-left"> - <<: *alignment_drupal_section - -ckeditor5_alignment.center: - ckeditor5: *alignment_ckeditor5_section - drupal: - label: Align center - toolbar_items: - "alignment:center": - label: Align center - elements: - - <$text-container class="text-align-center"> - <<: *alignment_drupal_section - -ckeditor5_alignment.right: - ckeditor5: *alignment_ckeditor5_section - drupal: - label: Align right - toolbar_items: - "alignment:right": - label: Align right - elements: - - <$text-container class="text-align-right"> - <<: *alignment_drupal_section - -ckeditor5_alignment.justify: - ckeditor5: *alignment_ckeditor5_section - drupal: - label: Justify - toolbar_items: - "alignment:justify": - label: Justify - elements: - - <$text-container class="text-align-justify"> - <<: *alignment_drupal_section - ckeditor5_removeFormat: ckeditor5: plugins: [removeFormat.RemoveFormat] diff --git a/core/modules/ckeditor5/ckeditor5.post_update.php b/core/modules/ckeditor5/ckeditor5.post_update.php new file mode 100644 index 000000000000..bb82debc765c --- /dev/null +++ b/core/modules/ckeditor5/ckeditor5.post_update.php @@ -0,0 +1,53 @@ +<?php + +/** + * @file + * Post update functions for CKEditor 5. + */ + +use Drupal\Core\Config\Entity\ConfigEntityUpdater; +use Drupal\editor\Entity\Editor; + +/** + * Updates if an already migrated CKEditor 5 configuration for text formats + * has alignment shown as individual buttons instead of a dropdown. + */ +function ckeditor5_post_update_alignment_buttons(&$sandbox = []) { + $config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class); + + $callback = function (Editor $editor) { + // Only try to update editors using CKEditor 5. + if ($editor->getEditor() !== 'ckeditor5') { + return FALSE; + } + + $needs_update = FALSE; + // Only update if the editor is using the non-dropdown buttons. + $settings = $editor->getSettings(); + $old_alignment_buttons_to_types = [ + 'alignment:left' => 'left', + 'alignment:right' => 'right', + 'alignment:center' => 'center', + 'alignment:justify' => 'justify', + ]; + if (is_array($settings['toolbar']['items'])) { + foreach ($old_alignment_buttons_to_types as $button => $type) { + if (in_array($button, $settings['toolbar']['items'], TRUE)) { + $settings['toolbar']['items'] = array_values(array_diff($settings['toolbar']['items'], [$button])); + $settings['plugins']['ckeditor5_alignment']['enabled_alignments'][] = $type; + if (!in_array('alignment', $settings['toolbar']['items'], TRUE)) { + $settings['toolbar']['items'][] = 'alignment'; + } + // Flag this display as needing to be updated. + $needs_update = TRUE; + } + } + } + if ($needs_update) { + $editor->setSettings($settings); + } + return $needs_update; + }; + + $config_entity_updater->update($sandbox, 'editor', $callback); +} diff --git a/core/modules/ckeditor5/config/schema/ckeditor5.schema.yml b/core/modules/ckeditor5/config/schema/ckeditor5.schema.yml index 3a154b310ff7..b7c670a6832c 100644 --- a/core/modules/ckeditor5/config/schema/ckeditor5.schema.yml +++ b/core/modules/ckeditor5/config/schema/ckeditor5.schema.yml @@ -87,6 +87,27 @@ ckeditor5.plugin.ckeditor5_sourceEditing: SourceEditingRedundantTags: [] SourceEditingPreventSelfXssConstraint: [] +# Plugin \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Alignment +ckeditor5.plugin.ckeditor5_alignment: + type: mapping + label: Alignments + mapping: + enabled_alignments: + type: sequence + label: 'Enabled Alignments' + constraints: + NotBlank: + message: "Enable at least one alignment, otherwise disable the Alignment button." + sequence: + type: string + label: 'Alignment type' + constraints: + Choice: + - left + - center + - right + - justify + # Plugin \Drupal\ckeditor5\Plugin\CKEditor5Plugin\ListPlugin ckeditor5.plugin.ckeditor5_list: type: mapping diff --git a/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Core.php b/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Core.php index 39b2097e4e87..25e132222a18 100644 --- a/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Core.php +++ b/core/modules/ckeditor5/src/Plugin/CKEditor4To5Upgrade/Core.php @@ -59,6 +59,7 @@ * }, * cke5_plugin_elements_subset_configuration = { * "ckeditor5_heading", + * "ckeditor5_alignment", * "ckeditor5_list", * "media_media", * } @@ -73,6 +74,7 @@ class Core extends PluginBase implements CKEditor4To5UpgradePluginInterface { * {@inheritdoc} */ public function mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem(string $cke4_button, HTMLRestrictions $text_format_html_restrictions): ?array { + static $alignment_mapped; switch ($cke4_button) { // @see \Drupal\ckeditor\Plugin\CKEditorPlugin\DrupalImage case 'DrupalImage': @@ -103,16 +105,14 @@ public function mapCKEditor4ToolbarButtonToCKEditor5ToolbarItem(string $cke4_but return ['blockQuote']; case 'JustifyLeft': - return ["alignment:left"]; - case 'JustifyCenter': - return ["alignment:center"]; - case 'JustifyRight': - return ["alignment:right"]; - case 'JustifyBlock': - return ["alignment:justify"]; + if (!isset($alignment_mapped)) { + $alignment_mapped = TRUE; + return ['alignment']; + } + return NULL; case 'HorizontalRule': return ['horizontalLine']; @@ -228,6 +228,37 @@ public function computeCKEditor5PluginSubsetConfiguration(string $cke5_plugin_id } return $configuration; + case 'ckeditor5_alignment': + $alignment_classes_to_types = [ + 'text-align-left' => 'left', + 'text-align-right' => 'right', + 'text-align-center' => 'center', + 'text-align-justify' => 'justify', + ]; + $restrictions = $text_format->getHtmlRestrictions(); + if ($restrictions === FALSE) { + // The default is to allow all alignments. This makes sense when there + // are no restrictions. + // @see \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Alignment::DEFAULT_CONFIGURATION + return NULL; + } + // Otherwise, enable alignment types based on the provided restrictions. + // I.e. if a tag is found with a text-align-{alignment type} class, + // activate that alignment type. + $configuration = []; + foreach ($restrictions['allowed'] as $tag) { + $classes = isset($tag['class']) && is_array($tag['class']) ? $tag['class'] : []; + foreach (array_keys($classes) as $class) { + if (isset($alignment_classes_to_types[$class])) { + $configuration['enabled_alignments'][] = $alignment_classes_to_types[$class]; + } + } + } + if (isset($configuration['enabled_alignments'])) { + $configuration['enabled_alignments'] = array_unique($configuration['enabled_alignments']); + } + return $configuration; + case 'ckeditor5_list': $restrictions = $text_format->getHtmlRestrictions(); if ($restrictions === FALSE) { diff --git a/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php b/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php new file mode 100644 index 000000000000..7a3a8af49489 --- /dev/null +++ b/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Alignment.php @@ -0,0 +1,125 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\ckeditor5\Plugin\CKEditor5Plugin; + +use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableTrait; +use Drupal\ckeditor5\Plugin\CKEditor5PluginDefault; +use Drupal\ckeditor5\Plugin\CKEditor5PluginConfigurableInterface; +use Drupal\ckeditor5\Plugin\CKEditor5PluginElementsSubsetInterface; +use Drupal\Core\Form\FormStateInterface; +use Drupal\editor\EditorInterface; +use Drupal\ckeditor5\HTMLRestrictions; + +/** + * CKEditor 5 Alignment plugin. + * + * @internal + * Plugin classes are internal. + */ +class Alignment extends CKEditor5PluginDefault implements CKEditor5PluginConfigurableInterface, CKEditor5PluginElementsSubsetInterface { + + use CKEditor5PluginConfigurableTrait; + + /** + * The default configuration for this plugin. + * + * @var string[][] + */ + const DEFAULT_CONFIGURATION = [ + 'enabled_alignments' => [ + 'left', + 'center', + 'right', + 'justify', + ], + ]; + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return static::DEFAULT_CONFIGURATION; + } + + /** + * {@inheritdoc} + * + * Form for choosing which alignment types are available. + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $form['enabled_alignments'] = [ + '#type' => 'fieldset', + '#title' => $this->t('Enabled Alignments'), + '#description' => $this->t('These are the alignment types that will appear in the alignment dropdown.'), + ]; + + foreach ($this->getPluginDefinition()->getCKEditor5Config()['alignment']['options'] as $alignment_option) { + $name = $alignment_option['name']; + $form['enabled_alignments'][$name] = [ + '#type' => 'checkbox', + '#title' => $this->t($name), + '#return_value' => $name, + '#default_value' => in_array($name, $this->configuration['enabled_alignments'], TRUE) ? $name : NULL, + ]; + } + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateConfigurationForm(array &$form, FormStateInterface $form_state) { + // Match the config schema structure at ckeditor5.plugin.ckeditor5_alignment. + $form_value = $form_state->getValue('enabled_alignments'); + $config_value = array_values(array_filter($form_value)); + $form_state->setValue('enabled_alignments', $config_value); + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['enabled_alignments'] = $form_state->getValue('enabled_alignments'); + } + + /** + * {@inheritdoc} + * + * Filters the alignment options to those chosen in editor config. + */ + public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array { + $enabled_alignments = $this->configuration['enabled_alignments']; + $all_alignment_options = $static_plugin_config['alignment']['options']; + + $configured_alignment_options = array_filter($all_alignment_options, function ($option) use ($enabled_alignments) { + return in_array($option['name'], $enabled_alignments, TRUE); + }); + + return [ + 'alignment' => [ + 'options' => array_values($configured_alignment_options), + ], + ]; + } + + /** + * {@inheritdoc} + */ + public function getElementsSubset(): array { + $enabled_alignments = $this->configuration['enabled_alignments']; + $plugin_definition = $this->getPluginDefinition(); + $all_elements = $plugin_definition->getElements(); + $subset = HTMLRestrictions::fromString(implode($all_elements)); + foreach ($plugin_definition->getCKEditor5Config()['alignment']['options'] as $configured_alignment) { + if (!in_array($configured_alignment['name'], $enabled_alignments, TRUE)) { + $element_string = '<$text-container class=' . '"' . $configured_alignment["className"] . '"' . '>'; + $subset = $subset->diff(HTMLRestrictions::fromString($element_string)); + } + } + return $subset->toCKEditor5ElementsArray(); + } + +} diff --git a/core/modules/ckeditor5/tests/fixtures/update/ckeditor5-3259593.php b/core/modules/ckeditor5/tests/fixtures/update/ckeditor5-3259593.php new file mode 100644 index 000000000000..5c485aba0783 --- /dev/null +++ b/core/modules/ckeditor5/tests/fixtures/update/ckeditor5-3259593.php @@ -0,0 +1,54 @@ +<?php + +/** + * @file + * Test fixture. + */ + +use Drupal\Core\Database\Database; +use Drupal\Core\Serialization\Yaml; + +$connection = Database::getConnection(); + +// Update core.extension. +$extensions = $connection->select('config') + ->fields('config', ['data']) + ->condition('collection', '') + ->condition('name', 'core.extension') + ->execute() + ->fetchField(); +$extensions = unserialize($extensions); +$extensions['module']['ckeditor5'] = 0; +$connection->update('config') + ->fields(['data' => serialize($extensions)]) + ->condition('collection', '') + ->condition('name', 'core.extension') + ->execute(); + +$test_format_format = Yaml::decode(file_get_contents(__DIR__ . '/filter.format.test_format.yml')); +$connection->insert('config') + ->fields([ + 'collection', + 'name', + 'data', + ]) + ->values([ + 'collection' => '', + 'name' => 'filter.format.test_format', + 'data' => serialize($test_format_format), + ]) + ->execute(); + +$test_format_editor = Yaml::decode(file_get_contents(__DIR__ . '/editor.editor.test_format.yml')); +$connection->insert('config') + ->fields([ + 'collection', + 'name', + 'data', + ]) + ->values([ + 'collection' => '', + 'name' => 'editor.editor.test_format', + 'data' => serialize($test_format_editor), + ]) + ->execute(); diff --git a/core/modules/ckeditor5/tests/fixtures/update/editor.editor.test_format.yml b/core/modules/ckeditor5/tests/fixtures/update/editor.editor.test_format.yml new file mode 100644 index 000000000000..341c532c88c9 --- /dev/null +++ b/core/modules/ckeditor5/tests/fixtures/update/editor.editor.test_format.yml @@ -0,0 +1,22 @@ +uuid: f962b8c7-4c74-4100-b6de-08e6a65ff43d +langcode: en +status: true +dependencies: + config: + - filter.format.test_format + module: + - ckeditor5 +format: test_format +editor: ckeditor5 +settings: + toolbar: + items: + - link + - bold + - italic + - 'alignment:center' + - sourceEditing + plugins: + ckeditor5_sourceEditing: + allowed_tags: { } +image_upload: { } diff --git a/core/modules/ckeditor5/tests/fixtures/update/filter.format.test_format.yml b/core/modules/ckeditor5/tests/fixtures/update/filter.format.test_format.yml new file mode 100644 index 000000000000..e47cd4d3c518 --- /dev/null +++ b/core/modules/ckeditor5/tests/fixtures/update/filter.format.test_format.yml @@ -0,0 +1,17 @@ +uuid: 343a36d6-5852-45f5-b4de-437551cb9caf +langcode: en +status: true +dependencies: { } +name: 'Test format' +format: test_format +weight: 0 +filters: + filter_html: + id: filter_html + provider: filter + status: true + weight: -10 + settings: + allowed_html: '<br> <p class="text-align-center"> <strong> <em> <a href>' + filter_html_help: true + filter_html_nofollow: false diff --git a/core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateAlignmentTest.php b/core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateAlignmentTest.php new file mode 100644 index 000000000000..7ba87460f180 --- /dev/null +++ b/core/modules/ckeditor5/tests/src/Functional/Update/CKEditor5UpdateAlignmentTest.php @@ -0,0 +1,62 @@ +<?php + +namespace Drupal\Tests\ckeditor5\Functional\Update; + +use Drupal\editor\Entity\Editor; +use Drupal\FunctionalTests\Update\UpdatePathTestBase; +use Drupal\Tests\ckeditor5\Traits\CKEditor5TestTrait; + +/** + * Tests the update path for CKEditor 5 alignment. + * + * @group Update + */ +class CKEditor5UpdateAlignmentTest extends UpdatePathTestBase { + + use CKEditor5TestTrait; + + /** + * {@inheritdoc} + */ + protected $defaultTheme = 'stark'; + + /** + * {@inheritdoc} + */ + protected function setDatabaseDumpFiles() { + $this->databaseDumpFiles = [ + __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.3.0.filled.standard.php.gz', + __DIR__ . '/../../../fixtures/update/ckeditor5-3259593.php', + ]; + } + + /** + * Tests that CKEditor 5 alignment configurations that are individual buttons + * are updated to be in dropdown form in the toolbar. + */ + public function testUpdateAlignmentButtons() { + $editor = Editor::load('test_format'); + $settings = $editor->getSettings(); + $this->assertContains('alignment:center', $settings['toolbar']['items']); + + $this->runUpdates(); + + $expected_toolbar_items = [ + 'link', + 'bold', + 'italic', + 'sourceEditing', + 'alignment', + ]; + $expected_alignment_plugin = [ + 'enabled_alignments' => [ + 'center', + ], + ]; + $editor = Editor::load('test_format'); + $settings = $editor->getSettings(); + $this->assertEquals($expected_toolbar_items, $settings['toolbar']['items']); + $this->assertEquals($expected_alignment_plugin, $settings['plugins']['ckeditor5_alignment']); + } + +} diff --git a/core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php b/core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php index 747633aee788..2363bde9af2f 100644 --- a/core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php +++ b/core/modules/ckeditor5/tests/src/Kernel/CKEditor5PluginManagerTest.php @@ -1119,11 +1119,11 @@ public function testEnabledPlugins() { // Case 7: GHS is enabled for other text editors if they are using a // CKEditor 5 plugin that uses wildcard tags. - $settings['toolbar']['items'][] = 'alignment:center'; + $settings['toolbar']['items'][] = 'alignment'; $editor->setSettings($settings); $plugin_ids = array_keys($this->manager->getEnabledDefinitions($editor)); $expected_plugins = array_merge($expected_plugins, [ - 'ckeditor5_alignment.center', + 'ckeditor5_alignment', 'ckeditor5_wildcardHtmlSupport', ]); sort($expected_plugins); diff --git a/core/modules/ckeditor5/tests/src/Kernel/ConfigurablePluginTest.php b/core/modules/ckeditor5/tests/src/Kernel/ConfigurablePluginTest.php index f35ad49dd1e5..73af33918749 100644 --- a/core/modules/ckeditor5/tests/src/Kernel/ConfigurablePluginTest.php +++ b/core/modules/ckeditor5/tests/src/Kernel/ConfigurablePluginTest.php @@ -71,6 +71,14 @@ public function testDefaults() { 'reversed' => TRUE, 'startIndex' => TRUE, ], + 'ckeditor5_alignment' => [ + 'enabled_alignments' => [ + 0 => 'left', + 1 => 'center', + 2 => 'right', + 3 => 'justify', + ], + ], 'ckeditor5_imageResize' => [ 'allow_resize' => TRUE, ], diff --git a/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php b/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php index 7ff22ce7a8ed..bd085b940dfc 100644 --- a/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php +++ b/core/modules/ckeditor5/tests/src/Kernel/SmartDefaultSettingsTest.php @@ -752,14 +752,19 @@ public function provider() { 'toolbar' => [ 'items' => array_merge( array_slice($basic_html_test_case['expected_ckeditor5_settings']['toolbar']['items'], 0, -1), - [ - 'alignment:center', - 'alignment:justify', - ], + ['alignment'], array_slice($basic_html_test_case['expected_ckeditor5_settings']['toolbar']['items'], -1) ), ], - 'plugins' => $basic_html_test_case['expected_ckeditor5_settings']['plugins'], + 'plugins' => array_merge( + array_slice($basic_html_test_case['expected_ckeditor5_settings']['plugins'], 0, 1), + [ + 'ckeditor5_alignment' => [ + 'enabled_alignments' => ['center', 'justify'], + ], + ], + array_slice($basic_html_test_case['expected_ckeditor5_settings']['plugins'], 1), + ), ], 'expected_superset' => implode(' ', [ // Note that aligning left and right is being added, on top of what the @@ -779,7 +784,7 @@ public function provider() { 'expected_fundamental_compatibility_violations' => $basic_html_test_case['expected_fundamental_compatibility_violations'], 'expected_messages' => array_merge_recursive($basic_html_test_case['expected_messages'], [ 'status' => [ - 'The following plugins were enabled to support specific attributes that are allowed by this text format: <em class="placeholder">Align center ( for tag: <p> to support: class with value(s): text-align-center), Justify ( for tag: <p> to support: class with value(s): text-align-justify)</em>.', + 'The following plugins were enabled to support specific attributes that are allowed by this text format: <em class="placeholder">Alignment ( for tag: <p> to support: class with value(s): text-align-center, text-align-justify)</em>.', 'This format\'s HTML filters includes plugins that support the following tags, but not some of their attributes. To ensure these attributes remain supported by this text format, the following were added to the Source Editing plugin\'s <em>Manually editable HTML tags</em>: <a hreflang> <blockquote cite> <ul type> <ol type> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>.', ], ]), diff --git a/core/modules/ckeditor5/tests/src/Kernel/ValidatorsTest.php b/core/modules/ckeditor5/tests/src/Kernel/ValidatorsTest.php index f15be4e87665..cc0c1bcb50ec 100644 --- a/core/modules/ckeditor5/tests/src/Kernel/ValidatorsTest.php +++ b/core/modules/ckeditor5/tests/src/Kernel/ValidatorsTest.php @@ -746,7 +746,7 @@ public function providerPair(): array { 'settings.plugins.ckeditor5_sourceEditing.allowed_tags.1' => 'The following tag(s) are already supported by available plugins and should not be added to the Source Editing "Manually editable HTML tags" field. Instead, enable the following plugins to support these tags: <em class="placeholder">Table (<table>)</em>.', 'settings.plugins.ckeditor5_sourceEditing.allowed_tags.3' => 'The following attribute(s) are already supported by enabled plugins and should not be added to the Source Editing "Manually editable HTML tags" field: <em class="placeholder">Language (<span lang>)</em>.', 'settings.plugins.ckeditor5_sourceEditing.allowed_tags.5' => 'The following tag(s) are already supported by available plugins and should not be added to the Source Editing "Manually editable HTML tags" field. Instead, enable the following plugins to support these tags: <em class="placeholder">Code Block (<code class="language-*">)</em>.', - 'settings.plugins.ckeditor5_sourceEditing.allowed_tags.6' => 'The following attribute(s) are already supported by available plugins and should not be added to the Source Editing "Manually editable HTML tags" field. Instead, enable the following plugins to support these attributes: <em class="placeholder">Alignment (<h2 class="text-align-center">), Align center (<h2 class="text-align-center">)</em>.', + 'settings.plugins.ckeditor5_sourceEditing.allowed_tags.6' => 'The following attribute(s) are already supported by available plugins and should not be added to the Source Editing "Manually editable HTML tags" field. Instead, enable the following plugins to support these attributes: <em class="placeholder">Alignment (<h2 class="text-align-center">)</em>.', ], ]; $data['INVALID some invalid Source Editable tags provided by plugin and another available in a not enabled plugin'] = [ diff --git a/core/modules/ckeditor5/tests/src/Kernel/WildcardHtmlSupportTest.php b/core/modules/ckeditor5/tests/src/Kernel/WildcardHtmlSupportTest.php index 85623982b4eb..1e9b93f8beba 100644 --- a/core/modules/ckeditor5/tests/src/Kernel/WildcardHtmlSupportTest.php +++ b/core/modules/ckeditor5/tests/src/Kernel/WildcardHtmlSupportTest.php @@ -57,7 +57,7 @@ public function testGhsConfiguration(string $filter_html_allowed, array $source_ ], ], ])->save(); - $editor = Editor::create([ + $editor_config = [ 'editor' => 'ckeditor5', 'format' => 'test_format', 'settings' => [ @@ -73,7 +73,14 @@ public function testGhsConfiguration(string $filter_html_allowed, array $source_ 'image_upload' => [ 'status' => FALSE, ], - ]); + ]; + if (in_array('alignment', $additional_toolbar_items, TRUE)) { + $editor_config['settings']['plugins']['ckeditor5_alignment'] = [ + 'enabled_alignments' => ['left', 'center', 'right', 'justify'], + ]; + } + + $editor = Editor::create($editor_config); $editor->save(); $this->assertSame([], array_map( function (ConstraintViolation $v) { diff --git a/core/modules/ckeditor5/tests/src/Unit/AlignmentPluginTest.php b/core/modules/ckeditor5/tests/src/Unit/AlignmentPluginTest.php new file mode 100644 index 000000000000..6e3ef192a32c --- /dev/null +++ b/core/modules/ckeditor5/tests/src/Unit/AlignmentPluginTest.php @@ -0,0 +1,117 @@ +<?php + +declare(strict_types=1); + +namespace Drupal\Tests\ckeditor5\Unit; + +use Drupal\ckeditor5\Plugin\CKEditor5Plugin\Alignment; +use Drupal\editor\EditorInterface; +use Drupal\Tests\UnitTestCase; +use Symfony\Component\Yaml\Yaml; + +/** + * @coversDefaultClass \Drupal\ckeditor5\Plugin\CKEditor5Plugin\Alignment + * @group ckeditor5 + * @internal + */ +class AlignmentPluginTest extends UnitTestCase { + + /** + * Provides a list of configs to test. + */ + public function providerGetDynamicPluginConfig(): array { + return [ + 'All alignments' => [ + Alignment::DEFAULT_CONFIGURATION, + [ + 'alignment' => [ + 'options' => [ + [ + 'name' => 'left', + 'className' => 'text-align-left', + ], + [ + 'name' => 'center', + 'className' => 'text-align-center', + ], + [ + 'name' => 'right', + 'className' => 'text-align-right', + ], + [ + 'name' => 'justify', + 'className' => 'text-align-justify', + ], + ], + ], + ], + ], + 'No alignments allowed' => [ + [ + 'enabled_alignments' => [], + ], + [ + 'alignment' => [ + 'options' => [], + ], + ], + ], + 'Left only' => [ + [ + 'enabled_alignments' => [ + 'left', + ], + ], + [ + 'alignment' => [ + 'options' => [ + [ + 'name' => 'left', + 'className' => 'text-align-left', + ], + ], + ], + ], + ], + 'Left and justify only' => [ + [ + 'enabled_alignments' => [ + 'left', + 'justify', + ], + ], + [ + 'alignment' => [ + 'options' => [ + [ + 'name' => 'left', + 'className' => 'text-align-left', + ], + [ + 'name' => 'justify', + 'className' => 'text-align-justify', + ], + ], + ], + ], + ], + ]; + } + + /** + * @covers ::getDynamicPluginConfig + * @dataProvider providerGetDynamicPluginConfig + */ + public function testGetDynamicPluginConfig(array $configuration, array $expected_dynamic_config): void { + // Read the CKEditor 5 plugin's static configuration from YAML. + $ckeditor5_plugin_definitions = Yaml::parseFile(__DIR__ . '/../../../ckeditor5.ckeditor5.yml'); + $static_plugin_config = $ckeditor5_plugin_definitions['ckeditor5_alignment']['ckeditor5']['config']; + + $plugin = new Alignment($configuration, 'ckeditor5_alignment', NULL); + $dynamic_plugin_config = $plugin->getDynamicPluginConfig($static_plugin_config, $this->prophesize(EditorInterface::class) + ->reveal()); + + $this->assertSame($expected_dynamic_config, $dynamic_plugin_config); + } + +} -- GitLab