You are here

public function CKEditorTest::testGetJSSettings in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/ckeditor/tests/src/Kernel/CKEditorTest.php \Drupal\Tests\ckeditor\Kernel\CKEditorTest::testGetJSSettings()

Tests CKEditor::getJSSettings().

File

core/modules/ckeditor/tests/src/Kernel/CKEditorTest.php, line 86

Class

CKEditorTest
Tests for the 'CKEditor' text editor plugin.

Namespace

Drupal\Tests\ckeditor\Kernel

Code

public function testGetJSSettings() {
  $editor = Editor::load('filtered_html');
  $query_string = '?0=';

  // Default toolbar.
  $expected_config = $this
    ->getDefaultInternalConfig() + [
    'drupalImage_dialogTitleAdd' => 'Insert Image',
    'drupalImage_dialogTitleEdit' => 'Edit Image',
    'drupalLink_dialogTitleAdd' => 'Add Link',
    'drupalLink_dialogTitleEdit' => 'Edit Link',
    'allowedContent' => $this
      ->getDefaultAllowedContentConfig(),
    'disallowedContent' => $this
      ->getDefaultDisallowedContentConfig(),
    'toolbar' => $this
      ->getDefaultToolbarConfig(),
    'contentsCss' => $this
      ->getDefaultContentsCssConfig(),
    'extraPlugins' => 'drupalimage,drupallink',
    'language' => 'en',
    'stylesSet' => FALSE,
    'drupalExternalPlugins' => [
      'drupalimage' => $this->fileUrlGenerator
        ->generateString('core/modules/ckeditor/js/plugins/drupalimage/plugin.js'),
      'drupallink' => $this->fileUrlGenerator
        ->generateString('core/modules/ckeditor/js/plugins/drupallink/plugin.js'),
    ],
  ];
  $this
    ->assertEquals($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([
    'ckeditor_test',
  ]);
  $this->container
    ->get('plugin.manager.editor')
    ->clearCachedDefinitions();
  $this->ckeditor = $this->container
    ->get('plugin.manager.editor')
    ->createInstance('ckeditor');
  $this->container
    ->get('plugin.manager.ckeditor.plugin')
    ->clearCachedDefinitions();
  $settings = $editor
    ->getSettings();
  $settings['toolbar']['rows'][0][0]['items'][] = 'Strike';
  $settings['toolbar']['rows'][0][0]['items'][] = 'Format';
  $editor
    ->setSettings($settings);
  $editor
    ->save();
  $expected_config['toolbar'][0]['items'][] = 'Strike';
  $expected_config['toolbar'][0]['items'][] = 'Format';
  $expected_config['format_tags'] = 'p;h2;h3;h4;h5;h6';
  $expected_config['extraPlugins'] .= ',llama_contextual,llama_contextual_and_button';
  $expected_config['drupalExternalPlugins']['llama_contextual'] = $this->fileUrlGenerator
    ->generateString('core/modules/ckeditor/tests/modules/js/llama_contextual.js');
  $expected_config['drupalExternalPlugins']['llama_contextual_and_button'] = $this->fileUrlGenerator
    ->generateString('core/modules/ckeditor/tests/modules/js/llama_contextual_and_button.js');
  $expected_config['contentsCss'][] = $this->fileUrlGenerator
    ->generateString('core/modules/ckeditor/tests/modules/ckeditor_test.css') . $query_string;
  $this
    ->assertEquals($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 = $editor
    ->getFilterFormat();
  $format
    ->filters('filter_html')->settings['allowed_html'] .= '<pre class> <h1> <blockquote class="*"> <address class="foo bar-* *">';
  $format
    ->save();
  $expected_config['allowedContent']['pre'] = [
    'attributes' => 'class',
    'styles' => FALSE,
    'classes' => TRUE,
  ];
  $expected_config['allowedContent']['h1'] = [
    'attributes' => FALSE,
    'styles' => FALSE,
    'classes' => FALSE,
  ];
  $expected_config['allowedContent']['blockquote'] = [
    'attributes' => 'class',
    'styles' => FALSE,
    'classes' => TRUE,
  ];
  $expected_config['allowedContent']['address'] = [
    'attributes' => 'class',
    'styles' => FALSE,
    'classes' => 'foo,bar-*',
  ];
  $expected_config['format_tags'] = 'p;h1;h2;h3;h4;h5;h6;pre';
  $this
    ->assertEquals($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', [
    'status' => 0,
  ]);
  $format
    ->save();
  $expected_config['allowedContent'] = TRUE;
  $expected_config['disallowedContent'] = FALSE;
  $expected_config['format_tags'] = 'p;h1;h2;h3;h4;h5;h6;pre';
  $this
    ->assertEquals($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', [
    'status' => 1,
    'settings' => [
      'restrictions' => [
        'allowed' => [
          'p' => TRUE,
          'a' => [
            'href' => TRUE,
            'rel' => [
              'nofollow' => TRUE,
            ],
            'class' => [
              'external' => TRUE,
            ],
            'target' => [
              '_blank' => FALSE,
            ],
          ],
          'span' => [
            'class' => [
              'dodo' => FALSE,
            ],
            'property' => [
              'dc:*' => TRUE,
            ],
            'rel' => [
              'foaf:*' => FALSE,
            ],
            'style' => [
              'underline' => FALSE,
              'color' => FALSE,
              'font-size' => TRUE,
            ],
          ],
          '*' => [
            'style' => FALSE,
            'on*' => FALSE,
            'class' => [
              'is-a-hipster-llama' => TRUE,
              'and-more' => TRUE,
            ],
            'data-*' => TRUE,
          ],
          'del' => FALSE,
        ],
      ],
    ],
  ]);
  $format
    ->save();
  $expected_config['allowedContent'] = [
    'p' => [
      'attributes' => TRUE,
      'styles' => FALSE,
      'classes' => 'is-a-hipster-llama,and-more',
    ],
    'a' => [
      'attributes' => 'href,rel,class,target',
      'styles' => FALSE,
      'classes' => 'external',
    ],
    'span' => [
      'attributes' => 'class,property,rel,style',
      'styles' => 'font-size',
      'classes' => FALSE,
    ],
    '*' => [
      'attributes' => 'class,data-*',
      'styles' => FALSE,
      'classes' => 'is-a-hipster-llama,and-more',
    ],
    'del' => [
      'attributes' => FALSE,
      'styles' => FALSE,
      'classes' => FALSE,
    ],
  ];
  $expected_config['disallowedContent'] = [
    'span' => [
      'styles' => 'underline,color',
      'classes' => 'dodo',
    ],
    '*' => [
      'attributes' => 'on*',
    ],
  ];
  $expected_config['format_tags'] = 'p';
  $this
    ->assertEquals($expected_config, $this->ckeditor
    ->getJSSettings($editor), 'Generated JS settings are correct for customized configuration.');
}