summaryrefslogtreecommitdiffstats
path: root/core/modules/ckeditor/lib/Drupal/ckeditor/Tests/CKEditorTest.php
blob: 971d12e058739162e8918c9895445d4d777829e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php

/**
 * @file
 * Definition of \Drupal\ckeditor\Tests\CKEditorTest.
 */

namespace Drupal\ckeditor\Tests;

use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\editor\Plugin\EditorManager;
use Drupal\ckeditor\Plugin\Editor\CKEditor;

/**
 * Tests for the 'CKEditor' text editor plugin.
 */
class CKEditorTest extends DrupalUnitTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('system', 'editor', 'ckeditor', 'filter_test');

  /**
   * An instance of the "CKEditor" text editor plugin.
   *
   * @var \Drupal\ckeditor\Plugin\Editor\CKEditor;
   */
  protected $ckeditor;

  public static function getInfo() {
    return array(
      'name' => 'CKEditor text editor plugin',
      'description' => 'Tests all aspects of the CKEditor text editor plugin.',
      'group' => 'CKEditor',
    );
  }

  function setUp() {
    parent::setUp();

    // Install the Filter module.
    $this->installSchema('system', 'url_alias');
    $this->enableModules(array('user', 'filter'));

    // Create text format, associate CKEditor.
    $filtered_html_format = entity_create('filter_format', array(
      'format' => 'filtered_html',
      'name' => 'Filtered HTML',
      'weight' => 0,
      'filters' => array(
        'filter_html' => array(
          'status' => 1,
          'settings' => array(
            'allowed_html' => '<h4> <h5> <h6> <p> <br> <strong> <a>',
          )
        ),
      ),
    ));
    $filtered_html_format->save();
    $editor = entity_create('editor', array(
      'format' => 'filtered_html',
      'editor' => 'ckeditor',
    ));
    $editor->save();

    // Create "CKEditor" text editor plugin instance.
    $manager = $this->container->get('plugin.manager.editor');
    $this->ckeditor = $manager->createInstance('ckeditor');
  }

  /**
   * Tests CKEditor::getJSSettings().
   */
  function testGetJSSettings() {
    $editor = entity_load('editor', 'filtered_html');

    // Default toolbar.
    $expected_config = $this->getDefaultInternalConfig() + array(
      'allowedContent' => $this->getDefaultAllowedContentConfig(),
      'toolbar' => $this->getDefaultToolbarConfig(),
      'contentsCss' => $this->getDefaultContentsCssConfig(),
      'extraPlugins' => '',
      'language' => 'en',
      'stylesSet' => FALSE,
      'drupalExternalPlugins' => array(),
    );
    ksort($expected_config);
    $this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for default configuration.');

    // Customize the configuration: add button, have two contextually enabled
    // buttons, and configure a CKEditor plugin setting.
    $this->enableModules(array('ckeditor_test'));
    drupal_container()->get('plugin.manager.ckeditor.plugin')->clearCachedDefinitions();
    $editor->settings['toolbar']['buttons'][0][] = 'Strike';
    $editor->settings['toolbar']['buttons'][1][] = 'Format';
    $editor->settings['plugins']['internal']['link_shortcut'] = 'CTRL+K';
    $editor->save();
    $expected_config['toolbar'][count($expected_config['toolbar'])-2]['items'][] = 'Strike';
    $expected_config['toolbar'][]['items'][] = 'Format';
    $expected_config['toolbar'][] = '/';
    $expected_config['format_tags'] = 'p;h4;h5;h6';
    $expected_config['extraPlugins'] = 'llama_contextual,llama_contextual_and_button';
    $expected_config['drupalExternalPlugins']['llama_contextual'] = file_create_url('core/modules/ckeditor/tests/modules/js/llama_contextual.js');
    $expected_config['drupalExternalPlugins']['llama_contextual_and_button'] = file_create_url('core/modules/ckeditor/tests/modules/js/llama_contextual_and_button.js');
    $expected_config['contentsCss'][] = file_create_url('core/modules/ckeditor/tests/modules/ckeditor_test.css');
    $expected_config['keystrokes'] = array(array(1114187, 'link'), array(1114188, NULL));
    ksort($expected_config);
    $this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.');

    // Change the allowed HTML tags; the "allowedContent" and "format_tags"
    // settings for CKEditor should automatically be updated as well.
    $format = entity_load('filter_format', 'filtered_html');
    $format->filters('filter_html')->settings['allowed_html'] .= '<pre> <h3>';
    $format->save();
    $expected_config['allowedContent']['pre'] = array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE);
    $expected_config['allowedContent']['h3'] = array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE);
    $expected_config['format_tags'] = 'p;h3;h4;h5;h6;pre';
    $this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.');

    // Disable the filter_html filter: allow *all *tags.
    $format->setFilterConfig('filter_html', array('status' => 0));
    $format->save();
    $expected_config['allowedContent'] = TRUE;
    $expected_config['format_tags'] = 'p;h1;h2;h3;h4;h5;h6;pre';
    $this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.');

    // Enable the filter_test_restrict_tags_and_attributes filter.
    $format->setFilterConfig('filter_test_restrict_tags_and_attributes', array(
      'status' => 1,
      'settings' => array(
        'restrictions' => array(
          'allowed' => array(
            'p' => TRUE,
            'a' => array(
              'href' => TRUE,
              'rel' => array('nofollow' => TRUE),
              'class' => array('external' => TRUE),
              'target' => array('_blank' => FALSE),
            ),
            'span' => array(
              'class' => array('dodo' => FALSE),
              'property' => array('dc:*' => TRUE),
              'rel' => array('foaf:*' => FALSE),
            ),
            '*' => array(
              'style' => FALSE,
              'class' => array('is-a-hipster-llama' => TRUE, 'and-more' => TRUE),
              'data-*' => TRUE,
            ),
            'del' => FALSE,
          )
        ),
      ),
    ));
    $format->save();
    $expected_config['allowedContent'] = array(
      'p' => array(
        'attributes' => TRUE,
        'styles' => FALSE,
        'classes' => 'is-a-hipster-llama,and-more',
      ),
      'a' => array(
        'attributes' => 'href,rel,class,target',
        'classes' => 'external',
      ),
      'span' => array(
        'attributes' => 'class,property,rel',
      ),
      '*' => array(
        'attributes' => 'class,data-*',
        'classes' => 'is-a-hipster-llama,and-more',
      ),
      'del' => array(
        'attributes' => FALSE,
        'styles' => FALSE,
        'classes' => FALSE,
      ),
    );
    $expected_config['format_tags'] = 'p';
    ksort($expected_config);
    $this->assertIdentical($expected_config, $this->ckeditor->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.');
  }

  /**
   * Tests CKEditor::buildToolbarJSSetting().
   */
  function testBuildToolbarJSSetting() {
    $editor = entity_load('editor', 'filtered_html');

    // Default toolbar.
    $expected = $this->getDefaultToolbarConfig();
    $this->assertIdentical($expected, $this->ckeditor->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for default toolbar.');

    // Customize the configuration.
    $editor->settings['toolbar']['buttons'][0][] = 'Strike';
    $editor->save();
    $expected[count($expected)-2]['items'][] = 'Strike';
    $this->assertIdentical($expected, $this->ckeditor->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for customized toolbar.');

    // Enable the editor_test module, customize further.
    $this->enableModules(array('ckeditor_test'));
    drupal_container()->get('plugin.manager.ckeditor.plugin')->clearCachedDefinitions();
    $editor->settings['toolbar']['buttons'][0][] = 'Llama';
    $editor->save();
    $expected[count($expected)-2]['items'][] = 'Llama';
    $this->assertIdentical($expected, $this->ckeditor->buildToolbarJSSetting($editor), '"toolbar" configuration part of JS settings built correctly for customized toolbar with contrib module-provided CKEditor plugin.');
  }

  /**
   * Tests CKEditor::buildContentsCssJSSetting().
   */
  function testBuildContentsCssJSSetting() {
    $editor = entity_load('editor', 'filtered_html');

    // Default toolbar.
    $expected = $this->getDefaultContentsCssConfig();
    $this->assertIdentical($expected, $this->ckeditor->buildContentsCssJSSetting($editor), '"contentsCss" configuration part of JS settings built correctly for default toolbar.');

    // Enable the editor_test module, which implements hook_ckeditor_css_alter().
    $this->enableModules(array('ckeditor_test'));
    $expected[] = file_create_url('core/modules/ckeditor/tests/modules/ckeditor_test.css');
    $this->assertIdentical($expected, $this->ckeditor->buildContentsCssJSSetting($editor), '"contentsCss" configuration part of JS settings built correctly while a hook_ckeditor_css_alter() implementation exists.');

    // @todo test coverage for _ckeditor_theme_css(), by including a custom theme in this test with a "ckeditor_stylesheets" entry in its .info file.
  }

  /**
   * Tests Internal::getConfig().
   */
  function testInternalGetConfig() {
    $editor = entity_load('editor', 'filtered_html');
    $manager = drupal_container()->get('plugin.manager.ckeditor.plugin');
    $internal_plugin = $manager->createInstance('internal');

    // Default toolbar.
    $expected = $this->getDefaultInternalConfig();
    $expected['allowedContent'] = $this->getDefaultAllowedContentConfig();
    $this->assertIdentical($expected, $internal_plugin->getConfig($editor), '"Internal" plugin configuration built correctly for default toolbar.');

    // Format dropdown/button enabled: new setting should be present.
    $editor->settings['toolbar']['buttons'][0][] = 'Format';
    $expected['format_tags'] = 'p;h4;h5;h6';
    $this->assertIdentical($expected, $internal_plugin->getConfig($editor), '"Internal" plugin configuration built correctly for customized toolbar.');
  }

  /**
   * Tests StylesCombo::getConfig().
   */
  function testStylesComboGetConfig() {
    $editor = entity_load('editor', 'filtered_html');
    $manager = drupal_container()->get('plugin.manager.ckeditor.plugin');
    $stylescombo_plugin = $manager->createInstance('stylescombo');

    // Styles dropdown/button enabled: new setting should be present.
    $editor->settings['toolbar']['buttons'][0][] = 'Styles';
    $editor->settings['plugins']['stylescombo']['styles'] = '';
    $editor->save();
    $expected['stylesSet'] = array();
    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Configure the optional "styles" setting in odd ways that shouldn't affect
    // the end result.
    $editor->settings['plugins']['stylescombo']['styles'] = "   \n";
    $editor->save();
    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor));
    $editor->settings['plugins']['stylescombo']['styles'] = "\r\n  \n  \r  \n ";
    $editor->save();
    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Now configure it properly, the end result should change.
    $editor->settings['plugins']['stylescombo']['styles'] = "h1.title|Title\np.mAgical.Callout|Callout";
    $editor->save();
    $expected['stylesSet'] = array(
      array('name' => 'Title', 'element' => 'h1', 'attributes' => array('class' => 'title')),
      array('name' => 'Callout', 'element' => 'p', 'attributes' => array('class' => 'mAgical Callout')),
    );
    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Same configuration, but now interspersed with nonsense. Should yield the
    // same result.
    $editor->settings['plugins']['stylescombo']['styles'] = "  h1 .title   |  Title \r \n\r  \np.mAgical  .Callout|Callout\r";
    $editor->save();
    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Slightly different configuration: class names are optional.
    $editor->settings['plugins']['stylescombo']['styles'] = "      h1 |  Title ";
    $editor->save();
    $expected['stylesSet'] = array(array('name' => 'Title', 'element' => 'h1'));
    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

    // Invalid syntax should cause stylesSet to be set to FALSE.
    $editor->settings['plugins']['stylescombo']['styles'] = "h1";
    $editor->save();
    $expected['stylesSet'] = FALSE;
    $this->assertIdentical($expected, $stylescombo_plugin->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');
  }

  protected function getDefaultInternalConfig() {
    return array(
      'customConfig' => '',
      'pasteFromWordPromptCleanup' => TRUE,
      'removeDialogTabs' => 'image:Link;image:advanced;link:advanced',
      'resize_dir' => 'vertical',
      'keystrokes' =>  array(array(0x110000 + 75, 'link'), array(0x110000 + 76, NULL)),
    );
  }

  protected function getDefaultAllowedContentConfig() {
    return array(
      'h4' => array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE),
      'h5' => array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE),
      'h6' => array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE),
      'p' => array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE),
      'br' => array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE),
      'strong' => array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE),
      'a' => array('attributes' => TRUE, 'styles' => FALSE, 'classes' => TRUE),
    );
  }

  protected function getDefaultToolbarConfig() {
    return array(
      0 => array('items' => array('Bold', 'Italic')),
      1 => array('items' => array('Link', 'Unlink')),
      2 => array('items' => array('BulletedList', 'NumberedList')),
      3 => array('items' => array('Blockquote', 'Image')),
      4 => array('items' => array('Source')),
      5 => '/'
    );
  }

  protected function getDefaultContentsCssConfig() {
    return array(
      file_create_url('core/modules/ckeditor/css/ckeditor-iframe.css'),
    );
  }

}