View source
<?php
namespace Drupal\Tests\content_translation\Functional;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\content_translation_test\Entity\EntityTestTranslatableNoUISkip;
class ContentTranslationLinkTagTest extends BrowserTestBase {
protected static $modules = [
'entity_test',
'content_translation',
'content_translation_test',
'language',
];
protected $defaultTheme = 'stark';
protected $langcodes;
protected function setUp() : void {
parent::setUp();
$user = $this
->drupalCreateUser([
'view test entity',
'view test entity translations',
'administer entity_test content',
]);
$this
->drupalLogin($user);
$this->langcodes = [
'it',
'fr',
];
foreach ($this->langcodes as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$this
->rebuildContainer();
}
protected function createTranslatableEntity() {
$entity = EntityTestMul::create([
'label' => $this
->randomString(),
]);
foreach ($this->langcodes as $langcode) {
$entity
->addTranslation($langcode, [
'label' => $this
->randomString(),
]);
}
$entity
->save();
return $entity;
}
public function testCanonicalAlternateTags() {
$languageManager = $this->container
->get('language_manager');
$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();
$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));
foreach ($urls as $langcode => $url) {
$this
->drupalGet($url);
foreach ($urls as $langcode_alternate => $url_alternate) {
$this
->assertSession()
->elementAttributeContains('xpath', "head/link[@rel='alternate' and @hreflang='{$langcode_alternate}']", 'href', $url_alternate
->toString());
}
}
$entity_canonical = '/entity_test_mul/manage/' . $entity
->id();
$this
->config('system.site')
->set('page.front', $entity_canonical)
->save();
foreach ($urls as $langcode => $url) {
$this
->drupalGet($url);
foreach ($entity
->getTranslationLanguages() as $language) {
$frontpage_path = Url::fromRoute('<front>', [], [
'absolute' => TRUE,
'language' => $language,
])
->toString();
$this
->assertSession()
->elementAttributeContains('xpath', "head/link[@rel='alternate' and @hreflang='{$language->getId()}']", 'href', $frontpage_path);
}
}
}
public function testCanonicalAlternateTagsMissing() {
$entityTypeManager = $this->container
->get('entity_type.manager');
$definition = $entityTypeManager
->getDefinition('entity_test_translatable_no_skip');
$this
->assertFalse($definition
->hasLinkTemplate('canonical'), 'Canonical link template does not exist for entity_test_translatable_no_skip entity.');
$entity = EntityTestTranslatableNoUISkip::create();
$entity
->save();
$this
->drupalGet($entity
->toUrl('edit-form'));
$this
->assertSession()
->statusCodeEquals(200);
$result = $this
->xpath('//link[@rel="alternate" and @hreflang]');
$this
->assertEmpty($result, 'No alternate link tag found.');
}
}