3 namespace Drupal\taxonomy\Tests;
6 use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;
9 * Tests for proper breadcrumb translation.
13 class TermTranslationTest extends TaxonomyTestBase {
15 use AssertBreadcrumbTrait;
16 use TaxonomyTranslationTestTrait;
19 * Term to translated term mapping.
23 protected $termTranslationMap = [
24 'one' => 'translatedOne',
25 'two' => 'translatedTwo',
26 'three' => 'translatedThree',
32 * @var \Drupal\taxonomy\Entity\Term[]
34 protected $terms = [];
39 public static $modules = ['taxonomy', 'language', 'content_translation'];
44 protected function setUp() {
46 $this->setupLanguages();
47 $this->vocabulary = $this->createVocabulary();
48 $this->enableTranslation();
50 $this->setUpTermReferenceField();
54 * Test translated breadcrumbs.
56 public function testTranslatedBreadcrumbs() {
57 // Ensure non-translated breadcrumb is correct.
58 $breadcrumb = [Url::fromRoute('<front>')->toString() => 'Home'];
59 foreach ($this->terms as $term) {
60 $breadcrumb[$term->url()] = $term->label();
62 // The last item will not be in the breadcrumb.
63 array_pop($breadcrumb);
65 // Check the breadcrumb on the leaf term page.
66 $term = $this->getLeafTerm();
67 $this->assertBreadcrumb($term->urlInfo(), $breadcrumb, $term->label());
69 $languages = \Drupal::languageManager()->getLanguages();
71 // Construct the expected translated breadcrumb.
72 $breadcrumb = [Url::fromRoute('<front>', [], ['language' => $languages[$this->translateToLangcode]])->toString() => 'Home'];
73 foreach ($this->terms as $term) {
74 $translated = $term->getTranslation($this->translateToLangcode);
75 $url = $translated->url('canonical', ['language' => $languages[$this->translateToLangcode]]);
76 $breadcrumb[$url] = $translated->label();
78 array_pop($breadcrumb);
80 // Check for the translated breadcrumb on the translated leaf term page.
81 $term = $this->getLeafTerm();
82 $translated = $term->getTranslation($this->translateToLangcode);
83 $this->assertBreadcrumb($translated->urlInfo('canonical', ['language' => $languages[$this->translateToLangcode]]), $breadcrumb, $translated->label());
88 * Test translation of terms are showed in the node.
90 public function testTermsTranslation() {
92 // Set the display of the term reference field on the article content type
93 // to "Check boxes/radio buttons".
94 entity_get_form_display('node', 'article', 'default')
95 ->setComponent($this->termFieldName, [
96 'type' => 'options_buttons',
99 $this->drupalLogin($this->drupalCreateUser(['create article content']));
101 // Test terms are listed.
102 $this->drupalget('node/add/article');
103 $this->assertText('one');
104 $this->assertText('two');
105 $this->assertText('three');
107 // Test terms translated are listed.
108 $this->drupalget('hu/node/add/article');
109 $this->assertText('translatedOne');
110 $this->assertText('translatedTwo');
111 $this->assertText('translatedThree');
115 * Setup translated terms in a hierarchy.
117 protected function setUpTerms() {
119 foreach ($this->termTranslationMap as $name => $translation) {
121 $term = $this->createTerm($this->vocabulary, [
123 'langcode' => $this->baseLangcode,
124 'parent' => $parent_vid,
127 $term->addTranslation($this->translateToLangcode, [
128 'name' => $translation,
132 // Each term is nested under the last.
133 $parent_vid = $term->id();
135 $this->terms[] = $term;
140 * Get the final (leaf) term in the hierarchy.
142 * @return \Drupal\taxonomy\Entity\Term
143 * The final term in the hierarchy.
145 protected function getLeafTerm() {
146 return $this->terms[count($this->termTranslationMap) - 1];