You are here

public function LinkValidationApiTest::testValidateExternalLinks in Link 7

Validate External Links.

File

tests/LinkValidationApiTest.test, line 181
Validate the link_validate_url() validation API.

Class

LinkValidationApiTest
Validate the link_validate_url() validation API.

Code

public function testValidateExternalLinks() {
  $links = array(
    'http://localhost:8080/',
    'www.example.com',
    'www.example.com/',
    'http://username:p%40ssw0rd!@www.example.com/',
    'http://@www.example.com/',
    'http://username:@www.example.com/',
    'http://username:password@www.example.com:8080/',
    'http://127.0.0.1:80/',
    'http://127.173.24.255:4723/',
    '127.173.24.255:4723/',
    'http://255.255.255.255:4823/',
    'www.test-site.com',
    'http://example.com/index.php?q=node/123',
    'http://example.com/?first_name=Joe Bob&last_name=Smith',
    // Anchors.
    'http://www.example.com/index.php#test',
    'http://www.example.com/index.php#this@that.',
    'http://www.example.com/index.php#',
    'http://www.cnn.com/video/#/video/politics/2008/12/09/intv.madeleine.albright.cnn',
    'http://www.archive.org/stream/aesopsfables00aesorich#page/n7/mode/2up',
    'http://www.example.com/blah/#this@that?',
  );

  // Test all of the protocols.
  $allowed_protocols = variable_get('filter_allowed_protocols', array(
    'http',
    'https',
    'ftp',
    'news',
    'nntp',
    'telnet',
    'mailto',
    'irc',
    'ssh',
    'sftp',
    'webcal',
  ));
  foreach ($allowed_protocols as $protocol) {
    if ($protocol !== 'news' && $protocol !== 'mailto') {
      $links[] = $protocol . '://www.example.com';
    }
  }
  foreach ($links as $link) {
    $type = link_url_type($link);
    $this
      ->assertEqual(LINK_EXTERNAL, $type, 'Testing that ' . $link . ' is an external link.');
    $valid = link_validate_url($link);
    $this
      ->assertTrue($valid, 'Test ' . $link . ' is valid external link.');

    // The following two lines are commented out and only used for
    // comparisons.
    // @code
    // $valid2 = valid_url($link, TRUE);
    // $this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");.
    // @endcode
  }
}