$url,
'title' => $title,
'attributes' => $attributes,
);
}
/**
* Takes a URL, sees if it can validate that the URL is valid.
*/
protected function linkTestValidateUrl($url) {
$field_name = $this->createLinkField();
$label = $this->randomName();
$settings = array(
'title' => $label,
$field_name => array(
LANGUAGE_NONE => array(
array(
'title' => $label,
'url' => $url,
),
),
),
);
$node = $this->drupalCreateNode($settings);
$this->assertNotNull($node, ' has been created.', 'Node created');
$this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']);
}
}
/**
* Class for Validate Test.
*/
class LinkValidateTest extends LinkValidateTestCase {
/**
* Get Info.
*/
public static function getInfo() {
return array(
'name' => 'Link Validation Tests',
'description' => 'Tests the field validation.',
'group' => 'Link',
);
}
/**
* Validate basic URL.
*/
public function testLinkValidateBasicUrl() {
$this->linkTestValidateUrl('http://www.example.com');
}
/**
* Test if we're stopped from posting a bad url on default validation.
*/
public function testLinkValidateBadUrlValidateDefault() {
$this->web_user = $this->drupalCreateUser(array(
'administer content types',
'administer fields',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages',
));
$this->drupalLogin($this->web_user);
// Create field.
$name = strtolower($this->randomName());
$edit = array(
'fields[_add_new_field][label]' => $name,
'fields[_add_new_field][field_name]' => $name,
'fields[_add_new_field][type]' => 'link_field',
'fields[_add_new_field][widget_type]' => 'link_field',
);
$this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
$this->drupalPost(NULL, array(), t('Save field settings'));
$this->drupalPost(NULL, array(), t('Save settings'));
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
// Create page form.
$this->drupalGet('node/add/page');
$field_name = 'field_' . $name;
$this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
$this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
$edit = array(
'title' => 'Simple Title',
$field_name . '[und][0][url]' => 'edik:naw',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The value @value provided for @field is not a valid URL.', array(
'@value' => 'edik:naw',
'@field' => $name,
)));
}
/**
* Test if we're stopped from posting a bad url with validation on.
*/
public function testLinkValidateBadUrlValidateOn() {
$this->web_user = $this->drupalCreateUser(array(
'administer content types',
'administer fields',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages',
));
$this->drupalLogin($this->web_user);
// Create field.
$name = strtolower($this->randomName());
$edit = array(
'fields[_add_new_field][label]' => $name,
'fields[_add_new_field][field_name]' => $name,
'fields[_add_new_field][type]' => 'link_field',
'fields[_add_new_field][widget_type]' => 'link_field',
);
$this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
$this->drupalPost(NULL, array(), t('Save field settings'));
$this->drupalPost(NULL, array('instance[settings][validate_url]' => TRUE), t('Save settings'));
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
// Create page form.
$this->drupalGet('node/add/page');
$field_name = 'field_' . $name;
$this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
$this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
$edit = array(
'title' => 'Simple Title',
$field_name . '[und][0][url]' => 'edik:naw',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertText(t('The value @value provided for @field is not a valid URL.', array(
'@field' => $name,
'@value' => 'edik:naw',
)));
}
/**
* Test if we can post a bad url if the validation is expressly turned off.
*/
public function testLinkValidateBadUrlValidateOff() {
$this->web_user = $this->drupalCreateUser(array(
'administer content types',
'administer fields',
'administer nodes',
'administer filters',
'access content',
'create page content',
'access administration pages',
));
$this->drupalLogin($this->web_user);
// Create field.
$name = strtolower($this->randomName());
$edit = array(
'fields[_add_new_field][label]' => $name,
'fields[_add_new_field][field_name]' => $name,
'fields[_add_new_field][type]' => 'link_field',
'fields[_add_new_field][widget_type]' => 'link_field',
);
$this->drupalPost('admin/structure/types/manage/page/fields', $edit, t('Save'));
$this->drupalPost(NULL, array(), t('Save field settings'));
$this->drupalPost(NULL, array('instance[settings][validate_url]' => FALSE), t('Save settings'));
// @codingStandardsIgnoreLine
/*$instance_details = db_query("SELECT * FROM {field_config_instance} WHERE field_name = :field_name AND bundle = 'page'", array(':field_name' => 'field_'. $name))->fetchObject();
$this->fail('
'. print_r($instance_details, TRUE) .'
');
$this->fail(''. print_r(unserialize($instance_details->data), TRUE) .'
');*/
// Is field created?
$this->assertRaw(t('Saved %label configuration', array('%label' => $name)), 'Field added');
node_types_rebuild();
menu_rebuild();
// Create page form.
$this->drupalGet('node/add/page');
$field_name = 'field_' . $name;
$this->assertField('edit-field-' . $name . '-und-0-title', 'Title found');
$this->assertField('edit-field-' . $name . '-und-0-url', 'URL found');
$edit = array(
'title' => 'Simple Title',
$field_name . '[und][0][url]' => 'edik:naw',
);
$this->drupalPost(NULL, $edit, t('Save'));
$this->assertNoText(t('The value %value provided for %field is not a valid URL.', array(
'%field' => $name,
'%value' => 'edik:naw',
)));
}
/**
* Validate switching between validation status.
*
* Test if a bad url can sneak through un-filtered if we play with the
* validation...
*/
public function xTestLinkValidateSwitchingBetweenValidationStatus() {
$this->acquireContentTypes(1);
$this->web_user = $this->drupalCreateUser(array(
'administer content types',
'administer fields',
'administer nodes',
'access administration pages',
'access content',
'create ' . $this->content_types[0]->type . ' content',
'edit any ' . $this->content_types[0]->type . ' content',
));
$this->drupalLogin($this->web_user);
variable_set('node_options_' . $this->content_types[0]->name, array(
'status',
'promote',
));
$field_settings = array(
'type' => 'link',
'widget_type' => 'link',
'type_name' => $this->content_types[0]->name,
// <-- This is needed or we have an error.
'attributes' => array(),
'validate_url' => 0,
);
$field = $this->createField($field_settings, 0);
$this->acquireNodes(2);
$this->drupalGet('node/' . $this->nodes[0]->nid);
$edit = array();
$title = $this->randomName();
$url = 'javascript:alert("http://example.com/' . $this->randomName() . '")';
$edit[$field['field_name'] . '[0][url]'] = $url;
$edit[$field['field_name'] . '[0][title]'] = $title;
$this->drupalPost('node/' . $this->nodes[0]->nid . '/edit', $edit, t('Save'));
// $this->pass($this->content);.
// @codingStandardsIgnoreLine
$this->assertNoText(t('The value %value provided for %field is not a valid URL.', array(
// @codingStandardsIgnoreLine
'%field' => $name,
'%value' => trim($url),
)));
// Make sure we get a new version!
$node = node_load($this->nodes[0]->nid, NULL, TRUE);
$this->assertEqual($url, $node->{$field['field_name']}[0]['url']);
$this->drupalGet('node/' . $node->nid);
$this->assertNoRaw($url, 'Make sure Javascript does not display.');
// Turn the array validation back _on_.
$edit = array('validate_url' => TRUE);
$node_type_link = str_replace('_', '-', $node->type);
// @codingStandardsIgnoreLine
// $this->drupalGet('admin/content/node-type/'. $node_type_link .'/fields'); ///'. $field['field_name']);
// $this->fail($this->content);.
$this->drupalPost('admin/content/node-type/' . $node_type_link . '/fields/' . $field['field_name'], $edit, t('Save field settings'));
$this->drupalGet('node/' . $node->nid);
// This actually works because the display_url goes through the core
// url() function. But we should have a test that makes sure it continues
// to work.
$this->assertNoRaw($url, 'Make sure Javascript does not display.');
// $this->fail($this->content);.
}
/**
* Validate that '' is a valid url.
*/
public function testLinkFrontUrl() {
$this->linkTestValidateUrl('');
}
/**
* Validate that an internal url would be accepted.
*/
public function testLinkInternalUrl() {
// Create the content first.
$node = $this->drupalCreateNode();
$link = 'node/' . $node->nid;
$this->linkTestValidateUrl($link);
$type = link_url_type($link);
$this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
}
/**
* Validate a simple mailto.
*/
public function testLinkMailto() {
$this->linkTestValidateUrl('mailto:jcfiala@gmail.com');
}
/**
* Check link external https.
*/
public function testLinkExternalHttps() {
$this->linkTestValidateUrl('https://www.example.com/');
}
/**
* Check link FTP.
*/
public function testLinkFtp() {
$this->linkTestValidateUrl('ftp://www.example.com/');
}
}
/**
* Validate Test News.
*/
class LinkValidateTestNews extends LinkValidateTestCase {
/**
* Get Info.
*/
public static function getInfo() {
return array(
'name' => 'Link News Validation Tests',
'description' => 'Tests the field validation for usenet urls.',
'group' => 'Link',
);
}
/**
* Validate a news link to a message group.
*/
public function testLinkNews() {
$this->linkTestValidateUrl('news:comp.infosystems.www.misc');
}
/**
* Validate a news link to a message id. Said ID copied off of google groups.
*/
public function testLinkNewsMessage() {
$this->linkTestValidateUrl('news:hj0db8$vrm$1@news.eternal-september.org');
}
}
/**
* Validate Specific URL.
*/
class LinkValidateSpecificURL extends LinkValidateTestCase {
/**
* Get Info.
*/
public static function getInfo() {
return array(
'name' => 'Link Specific URL Validation Tests',
'description' => 'Tests field validation with unusual urls',
'group' => 'Link',
);
}
/**
* Lets throw in a lot of umlouts for testing!
*/
public function testUmloutUrl() {
$this->linkTestValidateUrl('http://üÜü.exämple.com/nöde');
}
/**
* Check umlout mailto.
*/
public function testUmloutMailto() {
$this->linkTestValidateUrl('mailto:Üser@exÅmple.com');
}
/**
* Check German b in URL, aka Eszett.
*/
public function testGermanEszettUrl() {
$this->linkTestValidateUrl('http://www.test.com/ßstuff');
}
/**
* Check Spanish ñ in URL.
*/
public function testSpecialEneUrl() {
$this->linkTestValidateUrl('http://www.testÑñ.com/');
}
/**
* Curly brackets in query.
*/
public function testCurlyBracketsInQuery() {
$this->linkTestValidateUrl('http://www.healthyteennetwork.org/index.asp?Type=B_PR&SEC={2AE1D600-4FC6-4B4D-8822-F1D5F072ED7B}&DE={235FD1E7-208D-4363-9854-4E6775EB8A4C}');
}
/**
* Here, we're testing that a very long url is stored properly in the db.
*
* Basically, trying to test http://drupal.org/node/376818
*/
public function testLinkUrlFieldIsBig() {
$long_url = 'http://th.wikipedia.org/wiki/%E0%B9%82%E0%B8%A3%E0%B8%87%E0%B9%80%E0%B8%A3%E0%B8%B5%E0%B8%A2%E0%B8%99%E0%B9%80%E0%B8%9A%E0%B8%8D%E0%B8%88%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A%E0%B8%B9%E0%B8%97%E0%B8%B4%E0%B8%A8_%E0%B8%99%E0%B8%84%E0%B8%A3%E0%B8%A8%E0%B8%A3%E0%B8%B5%E0%B8%98%E0%B8%A3%E0%B8%A3%E0%B8%A1%E0%B8%A3%E0%B8%B2%E0%B8%8A';
$this->linkTestValidateUrl($long_url);
}
}
/**
* Validate Url Light.
*
* 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 !
*/
class LinkValidateUrlLight extends DrupalWebTestCase {
/**
* Get Info.
*/
public static function getInfo() {
return array(
'name' => 'Link Light Validation Tests',
'description' => 'Tests the link_validate_url() function by itself, without invoking the full drupal/cck lifecycle.',
'group' => 'Link',
);
}
/**
* {@inheritdoc}
*/
public function setUp(array $modules = array()) {
$modules[] = 'link';
parent::setUp($modules);
}
/**
* Name Link Type.
*
* Translates the LINK type constants to english for display and debugging of
* tests.
*
* @codingStandardsIgnoreStart
*/
public function name_Link_Type($type) {
// @codingStandardsIgnoreEnd
switch ($type) {
case LINK_FRONT:
return "Front";
case LINK_EMAIL:
return "Email";
case LINK_TEL:
return "Telephone";
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 labeled works.
*/
public function testValidateFrontLink() {
$valid = link_validate_url('');
$this->assertEqual(LINK_FRONT, $valid, 'Make sure that front link is verified and identified');
}
/**
* Validate Email Link.
*/
public function testValidateEmailLink() {
$valid = link_validate_url('mailto:bob@example.com');
$this->assertEqual(LINK_EMAIL, $valid, "Make sure a basic mailto is verified and identified");
}
/**
* Validate Email Link Bad.
*/
public function testValidateEmailLinkBad() {
$valid = link_validate_url(':bob@example.com');
$this->assertEqual(FALSE, $valid, 'Make sure just a bad address is correctly failed');
}
/**
* Confirm that valid tel: links work as expected.
*/
public function testValidateTelLinks() {
$links = array(
'tel:01',
'tel:123456789012345',
'tel:+123456789012345',
);
foreach ($links as $link) {
$type = link_url_type($link);
$this->assertEqual(LINK_TEL, $type, 'Test ' . $link . ' is a tel link.');
$valid = link_validate_url($link);
$this->assertTrue($valid, 'Test ' . $link . ' is valid tel link.');
}
}
/**
* Confirm that invalid tel: links work as expected.
*/
public function testValidateTelLinksBad() {
$links = array(
'tel:0',
'tel:1234567890123456',
'tel:+1',
'tel:+0123456789',
'tel:+1234567890123456',
':12345678',
);
foreach ($links as $link) {
$type = link_url_type($link);
$this->assertFalse($type, 'Test ' . $link . ' is not a tel link.');
$valid = link_validate_url($link);
$this->assertFalse($valid, 'Test ' . $link . ' is not a valid tel link.');
}
}
/**
* Validate Newsgroup Link.
*/
public function testValidateNewsgroupLink() {
$valid = link_validate_url('news:comp.infosystems.www.misc');
$this->assertEqual(LINK_NEWS, $valid, 'Make sure link to newsgroup validates as news.');
}
/**
* Validate News Article Link.
*/
public 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 validates as news.');
}
/**
* Validate Bad Newsgroup Link.
*/
public 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.');
}
/**
* Validate Internal Links.
*/
public function testValidateInternalLinks() {
$tempfile = drupal_tempnam('public://files', 'test');
$links = array(
'rss.xml',
'foo#bar',
file_uri_target($tempfile),
drupal_realpath($tempfile),
);
foreach ($links as $link) {
$type = link_url_type($link);
$this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.');
$valid = link_validate_url($link);
$this->assertTrue($valid, 'Test ' . $link . ' is valid internal link.');
}
}
/**
* Validate External Links.
*/
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.
// $valid2 = valid_url($link, TRUE);
// $this->assertEqual(TRUE, $valid2, "Using valid_url() on $link.");.
}
}
/**
* Check Invalid External Links.
*/
public function testInvalidExternalLinks() {
$links = array(
'http://www.ex ample.com/',
// Bad ip!
'http://25.0.0/',
'http://4827.0.0.2/',
// ß not allowed in domain names!
'http://www.testß.com/',
// Bad TLD.
'http://.www.foo.bar./',
// Domains can't have sections starting with a dash.
// 'http://www.-fudge.com/',
'http://example.com/index.php?page=this\that',
'example@example.com',
);
foreach ($links as $link) {
$valid = link_validate_url($link);
$this->assertEqual(FALSE, $valid, 'Testing that ' . $link . ' is not a valid link.');
}
}
}