You are here

protected function NodeTranslationUITest::doTestAlternateHreflangLinks in Drupal 8

Same name and namespace in other branches
  1. 9 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()

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 399

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.
        $links = $this
          ->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]', [
          ':href' => $language_url
            ->toString(),
          ':hreflang' => $alternate_langcode,
        ]);
        if ($translations[$alternate_langcode]
          ->isPublished()) {
          $this
            ->assert(isset($links[0]), new FormattableMarkup('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', [
            '%langcode' => $langcode,
            '%alternate_langcode' => $alternate_langcode,
            '%link' => $url
              ->toString(),
          ]));
        }
        else {
          $this
            ->assertFalse(isset($links[0]), new FormattableMarkup('The %langcode node translation has an hreflang link for unpublished %alternate_langcode translation: %link.', [
            '%langcode' => $langcode,
            '%alternate_langcode' => $alternate_langcode,
            '%link' => $url
              ->toString(),
          ]));
        }
      }
    }
  }
}