Commit f9b82e47 authored by Damien McKenna's avatar Damien McKenna
Browse files

Issue #3095484 by DamienMcKenna, jedihe, silverham, circuscowboy, dungahk,...

Issue #3095484 by DamienMcKenna, jedihe, silverham, circuscowboy, dungahk, codebymikey, bondjimbond, tcnolan7, La558, timwright_, Ron Collins, Summit, BrianLewisDesign, chegor, danbarron, SKrossa, wylbur, zorya: Query string, fragment stripped from URL.
parent d9645b30
......@@ -22,6 +22,10 @@ By DamienMcKenna: Unnecessary syntax change broke PHP 5.3 compatibility.
#3208869 by DamienMcKenna: Simplify tests by extending LinkBaseTestClass.
#3208869 by DamienMcKenna: Forgot to update LinkSanitizeTest.
#3208957 by DamienMcKenna: Add unit tests for _link_parse_url().
#3095484 by DamienMcKenna, jedihe, silverham, circuscowboy, dungahk,
codebymikey, bondjimbond, tcnolan7, La558, timwright_, Ron Collins, Summit,
BrianLewisDesign, chegor, danbarron, SKrossa, wylbur, zorya: Query string,
fragment stripped from URL.
Link 7.x-1.7, 2019-11-14
......
......@@ -689,7 +689,13 @@ function _link_sanitize(&$item, $delta, &$field, $instance, &$entity) {
$display_url = str_replace('mailto:', '', $url);
}
elseif ($type === LINK_EXTERNAL) {
$display_url = $item['url'];
$display_url = url($url_parts['url'],
array(
'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
'absolute' => TRUE,
)
);
}
elseif ($type == LINK_TEL) {
$display_url = str_replace('tel:', '', $url);
......@@ -1234,7 +1240,6 @@ function link_field_formatter_settings_form($field, $instance, $view_mode, $form
* Implements hook_field_formatter_settings_summary().
*/
function link_field_formatter_settings_summary($field, $instance, $view_mode) {
$display = $instance['display'][$view_mode];
if ($display['type'] == 'link_domain') {
......@@ -1924,6 +1929,7 @@ function link_field_item_property_info() {
'type' => 'text',
'label' => t('The URL of the link.'),
'setter callback' => 'entity_property_verbatim_set',
'getter callback' => 'link_url_property_get',
);
$properties['attributes'] = array(
'type' => 'struct',
......@@ -1939,6 +1945,17 @@ function link_field_item_property_info() {
return $properties;
}
/**
* Callback for getting the URL property.
*
* @see entity_metadata_entity_get_properties()
*/
function link_url_property_get($data, array $options, $name, $type, $info) {
$url = entity_property_verbatim_get($data, $options, $name, $type, $info);
return url($url, $data + $options);
}
/**
* Entity property info getter callback for link attributes.
*/
......
......@@ -512,4 +512,61 @@ class LinkFieldCrudTest extends LinkBaseTestClass {
$this->assertPattern('|class\s?=\s?"' . $link_class_name . '"|', "Classes $link_class_name exist on page.");
}
/**
* Tests that link titles properly preserve the query params.
*/
public function testLinkTitlePreservesQueryParams() {
// Create field.
$field_name = $this->createLinkField();
// Create page form.
$this->drupalGet('node/add/page');
$this->assertField($field_name . '[und][0][title]', 'Title found');
$this->assertField($field_name . '[und][0][url]', 'URL found');
$link_title_tests = array(
array(
'href' => 'http://example.com/' . $this->randomName(),
),
array(
'href' => 'http://example.com/' . $this->randomName() . '?property=value',
),
array(
'href' => 'http://example.com/' . $this->randomName() . '#position',
),
array(
'href' => 'http://example.com/' . $this->randomName() . '#lower?property=value2',
),
array(
'href' => 'http://example.com/' . $this->randomName() . '?property=value2#lower',
),
);
foreach ($link_title_tests as &$input) {
$this->drupalGet('node/add/page');
$this->assertResponse(200);
// Intentionally set empty title, to force using the url as the title.
$edit = array(
'title' => 'Some node title',
$field_name . '[und][0][title]' => '',
$field_name . '[und][0][url]' => $input['href'],
);
$this->drupalPost(NULL, $edit, t('Save'));
// Confirm the node saved correctly.
$this->assertResponse(200);
$this->assertText($edit['title']);
$input['url'] = $this->getUrl();
}
// Change to anonymous user.
$this->drupalLogout();
// Loop over the nodes, confirm each URL is rendered correctly.
foreach ($link_title_tests as $input) {
$this->drupalGet($input['url']);
$this->assertRaw(l($input['href'], $input['href']), "Test that the link title has been set to " . $input['href']);
}
}
}
......@@ -107,4 +107,72 @@ class LinkSanitizeTest extends LinkBaseTestClass {
$this->assertEqual(NULL, $item['fragment']);
}
/**
* Test that query URLs can be turned into absolute URLs.
*/
public function testBlankTitle() {
$item['title'] = '';
$item['url'] = 'https://www.drupal.org/';
list($field, $instance, $entity) = $this->generateParams();
// Disable the URL cutoff to confirm the URL.
$instance['settings']['display']['url_cutoff'] = FALSE;
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
$this->assertEqual('https://www.drupal.org/', $item['display_url']);
$this->assertEqual('https://www.drupal.org/', $item['url']);
$this->assertEqual(array(), $item['query']);
$this->assertEqual(NULL, $item['fragment']);
}
/**
* Test that query URLs can be turned into absolute URLs.
*/
public function testBlankTitleWithQuery() {
$item['title'] = '';
$item['url'] = 'https://www.drupal.org/?page=42';
list($field, $instance, $entity) = $this->generateParams();
// Disable the URL cutoff to confirm the URL.
$instance['settings']['display']['url_cutoff'] = FALSE;
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
$this->assertEqual('https://www.drupal.org/?page=42', $item['display_url']);
$this->assertEqual('https://www.drupal.org/', $item['url']);
$this->assertEqual(array('page' => 42), $item['query']);
$this->assertEqual(NULL, $item['fragment']);
}
/**
* Test that query URLs can be turned into absolute URLs.
*/
public function testBlankTitleWithFragment() {
$item['title'] = '';
$item['url'] = 'https://www.drupal.org/#something';
list($field, $instance, $entity) = $this->generateParams();
// Disable the URL cutoff to confirm the URL.
$instance['settings']['display']['url_cutoff'] = FALSE;
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
$this->assertEqual('https://www.drupal.org/#something', $item['display_url']);
$this->assertEqual('https://www.drupal.org/', $item['url']);
$this->assertEqual(array(), $item['query']);
$this->assertEqual('something', $item['fragment']);
}
/**
* Test that query URLs can be turned into absolute URLs.
*/
public function testBlankTitleWithQueryAndFragment() {
$item['title'] = '';
$item['url'] = 'https://www.drupal.org/?page=42#something';
list($field, $instance, $entity) = $this->generateParams();
// Disable the URL cutoff to confirm the URL.
$instance['settings']['display']['url_cutoff'] = FALSE;
_link_sanitize($item, NULL, $field, $instance, $entity);
$this->verbose('<pre>' . print_r($item, TRUE) . '</pre>');
$this->assertEqual('https://www.drupal.org/?page=42#something', $item['display_url']);
$this->assertEqual('https://www.drupal.org/', $item['url']);
$this->assertEqual(array('page' => 42), $item['query']);
$this->assertEqual('something', $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