Issue #2521782 by paulmckibben, swentel: HTML head has alternate hreflang links to unpublished translations

8.1.x
Nathaniel Catchpole 2016-02-17 10:30:47 +09:00
parent edb618f556
commit 5a25b3fba9
2 changed files with 39 additions and 11 deletions

View File

@ -566,6 +566,11 @@ function content_translation_page_attachments(&$page) {
if ($entity instanceof ContentEntityInterface) {
// Current route represents a content entity. Build hreflang links.
foreach ($entity->getTranslationLanguages() as $language) {
// Skip any translation that cannot be viewed.
$translation = $entity->getTranslation($language->getId());
if (!$translation->access('view')) {
continue;
}
$url = $entity->urlInfo()
->setOption('language', $language)
->setAbsolute()

View File

@ -352,8 +352,18 @@ class NodeTranslationUITest extends ContentTranslationUITestBase {
// Test that the node page displays the correct translations.
$this->doTestTranslations('node/' . $node->id(), $values);
// Unpublish one of the translations.
foreach ($node->getTranslationLanguages() as $lang) {
if ($lang->getId() != 'en') {
$translation = $node->getTranslation($lang->getId());
$translation->setPublished(NODE_NOT_PUBLISHED);
$translation->save();
break;
}
}
// Test that the node page has the correct alternate hreflang links.
$this->doTestAlternateHreflangLinks($node->urlInfo());
$this->doTestAlternateHreflangLinks($node);
}
/**
@ -373,26 +383,39 @@ class NodeTranslationUITest extends ContentTranslationUITestBase {
}
/**
* Tests that the given path provides the correct alternate hreflang links.
* Tests the given node's page view for correct alternate hreflang links.
*
* @param \Drupal\Core\Url $url
* The path to be tested.
* @param \Drupal\node\Entity\Node $node
* The node to be tested.
*
* @throws \Drupal\Core\Entity\EntityMalformedException
*/
protected function doTestAlternateHreflangLinks(Url $url) {
protected function doTestAlternateHreflangLinks(Node $node) {
$url = $node->urlInfo();
$languages = $this->container->get('language_manager')->getLanguages();
$url->setAbsolute();
$urls = [];
$translations = [];
foreach ($this->langcodes as $langcode) {
$language_url = clone $url;
$urls[$langcode] = $language_url->setOption('language', $languages[$langcode]);
$translations[$langcode] = $node->getTranslation($langcode);
}
foreach ($this->langcodes as $langcode) {
$this->drupalGet($urls[$langcode]);
foreach ($urls as $alternate_langcode => $language_url) {
// Retrieve desired link elements from the HTML head.
$links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
array(':href' => $language_url->toString(), ':hreflang' => $alternate_langcode));
$this->assert(isset($links[0]), format_string('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString())));
// Skip unpublished translations.
if ($translations[$langcode]->isPublished()) {
$this->drupalGet($urls[$langcode]);
foreach ($urls as $alternate_langcode => $language_url) {
// Retrieve desired link elements from the HTML head.
$links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]',
array(':href' => $language_url->toString(), ':hreflang' => $alternate_langcode));
if ($translations[$alternate_langcode]->isPublished()) {
$this->assert(isset($links[0]), format_string('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString())));
}
else {
$this->assertFalse(isset($links[0]), format_string('The %langcode node translation has an hreflang link for unpublished %alternate_langcode translation: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url->toString())));
}
}
}
}
}