You are here

protected function NodeTranslationUITest::doTestAlternateHreflangLinks in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/node/tests/src/Functional/NodeTranslationUITest.php \Drupal\Tests\node\Functional\NodeTranslationUITest::doTestAlternateHreflangLinks()
  2. 9 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.

File

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

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);
        }
      }
    }
  }
}