From 48715daae5c9cbb29debb8d8ea3fbe7073058b4a Mon Sep 17 00:00:00 2001 From: catch Date: Wed, 27 Apr 2022 21:27:02 +0100 Subject: [PATCH] Issue #3229078 by scott_euser, Wim Leers, hooroomoo, brentg, yogeshmpawar, catch: Unit tests for all @CKEditor5Plugin plugin classes (cherry picked from commit b0d868fcfb0aad257964c63b1b524edbacf8f5f3) --- .../src/Plugin/CKEditor5Plugin/Heading.php | 2 +- .../tests/src/Unit/HeadingPluginTest.php | 120 ++++++++++++ .../tests/src/Unit/LanguagePluginTest.php | 110 +++++++++++ .../src/Unit/SourceEditingPluginTest.php | 179 ++++++++++++++++++ 4 files changed, 410 insertions(+), 1 deletion(-) create mode 100644 core/modules/ckeditor5/tests/src/Unit/HeadingPluginTest.php create mode 100644 core/modules/ckeditor5/tests/src/Unit/LanguagePluginTest.php create mode 100644 core/modules/ckeditor5/tests/src/Unit/SourceEditingPluginTest.php diff --git a/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Heading.php b/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Heading.php index 0dcd50a4638..289890acd20 100644 --- a/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Heading.php +++ b/core/modules/ckeditor5/src/Plugin/CKEditor5Plugin/Heading.php @@ -170,7 +170,7 @@ class Heading extends CKEditor5PluginDefault implements CKEditor5PluginConfigura * Filters the header options to those chosen in editor config. */ public function getDynamicPluginConfig(array $static_plugin_config, EditorInterface $editor): array { - $enabled_headings = $this->getEnabledHeadings($editor); + $enabled_headings = $this->getEnabledHeadings(); $all_heading_options = $static_plugin_config['heading']['options']; $configured_heading_options = array_filter($all_heading_options, function ($option) use ($enabled_headings) { diff --git a/core/modules/ckeditor5/tests/src/Unit/HeadingPluginTest.php b/core/modules/ckeditor5/tests/src/Unit/HeadingPluginTest.php new file mode 100644 index 00000000000..bd20e30d9f1 --- /dev/null +++ b/core/modules/ckeditor5/tests/src/Unit/HeadingPluginTest.php @@ -0,0 +1,120 @@ + 'paragraph', + 'title' => 'Paragraph', + 'class' => 'ck-heading_paragraph', + ]; + $headings = []; + foreach (range(2, 6) as $number) { + $headings[$number] = [ + 'model' => 'heading' . $number, + 'view' => 'h' . $number, + 'title' => 'Heading ' . $number, + 'class' => 'ck-heading_heading' . $number, + ]; + } + + return [ + 'All headings' => [ + Heading::DEFAULT_CONFIGURATION, + [ + 'heading' => [ + 'options' => [ + $paragraph, + $headings[2], + $headings[3], + $headings[4], + $headings[5], + $headings[6], + ], + ], + ], + ], + 'Only required headings' => [ + [ + 'enabled_headings' => [], + ], + [ + 'heading' => [ + 'options' => [ + $paragraph, + ], + ], + ], + ], + 'Heading 2 only' => [ + [ + 'enabled_headings' => [ + 'heading2', + ], + ], + [ + 'heading' => [ + 'options' => [ + $paragraph, + $headings[2], + ], + ], + ], + ], + 'Heading 2 and 3 only' => [ + [ + 'enabled_headings' => [ + 'heading2', + 'heading3', + ], + ], + [ + 'heading' => [ + 'options' => [ + $paragraph, + $headings[2], + $headings[3], + ], + ], + ], + ], + ]; + } + + /** + * @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_heading']['ckeditor5']['config']; + + $plugin = new Heading($configuration, 'ckeditor5_heading', NULL); + $dynamic_plugin_config = $plugin->getDynamicPluginConfig($static_plugin_config, $this->prophesize(EditorInterface::class) + ->reveal()); + + $this->assertSame($expected_dynamic_config, $dynamic_plugin_config); + } + +} diff --git a/core/modules/ckeditor5/tests/src/Unit/LanguagePluginTest.php b/core/modules/ckeditor5/tests/src/Unit/LanguagePluginTest.php new file mode 100644 index 00000000000..162e7597865 --- /dev/null +++ b/core/modules/ckeditor5/tests/src/Unit/LanguagePluginTest.php @@ -0,0 +1,110 @@ + [ + 'textPartLanguage' => [ + [ + 'title' => 'Arabic', + 'languageCode' => 'ar', + 'textDirection' => 'rtl', + ], + [ + 'title' => 'Chinese, Simplified', + 'languageCode' => 'zh-hans', + ], + [ + 'title' => 'English', + 'languageCode' => 'en', + ], + [ + 'title' => 'French', + 'languageCode' => 'fr', + ], + [ + 'title' => 'Russian', + 'languageCode' => 'ru', + ], + [ + 'title' => 'Spanish', + 'languageCode' => 'es', + ], + ], + ], + ]; + return [ + 'un' => [ + ['language_list' => 'un'], + $un_expected_output, + ], + 'all' => [ + ['language_list' => 'all'], + [ + 'language' => [ + 'textPartLanguage' => $this->buildExpectedDynamicConfig(LanguageManager::getStandardLanguageList()), + ], + ], + ], + 'default configuration' => [ + [], + $un_expected_output, + ], + ]; + } + + /** + * Builds the expected dynamic configuration output given a language list. + * + * @param array $language_list + * The languages list from the language manager. + * + * @return array + * The expected output of the dynamic plugin configuration. + */ + protected static function buildExpectedDynamicConfig(array $language_list) { + $expected_language_config = []; + foreach ($language_list as $language_code => $language_list_item) { + $item = [ + 'title' => $language_list_item[0], + 'languageCode' => $language_code, + ]; + if (isset($language_list_item[2])) { + $item['textDirection'] = $language_list_item[2]; + } + $expected_language_config[$item['title']] = $item; + } + ksort($expected_language_config); + return array_values($expected_language_config); + } + + /** + * @covers ::getDynamicPluginConfig + * @dataProvider providerGetDynamicPluginConfig + */ + public function testGetDynamicPluginConfig(array $configuration, array $expected_dynamic_config): void { + $plugin = new Language($configuration, 'ckeditor5_language', NULL); + $dynamic_config = $plugin->getDynamicPluginConfig([], $this->prophesize(EditorInterface::class) + ->reveal()); + $this->assertSame($expected_dynamic_config, $dynamic_config); + } + +} diff --git a/core/modules/ckeditor5/tests/src/Unit/SourceEditingPluginTest.php b/core/modules/ckeditor5/tests/src/Unit/SourceEditingPluginTest.php new file mode 100644 index 00000000000..2a025b38fda --- /dev/null +++ b/core/modules/ckeditor5/tests/src/Unit/SourceEditingPluginTest.php @@ -0,0 +1,179 @@ + [ + [ + 'allowed_tags' => [], + ], + [ + 'htmlSupport' => [ + 'allow' => [], + ], + ], + ], + 'Simple' => [ + [ + 'allowed_tags' => [ + '', + '', + '', + '', + '', + ], + ], + [ + 'htmlSupport' => [ + 'allow' => [ + [ + 'name' => 'foo1', + ], + [ + 'name' => 'foo2', + 'attributes' => [ + [ + 'key' => 'bar', + 'value' => TRUE, + ], + ], + ], + [ + 'name' => 'foo3', + 'attributes' => [ + [ + 'key' => 'bar', + 'value' => [ + 'regexp' => [ + 'pattern' => '/^(baz)$/', + ], + ], + ], + ], + ], + [ + 'name' => 'foo4', + 'attributes' => [ + [ + 'key' => 'bar', + 'value' => [ + 'regexp' => [ + 'pattern' => '/^(baz|qux)$/', + ], + ], + ], + ], + ], + [ + 'name' => 'foo5', + 'attributes' => [ + [ + 'key' => 'bar', + 'value' => [ + 'regexp' => [ + 'pattern' => '/^(baz)$/', + ], + ], + ], + [ + 'key' => 'qux', + 'value' => [ + 'regexp' => [ + 'pattern' => '/^(foo)$/', + ], + ], + ], + ], + ], + ], + ], + ], + ], + 'Prefix wildcards' => [ + [ + 'allowed_tags' => [ + '', + '', + '', + '', + '', + ], + ], + [ + 'htmlSupport' => [ + 'allow' => [ + [ + 'name' => 'foo1', + 'attributes' => [ + [ + 'key' => [ + 'regexp' => [ + 'pattern' => '/^bar-.*$/', + ], + ], + 'value' => TRUE, + ], + ], + ], + [ + 'name' => 'foo2', + 'attributes' => [ + [ + 'key' => 'bar', + 'value' => [ + 'regexp' => [ + 'pattern' => '/^(baz-.*)$/', + ], + ], + ], + ], + ], + [ + 'name' => 'foo3', + 'attributes' => [ + [ + 'key' => 'bar', + 'value' => [ + 'regexp' => [ + 'pattern' => '/^(baz|qux-.*)$/', + ], + ], + ], + ], + ], + ], + ], + ], + ], + ]; + } + + /** + * @covers ::getDynamicPluginConfig + * @dataProvider providerGetDynamicPluginConfig + */ + public function testGetDynamicPluginConfig(array $configuration, array $expected_dynamic_config): void { + $plugin = new SourceEditing($configuration, 'ckeditor5_sourceEditing', NULL); + $dynamic_plugin_config = $plugin->getDynamicPluginConfig([], $this->prophesize(EditorInterface::class) + ->reveal()); + $this->assertSame($expected_dynamic_config, $dynamic_plugin_config); + } + +}