Commit dc26f18a authored by torotil's avatar torotil Committed by Damien McKenna
Browse files

Issue #3121196 by torotil, DamienMcKenna, calebtr: Support relative fragment or query-only links.

parent 48d3cea2
......@@ -13,6 +13,8 @@ By DamienMcKenna: Unnecessary syntax change broke PHP 5.3 compatibility.
#3156021 by roman.haluska, klausi, DamienMcKenna, RenatoG: According to RFC4343
uppercase letters in the domain name should be allowed.
#3208147 by DamienMcKenna: Simplify nested ternary logic in _link_sanitize().
#3121196 by torotil, DamienMcKenna, calebtr: Support relative fragment or query
-only links.
Link 7.x-1.7, 2019-11-14
......
......@@ -21,5 +21,6 @@ files[] = tests/LinkFieldAttributesTest.test
files[] = tests/LinkFieldCrudTest.test
files[] = tests/LinkFieldValidateTest.test
files[] = tests/LinkPathPrefixesTest.test
files[] = tests/LinkSanitizeTest.test
files[] = tests/LinkTokenTest.test
files[] = tests/LinkValidationApiTest.test
......@@ -591,6 +591,11 @@ function _link_validate(&$item, $delta, $field, $entity, $instance, $langcode, &
/**
* Clean up user-entered values for a link field according to field settings.
*
* Note: this cannot be properly unit tested as it checks for certain entity
* values.
*
* @todo Rewrite so that the logic can be unit tested.
*
* @param array $item
* A single link item, usually containing url, title, and attributes.
* @param int $delta
......@@ -658,8 +663,13 @@ function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
$type = LINK_EXTERNAL;
}
elseif ($type == LINK_FRAGMENT || $type == LINK_QUERY) {
// If type is a fragment or query, then use the current URL.
$item['url'] = $_GET['q'] . $item['url'];
// Treat fragment or query-only links as external.
if (!empty($instance['settings']['absolute_url'])) {
$item['url'] = $_GET['q'] . $item['url'];
}
else {
$item['external'] = TRUE;
}
}
$url = link_cleanup_url($item['url']);
$url_parts = _link_parse_url($url);
......
<?php
/**
* Test for _link_sanitize().
*
* @todo Rewrite as a unit test once _link_sanitize() is refactored.
*/
class LinkSanitizeTest extends DrupalWebTestCase {
/**
* Backup of $_GET['q'] to be able to revert it to its original state.
*
* @var string
*/
protected $q;
/**
* Get Info.
*/
public static function getInfo() {
return array(
'name' => 'Link Sanitation Tests',
'description' => 'Tests the _link_sanitize() function by itself, without invoking the field life cycle.',
'group' => 'Link',
);
}
/**
* {@inheritdoc}
*/
public function setUp(array $modules = array()) {
$modules[] = 'link';
$modules[] = 'node';
parent::setUp($modules);
$this->q = $_GET['q'];
$_GET['q'] = 'test-path';
}
/**
* {@inheritdoc}
*/
public function tearDown(array $modules = array()) {
$_GET['q'] = $this->q;
parent::tearDown();
}
/**
* Generate parameters for testing _link_sanitize().
*/
protected function generateParams($settings = array()) {
$field = array();
$instance['entity_type'] = 'node';
$defaults['attributes'] = array();
$defaults['display']['url_cutoff'] = 20;
$defaults['title'] = 'Test title';
$settings += $defaults;
$settings['display'] += $defaults['display'];
$instance['settings'] = $settings;
$entity = NULL;
return array($field, $instance, $entity);
}
/**
* Test that relative hash URLs are returned as is.
*/
public function testRelativeHash() {
$item['url'] = '#hash-only';
list($field, $instance, $entity) = $this->generateParams();
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->assertEqual('#hash-only', $item['url']);
}
/**
* Test that relative hash URLs can be turned into absolute URLs.
*/
public function testAbsoluteHash() {
$item['url'] = '#hash-only';
list($field, $instance, $entity) = $this->generateParams(array(
'absolute_url' => TRUE,
));
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->assertEqual('test-path', $item['url']);
$this->assertEqual(NULL, $item['query']);
$this->assertEqual('hash-only', $item['fragment']);
}
/**
* Test that relative query URLs are returned as is.
*/
public function testRelativeQuery() {
$item['url'] = '?query=only';
list($field, $instance, $entity) = $this->generateParams();
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->assertEqual('?query=only', $item['url']);
}
/**
* Test that query URLs can be turned into absolute URLs.
*/
public function testAbsoluteQuery() {
$item['url'] = '?query=only';
list($field, $instance, $entity) = $this->generateParams(array(
'absolute_url' => TRUE,
));
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->assertEqual('test-path', $item['url']);
$this->assertEqual(array('query' => 'only'), $item['query']);
$this->assertEqual(NULL, $item['fragment']);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment