Index: modules/simpletest/tests/common.test =================================================================== RCS file: /cvs/drupal/drupal/modules/simpletest/tests/common.test,v retrieving revision 1.4 diff -u -p -r1.4 common.test --- modules/simpletest/tests/common.test 29 Aug 2008 14:45:19 -0000 1.4 +++ modules/simpletest/tests/common.test 29 Aug 2008 21:17:51 -0000 @@ -144,3 +144,74 @@ class DrupalHTTPRequestTestCase extends $this->assertTitle(variable_get('site_name', 'Drupal'), t('Site title matches.')); } } + +/** + * Test for valid_url(). + */ +class ValidUrlTestCase extends DrupalWebTestCase { + /** + * Implementation of getInfo(). + */ + function getInfo() { + return array( + 'name' => t('Valid Url'), + 'description' => t("Performs tests on Drupal's valid url function."), + 'group' => t('System') + ); + } + + function testValidUrl() { + // Array of urls that we would like to test. + $example_urls = array( + // Valid urls. + array('url' => 'example.com', 'pass' => 1), + array('url' => 'www.example.com', 'pass' => 1), + array('url' => 'ex-ample.com', 'pass' => 1), + array('url' => '3xampl3.com', 'pass' => 1), + array('url' => 'example.com/paren(the)sis', 'pass' => 1), + array('url' => 'example.com/index.html#pagetop', 'pass' => 1), + array('url' => 'example.com:8080', 'pass' => 1), + array('url' => 'sudomain.example.com', 'pass' => 1), + array('url' => 'example.com/index.php?q=node', 'pass' => 1), + array('url' => 'example.com/index.php?q=node¶m=false', 'pass' => 1), + array('url' => 'user@www.example.com', 'pass' => 1), + array('url' => 'user:pass@www.example.com:8080/login.php?do=login&style=%23#pagetop', 'pass' => 1), + + // Invalid urls. + array('url' => '', 'pass' => 0), + array('url' => 'ex_ample.com', 'pass' => 0), + array('url' => 'example', 'pass' => 0), + ); + + // test each of the urls + foreach ($example_urls as $example_url) { + $this->assertValidUrl($example_url); + } + } + + /** + * Test to see if the url is valid/invalid based on if it is expected to + * pass or fail with multiple protocols. + */ + function assertValidUrl($url) { + $protocols = variable_get('filter_allowed_protocols', array('ftp', 'http', 'https', 'irc', 'mailto', 'news', 'nntp', 'rtsp', 'sftp', 'ssh', 'telnet', 'webcal')); + $protocols[] = ''; + foreach ($protocols as $protocol) { + if ($protocol != '') { + $test_url = $protocol .'://'. $url['url']; + $valid_url = valid_url($test_url, TRUE); + } + else { + $test_url = $protocol . $url['url']; + $valid_url = valid_url($test_url); + } + + if ($url['pass'] == 1) { + $this->assertTrue($valid_url, $test_url .t(' is a valid url.')); + } + else { + $this->assertFalse($valid_url, $test_url .t(' is NOT a valid url.')); + } + } + } +} \ No newline at end of file