CKEditorAdminTest.php 14.7 KB
Newer Older
1 2
<?php

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 20 21 22

  /**
   * Modules to enable.
   *
   * @var array
   */
23
  protected static $modules = ['filter', 'editor', 'ckeditor'];
24

25 26 27 28 29
  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

30 31 32 33 34 35 36
  /**
   * A user with the 'administer filters' permission.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

37
  protected function setUp(): void {
38 39 40
    parent::setUp();

    // Create text format.
41
    $filtered_html_format = FilterFormat::create([
42 43 44
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
45 46
      'filters' => [],
    ]);
47 48 49
    $filtered_html_format->save();

    // Create admin user.
50
    $this->adminUser = $this->drupalCreateUser(['administer filters']);
51 52
  }

53 54 55
  /**
   * Tests configuring a text editor for an existing text format.
   */
56
  public function testExistingFormat() {
57
    $ckeditor = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
58

59
    $this->drupalLogin($this->adminUser);
60
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
61 62

    // Ensure no Editor config entity exists yet.
63
    $editor = Editor::load('filtered_html');
64
    $this->assertNull($editor, 'No Editor config entity exists yet.');
65 66 67 68 69

    // Verify the "Text Editor" <select> when a text editor is available.
    $select = $this->xpath('//select[@name="editor[editor]"]');
    $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
    $options = $this->xpath('//select[@name="editor[editor]"]/option');
70 71 72 73 74 75
    $this->assertCount(1, $select, 'The Text Editor select exists.');
    $this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
    $this->assertCount(2, $options, 'The Text Editor select has two options.');
    $this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
    $this->assertSame('CKEditor', $options[1]->getText(), 'Option 2 in the Text Editor select is "CKEditor".');
    $this->assertSame('selected', $options[0]->getAttribute('selected'), 'Option 1 ("None") is selected.');
76

77
    // Select the "CKEditor" editor and click the "Save configuration" button.
78
    $edit = [
79
      'editor[editor]' => 'ckeditor',
80
    ];
81
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
82 83
    $this->assertRaw(t('You must configure the selected text editor.'));

84
    // Ensure the CKEditor editor returns the expected default settings.
85 86 87
    $expected_default_settings = [
      'toolbar' => [
        'rows' => [
88
          // Button groups
89 90
          [
            [
91
              'name' => 'Formatting',
92
              'items' => ['Bold', 'Italic'],
93 94
            ],
            [
95
              'name' => 'Links',
96
              'items' => ['DrupalLink', 'DrupalUnlink'],
97 98
            ],
            [
99
              'name' => 'Lists',
100
              'items' => ['BulletedList', 'NumberedList'],
101 102
            ],
            [
103
              'name' => 'Media',
104
              'items' => ['Blockquote', 'DrupalImage'],
105 106
            ],
            [
107
              'name' => 'Tools',
108
              'items' => ['Source'],
109 110 111 112
            ],
          ],
        ],
      ],
113
      'plugins' => ['language' => ['language_list' => 'un']],
114
    ];
115
    $this->assertEquals($expected_default_settings, $ckeditor->getDefaultSettings());
116

117
    // Keep the "CKEditor" editor selected and click the "Configure" button.
118
    $this->drupalPostForm(NULL, $edit, 'editor_configure');
119
    $editor = Editor::load('filtered_html');
120
    $this->assertNull($editor, 'No Editor config entity exists yet.');
121

122
    // Ensure that drupalSettings is correct.
123
    $ckeditor_settings_toolbar = [
124 125 126
      '#theme' => 'ckeditor_settings_toolbar',
      '#editor' => Editor::create(['editor' => 'ckeditor']),
      '#plugins' => $this->container->get('plugin.manager.ckeditor.plugin')->getButtons(),
127
    ];
128 129
    $settings = $this->getDrupalSettings();
    $expected = $settings['ckeditor']['toolbarAdmin'];
130
    $this->assertEqual(
131
      $expected,
132 133 134 135
      $this->container->get('renderer')->renderPlain($ckeditor_settings_toolbar),
      'CKEditor toolbar settings are rendered as part of drupalSettings.'
    );

136 137
    // Ensure the toolbar buttons configuration value is initialized to the
    // expected default value.
138
    $expected_buttons_value = json_encode($expected_default_settings['toolbar']['rows']);
139
    $this->assertSession()->fieldValueEquals('editor[settings][toolbar][button_groups]', $expected_buttons_value);
140 141 142

    // Ensure the styles textarea exists and is initialized empty.
    $styles_textarea = $this->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
143
    $this->assertSession()->fieldValueEquals('editor[settings][plugins][stylescombo][styles]', '');
144
    $this->assertCount(1, $styles_textarea, 'The "styles" textarea exists.');
145 146

    // Submit the form to save the selection of CKEditor as the chosen editor.
147
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
148 149 150 151

    // Ensure an Editor object exists now, with the proper settings.
    $expected_settings = $expected_default_settings;
    $expected_settings['plugins']['stylescombo']['styles'] = '';
152
    $editor = Editor::load('filtered_html');
153
    $this->assertInstanceOf(Editor::class, $editor);
154
    $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
155 156

    // Configure the Styles plugin, and ensure the updated settings are saved.
157
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
158
    $edit = [
159
      'editor[settings][plugins][stylescombo][styles]' => "h1.title|Title\np.callout|Callout\n\n",
160
    ];
161
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
162
    $expected_settings['plugins']['stylescombo']['styles'] = "h1.title|Title\np.callout|Callout\n\n";
163
    $editor = Editor::load('filtered_html');
164
    $this->assertInstanceOf(Editor::class, $editor);
165
    $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
166 167 168 169

    // Change the buttons that appear on the toolbar (in JavaScript, this is
    // done via drag and drop, but here we can only emulate the end result of
    // that interaction). Test multiple toolbar rows and a divider within a row.
170
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
171
    $expected_settings['toolbar']['rows'][0][] = [
172
      'name' => 'Action history',
173 174 175
      'items' => ['Undo', '|', 'Redo', 'JustifyCenter'],
    ];
    $edit = [
176
      'editor[settings][toolbar][button_groups]' => json_encode($expected_settings['toolbar']['rows']),
177
    ];
178
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
179
    $editor = Editor::load('filtered_html');
180
    $this->assertInstanceOf(Editor::class, $editor);
181
    $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
182

183 184 185 186
    // Check that the markup we're setting for the toolbar buttons (actually in
    // JavaScript's drupalSettings, and Unicode-escaped) is correctly rendered.
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
    // Create function to encode HTML as we expect it in drupalSettings.
187
    $json_encode = function ($html) {
188 189 190 191 192 193 194 195 196 197 198
      return trim(Json::encode($html), '"');
    };
    // Check the Button separator.
    $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>'));
    // Check the Format dropdown.
    $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>'));
    // Check the Styles dropdown.
    $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>'));
    // Check strikethrough.
    $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>'));

199 200
    // Now enable the ckeditor_test module, which provides one configurable
    // CKEditor plugin — this should not affect the Editor config entity.
201
    \Drupal::service('module_installer')->install(['ckeditor_test']);
202
    $this->resetAll();
203
    $this->container->get('plugin.manager.ckeditor.plugin')->clearCachedDefinitions();
204
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
205
    $ultra_llama_mode_checkbox = $this->xpath('//input[@type="checkbox" and @name="editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]" and not(@checked)]');
206
    $this->assertCount(1, $ultra_llama_mode_checkbox, 'The "Ultra llama mode" checkbox exists and is not checked.');
207
    $editor = Editor::load('filtered_html');
208
    $this->assertInstanceOf(Editor::class, $editor);
209
    $this->assertEqual($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
210 211

    // Finally, check the "Ultra llama mode" checkbox.
212
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
213
    $edit = [
214
      'editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]' => '1',
215
    ];
216
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));
217
    $this->drupalGet('admin/config/content/formats/manage/filtered_html');
218
    $ultra_llama_mode_checkbox = $this->xpath('//input[@type="checkbox" and @name="editor[settings][plugins][llama_contextual_and_button][ultra_llama_mode]" and @checked="checked"]');
219
    $this->assertCount(1, $ultra_llama_mode_checkbox, 'The "Ultra llama mode" checkbox exists and is checked.');
220
    $expected_settings['plugins']['llama_contextual_and_button']['ultra_llama_mode'] = TRUE;
221
    $editor = Editor::load('filtered_html');
222
    $this->assertInstanceOf(Editor::class, $editor);
223
    $this->assertEqual($expected_settings, $editor->getSettings());
224 225 226 227 228 229 230 231 232 233 234

    $this->drupalGet('admin/config/content/formats/add');
    // Now attempt to add another filter format with the same editor and same
    // machine name.
    $edit = [
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'editor[editor]' => 'ckeditor',
    ];
    $this->submitForm($edit, 'editor_configure');
    $this->submitForm($edit, 'Save configuration');
235
    $this->assertSession()->statusCodeEquals(200);
236
    $this->assertText('The machine-readable name is already in use. It must be unique.');
237 238
  }

239 240 241 242 243 244
  /**
   * Tests configuring a text editor for a new text format.
   *
   * This test only needs to ensure that the basics of the CKEditor
   * configuration form work; details are tested in testExistingFormat().
   */
245
  public function testNewFormat() {
246
    $this->drupalLogin($this->adminUser);
247 248 249 250 251 252
    $this->drupalGet('admin/config/content/formats/add');

    // Verify the "Text Editor" <select> when a text editor is available.
    $select = $this->xpath('//select[@name="editor[editor]"]');
    $select_is_disabled = $this->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
    $options = $this->xpath('//select[@name="editor[editor]"]/option');
253 254 255 256 257 258
    $this->assertCount(1, $select, 'The Text Editor select exists.');
    $this->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
    $this->assertCount(2, $options, 'The Text Editor select has two options.');
    $this->assertSame('None', $options[0]->getText(), 'Option 1 in the Text Editor select is "None".');
    $this->assertSame('CKEditor', $options[1]->getText(), 'Option 2 in the Text Editor select is "CKEditor".');
    $this->assertSame('selected', $options[0]->getAttribute('selected'), 'Option 1 ("None") is selected.');
259 260 261

    // Name our fancy new text format, select the "CKEditor" editor and click
    // the "Configure" button.
262
    $edit = [
263 264 265
      'name' => 'My amazing text format',
      'format' => 'amazing_format',
      'editor[editor]' => 'ckeditor',
266
    ];
267
    $this->drupalPostForm(NULL, $edit, 'editor_configure');
268
    $filter_format = FilterFormat::load('amazing_format');
269
    $this->assertNull($filter_format, 'No FilterFormat config entity exists yet.');
270
    $editor = Editor::load('amazing_format');
271
    $this->assertNull($editor, 'No Editor config entity exists yet.');
272 273 274 275 276 277

    // Ensure the toolbar buttons configuration value is initialized to the
    // default value.
    $ckeditor = $this->container->get('plugin.manager.editor')->createInstance('ckeditor');
    $default_settings = $ckeditor->getDefaultSettings();
    $expected_buttons_value = json_encode($default_settings['toolbar']['rows']);
278
    $this->assertSession()->fieldValueEquals('editor[settings][toolbar][button_groups]', $expected_buttons_value);
279

280
    // Regression test for https://www.drupal.org/node/2606460.
281 282
    $settings = $this->getDrupalSettings();
    $expected = $settings['ckeditor']['toolbarAdmin'];
283
    $this->assertStringContainsString('<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>', $expected);
284

285 286
    // Ensure the styles textarea exists and is initialized empty.
    $styles_textarea = $this->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
287
    $this->assertSession()->fieldValueEquals('editor[settings][plugins][stylescombo][styles]', '');
288
    $this->assertCount(1, $styles_textarea, 'The "styles" textarea exists.');
289 290 291 292 293 294

    // Submit the form to create both a new text format and an associated text
    // editor.
    $this->drupalPostForm(NULL, $edit, t('Save configuration'));

    // Ensure a FilterFormat object exists now.
295
    $filter_format = FilterFormat::load('amazing_format');
296
    $this->assertInstanceOf(FilterFormatInterface::class, $filter_format);
297 298 299 300

    // Ensure an Editor object exists now, with the proper settings.
    $expected_settings = $default_settings;
    $expected_settings['plugins']['stylescombo']['styles'] = '';
301
    $editor = Editor::load('amazing_format');
302
    $this->assertInstanceOf(Editor::class, $editor);
303
    $this->assertEquals($expected_settings, $editor->getSettings(), 'The Editor config entity has the correct settings.');
304 305
  }

306
}