diff -u b/includes/common.inc b/includes/common.inc --- b/includes/common.inc +++ b/includes/common.inc @@ -2944,7 +2944,7 @@ } /** - * Adds a LINK tag with a distinct attribute to the page's HEAD. + * Adds a LINK tag with distinct attributes to the page's HEAD. * * This function can be called as long the HTML header hasn't been sent, which * on normal pages is up through the preprocess step of theme('html'). Adding @@ -2952,8 +2952,7 @@ * 'hreflang' attributes. * * @param $attributes - * Associative array of element attributes including 'rel', 'href' and - * 'hreflang'. + * Associative array of element attributes including 'href' and 'rel'. * @param $header * Optional flag to determine if a HTTP 'Link:' header should be sent. */ @@ -2971,7 +2970,7 @@ $element['#attached']['drupal_add_http_header'][] = array('Link', $href . drupal_http_header_attributes($attributes), TRUE); } - drupal_add_html_head($element, 'drupal_add_html_head_link:' . (isset($attributes['hreflang']) ? $attributes['hreflang'] . ':' : '') . $attributes['rel'] . ':' . $href); + drupal_add_html_head($element, 'drupal_add_html_head_link:' . $attributes['rel'] . ':' . (isset($attributes['hreflang']) ? "{$attributes['hreflang']}:" : '') . $href); } /** only in patch2: unchanged: --- a/modules/simpletest/tests/common.test +++ b/modules/simpletest/tests/common.test @@ -3297,3 +3297,45 @@ class BlockInterestCohortTest extends DrupalWebTestCase { } } + +/** + * Test for drupal_add_html_head_link(). + */ +class DrupalAddHtmlHeadLinkTest extends DrupalWebTestCase { + + /** + * {@inheritdoc} + */ + public static function getInfo() { + return array( + 'name' => 'Add HTML head link', + 'description' => 'Test for drupal_add_html_head_link().', + 'group' => 'System', + ); + } + + /** + * {@inheritdoc} + */ + function setUp() { + parent::setUp('common_test'); + } + + /** + * Tests drupal_add_html_head_link(). + */ + function testDrupalAddHtmlHeadLink() { + $this->drupalGet('common-test/html_head_link'); + $expected_link_header = implode(',', array( + '; rel="alternate"', + '; hreflang="nl"; rel="alternate"', + '; hreflang="de"; rel="alternate"', + )); + $this->assertEqual($this->drupalGetHeader('Link'), $expected_link_header); + + // Check that duplicate alternate URLs with different hreflangs are allowed. + $test_link = $this->xpath('//head/link[@rel="alternate"][@href="/foo/bar"]'); + $this->assertEqual(count($test_link), 2, 'Duplicate alternate URLs are allowed.'); + } + +} only in patch2: unchanged: --- a/modules/simpletest/tests/common_test.module +++ b/modules/simpletest/tests/common_test.module @@ -64,6 +64,12 @@ function common_test_menu() { 'access arguments' => array('access content'), 'type' => MENU_CALLBACK, ); + $items['common-test/html_head_link'] = array( + 'title' => 'Test HTML head link', + 'page callback' => 'common_test_html_head_link', + 'access arguments' => array('access content'), + 'type' => MENU_CALLBACK, + ); return $items; } @@ -314,3 +320,28 @@ function existing_permissions_policy_header() { drupal_add_http_header('Permissions-Policy', 'geolocation=()'); print __FUNCTION__; } + +/** + * Page callback. + */ +function common_test_html_head_link() { + drupal_add_html_head_link(array( + 'href' => '/foo?bar=&baz=false', + 'rel' => 'alternate', + ), TRUE); + drupal_add_html_head_link(array( + 'href' => '/not-added-to-http-headers', + 'rel' => 'alternate', + ), FALSE); + drupal_add_html_head_link(array( + 'href' => '/foo/bar', + 'hreflang' => 'nl', + 'rel' => 'alternate', + ), TRUE); + drupal_add_html_head_link(array( + 'href' => '/foo/bar', + 'hreflang' => 'de', + 'rel' => 'alternate', + ), TRUE); + return ''; +}