Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / content_translation / tests / src / Functional / ContentTranslationLinkTagTest.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\entity_test\Entity\EntityTestMul;
8 use Drupal\content_translation_test\Entity\EntityTestTranslatableNoUISkip;
9
10 /**
11  * Tests whether canonical link tags are present for content entities.
12  *
13  * @group content_translation
14  */
15 class ContentTranslationLinkTagTest extends BrowserTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['entity_test', 'content_translation', 'content_translation_test', 'language'];
21
22   /**
23    * The added languages.
24    *
25    * @var string[]
26    */
27   protected $langcodes;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     // Set up user.
36     $user = $this->drupalCreateUser([
37       'view test entity',
38       'view test entity translations',
39       'administer entity_test content',
40     ]);
41     $this->drupalLogin($user);
42
43     // Add additional languages.
44     $this->langcodes = ['it', 'fr'];
45     foreach ($this->langcodes as $langcode) {
46       ConfigurableLanguage::createFromLangcode($langcode)->save();
47     }
48
49     // Rebuild the container so that the new languages are picked up by services
50     // that hold a list of languages.
51     $this->rebuildContainer();
52   }
53
54   /**
55    * Create a test entity with translations.
56    *
57    * @return \Drupal\Core\Entity\EntityInterface
58    *   An entity with translations.
59    */
60   protected function createTranslatableEntity() {
61     $entity = EntityTestMul::create(['label' => $this->randomString()]);
62
63     // Create translations for non default languages.
64     foreach ($this->langcodes as $langcode) {
65       $entity->addTranslation($langcode, ['label' => $this->randomString()]);
66     }
67     $entity->save();
68
69     return $entity;
70   }
71
72   /**
73    * Tests alternate link tag found for entity types with canonical links.
74    */
75   public function testCanonicalAlternateTags() {
76     /** @var \Drupal\Core\Language\LanguageManagerInterface $languageManager */
77     $languageManager = $this->container->get('language_manager');
78     /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
79     $entityTypeManager = $this->container->get('entity_type.manager');
80
81     $definition = $entityTypeManager->getDefinition('entity_test_mul');
82     $this->assertTrue($definition->hasLinkTemplate('canonical'), 'Canonical link template found for entity_test.');
83
84     $entity = $this->createTranslatableEntity();
85     $url_base = $entity->toUrl('canonical')
86       ->setAbsolute();
87
88     $langcodes_all = $this->langcodes;
89     $langcodes_all[] = $languageManager
90       ->getDefaultLanguage()
91       ->getId();
92
93     /** @var \Drupal\Core\Url[] $urls */
94     $urls = array_map(
95       function ($langcode) use ($url_base, $languageManager) {
96         $url = clone $url_base;
97         return $url
98           ->setOption('language', $languageManager->getLanguage($langcode));
99       },
100       array_combine($langcodes_all, $langcodes_all)
101     );
102
103     // Ensure link tags for all languages are found on each language variation
104     // page of an entity.
105     foreach ($urls as $langcode => $url) {
106       $this->drupalGet($url);
107       foreach ($urls as $langcode_alternate => $url_alternate) {
108         $args = [':href' => $url_alternate->toString(), ':hreflang' => $langcode_alternate];
109         $links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]', $args);
110         $message = sprintf('The "%s" translation has the correct alternate hreflang link for "%s": %s.', $langcode, $langcode_alternate, $url->toString());
111         $this->assertTrue(isset($links[0]), $message);
112       }
113     }
114   }
115
116   /**
117    * Tests alternate link tag missing for entity types without canonical links.
118    */
119   public function testCanonicalAlternateTagsMissing() {
120     /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager */
121     $entityTypeManager = $this->container->get('entity_type.manager');
122
123     $definition = $entityTypeManager->getDefinition('entity_test_translatable_no_skip');
124     // Ensure 'canonical' link template does not exist, in case it is added in
125     // the future.
126     $this->assertFalse($definition->hasLinkTemplate('canonical'), 'Canonical link template does not exist for entity_test_translatable_no_skip entity.');
127
128     $entity = EntityTestTranslatableNoUISkip::create();
129     $entity->save();
130     $this->drupalGet($entity->toUrl('edit-form'));
131
132     $this->assertSession()->statusCodeEquals(200);
133     $result = $this->xpath('//link[@rel="alternate" and @hreflang]');
134     $this->assertFalse($result, 'No alternate link tag found.');
135   }
136
137 }