3 namespace Drupal\Tests\ckeditor\Kernel;
5 use Drupal\editor\Entity\Editor;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\filter\Entity\FilterFormat;
10 * Tests different ways of enabling CKEditor plugins.
14 class CKEditorPluginManagerTest extends KernelTestBase {
21 public static $modules = ['system', 'user', 'filter', 'editor', 'ckeditor'];
24 * The manager for "CKEditor plugin" plugins.
26 * @var \Drupal\Component\Plugin\PluginManagerInterface
30 protected function setUp() {
33 // Install the Filter module.
35 // Create text format, associate CKEditor.
36 $filtered_html_format = FilterFormat::create([
37 'format' => 'filtered_html',
38 'name' => 'Filtered HTML',
42 $filtered_html_format->save();
43 $editor = Editor::create([
44 'format' => 'filtered_html',
45 'editor' => 'ckeditor',
51 * Tests the enabling of plugins.
53 public function testEnabledPlugins() {
54 $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
55 $editor = Editor::load('filtered_html');
57 // Case 1: no CKEditor plugins.
58 $definitions = array_keys($this->manager->getDefinitions());
60 $this->assertIdentical(['drupalimage', 'drupalimagecaption', 'drupallink', 'internal', 'language', 'stylescombo'], $definitions, 'No CKEditor plugins found besides the built-in ones.');
62 'drupalimage' => drupal_get_path('module', 'ckeditor') . '/js/plugins/drupalimage/plugin.js',
63 'drupallink' => drupal_get_path('module', 'ckeditor') . '/js/plugins/drupallink/plugin.js',
65 $this->assertIdentical($enabled_plugins, $this->manager->getEnabledPluginFiles($editor), 'Only built-in plugins are enabled.');
66 $this->assertIdentical(['internal' => NULL] + $enabled_plugins, $this->manager->getEnabledPluginFiles($editor, TRUE), 'Only the "internal" plugin is enabled.');
68 // Enable the CKEditor Test module, which has the Llama plugin (plus four
69 // variations of it, to cover all possible ways a plugin can be enabled) and
70 // clear the editor manager's cache so it is picked up.
71 $this->enableModules(['ckeditor_test']);
72 $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
73 $this->manager->clearCachedDefinitions();
75 // Case 2: CKEditor plugins are available.
76 $plugin_ids = array_keys($this->manager->getDefinitions());
78 $this->assertIdentical(['drupalimage', 'drupalimagecaption', 'drupallink', 'internal', 'language', 'llama', 'llama_button', 'llama_contextual', 'llama_contextual_and_button', 'llama_css', 'stylescombo'], $plugin_ids, 'Additional CKEditor plugins found.');
79 $this->assertIdentical($enabled_plugins, $this->manager->getEnabledPluginFiles($editor), 'Only the internal plugins are enabled.');
80 $this->assertIdentical(['internal' => NULL] + $enabled_plugins, $this->manager->getEnabledPluginFiles($editor, TRUE), 'Only the "internal" plugin is enabled.');
82 // Case 3: enable each of the newly available plugins, if possible:
83 // a. Llama: cannot be enabled, since it does not implement
84 // CKEditorPluginContextualInterface nor CKEditorPluginButtonsInterface.
85 // b. LlamaContextual: enabled by adding the 'Strike' button, which is
86 // part of another plugin!
87 // c. LlamaButton: automatically enabled by adding its 'Llama' button.
88 // d. LlamaContextualAndButton: enabled by either b or c.
89 // e. LlamaCSS: automatically enabled by add its 'LlamaCSS' button.
90 // Below, we will first enable the "Llama" button, which will cause the
91 // LlamaButton and LlamaContextualAndButton plugins to be enabled. Then we
92 // will remove the "Llama" button and add the "Strike" button, which will
93 // cause the LlamaContextual and LlamaContextualAndButton plugins to be
94 // enabled. Then we will add the "Strike" button back again, which would
95 // cause LlamaButton, LlamaContextual and LlamaContextualAndButton to be
96 // enabled. Finally, we will add the "LlamaCSS" button which would cause
97 // all four plugins to be enabled.
98 $settings = $editor->getSettings();
99 $original_toolbar = $settings['toolbar'];
100 $settings['toolbar']['rows'][0][0]['items'][] = 'Llama';
101 $editor->setSettings($settings);
104 $file['b'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_button.js';
105 $file['c'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_contextual.js';
106 $file['cb'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_contextual_and_button.js';
107 $file['css'] = drupal_get_path('module', 'ckeditor_test') . '/js/llama_css.js';
108 $expected = $enabled_plugins + ['llama_button' => $file['b'], 'llama_contextual_and_button' => $file['cb']];
109 $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.');
110 $this->assertIdentical(['internal' => NULL] + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LlamaButton and LlamaContextualAndButton plugins are enabled.');
111 $settings['toolbar'] = $original_toolbar;
112 $settings['toolbar']['rows'][0][0]['items'][] = 'Strike';
113 $editor->setSettings($settings);
115 $expected = $enabled_plugins + ['llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']];
116 $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LLamaContextual and LlamaContextualAndButton plugins are enabled.');
117 $this->assertIdentical(['internal' => NULL] + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LlamaContextual and LlamaContextualAndButton plugins are enabled.');
118 $settings['toolbar']['rows'][0][0]['items'][] = 'Llama';
119 $editor->setSettings($settings);
121 $expected = $enabled_plugins + ['llama_button' => $file['b'], 'llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb']];
122 $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.');
123 $this->assertIdentical(['internal' => NULL] + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LLamaButton, LlamaContextual and LlamaContextualAndButton plugins are enabled.');
124 $settings['toolbar']['rows'][0][0]['items'][] = 'LlamaCSS';
125 $editor->setSettings($settings);
127 $expected = $enabled_plugins + ['llama_button' => $file['b'], 'llama_contextual' => $file['c'], 'llama_contextual_and_button' => $file['cb'], 'llama_css' => $file['css']];
128 $this->assertIdentical($expected, $this->manager->getEnabledPluginFiles($editor), 'The LlamaButton, LlamaContextual, LlamaContextualAndButton and LlamaCSS plugins are enabled.');
129 $this->assertIdentical(['internal' => NULL] + $expected, $this->manager->getEnabledPluginFiles($editor, TRUE), 'The LLamaButton, LlamaContextual, LlamaContextualAndButton and LlamaCSS plugins are enabled.');
133 * Tests the iframe instance CSS files of plugins.
135 public function testCssFiles() {
136 $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
137 $editor = Editor::load('filtered_html');
139 // Case 1: no CKEditor iframe instance CSS file.
140 $this->assertIdentical([], $this->manager->getCssFiles($editor), 'No iframe instance CSS file found.');
142 // Enable the CKEditor Test module, which has the LlamaCss plugin and
143 // clear the editor manager's cache so it is picked up.
144 $this->enableModules(['ckeditor_test']);
145 $this->manager = $this->container->get('plugin.manager.ckeditor.plugin');
146 $settings = $editor->getSettings();
147 // LlamaCss: automatically enabled by adding its 'LlamaCSS' button.
148 $settings['toolbar']['rows'][0][0]['items'][] = 'LlamaCSS';
149 $editor->setSettings($settings);
152 // Case 2: CKEditor iframe instance CSS file.
154 'llama_css' => [drupal_get_path('module', 'ckeditor_test') . '/css/llama.css'],
156 $this->assertIdentical($expected, $this->manager->getCssFiles($editor), 'Iframe instance CSS file found.');