You are here

function CKEditorTest::testGetJSSettings in Zircon Profile 8.0

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

Tests CKEditor::getJSSettings().

File

core/modules/ckeditor/src/Tests/CKEditorTest.php, line 76
Contains \Drupal\ckeditor\Tests\CKEditorTest.

Class

CKEditorTest
Tests for the 'CKEditor' text editor plugin.

Namespace

Drupal\ckeditor\Tests

Code

function testGetJSSettings() {
  $editor = entity_load('editor', 'filtered_html');

  // Default toolbar.
  $expected_config = $this
    ->getDefaultInternalConfig() + array(
    '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' => array(
      'drupalimage' => file_create_url('core/modules/ckeditor/js/plugins/drupalimage/plugin.js'),
      'drupallink' => file_create_url('core/modules/ckeditor/js/plugins/drupallink/plugin.js'),
    ),
  );
  $expected_config = $this
    ->castSafeStrings($expected_config);
  ksort($expected_config);
  ksort($expected_config['allowedContent']);
  $this
    ->assertIdentical($expected_config, $this
    ->castSafeStrings($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',
  ));
  $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'] = 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');
  ksort($expected_config);
  $this
    ->assertIdentical($expected_config, $this
    ->castSafeStrings($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'] = array(
    'attributes' => 'class',
    'styles' => FALSE,
    'classes' => TRUE,
  );
  $expected_config['allowedContent']['h1'] = array(
    'attributes' => FALSE,
    'styles' => FALSE,
    'classes' => FALSE,
  );
  $expected_config['allowedContent']['blockquote'] = array(
    'attributes' => 'class',
    'styles' => FALSE,
    'classes' => TRUE,
  );
  $expected_config['allowedContent']['address'] = array(
    'attributes' => 'class',
    'styles' => FALSE,
    'classes' => 'foo,bar-*',
  );
  $expected_config['format_tags'] = 'p;h1;h2;h3;h4;h5;h6;pre';
  ksort($expected_config['allowedContent']);
  $this
    ->assertIdentical($expected_config, $this
    ->castSafeStrings($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['disallowedContent'] = FALSE;
  $expected_config['format_tags'] = 'p;h1;h2;h3;h4;h5;h6;pre';
  $this
    ->assertIdentical($expected_config, $this
    ->castSafeStrings($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,
            ),
            'style' => array(
              'underline' => FALSE,
              'color' => FALSE,
              'font-size' => TRUE,
            ),
          ),
          '*' => array(
            'style' => FALSE,
            'on*' => 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',
      'styles' => FALSE,
      'classes' => 'external',
    ),
    'span' => array(
      'attributes' => 'class,property,rel,style',
      'styles' => 'font-size',
      'classes' => FALSE,
    ),
    '*' => array(
      'attributes' => 'class,data-*',
      'styles' => FALSE,
      'classes' => 'is-a-hipster-llama,and-more',
    ),
    'del' => array(
      'attributes' => FALSE,
      'styles' => FALSE,
      'classes' => FALSE,
    ),
  );
  $expected_config['disallowedContent'] = array(
    'span' => array(
      'styles' => 'underline,color',
      'classes' => 'dodo',
    ),
    '*' => array(
      'attributes' => 'on*',
    ),
  );
  $expected_config['format_tags'] = 'p';
  ksort($expected_config);
  ksort($expected_config['allowedContent']);
  ksort($expected_config['disallowedContent']);
  $this
    ->assertIdentical($expected_config, $this
    ->castSafeStrings($this->ckeditor
    ->getJSSettings($editor)), 'Generated JS settings are correct for customized configuration.');
}