You are here

public function CkeditorConfigTest::testFormSubmission in CKEditor custom config 8.2

Verify settings form validation, submission, and storage.

File

tests/src/FunctionalJavascript/CkeditorConfigTest.php, line 87

Class

CkeditorConfigTest
Verify settings form validation, submission, and storage.

Namespace

Drupal\Tests\ckeditor_config\FunctionalJavascript

Code

public function testFormSubmission() {
  $assert_session = $this
    ->assertSession();
  $page = $this
    ->getSession()
    ->getPage();

  // Navigate to configuration page for testing text format.
  $this
    ->drupalGet("admin/config/content/formats/manage/testing_text_format");
  $page
    ->fillField('edit-editor-editor', 'ckeditor');
  $assert_session
    ->assertWaitOnAjaxRequest();

  // Enter a value that will fail validation.
  $this
    ->clickLink('CKEditor custom configuration');
  $test_value = 'forcePasteAsPlainText = true' . PHP_EOL . 'pasteFromWordPromptCleanup false';
  $page
    ->fillField('editor[settings][plugins][customconfig][ckeditor_custom_config]', $test_value);
  $page
    ->pressButton('Save configuration');
  $assert_session
    ->waitForElement('xpath', '//div[@role="alert"]');
  $assert_session
    ->elementTextContains('xpath', '//div[@role="alert"]', 'The configuration syntax on line 2 is incorrect.');

  // Enter a value that will pass validation.
  $this
    ->clickLink('CKEditor custom configuration');
  $test_value = 'forcePasteAsPlainText = true' . PHP_EOL . 'pasteFromWordPromptCleanup = false' . PHP_EOL . 'removePlugins = font' . PHP_EOL . 'tabIndex = 3';
  $page
    ->fillField('editor[settings][plugins][customconfig][ckeditor_custom_config]', $test_value);
  $page
    ->pressButton('Save configuration');
  $assert_session
    ->elementTextContains('xpath', '//div[@aria-label="Status message"]', 'The text format Testing Text Format has been updated.');

  // Verify submitted value is same as value stored in config.
  $editor = editor_load('testing_text_format');
  $settings = $editor
    ->getSettings();
  $stored_value = $settings['plugins']['customconfig']['ckeditor_custom_config'];

  // Normalize line endings in config value.
  $stored_value = str_replace([
    "\r\n",
    "\n",
    "\r",
  ], PHP_EOL, $stored_value);
  $this
    ->assertIdentical($test_value, $stored_value);

  // Verify submitted values are rendered in drupal-settings-json.
  $this
    ->drupalGet('node/add/page');
  $this
    ->waitForEditor();
  $settings = $this
    ->getDrupalSettings();
  $settings = $settings['editor']['formats']['testing_text_format']['editorSettings'];
  $this
    ->assertIdentical($settings['forcePasteAsPlainText'], TRUE);

  // This value overrides the default value set by the CKEditor's
  // Internal plugin.
  $this
    ->assertIdentical($settings['pasteFromWordPromptCleanup'], FALSE);
  $this
    ->assertIdentical($settings['removePlugins'], 'font');
  $this
    ->assertIdentical($settings['tabIndex'], 3);
}