You are here

public function CKEditorAdminTest::testNewFormat in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/ckeditor/tests/src/Functional/CKEditorAdminTest.php \Drupal\Tests\ckeditor\Functional\CKEditorAdminTest::testNewFormat()
  2. 10 core/modules/ckeditor/tests/src/Functional/CKEditorAdminTest.php \Drupal\Tests\ckeditor\Functional\CKEditorAdminTest::testNewFormat()

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().

File

core/modules/ckeditor/tests/src/Functional/CKEditorAdminTest.php, line 239

Class

CKEditorAdminTest
Tests administration of CKEditor.

Namespace

Drupal\Tests\ckeditor\Functional

Code

public function testNewFormat() {
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/config/content/formats/add');

  // Verify the "Text Editor" <select> when a text editor is available.
  $select = $this
    ->assertSession()
    ->selectExists('editor[editor]');
  $this
    ->assertFalse($select
    ->hasAttribute('disabled'));
  $options = $select
    ->findAll('css', 'option');
  $this
    ->assertCount(2, $options);
  $this
    ->assertSame('None', $options[0]
    ->getText());
  $this
    ->assertSame('CKEditor', $options[1]
    ->getText());
  $this
    ->assertTrue($options[0]
    ->isSelected());

  // Name our fancy new text format, select the "CKEditor" editor and click
  // the "Configure" button.
  $edit = [
    'name' => 'My amazing text format',
    'format' => 'amazing_format',
    'editor[editor]' => 'ckeditor',
  ];
  $this
    ->submitForm($edit, 'editor_configure');
  $filter_format = FilterFormat::load('amazing_format');
  $this
    ->assertNull($filter_format, 'No FilterFormat config entity exists yet.');
  $editor = Editor::load('amazing_format');
  $this
    ->assertNull($editor, 'No Editor config entity exists yet.');

  // 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']);
  $this
    ->assertSession()
    ->fieldValueEquals('editor[settings][toolbar][button_groups]', $expected_buttons_value);

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

  // Ensure the styles textarea exists and is initialized empty.
  $this
    ->assertSession()
    ->fieldValueEquals('editor[settings][plugins][stylescombo][styles]', '');

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

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

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