aeba60fd7545bc0cd125153ebedca0060c5e6ef9
[yaffs-website] / tests / src / Functional / CKEditorAdminTest.php
1 <?php
2
3 namespace Drupal\Tests\ckeditor\Functional;
4
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;
10
11 /**
12  * Tests administration of CKEditor.
13  *
14  * @group ckeditor
15  */
16 class CKEditorAdminTest extends BrowserTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['filter', 'editor', 'ckeditor'];
24
25   /**
26    * A user with the 'administer filters' permission.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   protected function setUp() {
33     parent::setUp();
34
35     // Create text format.
36     $filtered_html_format = FilterFormat::create([
37       'format' => 'filtered_html',
38       'name' => 'Filtered HTML',
39       'weight' => 0,
40       'filters' => [],
41     ]);
42     $filtered_html_format->save();
43
44     // Create admin user.
45     $this->adminUser = $this->drupalCreateUser(['administer filters']);
46   }
47
48   /**
49    * Tests configuring a text editor for an existing text format.
50    */
51   public function testExistingFormat() {
52     $ckeditor = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
53
54     $this->drupalLogin($this->adminUser);
55     $this->drupalGet('admin/config/content/formats/manage/filtered_html');
56
57     // Ensure no Editor config entity exists yet.
58     $editor = Editor::load('filtered_html');
59     $this->assertFalse($editor, 'No Editor config entity exists yet.');
60
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.');
71
72     // Select the "CKEditor" editor and click the "Save configuration" button.
73     $edit = [
74       'editor[editor]' => 'ckeditor',
75     ];
76     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
77     $this->assertRaw(t('You must configure the selected text editor.'));
78
79     // Ensure the CKEditor editor returns the expected default settings.
80     $expected_default_settings = [
81       'toolbar' => [
82         'rows' => [
83           // Button groups
84           [
85             [
86               'name' => 'Formatting',
87               'items' => ['Bold', 'Italic'],
88             ],
89             [
90               'name' => 'Links',
91               'items' => ['DrupalLink', 'DrupalUnlink'],
92             ],
93             [
94               'name' => 'Lists',
95               'items' => ['BulletedList', 'NumberedList'],
96             ],
97             [
98               'name' => 'Media',
99               'items' => ['Blockquote', 'DrupalImage'],
100             ],
101             [
102               'name' => 'Tools',
103               'items' => ['Source'],
104             ],
105           ],
106         ],
107       ],
108       'plugins' => ['language' => ['language_list' => 'un']],
109     ];
110     $this->assertIdentical($this->castSafeStrings($ckeditor->getDefaultSettings()), $expected_default_settings);
111
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.');
116
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(),
122     ];
123     $settings = $this->getDrupalSettings();
124     $expected = $settings['ckeditor']['toolbarAdmin'];
125     $this->assertEqual(
126       $expected,
127       $this->container->get('renderer')->renderPlain($ckeditor_settings_toolbar),
128       'CKEditor toolbar settings are rendered as part of drupalSettings.'
129     );
130
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);
135
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.');
140
141     // Submit the form to save the selection of CKEditor as the chosen editor.
142     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
143
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.');
150
151     // Configure the Styles plugin, and ensure the updated settings are saved.
152     $this->drupalGet('admin/config/content/formats/manage/filtered_html');
153     $edit = [
154       'editor[settings][plugins][stylescombo][styles]' => "h1.title|Title\np.callout|Callout\n\n",
155     ];
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.');
161
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'],
169     ];
170     $edit = [
171       'editor[settings][toolbar][button_groups]' => json_encode($expected_settings['toolbar']['rows']),
172     ];
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.');
177
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), '"');
184     };
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>'));
193
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']);
197     $this->resetAll();
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.');
205
206     // Finally, check the "Ultra llama mode" checkbox.
207     $this->drupalGet('admin/config/content/formats/manage/filtered_html');
208     $edit = [
209       'editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]' => '1',
210     ];
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());
219
220     $this->drupalGet('admin/config/content/formats/add');
221     // Now attempt to add another filter format with the same editor and same
222     // machine name.
223     $edit = [
224       'format' => 'filtered_html',
225       'name' => 'Filtered HTML',
226       'editor[editor]' => 'ckeditor',
227     ];
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.');
232   }
233
234   /**
235    * Tests configuring a text editor for a new text format.
236    *
237    * This test only needs to ensure that the basics of the CKEditor
238    * configuration form work; details are tested in testExistingFormat().
239    */
240   public function testNewFormat() {
241     $this->drupalLogin($this->adminUser);
242     $this->drupalGet('admin/config/content/formats/add');
243
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.');
254
255     // Name our fancy new text format, select the "CKEditor" editor and click
256     // the "Configure" button.
257     $edit = [
258       'name' => 'My amazing text format',
259       'format' => 'amazing_format',
260       'editor[editor]' => 'ckeditor',
261     ];
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.');
267
268     // Ensure the toolbar buttons configuration value is initialized to the
269     // default value.
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);
274
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);
279
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.');
284
285     // Submit the form to create both a new text format and an associated text
286     // editor.
287     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
288
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.');
292
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.');
299   }
300
301 }