Issue #3314770 by Wim Leers, smustgrave, bnjmnm, alexpott: Enforce order of CKEditor 5 plugin settings in config export (as well as other sequences)

(cherry picked from commit cd4c806b0d)
merge-requests/2710/merge
catch 2022-10-21 13:35:32 +01:00
parent 4a5ae1cf78
commit 0679e656fd
6 changed files with 126 additions and 23 deletions

View File

@ -65,3 +65,27 @@ function ckeditor5_post_update_image_toolbar_item(&$sandbox = []) {
$config_entity_updater->update($sandbox, 'editor', $callback);
}
/**
* Updates Text Editors using CKEditor 5 to sort plugin settings by plugin key.
*/
function ckeditor5_post_update_plugins_settings_export_order(&$sandbox = []) {
$config_entity_updater = \Drupal::classResolver(ConfigEntityUpdater::class);
$config_entity_updater->update($sandbox, 'editor', function (Editor $editor): bool {
// Only try to update editors using CKEditor 5.
if ($editor->getEditor() !== 'ckeditor5') {
return FALSE;
}
$settings = $editor->getSettings();
// Nothing to do if there are fewer than two plugins with settings.
if (count($settings['plugins']) < 2) {
return FALSE;
}
ksort($settings['plugins']);
$editor->setSettings($settings);
return TRUE;
});
}

View File

@ -10,6 +10,7 @@ editor.settings.ckeditor5:
mapping:
items:
type: sequence
orderby: ~
label: 'Items'
sequence:
type: ckeditor5.toolbar_item
@ -20,6 +21,7 @@ editor.settings.ckeditor5:
plugins:
type: sequence
label: 'Plugins'
orderby: key
sequence:
type: ckeditor5.plugin.[%key]
constraints:
@ -51,6 +53,7 @@ ckeditor5.plugin.ckeditor5_heading:
mapping:
enabled_headings:
type: sequence
orderby: value
label: 'Enabled Headings'
constraints:
NotBlank:
@ -80,6 +83,7 @@ ckeditor5.plugin.ckeditor5_sourceEditing:
mapping:
allowed_tags:
type: sequence
orderby: ~
label: 'Allowed Tags'
sequence:
type: ckeditor5.element
@ -95,6 +99,7 @@ ckeditor5.plugin.ckeditor5_alignment:
mapping:
enabled_alignments:
type: sequence
orderby: value
label: 'Enabled Alignments'
constraints:
NotBlank:
@ -143,6 +148,7 @@ ckeditor5.plugin.ckeditor5_style:
mapping:
styles:
type: sequence
orderby: ~
label: 'Styles'
constraints:
NotBlank:

View File

@ -0,0 +1,69 @@
<?php
namespace Drupal\Tests\ckeditor5\Functional\Update;
use Drupal\editor\Entity\Editor;
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
/**
* @covers ckeditor5_post_update_plugins_settings_export_order()
* @group Update
* @group ckeditor5
*/
class CKEditor5UpdatePluginSettingsSortTest extends UpdatePathTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setDatabaseDumpFiles() {
$this->databaseDumpFiles = [
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.filled.standard.php.gz',
];
}
/**
* Ensure settings for CKEditor 5 plugins are sorted by plugin key.
*/
public function testUpdatePluginSettingsSortPostUpdate(): void {
$editor = Editor::load('basic_html');
$settings = $editor->getSettings();
$plugin_settings_before = array_keys($settings['plugins']);
$this->runUpdates();
$editor = Editor::load('basic_html');
$settings = $editor->getSettings();
$plugin_settings_after = array_keys($settings['plugins']);
// Different sort before and after, but the same values.
$this->assertNotSame($plugin_settings_before, $plugin_settings_after);
sort($plugin_settings_before);
$this->assertSame($plugin_settings_before, $plugin_settings_after);
}
/**
* Ensure settings for CKEditor 5 plugins are sorted by plugin key.
*/
public function testUpdatePluginSettingsSortEntitySave(): void {
$editor = Editor::load('basic_html');
$settings = $editor->getSettings();
$plugin_settings_before = array_keys($settings['plugins']);
$editor->save();
$editor = Editor::load('basic_html');
$settings = $editor->getSettings();
$plugin_settings_after = array_keys($settings['plugins']);
// Different sort before and after, but the same values.
$this->assertNotSame($plugin_settings_before, $plugin_settings_after);
sort($plugin_settings_before);
$this->assertSame($plugin_settings_before, $plugin_settings_after);
}
}

View File

@ -497,6 +497,10 @@ class SmartDefaultSettingsTest extends KernelTestBase {
$updated_text_editor->toArray()
);
// Save this to ensure the config export order is applied.
// @see \Drupal\Core\Config\StorableConfigBase::castValue()
$updated_text_editor->save();
// We should now have the expected data in the Editor config entity.
$this->assertSame('ckeditor5', $updated_text_editor->getEditor());
$this->assertSame($expected_ckeditor5_settings, $updated_text_editor->getSettings());
@ -922,12 +926,12 @@ class SmartDefaultSettingsTest extends KernelTestBase {
),
],
'plugins' => array_merge(
$basic_html_test_case['expected_ckeditor5_settings']['plugins'],
[
'ckeditor5_alignment' => [
'enabled_alignments' => ['center', 'justify'],
],
],
$basic_html_test_case['expected_ckeditor5_settings']['plugins'],
),
],
'expected_superset' => implode(' ', [
@ -1124,6 +1128,10 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'heading6',
],
],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
'ckeditor5_sourceEditing' => [
'allowed_tags' => [
'<cite>',
@ -1141,10 +1149,6 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'<h6 id>',
],
],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
],
],
'expected_superset' => '<br> <p>',
@ -1259,6 +1263,10 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'heading6',
],
],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
'ckeditor5_sourceEditing' => [
'allowed_tags' => [
'<cite>',
@ -1276,10 +1284,6 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'<h6 id>',
],
],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
],
],
'expected_superset' => '<br> <p>',
@ -1366,6 +1370,11 @@ class SmartDefaultSettingsTest extends KernelTestBase {
],
],
'plugins' => [
'ckeditor5_sourceEditing' => [
'allowed_tags' => [
'<span>',
],
],
'ckeditor5_style' => [
'styles' => [
[
@ -1374,11 +1383,6 @@ class SmartDefaultSettingsTest extends KernelTestBase {
],
],
],
'ckeditor5_sourceEditing' => [
'allowed_tags' => [
'<span>',
],
],
],
],
'expected_superset' => '',

View File

@ -33,6 +33,11 @@ settings:
- heading4
- heading5
- heading6
ckeditor5_imageResize:
allow_resize: true
ckeditor5_list:
reversed: false
startIndex: true
ckeditor5_sourceEditing:
allowed_tags:
- '<cite>'
@ -49,11 +54,6 @@ settings:
- '<h5 id>'
- '<h6 id>'
- '<span>'
ckeditor5_list:
reversed: false
startIndex: true
ckeditor5_imageResize:
allow_resize: true
image_upload:
status: true
scheme: public

View File

@ -39,13 +39,13 @@ settings:
- heading4
- heading5
- heading6
ckeditor5_sourceEditing:
allowed_tags: { }
ckeditor5_imageResize:
allow_resize: true
ckeditor5_list:
reversed: true
startIndex: true
ckeditor5_imageResize:
allow_resize: true
ckeditor5_sourceEditing:
allowed_tags: { }
image_upload:
status: true
scheme: public