You are here

public function ContentTranslationLinkTagTest::testCanonicalAlternateTags in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/content_translation/tests/src/Functional/ContentTranslationLinkTagTest.php \Drupal\Tests\content_translation\Functional\ContentTranslationLinkTagTest::testCanonicalAlternateTags()

Tests alternate link tag found for entity types with canonical links.

File

core/modules/content_translation/tests/src/Functional/ContentTranslationLinkTagTest.php, line 86

Class

ContentTranslationLinkTagTest
Tests whether canonical link tags are present for content entities.

Namespace

Drupal\Tests\content_translation\Functional

Code

public function testCanonicalAlternateTags() {

  /** @var \Drupal\Core\Language\LanguageManagerInterface $languageManager */
  $languageManager = $this->container
    ->get('language_manager');

  /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
  $entityTypeManager = $this->container
    ->get('entity_type.manager');
  $definition = $entityTypeManager
    ->getDefinition('entity_test_mul');
  $this
    ->assertTrue($definition
    ->hasLinkTemplate('canonical'), 'Canonical link template found for entity_test.');
  $entity = $this
    ->createTranslatableEntity();
  $url_base = $entity
    ->toUrl('canonical')
    ->setAbsolute();
  $langcodes_all = $this->langcodes;
  $langcodes_all[] = $languageManager
    ->getDefaultLanguage()
    ->getId();

  /** @var \Drupal\Core\Url[] $urls */
  $urls = array_map(function ($langcode) use ($url_base, $languageManager) {
    $url = clone $url_base;
    return $url
      ->setOption('language', $languageManager
      ->getLanguage($langcode));
  }, array_combine($langcodes_all, $langcodes_all));

  // Ensure link tags for all languages are found on each language variation
  // page of an entity.
  foreach ($urls as $langcode => $url) {
    $this
      ->drupalGet($url);
    foreach ($urls as $langcode_alternate => $url_alternate) {
      $args = [
        ':href' => $url_alternate
          ->toString(),
        ':hreflang' => $langcode_alternate,
      ];
      $links = $this
        ->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]', $args);
      $message = sprintf('The "%s" translation has the correct alternate hreflang link for "%s": %s.', $langcode, $langcode_alternate, $url
        ->toString());
      $this
        ->assertTrue(isset($links[0]), $message);
    }
  }

  // Configure entity path as a front page.
  $entity_canonical = '/entity_test_mul/manage/' . $entity
    ->id();
  $this
    ->config('system.site')
    ->set('page.front', $entity_canonical)
    ->save();

  // Tests hreflangs when using entities as a front page.
  foreach ($urls as $langcode => $url) {
    $this
      ->drupalGet($url);
    foreach ($entity
      ->getTranslationLanguages() as $language) {
      $frontpage_path = Url::fromRoute('<front>', [], [
        'absolute' => TRUE,
        'language' => $language,
      ])
        ->toString();
      $args = [
        ':href' => $frontpage_path,
        ':hreflang' => $language
          ->getId(),
      ];
      $links = $this
        ->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]', $args);
      $this
        ->assertArrayHasKey(0, $links);
    }
  }
}