1 filter_dialog.test public FilterEditorLinkValidateTestCase::testEditorLinkValidate()

Check function _filter_format_editor_link_url_validate().

File

core/modules/filter/tests/filter_dialog.test, line 65
Functional tests for the Filter module.

Class

FilterEditorLinkValidateTestCase
Test for filter dialog link validation.

Code

public function testEditorLinkValidate() {
  // Create a node and get the nid for the calling path.
  $node = $this->backdropCreateNode();
  $calling_path = "node/{$node->nid}/edit";
  $link_token = filter_editor_dialog_token($this->format, 'link', $this->editorUser, $calling_path);
  $german = language_load('de');
  $options = array(
    'query' => array(
      'token' => $link_token,
      'calling_path' => $calling_path,
    ),
    'language' => $german,
  );
  $this->backdropGet('editor/dialog/link/' . $this->format->format, $options);

  $edit = array(
    'attributes[text]' => 'The link',
  );
  $invalid_urls = array(
    'javascript://boo',
    'http://foo>bar.baz',
    'http://foobar\.baz',
  );
  foreach ($invalid_urls as $url) {
    $edit['attributes[href]'] = $url;
    $this->backdropPost(NULL, $edit, t('Save'));
    $this->assertRaw('The URL <em class="placeholder">' . check_plain($url) . '</em> is not valid.', format_string('Invalid URL %url did not pass validation', array(
      '%url' => $url,
    )));
  }

  $valid_urls = array(
    // Should get altered.
    'node/1' => '/de/node/1',
    // cspell:disable-next-line
    'ärger-im-büro' => '/de/%C3%A4rger-im-b%C3%BCro',
    '<front>?page=1#main-content' => '/de?page=1#main-content',
    // Should stay untouched.
    '/en/node/1' => '/en/node/1',
    '#main-content' => '#main-content',
    'http://domain.tld:8080/index.php?page=3&foo=bar' => 'http://domain.tld:8080/index.php?page=3&foo=bar',
    'http://user:pass@domain.tld/path' => 'http://user:pass@domain.tld/path',
    '/de/%C3%A4rger-im-b%C3%BCro' => '/de/%C3%A4rger-im-b%C3%BCro',
    '//domain.tld' => '//domain.tld',
    'mailto:boo@example.com' => 'mailto:boo@example.com',
    'telnet://host.tld' => 'telnet://host.tld',
  );
  foreach ($valid_urls as $url => $altered_or_not) {
    $edit['attributes[href]'] = $url;
    $this->backdropPost(NULL, $edit, t('Save'));
    // No form error.
    $this->assertNoRaw('The URL <em class="placeholder">' . check_plain($url) . '</em> is not valid.', format_string('Valid URL %url passes validation', array(
      '%url' => $url,
    )));
    // The filter_formtest helper module sets the value from $form_state as
    // message. It would be impossible to validate, what CKEditor inserts via
    // Javascript.
    $this->assertRaw('Output href is: ' . check_plain($altered_or_not), format_string('Expected output value @value found', array(
      '@value' => $altered_or_not,
    )));
  }
}