You are here

public function CKEditorAdminTest::testNewFormat in Drupal 8

Same name and namespace in other branches
  1. 9 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 245

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
    ->xpath('//select[@name="editor[editor]"]');
  $select_is_disabled = $this
    ->xpath('//select[@name="editor[editor]" and @disabled="disabled"]');
  $options = $this
    ->xpath('//select[@name="editor[editor]"]/option');
  $this
    ->assertCount(1, $select, 'The Text Editor select exists.');
  $this
    ->assertCount(0, $select_is_disabled, 'The Text Editor select is not disabled.');
  $this
    ->assertCount(2, $options, 'The Text Editor select has two options.');
  $this
    ->assertSame('None', $options[0]
    ->getText(), 'Option 1 in the Text Editor select is "None".');
  $this
    ->assertSame('CKEditor', $options[1]
    ->getText(), 'Option 2 in the Text Editor select is "CKEditor".');
  $this
    ->assertSame('selected', $options[0]
    ->getAttribute('selected'), 'Option 1 ("None") is selected.');

  // 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
    ->drupalPostForm(NULL, $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
    ->assertFieldByName('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.
  $styles_textarea = $this
    ->xpath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]');
  $this
    ->assertFieldByXPath('//textarea[@name="editor[settings][plugins][stylescombo][styles]"]', '', 'The styles textarea exists and is empty.');
  $this
    ->assertCount(1, $styles_textarea, 'The "styles" textarea exists.');

  // Submit the form to create both a new text format and an associated text
  // editor.
  $this
    ->drupalPostForm(NULL, $edit, t('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
    ->assertEqual($this
    ->castSafeStrings($expected_settings), $this
    ->castSafeStrings($editor
    ->getSettings()), 'The Editor config entity has the correct settings.');
}