diff --git a/link.module b/link.module index c7f5281..49fdd41 100644 --- a/link.module +++ b/link.module @@ -334,7 +334,7 @@ function link_field_validate($entity_type, $entity, $field, $instance, $langcode */ function link_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $value) { - _link_process($items[$delta], $delta, $field, $entity, $instance); + _link_process($items[$delta], $delta, $field, $entity); } } @@ -343,7 +343,7 @@ function link_field_insert($entity_type, $entity, $field, $instance, $langcode, */ function link_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) { foreach ($items as $delta => $value) { - _link_process($items[$delta], $delta, $field, $entity, $instance); + _link_process($items[$delta], $delta, $field, $entity); } } @@ -434,12 +434,10 @@ function _link_load($field, $item, $instance) { * The field structure array. * @param object $entity * Entity object. - * @param array $instance - * The instance structure for $field on $entity's bundle. * * @codingStandardsIgnoreStart */ -function _link_process(&$item, $delta, $field, $entity, $instance) { +function _link_process(&$item, $delta, $field, $entity) { // @codingStandardsIgnoreEnd // Trim whitespace from URL. if (!empty($item['url'])) { @@ -459,8 +457,7 @@ function _link_process(&$item, $delta, $field, $entity, $instance) { // Don't save an invalid default value (e.g. 'http://'). if ((isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url']) && is_object($entity)) { - $langcode = !empty($entity) ? field_language($instance['entity_type'], $entity, $instance['field_name']) : LANGUAGE_NONE; - if (!link_validate_url($item['url'], $langcode)) { + if (!link_validate_url($item['url'])) { unset($item['url']); } } @@ -472,7 +469,7 @@ function _link_process(&$item, $delta, $field, $entity, $instance) { function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &$optional_field_found, &$errors) { if ($item['url'] && !(isset($instance['default_value'][$delta]['url']) && $item['url'] === $instance['default_value'][$delta]['url'] && !$instance['required'])) { // Validate the link. - if (!link_validate_url(trim($item['url']), $langcode)) { + if (!link_validate_url(trim($item['url']))) { $errors[$field['field_name']][$langcode][$delta][] = array( 'error' => 'link_required', 'message' => t('The value %value provided for %field is not a valid URL.', array( @@ -572,7 +569,7 @@ function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) { } } - $type = link_url_type($item['url']); + $type = link_validate_url($item['url']); // If the type of the URL cannot be determined and URL validation is disabled, // then assume LINK_EXTERNAL for later processing. if ($type == FALSE && $instance['settings']['validate_url'] === 0) { @@ -1371,7 +1368,7 @@ function link_views_api() { */ function link_cleanup_url($url, $protocol = 'http') { $url = trim($url); - $type = link_url_type($url); + $type = link_validate_url($url); if ($type === LINK_EXTERNAL) { // Check if there is no protocol specified. @@ -1390,48 +1387,6 @@ function link_cleanup_url($url, $protocol = 'http') { return $url; } -/** - * Validates a URL. - * - * @param string $text - * Url to be validated. - * @param string $langcode - * An optional language code to look up the path in. - * - * @return bool - * True if a valid link, FALSE otherwise. - */ -function link_validate_url($text, $langcode = NULL) { - - $text = _link_clean_relative($text); - $text = link_cleanup_url($text); - $type = link_url_type($text); - - if ($type && ($type == LINK_INTERNAL || $type == LINK_EXTERNAL)) { - $flag = valid_url($text, TRUE); - if (!$flag) { - $normal_path = drupal_get_normal_path($text, $langcode); - $parsed_link = parse_url($normal_path, PHP_URL_PATH); - if ($normal_path != $parsed_link) { - $normal_path = $parsed_link; - } - $flag = drupal_valid_path($normal_path); - } - if (!$flag) { - $flag = file_exists($normal_path); - } - if (!$flag) { - $uri = file_build_uri($normal_path); - $flag = file_exists($uri); - } - } - else { - $flag = (bool) $type; - } - - return $flag; -} - /** * Cleaner of relatives urls. * @@ -1458,19 +1413,19 @@ function _link_clean_relative($url) { } /** - * Type check a URL. + * Validates a URL. * * Accepts all URLs following RFC 1738 standard for URL formation and all e-mail * addresses following the RFC 2368 standard for mailto address formation. * * @param string $text - * Url to be checked. + * Url to be validated. * * @return mixed * Returns boolean FALSE if the URL is not valid. On success, returns one of * the LINK_(linktype) constants. */ -function link_url_type($text) { +function link_validate_url($text) { // @TODO Complete letters. // @codingStandardsIgnoreStart $link_ichars_domain = (string) html_entity_decode(implode("", array( diff --git a/tests/link.validate.test b/tests/link.validate.test index 6338840..68f79dc 100644 --- a/tests/link.validate.test +++ b/tests/link.validate.test @@ -31,24 +31,24 @@ class LinkValidateTestCase extends LinkBaseTestClass { $field_name = $this->createLinkField(); + $permission = 'create page content'; + $this->checkPermissions(array($permission), TRUE); + + $this->drupalGet('node/add/page'); + $label = $this->randomName(); - $settings = array( - 'title' => $label, - $field_name => array( - LANGUAGE_NONE => array( - array( - 'title' => $label, - 'url' => $url, - ), - ), - ), + $edit = array( + $field_name . '[und][0][title]' => $label, + $field_name . '[und][0][url]' => $url, ); + $this->drupalPost(NULL, $edit, t('Save')); + $this->assertRaw(' has been created.', 'Node created'); - $node = $this->drupalCreateNode($settings); + $nid = 1; //$matches[1]; - $this->assertNotNull($node, ' has been created.', 'Node created'); + $node = node_load($nid); - $this->assertEqual($url, $node->{$field_name}[LANGUAGE_NONE][0]['url']); + $this->assertEqual($url, $node->{$field_name}['und'][0]['url']); } } @@ -339,13 +339,7 @@ class LinkValidateTest extends LinkValidateTestCase { */ public function test_link_internal_url() { // @codingStandardsIgnoreEnd - // Create the content first. - $node = $this->drupalCreateNode(); - - $link = 'node/' . $node->nid; - $this->link_test_validate_url($link); - $type = link_url_type($link); - $this->assertEqual(LINK_INTERNAL, $type, 'Test ' . $link . ' is an internal link.'); + $this->link_test_validate_url('node/32'); } /** @@ -565,7 +559,7 @@ class LinkValidateUrlLight extends DrupalWebTestCase { } /** - * Make sure that a link labeled works. + * Make sure that a link labelled works. */ public function testValidateFrontLink() { $valid = link_validate_url(''); @@ -647,18 +641,16 @@ class LinkValidateUrlLight extends DrupalWebTestCase { * Validate Internal Links. */ public function testValidateInternalLinks() { - $tempfile = drupal_tempnam('public://files', 'test'); $links = array( + 'node/5', 'rss.xml', - file_uri_target($tempfile), - drupal_realpath($tempfile), + 'files/test.jpg', + '/var/www/test', ); 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.'); + $this->assertEqual(LINK_INTERNAL, $valid, 'Test ' . $link . ' internal link.'); } } @@ -680,6 +672,7 @@ class LinkValidateUrlLight extends DrupalWebTestCase { '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', @@ -711,10 +704,8 @@ class LinkValidateUrlLight extends DrupalWebTestCase { } } 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.'); + $this->assertEqual(LINK_EXTERNAL, $valid, 'Testing that ' . $link . ' is a valid external link.'); // The following two lines are commented out and only used for // comparisons. // $valid2 = valid_url($link, TRUE); @@ -737,8 +728,6 @@ class LinkValidateUrlLight extends DrupalWebTestCase { '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);