View source
<?php
namespace Drupal\taxonomy\Tests;
use Drupal\Core\Url;
use Drupal\system\Tests\Menu\AssertBreadcrumbTrait;
class TermTranslationBreadcrumbTest extends TaxonomyTestBase {
use AssertBreadcrumbTrait;
use TaxonomyTranslationTestTrait;
protected $termTranslationMap = array(
'one' => 'translatedOne',
'two' => 'translatedTwo',
'three' => 'translatedThree',
);
protected $terms = array();
public static $modules = array(
'taxonomy',
'language',
'content_translation',
);
protected function setUp() {
parent::setUp();
$this
->setupLanguages();
$this->vocabulary = $this
->createVocabulary();
$this
->enableTranslation();
$this
->setUpTerms();
$this
->setUpTermReferenceField();
}
public function testTranslatedBreadcrumbs() {
$breadcrumb = array(
Url::fromRoute('<front>')
->toString() => 'Home',
);
foreach ($this->terms as $term) {
$breadcrumb[$term
->url()] = $term
->label();
}
array_pop($breadcrumb);
$term = $this
->getLeafTerm();
$this
->assertBreadcrumb($term
->urlInfo(), $breadcrumb, $term
->label());
$languages = \Drupal::languageManager()
->getLanguages();
$breadcrumb = array(
Url::fromRoute('<front>', [], [
'language' => $languages[$this->translateToLangcode],
])
->toString() => 'Home',
);
foreach ($this->terms as $term) {
$translated = $term
->getTranslation($this->translateToLangcode);
$url = $translated
->url('canonical', [
'language' => $languages[$this->translateToLangcode],
]);
$breadcrumb[$url] = $translated
->label();
}
array_pop($breadcrumb);
$term = $this
->getLeafTerm();
$translated = $term
->getTranslation($this->translateToLangcode);
$this
->assertBreadcrumb($translated
->urlInfo('canonical', [
'language' => $languages[$this->translateToLangcode],
]), $breadcrumb, $translated
->label());
}
protected function setUpTerms() {
$parent_vid = 0;
foreach ($this->termTranslationMap as $name => $translation) {
$term = $this
->createTerm($this->vocabulary, array(
'name' => $name,
'langcode' => $this->baseLangcode,
'parent' => $parent_vid,
));
$term
->addTranslation($this->translateToLangcode, array(
'name' => $translation,
));
$term
->save();
$parent_vid = $term
->id();
$this->terms[] = $term;
}
}
protected function getLeafTerm() {
return $this->terms[count($this->termTranslationMap) - 1];
}
}