You are here

public function CKEditorTest::testStylesComboGetConfig 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::testStylesComboGetConfig()

Tests StylesCombo::getConfig().

File

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

Class

CKEditorTest
Tests for the 'CKEditor' text editor plugin.

Namespace

Drupal\Tests\ckeditor\Kernel

Code

public function testStylesComboGetConfig() {
  $editor = Editor::load('filtered_html');
  $stylescombo_plugin = $this->container
    ->get('plugin.manager.ckeditor.plugin')
    ->createInstance('stylescombo');

  // Styles dropdown/button enabled: new setting should be present.
  $settings = $editor
    ->getSettings();
  $settings['toolbar']['rows'][0][0]['items'][] = 'Styles';
  $settings['plugins']['stylescombo']['styles'] = '';
  $editor
    ->setSettings($settings);
  $editor
    ->save();
  $expected['stylesSet'] = [];
  $this
    ->assertSame($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.
  $settings['plugins']['stylescombo']['styles'] = "   \n";
  $editor
    ->setSettings($settings);
  $editor
    ->save();
  $this
    ->assertSame($expected, $stylescombo_plugin
    ->getConfig($editor));
  $settings['plugins']['stylescombo']['styles'] = "\r\n  \n  \r  \n ";
  $editor
    ->setSettings($settings);
  $editor
    ->save();
  $this
    ->assertSame($expected, $stylescombo_plugin
    ->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');

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

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

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

  // Configuration that includes a dash in either the element or class name.
  $settings['plugins']['stylescombo']['styles'] = "drupal-entity.has-dashes|Allowing Dashes";
  $editor
    ->setSettings($settings);
  $editor
    ->save();
  $expected['stylesSet'] = [
    [
      'name' => 'Allowing Dashes',
      'element' => 'drupal-entity',
      'attributes' => [
        'class' => 'has-dashes',
      ],
    ],
  ];
  $this
    ->assertSame($expected, $stylescombo_plugin
    ->getConfig($editor), '"StylesCombo" plugin configuration built correctly for customized toolbar.');
}