3 namespace Drupal\Tests\ckeditor\Functional;
5 use Drupal\Component\Serialization\Json;
6 use Drupal\editor\Entity\Editor;
7 use Drupal\filter\FilterFormatInterface;
8 use Drupal\filter\Entity\FilterFormat;
9 use Drupal\Tests\BrowserTestBase;
12 * Tests administration of CKEditor.
16 class CKEditorAdminTest extends BrowserTestBase {
23 public static $modules = ['filter', 'editor', 'ckeditor'];
26 * A user with the 'administer filters' permission.
28 * @var \Drupal\user\UserInterface
32 protected function setUp() {
35 // Create text format.
36 $filtered_html_format = FilterFormat::create([
37 'format' => 'filtered_html',
38 'name' => 'Filtered HTML',
42 $filtered_html_format->save();
45 $this->adminUser = $this->drupalCreateUser(['administer filters']);
49 * Tests configuring a text editor for an existing text format.
51 public function testExistingFormat() {
52 $ckeditor = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
54 $this->drupalLogin($this->adminUser);
55 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
57 // Ensure no Editor config entity exists yet.
58 $editor = Editor::load('filtered_html');
59 $this->assertFalse($editor, 'No Editor config entity exists yet.');
61 // Verify the "Text Editor" <select> when a text editor is available.
62 $select = $this->xpath('//select[@name="editor[editor]"]');
63 $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
64 $options = $this->xpath('//select[@name="editor[editor]"]/option');
65 $this->assertCount(1, $select, 'The Text Editor select exists.');
66 $this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
67 $this->assertCount(2, $options, 'The Text Editor select has two options.');
68 $this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
69 $this->assertSame('CKEditor', $options[1]->getText(), 'Option 2 in the Text Editor select is "CKEditor".');
70 $this->assertSame('selected', $options[0]->getAttribute('selected'), 'Option 1 ("None") is selected.');
72 // Select the "CKEditor" editor and click the "Save configuration" button.
74 'editor[editor]' => 'ckeditor',
76 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
77 $this->assertRaw(t('You must configure the selected text editor.'));
79 // Ensure the CKEditor editor returns the expected default settings.
80 $expected_default_settings = [
86 'name' => 'Formatting',
87 'items' => ['Bold', 'Italic'],
91 'items' => ['DrupalLink', 'DrupalUnlink'],
95 'items' => ['BulletedList', 'NumberedList'],
99 'items' => ['Blockquote', 'DrupalImage'],
103 'items' => ['Source'],
108 'plugins' => ['language' => ['language_list' => 'un']],
110 $this->assertIdentical($this->castSafeStrings($ckeditor->getDefaultSettings()), $expected_default_settings);
112 // Keep the "CKEditor" editor selected and click the "Configure" button.
113 $this->drupalPostForm(NULL, $edit, 'editor_configure');
114 $editor = Editor::load('filtered_html');
115 $this->assertFalse($editor, 'No Editor config entity exists yet.');
117 // Ensure that drupalSettings is correct.
118 $ckeditor_settings_toolbar = [
119 '#theme' => 'ckeditor_settings_toolbar',
120 '#editor' => Editor::create(['editor' => 'ckeditor']),
121 '#plugins' => $this->container->get('plugin.manager.ckeditor.plugin')->getButtons(),
123 $settings = $this->getDrupalSettings();
124 $expected = $settings['ckeditor']['toolbarAdmin'];
127 $this->container->get('renderer')->renderPlain($ckeditor_settings_toolbar),
128 'CKEditor toolbar settings are rendered as part of drupalSettings.'
131 // Ensure the toolbar buttons configuration value is initialized to the
132 // expected default value.
133 $expected_buttons_value = json_encode($expected_default_settings['toolbar']['rows']);
134 $this->assertFieldByName('editor[settings][toolbar][button_groups]', $expected_buttons_value);
136 // Ensure the styles textarea exists and is initialized empty.
137 $styles_textarea = $this->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
138 $this->assertFieldByXPath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]', '', 'The styles textarea exists and is empty.');
139 $this->assertTrue(count($styles_textarea) === 1, 'The "styles" textarea exists.');
141 // Submit the form to save the selection of CKEditor as the chosen editor.
142 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
144 // Ensure an Editor object exists now, with the proper settings.
145 $expected_settings = $expected_default_settings;
146 $expected_settings['plugins']['stylescombo']['styles'] = '';
147 $editor = Editor::load('filtered_html');
148 $this->assertTrue($editor instanceof Editor, 'An Editor config entity exists now.');
149 $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
151 // Configure the Styles plugin, and ensure the updated settings are saved.
152 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
154 'editor[settings][plugins][stylescombo][styles]' => "h1.title|Title\np.callout|Callout\n\n",
156 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
157 $expected_settings['plugins']['stylescombo']['styles'] = "h1.title|Title\np.callout|Callout\n\n";
158 $editor = Editor::load('filtered_html');
159 $this->assertTrue($editor instanceof Editor, 'An Editor config entity exists.');
160 $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
162 // Change the buttons that appear on the toolbar (in JavaScript, this is
163 // done via drag and drop, but here we can only emulate the end result of
164 // that interaction). Test multiple toolbar rows and a divider within a row.
165 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
166 $expected_settings['toolbar']['rows'][0][] = [
167 'name' => 'Action history',
168 'items' => ['Undo', '|', 'Redo', 'JustifyCenter'],
171 'editor[settings][toolbar][button_groups]' => json_encode($expected_settings['toolbar']['rows']),
173 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
174 $editor = Editor::load('filtered_html');
175 $this->assertTrue($editor instanceof Editor, 'An Editor config entity exists.');
176 $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
178 // Check that the markup we're setting for the toolbar buttons (actually in
179 // JavaScript's drupalSettings, and Unicode-escaped) is correctly rendered.
180 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
181 // Create function to encode HTML as we expect it in drupalSettings.
182 $json_encode = function ($html) {
183 return trim(Json::encode($html), '"');
185 // Check the Button separator.
186 $this->assertRaw($json_encode('<li data-drupal-ckeditor-button-name="-" class="ckeditor-button-separator ckeditor-multiple-button" data-drupal-ckeditor-type="separator"><a href="#" role="button" aria-label="Button separator" class="ckeditor-separator"></a></li>'));
187 // Check the Format dropdown.
188 $this->assertRaw($json_encode('<li data-drupal-ckeditor-button-name="Format" class="ckeditor-button"><a href="#" role="button" aria-label="Format"><span class="ckeditor-button-dropdown">Format<span class="ckeditor-button-arrow"></span></span></a></li>'));
189 // Check the Styles dropdown.
190 $this->assertRaw($json_encode('<li data-drupal-ckeditor-button-name="Styles" class="ckeditor-button"><a href="#" role="button" aria-label="Styles"><span class="ckeditor-button-dropdown">Styles<span class="ckeditor-button-arrow"></span></span></a></li>'));
191 // Check strikethrough.
192 $this->assertRaw($json_encode('<li data-drupal-ckeditor-button-name="Strike" class="ckeditor-button"><a href="#" class="cke-icon-only cke_ltr" role="button" title="strike" aria-label="strike"><span class="cke_button_icon cke_button__strike_icon">strike</span></a></li>'));
194 // Now enable the ckeditor_test module, which provides one configurable
195 // CKEditor plugin — this should not affect the Editor config entity.
196 \Drupal::service('module_installer')->install(['ckeditor_test']);
198 $this->container->get('plugin.manager.ckeditor.plugin')->clearCachedDefinitions();
199 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
200 $ultra_llama_mode_checkbox = $this->xpath('//input[@type="checkbox" and @name="editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]" and not(@checked)]');
201 $this->assertTrue(count($ultra_llama_mode_checkbox) === 1, 'The "Ultra llama mode" checkbox exists and is not checked.');
202 $editor = Editor::load('filtered_html');
203 $this->assertTrue($editor instanceof Editor, 'An Editor config entity exists.');
204 $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
206 // Finally, check the "Ultra llama mode" checkbox.
207 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
209 'editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]' => '1',
211 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
212 $this->drupalGet('admin/config/content/formats/manage/filtered_html');
213 $ultra_llama_mode_checkbox = $this->xpath('//input[@type="checkbox" and @name="editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]" and @checked="checked"]');
214 $this->assertTrue(count($ultra_llama_mode_checkbox) === 1, 'The "Ultra llama mode" checkbox exists and is checked.');
215 $expected_settings['plugins']['llama_contextual_and_button']['ultra_llama_mode'] = TRUE;
216 $editor = Editor::load('filtered_html');
217 $this->assertTrue($editor instanceof Editor, 'An Editor config entity exists.');
218 $this->assertEqual($expected_settings, $editor->getSettings());
220 $this->drupalGet('admin/config/content/formats/add');
221 // Now attempt to add another filter format with the same editor and same
224 'format' => 'filtered_html',
225 'name' => 'Filtered HTML',
226 'editor[editor]' => 'ckeditor',
228 $this->submitForm($edit, 'editor_configure');
229 $this->submitForm($edit, 'Save configuration');
230 $this->assertResponse(200);
231 $this->assertText('The machine-readable name is already in use. It must be unique.');
235 * Tests configuring a text editor for a new text format.
237 * This test only needs to ensure that the basics of the CKEditor
238 * configuration form work; details are tested in testExistingFormat().
240 public function testNewFormat() {
241 $this->drupalLogin($this->adminUser);
242 $this->drupalGet('admin/config/content/formats/add');
244 // Verify the "Text Editor" <select> when a text editor is available.
245 $select = $this->xpath('//select[@name="editor[editor]"]');
246 $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
247 $options = $this->xpath('//select[@name="editor[editor]"]/option');
248 $this->assertCount(1, $select, 'The Text Editor select exists.');
249 $this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
250 $this->assertCount(2, $options, 'The Text Editor select has two options.');
251 $this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
252 $this->assertSame('CKEditor', $options[1]->getText(), 'Option 2 in the Text Editor select is "CKEditor".');
253 $this->assertSame('selected', $options[0]->getAttribute('selected'), 'Option 1 ("None") is selected.');
255 // Name our fancy new text format, select the "CKEditor" editor and click
256 // the "Configure" button.
258 'name' => 'My amazing text format',
259 'format' => 'amazing_format',
260 'editor[editor]' => 'ckeditor',
262 $this->drupalPostForm(NULL, $edit, 'editor_configure');
263 $filter_format = FilterFormat::load('amazing_format');
264 $this->assertFalse($filter_format, 'No FilterFormat config entity exists yet.');
265 $editor = Editor::load('amazing_format');
266 $this->assertFalse($editor, 'No Editor config entity exists yet.');
268 // Ensure the toolbar buttons configuration value is initialized to the
270 $ckeditor = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
271 $default_settings = $ckeditor->getDefaultSettings();
272 $expected_buttons_value = json_encode($default_settings['toolbar']['rows']);
273 $this->assertFieldByName('editor[settings][toolbar][button_groups]', $expected_buttons_value);
275 // Regression test for https://www.drupal.org/node/2606460.
276 $settings = $this->getDrupalSettings();
277 $expected = $settings['ckeditor']['toolbarAdmin'];
278 $this->assertTrue(strpos($expected, '<li data-drupal-ckeditor-button-name="Bold" class="ckeditor-button"><a href="#" class="cke-icon-only cke_ltr" role="button" title="bold" aria-label="bold"><span class="cke_button_icon cke_button__bold_icon">bold</span></a></li>') !== FALSE);
280 // Ensure the styles textarea exists and is initialized empty.
281 $styles_textarea = $this->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
282 $this->assertFieldByXPath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]', '', 'The styles textarea exists and is empty.');
283 $this->assertTrue(count($styles_textarea) === 1, 'The "styles" textarea exists.');
285 // Submit the form to create both a new text format and an associated text
287 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
289 // Ensure a FilterFormat object exists now.
290 $filter_format = FilterFormat::load('amazing_format');
291 $this->assertTrue($filter_format instanceof FilterFormatInterface, 'A FilterFormat config entity exists now.');
293 // Ensure an Editor object exists now, with the proper settings.
294 $expected_settings = $default_settings;
295 $expected_settings['plugins']['stylescombo']['styles'] = '';
296 $editor = Editor::load('amazing_format');
297 $this->assertTrue($editor instanceof Editor, 'An Editor config entity exists now.');
298 $this->assertEqual($this->castSafeStrings($expected_settings), $this->castSafeStrings($editor->getSettings()), 'The Editor config entity has the correct settings.');