3 namespace Drupal\Tests\token\Kernel;
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\language\Entity\ConfigurableLanguage;
11 * Tests taxonomy tokens.
15 class TaxonomyTest extends KernelTestBase {
24 public static $modules = ['taxonomy', 'text', 'language'];
29 public function setUp() {
32 $this->installEntitySchema('taxonomy_term');
34 // Create the default tags vocabulary.
35 $vocabulary = Vocabulary::create([
40 $this->vocab = $vocabulary;
44 * Test the additional taxonomy term tokens.
46 function testTaxonomyTokens() {
47 $root_term = $this->addTerm($this->vocab, ['name' => 'Root term', 'path' => ['alias' => '/root-term']]);
49 'url' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $root_term->id()], ['absolute' => TRUE])->toString(),
50 'url:absolute' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $root_term->id()], ['absolute' => TRUE])->toString(),
51 'url:relative' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $root_term->id()], ['absolute' => FALSE])->toString(),
52 'url:path' => '/root-term',
53 'url:unaliased:path' => "/taxonomy/term/{$root_term->id()}",
54 'edit-url' => Url::fromRoute('entity.taxonomy_term.edit_form', ['taxonomy_term' => $root_term->id()], ['absolute' => TRUE])->toString(),
56 'parents:count' => NULL,
57 'parents:keys' => NULL,
60 'url:alias' => '/root-term',
62 $this->assertTokens('term', ['term' => $root_term], $tokens);
64 $parent_term = $this->addTerm($this->vocab, ['name' => 'Parent term', 'parent' => $root_term->id()]);
66 'url' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $parent_term->id()], ['absolute' => TRUE])->toString(),
67 'url:absolute' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $parent_term->id()], ['absolute' => TRUE])->toString(),
68 'url:relative' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $parent_term->id()], ['absolute' => FALSE])->toString(),
69 'url:path' => "/taxonomy/term/{$parent_term->id()}",
70 'url:unaliased:path' => "/taxonomy/term/{$parent_term->id()}",
71 'edit-url' => Url::fromRoute('entity.taxonomy_term.edit_form', ['taxonomy_term' => $parent_term->id()], ['absolute' => TRUE])->toString(),
72 'parents' => 'Root term',
74 'parents:keys' => $root_term->id(),
75 'root' => $root_term->label(),
76 'root:tid' => $root_term->id(),
78 'url:alias' => "/taxonomy/term/{$parent_term->id()}",
80 $this->assertTokens('term', ['term' => $parent_term], $tokens);
82 $term = $this->addTerm($this->vocab, ['name' => 'Test term', 'parent' => $parent_term->id()]);
84 'parents' => 'Root term, Parent term',
86 'parents:keys' => implode(', ', [$root_term->id(), $parent_term->id()]),
88 $this->assertTokens('term', ['term' => $term], $tokens);
92 * Test the additional vocabulary tokens.
94 function testVocabularyTokens() {
95 $vocabulary = $this->vocab;
97 'machine-name' => 'tags',
98 'edit-url' => Url::fromRoute('entity.taxonomy_vocabulary.edit_form', ['taxonomy_vocabulary' => $vocabulary->id()], ['absolute' => TRUE])->toString(),
100 $this->assertTokens('vocabulary', ['vocabulary' => $vocabulary], $tokens);
103 function addVocabulary(array $vocabulary = []) {
105 'name' => mb_strtolower($this->randomMachineName(5)),
106 'nodes' => ['article' => 'article'],
108 $vocabulary = Vocabulary::create($vocabulary)->save();
112 function addTerm($vocabulary, array $term = []) {
114 'name' => mb_strtolower($this->randomMachineName(5)),
115 'vid' => $vocabulary->id(),
117 $term = Term::create($term);
123 * Test the multilingual terms.
125 function testMultilingualTerms() {
126 // Add a second language.
127 $language = ConfigurableLanguage::createFromLangcode('de');
130 // Create an english parent term and add a german translation for it.
131 $parent_term = $this->addTerm($this->vocab, [
132 'name' => 'english-parent-term',
135 $parent_term->addTranslation('de', [
136 'name' => 'german-parent-term',
139 // Create a term related to the parent term.
140 $child_term = $this->addTerm($this->vocab, [
141 'name' => 'english-child-term',
143 'parent' => $parent_term->id(),
145 $child_term->addTranslation('de', [
146 'name' => 'german-child-term',
149 // Expect the parent term to be in the specified language.
150 $this->assertTokens('term', ['term' => $child_term], ['parents' => 'german-parent-term'], ['langcode' => 'de']);
151 $this->assertTokens('term', ['term' => $child_term], ['root' => 'german-parent-term'], ['langcode' => 'de']);