You are here

class LinkValidateUrlLight in Link 6.2

A series of tests of links, only going against the link_validate_url function in link.module.

Validation is guided by the rules in http://tools.ietf.org/html/rfc1738 !

Hierarchy

Expanded class hierarchy of LinkValidateUrlLight

File

tests/link.validate.test, line 366
Tests that exercise the validation functions in the link module.

View source
class LinkValidateUrlLight extends DrupalUnitTestCase {
  function setUp() {

    // do we need to include something here?
    module_load_include('module', 'link');
    module_load_include('inc', 'link');
  }
  function getInfo() {
    return array(
      'name' => t('Link Light Validation Tests'),
      'description' => t('Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.'),
      'group' => t('Link'),
    );
  }

  /**
   * Translates the LINK type constants to english for display and debugging of tests
   */
  function name_Link_Type($type) {
    switch ($type) {
      case LINK_FRONT:
        return "Front";
      case LINK_EMAIL:
        return "Email";
      case LINK_NEWS:
        return "Newsgroup";
      case LINK_INTERNAL:
        return "Internal Link";
      case LINK_EXTERNAL:
        return "External Link";
      case FALSE:
        return "Invalid Link";
      default:
        return "Bad Value:" . $type;
    }
  }

  // Make sure that a link labelled <front> works.
  function testValidateFrontLink() {
    $valid = link_validate_url('<front>');
    $this
      ->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verfied and identified');
  }
  function testValidateEmailLink() {
    $valid = link_validate_url('mailto:bob@example.com');
    $this
      ->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
  }
  function testValidateEmailLinkBad() {
    $valid = link_validate_url(':bob@example.com');
    $this
      ->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
  }
  function testValidateNewsgroupLink() {
    $valid = link_validate_url('news:comp.infosystems.www.misc');
    $this
      ->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
  }
  function testValidateNewsArticleLink() {
    $valid = link_validate_url('news:hj0db8$vrm$1@news.eternal-september.org');
    $this
      ->assertEqual(LINK_NEWS, $valid, 'Make sure link to specific article valiates as news.');
  }
  function testValidateBadNewsgroupLink() {
    $valid = link_validate_url('news:comp.bad_name.misc');
    $this
      ->assertEqual(FALSE, $valid, 'newsgroup names can\'t contain underscores, so it should come back as invalid.');
  }
  function testValidateInternalLink() {
    $valid = link_validate_url('node/5');
    $this
      ->assertEqual(LINK_INTERNAL, $valid, 'Test normal internal link.');
  }
  function testValidateInternalLinkWithDot() {
    $valid = link_validate_url('rss.xml');
    $this
      ->assertEqual(LINK_INTERNAL, $valid, 'Test rss.xml internal link.');
  }
  function testValidateInternalLinkToFile() {
    $valid = link_validate_url('files/test.jpg');
    $this
      ->assertEqual(LINK_INTERNAL, $valid, 'Test files/test.jpg internal link.');
  }
  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/index.php?page=this\\that',
      '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) {
      $valid = link_validate_url($link);
      $this
        ->assertEqual(LINK_EXTERNAL, $valid, 'Testing that ' . $link . ' is a valid external link.');
    }
  }
  function testInvalidExternalLinks() {
    $links = array(
      'http://www.ex ample.com/',
      '//www.example.com/',
      'http://25.0.0/',
      // bad ip!
      'http://4827.0.0.2/',
      'http://www.testß.com/',
    );
    foreach ($links as $link) {
      $valid = link_validate_url($link);
      $this
        ->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTestCase::$assertions protected property Assertions thrown in that test case.
DrupalTestCase::$databasePrefix protected property The database prefix of this test run.
DrupalTestCase::$originalFileDirectory protected property The original file directory, before it was changed for testing purposes.
DrupalTestCase::$originalPrefix protected property The original database prefix, before it was changed for testing purposes.
DrupalTestCase::$results public property Current results of this test case.
DrupalTestCase::$skipClasses protected property This class is skipped when looking for the source of an assertion.
DrupalTestCase::$testId protected property The test run ID.
DrupalTestCase::$timeLimit protected property Time limit for the test.
DrupalTestCase::assert protected function Internal helper: stores the assert.
DrupalTestCase::assertEqual protected function Check to see if two values are equal.
DrupalTestCase::assertFalse protected function Check to see if a value is false (an empty string, 0, NULL, or FALSE).
DrupalTestCase::assertIdentical protected function Check to see if two values are identical.
DrupalTestCase::assertNotEqual protected function Check to see if two values are not equal.
DrupalTestCase::assertNotIdentical protected function Check to see if two values are not identical.
DrupalTestCase::assertNotNull protected function Check to see if a value is not NULL.
DrupalTestCase::assertNull protected function Check to see if a value is NULL.
DrupalTestCase::assertTrue protected function Check to see if a value is not false (not an empty string, 0, NULL, or FALSE).
DrupalTestCase::deleteAssert public static function Delete an assertion record by message ID.
DrupalTestCase::error protected function Fire an error assertion.
DrupalTestCase::errorHandler public function Handle errors during test runs.
DrupalTestCase::exceptionHandler protected function Handle exceptions.
DrupalTestCase::fail protected function Fire an assertion that is always negative.
DrupalTestCase::generatePermutations public static function Converts a list of possible parameters into a stack of permutations.
DrupalTestCase::getAssertionCall protected function Cycles through backtrace until the first non-assertion method is found.
DrupalTestCase::insertAssert public static function Store an assertion from outside the testing context.
DrupalTestCase::pass protected function Fire an assertion that is always positive.
DrupalTestCase::randomName public static function Generates a random string containing letters and numbers.
DrupalTestCase::randomString public static function Generates a random string of ASCII characters of codes 32 to 126.
DrupalTestCase::run public function Run all tests in this class.
DrupalTestCase::verbose protected function Logs verbose message in a text file.
DrupalUnitTestCase::tearDown protected function
DrupalUnitTestCase::__construct function Constructor for DrupalUnitTestCase. Overrides DrupalTestCase::__construct
LinkValidateUrlLight::getInfo function
LinkValidateUrlLight::name_Link_Type function Translates the LINK type constants to english for display and debugging of tests
LinkValidateUrlLight::setUp function Sets up unit test environment. Overrides DrupalUnitTestCase::setUp
LinkValidateUrlLight::testInvalidExternalLinks function
LinkValidateUrlLight::testValidateBadNewsgroupLink function
LinkValidateUrlLight::testValidateEmailLink function
LinkValidateUrlLight::testValidateEmailLinkBad function
LinkValidateUrlLight::testValidateExternalLinks function
LinkValidateUrlLight::testValidateFrontLink function
LinkValidateUrlLight::testValidateInternalLink function
LinkValidateUrlLight::testValidateInternalLinkToFile function
LinkValidateUrlLight::testValidateInternalLinkWithDot function
LinkValidateUrlLight::testValidateNewsArticleLink function
LinkValidateUrlLight::testValidateNewsgroupLink function