View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Core\Url;
use Drupal\Tests\system\Functional\Menu\AssertBreadcrumbTrait;
class TermTranslationTest extends TaxonomyTestBase {
use AssertBreadcrumbTrait;
use TaxonomyTranslationTestTrait;
protected $termTranslationMap = [
'one' => 'translatedOne',
'two' => 'translatedTwo',
'three' => 'translatedThree',
];
protected $terms = [];
protected static $modules = [
'taxonomy',
'language',
'content_translation',
];
protected $defaultTheme = 'classy';
protected function setUp() : void {
parent::setUp();
$this
->setupLanguages();
$this->vocabulary = $this
->createVocabulary();
$this
->enableTranslation();
$this
->setUpTerms();
$this
->setUpTermReferenceField();
}
public function testTranslatedBreadcrumbs() {
$breadcrumb = [
Url::fromRoute('<front>')
->toString() => 'Home',
];
foreach ($this->terms as $term) {
$breadcrumb[$term
->toUrl()
->toString()] = $term
->label();
}
array_pop($breadcrumb);
$term = $this
->getLeafTerm();
$this
->assertBreadcrumb($term
->toUrl(), $breadcrumb, $term
->label());
$languages = \Drupal::languageManager()
->getLanguages();
$breadcrumb = [
Url::fromRoute('<front>', [], [
'language' => $languages[$this->translateToLangcode],
])
->toString() => 'Home',
];
foreach ($this->terms as $term) {
$translated = $term
->getTranslation($this->translateToLangcode);
$url = $translated
->toUrl('canonical', [
'language' => $languages[$this->translateToLangcode],
])
->toString();
$breadcrumb[$url] = $translated
->label();
}
array_pop($breadcrumb);
$term = $this
->getLeafTerm();
$translated = $term
->getTranslation($this->translateToLangcode);
$this
->assertBreadcrumb($translated
->toUrl('canonical', [
'language' => $languages[$this->translateToLangcode],
]), $breadcrumb, $translated
->label());
}
public function testTermsTranslation() {
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'article')
->setComponent($this->termFieldName, [
'type' => 'options_buttons',
])
->save();
$this
->drupalLogin($this
->drupalCreateUser([
'create article content',
]));
$this
->drupalget('node/add/article');
$this
->assertSession()
->pageTextContains('one');
$this
->assertSession()
->pageTextContains('two');
$this
->assertSession()
->pageTextContains('three');
$this
->drupalget('hu/node/add/article');
$this
->assertSession()
->pageTextContains('translatedOne');
$this
->assertSession()
->pageTextContains('translatedTwo');
$this
->assertSession()
->pageTextContains('translatedThree');
}
protected function setUpTerms() {
$parent_vid = 0;
foreach ($this->termTranslationMap as $name => $translation) {
$term = $this
->createTerm($this->vocabulary, [
'name' => $name,
'langcode' => $this->baseLangcode,
'parent' => $parent_vid,
]);
$term
->addTranslation($this->translateToLangcode, [
'name' => $translation,
]);
$term
->save();
$parent_vid = $term
->id();
$this->terms[] = $term;
}
}
protected function getLeafTerm() {
return $this->terms[count($this->termTranslationMap) - 1];
}
}