Chris@17: 'translatedOne', Chris@17: 'two' => 'translatedTwo', Chris@17: 'three' => 'translatedThree', Chris@17: ]; Chris@17: Chris@17: /** Chris@17: * Created terms. Chris@17: * Chris@17: * @var \Drupal\taxonomy\Entity\Term[] Chris@17: */ Chris@17: protected $terms = []; Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: public static $modules = ['taxonomy', 'language', 'content_translation']; Chris@17: Chris@17: /** Chris@17: * {@inheritdoc} Chris@17: */ Chris@17: protected function setUp() { Chris@17: parent::setUp(); Chris@17: $this->setupLanguages(); Chris@17: $this->vocabulary = $this->createVocabulary(); Chris@17: $this->enableTranslation(); Chris@17: $this->setUpTerms(); Chris@17: $this->setUpTermReferenceField(); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test translated breadcrumbs. Chris@17: */ Chris@17: public function testTranslatedBreadcrumbs() { Chris@17: // Ensure non-translated breadcrumb is correct. Chris@17: $breadcrumb = [Url::fromRoute('')->toString() => 'Home']; Chris@17: foreach ($this->terms as $term) { Chris@18: $breadcrumb[$term->toUrl()->toString()] = $term->label(); Chris@17: } Chris@17: // The last item will not be in the breadcrumb. Chris@17: array_pop($breadcrumb); Chris@17: Chris@17: // Check the breadcrumb on the leaf term page. Chris@17: $term = $this->getLeafTerm(); Chris@18: $this->assertBreadcrumb($term->toUrl(), $breadcrumb, $term->label()); Chris@17: Chris@17: $languages = \Drupal::languageManager()->getLanguages(); Chris@17: Chris@17: // Construct the expected translated breadcrumb. Chris@17: $breadcrumb = [Url::fromRoute('', [], ['language' => $languages[$this->translateToLangcode]])->toString() => 'Home']; Chris@17: foreach ($this->terms as $term) { Chris@17: $translated = $term->getTranslation($this->translateToLangcode); Chris@18: $url = $translated->toUrl('canonical', ['language' => $languages[$this->translateToLangcode]])->toString(); Chris@17: $breadcrumb[$url] = $translated->label(); Chris@17: } Chris@17: array_pop($breadcrumb); Chris@17: Chris@17: // Check for the translated breadcrumb on the translated leaf term page. Chris@17: $term = $this->getLeafTerm(); Chris@17: $translated = $term->getTranslation($this->translateToLangcode); Chris@18: $this->assertBreadcrumb($translated->toUrl('canonical', ['language' => $languages[$this->translateToLangcode]]), $breadcrumb, $translated->label()); Chris@17: Chris@17: } Chris@17: Chris@17: /** Chris@17: * Test translation of terms are showed in the node. Chris@17: */ Chris@17: public function testTermsTranslation() { Chris@17: Chris@17: // Set the display of the term reference field on the article content type Chris@17: // to "Check boxes/radio buttons". Chris@17: entity_get_form_display('node', 'article', 'default') Chris@17: ->setComponent($this->termFieldName, [ Chris@17: 'type' => 'options_buttons', Chris@17: ]) Chris@17: ->save(); Chris@17: $this->drupalLogin($this->drupalCreateUser(['create article content'])); Chris@17: Chris@17: // Test terms are listed. Chris@17: $this->drupalget('node/add/article'); Chris@17: $this->assertText('one'); Chris@17: $this->assertText('two'); Chris@17: $this->assertText('three'); Chris@17: Chris@17: // Test terms translated are listed. Chris@17: $this->drupalget('hu/node/add/article'); Chris@17: $this->assertText('translatedOne'); Chris@17: $this->assertText('translatedTwo'); Chris@17: $this->assertText('translatedThree'); Chris@17: } Chris@17: Chris@17: /** Chris@17: * Setup translated terms in a hierarchy. Chris@17: */ Chris@17: protected function setUpTerms() { Chris@17: $parent_vid = 0; Chris@17: foreach ($this->termTranslationMap as $name => $translation) { Chris@17: Chris@17: $term = $this->createTerm($this->vocabulary, [ Chris@17: 'name' => $name, Chris@17: 'langcode' => $this->baseLangcode, Chris@17: 'parent' => $parent_vid, Chris@17: ]); Chris@17: Chris@17: $term->addTranslation($this->translateToLangcode, [ Chris@17: 'name' => $translation, Chris@17: ]); Chris@17: $term->save(); Chris@17: Chris@17: // Each term is nested under the last. Chris@17: $parent_vid = $term->id(); Chris@17: Chris@17: $this->terms[] = $term; Chris@17: } Chris@17: } Chris@17: Chris@17: /** Chris@17: * Get the final (leaf) term in the hierarchy. Chris@17: * Chris@17: * @return \Drupal\taxonomy\Entity\Term Chris@17: * The final term in the hierarchy. Chris@17: */ Chris@17: protected function getLeafTerm() { Chris@17: return $this->terms[count($this->termTranslationMap) - 1]; Chris@17: } Chris@17: Chris@17: }