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); $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: mapping:
items: items:
type: sequence type: sequence
orderby: ~
label: 'Items' label: 'Items'
sequence: sequence:
type: ckeditor5.toolbar_item type: ckeditor5.toolbar_item
@ -20,6 +21,7 @@ editor.settings.ckeditor5:
plugins: plugins:
type: sequence type: sequence
label: 'Plugins' label: 'Plugins'
orderby: key
sequence: sequence:
type: ckeditor5.plugin.[%key] type: ckeditor5.plugin.[%key]
constraints: constraints:
@ -51,6 +53,7 @@ ckeditor5.plugin.ckeditor5_heading:
mapping: mapping:
enabled_headings: enabled_headings:
type: sequence type: sequence
orderby: value
label: 'Enabled Headings' label: 'Enabled Headings'
constraints: constraints:
NotBlank: NotBlank:
@ -80,6 +83,7 @@ ckeditor5.plugin.ckeditor5_sourceEditing:
mapping: mapping:
allowed_tags: allowed_tags:
type: sequence type: sequence
orderby: ~
label: 'Allowed Tags' label: 'Allowed Tags'
sequence: sequence:
type: ckeditor5.element type: ckeditor5.element
@ -95,6 +99,7 @@ ckeditor5.plugin.ckeditor5_alignment:
mapping: mapping:
enabled_alignments: enabled_alignments:
type: sequence type: sequence
orderby: value
label: 'Enabled Alignments' label: 'Enabled Alignments'
constraints: constraints:
NotBlank: NotBlank:
@ -143,6 +148,7 @@ ckeditor5.plugin.ckeditor5_style:
mapping: mapping:
styles: styles:
type: sequence type: sequence
orderby: ~
label: 'Styles' label: 'Styles'
constraints: constraints:
NotBlank: 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() $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. // We should now have the expected data in the Editor config entity.
$this->assertSame('ckeditor5', $updated_text_editor->getEditor()); $this->assertSame('ckeditor5', $updated_text_editor->getEditor());
$this->assertSame($expected_ckeditor5_settings, $updated_text_editor->getSettings()); $this->assertSame($expected_ckeditor5_settings, $updated_text_editor->getSettings());
@ -922,12 +926,12 @@ class SmartDefaultSettingsTest extends KernelTestBase {
), ),
], ],
'plugins' => array_merge( 'plugins' => array_merge(
$basic_html_test_case['expected_ckeditor5_settings']['plugins'],
[ [
'ckeditor5_alignment' => [ 'ckeditor5_alignment' => [
'enabled_alignments' => ['center', 'justify'], 'enabled_alignments' => ['center', 'justify'],
], ],
], ],
$basic_html_test_case['expected_ckeditor5_settings']['plugins'],
), ),
], ],
'expected_superset' => implode(' ', [ 'expected_superset' => implode(' ', [
@ -1124,6 +1128,10 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'heading6', 'heading6',
], ],
], ],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
'ckeditor5_sourceEditing' => [ 'ckeditor5_sourceEditing' => [
'allowed_tags' => [ 'allowed_tags' => [
'<cite>', '<cite>',
@ -1141,10 +1149,6 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'<h6 id>', '<h6 id>',
], ],
], ],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
], ],
], ],
'expected_superset' => '<br> <p>', 'expected_superset' => '<br> <p>',
@ -1259,6 +1263,10 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'heading6', 'heading6',
], ],
], ],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
'ckeditor5_sourceEditing' => [ 'ckeditor5_sourceEditing' => [
'allowed_tags' => [ 'allowed_tags' => [
'<cite>', '<cite>',
@ -1276,10 +1284,6 @@ class SmartDefaultSettingsTest extends KernelTestBase {
'<h6 id>', '<h6 id>',
], ],
], ],
'ckeditor5_list' => [
'reversed' => FALSE,
'startIndex' => TRUE,
],
], ],
], ],
'expected_superset' => '<br> <p>', 'expected_superset' => '<br> <p>',
@ -1366,6 +1370,11 @@ class SmartDefaultSettingsTest extends KernelTestBase {
], ],
], ],
'plugins' => [ 'plugins' => [
'ckeditor5_sourceEditing' => [
'allowed_tags' => [
'<span>',
],
],
'ckeditor5_style' => [ 'ckeditor5_style' => [
'styles' => [ 'styles' => [
[ [
@ -1374,11 +1383,6 @@ class SmartDefaultSettingsTest extends KernelTestBase {
], ],
], ],
], ],
'ckeditor5_sourceEditing' => [
'allowed_tags' => [
'<span>',
],
],
], ],
], ],
'expected_superset' => '', 'expected_superset' => '',

View File

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

View File

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