function NodeTranslationUITest::doTestAlternateHreflangLinks

Same name in other branches
  1. 8.9.x core/modules/node/tests/src/Functional/NodeTranslationUITest.php \Drupal\Tests\node\Functional\NodeTranslationUITest::doTestAlternateHreflangLinks()
  2. 10 core/modules/node/tests/src/Functional/NodeTranslationUITest.php \Drupal\Tests\node\Functional\NodeTranslationUITest::doTestAlternateHreflangLinks()
  3. 11.x core/modules/node/tests/src/Functional/NodeTranslationUITest.php \Drupal\Tests\node\Functional\NodeTranslationUITest::doTestAlternateHreflangLinks()

Tests that the given path provides the correct alternate hreflang links.

Parameters

\Drupal\node\Entity\Node $node: The node to be tested.

1 call to NodeTranslationUITest::doTestAlternateHreflangLinks()
NodeTranslationUITest::testTranslationRendering in core/modules/node/tests/src/Functional/NodeTranslationUITest.php
Tests that translations are rendered properly.

File

core/modules/node/tests/src/Functional/NodeTranslationUITest.php, line 419

Class

NodeTranslationUITest
Tests the Node Translation UI.

Namespace

Drupal\Tests\node\Functional

Code

protected function doTestAlternateHreflangLinks(Node $node) {
    $url = $node->toUrl();
    $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) {
        // 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.
                $xpath = $this->assertSession()
                    ->buildXPathQuery('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]', [
                    ':href' => $language_url->toString(),
                    ':hreflang' => $alternate_langcode,
                ]);
                if ($translations[$alternate_langcode]->isPublished()) {
                    // Verify that the node translation has the correct alternate
                    // hreflang link for the alternate langcode.
                    $this->assertSession()
                        ->elementExists('xpath', $xpath);
                }
                else {
                    // Verify that the node translation does not have an alternate
                    // hreflang link for the alternate langcode.
                    $this->assertSession()
                        ->elementNotExists('xpath', $xpath);
                }
            }
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.