3 namespace Drupal\Tests\token\Kernel;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\language\Entity\ConfigurableLanguage;
11 * Tests taxonomy tokens.
15 class TaxonomyTest extends KernelTestBase {
23 public static $modules = array('taxonomy', 'text', 'language');
28 public function setUp() {
31 $this->installEntitySchema('taxonomy_term');
33 // Create the default tags vocabulary.
34 $vocabulary = Vocabulary::create([
39 $this->vocab = $vocabulary;
43 * Test the additional taxonomy term tokens.
45 function testTaxonomyTokens() {
46 $root_term = $this->addTerm($this->vocab, array('name' => 'Root term', 'path' => array('alias' => '/root-term')));
48 'url' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $root_term->id()], array('absolute' => TRUE))->toString(),
49 'url:absolute' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $root_term->id()], array('absolute' => TRUE))->toString(),
50 'url:relative' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $root_term->id()], array('absolute' => FALSE))->toString(),
51 'url:path' => '/root-term',
52 'url:unaliased:path' => "/taxonomy/term/{$root_term->id()}",
53 'edit-url' => Url::fromRoute('entity.taxonomy_term.edit_form', ['taxonomy_term' => $root_term->id()], array('absolute' => TRUE))->toString(),
55 'parents:count' => NULL,
56 'parents:keys' => NULL,
59 'url:alias' => '/root-term',
61 $this->assertTokens('term', array('term' => $root_term), $tokens);
63 $parent_term = $this->addTerm($this->vocab, array('name' => 'Parent term', 'parent' => $root_term->id()));
65 'url' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $parent_term->id()], array('absolute' => TRUE))->toString(),
66 'url:absolute' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $parent_term->id()], array('absolute' => TRUE))->toString(),
67 'url:relative' => Url::fromRoute('entity.taxonomy_term.canonical', ['taxonomy_term' => $parent_term->id()], array('absolute' => FALSE))->toString(),
68 'url:path' => "/taxonomy/term/{$parent_term->id()}",
69 'url:unaliased:path' => "/taxonomy/term/{$parent_term->id()}",
70 'edit-url' => Url::fromRoute('entity.taxonomy_term.edit_form', ['taxonomy_term' => $parent_term->id()], array('absolute' => TRUE))->toString(),
71 'parents' => 'Root term',
73 'parents:keys' => $root_term->id(),
74 'root' => $root_term->label(),
75 'root:tid' => $root_term->id(),
77 'url:alias' => "/taxonomy/term/{$parent_term->id()}",
79 $this->assertTokens('term', array('term' => $parent_term), $tokens);
81 $term = $this->addTerm($this->vocab, array('name' => 'Test term', 'parent' => $parent_term->id()));
83 'parents' => 'Root term, Parent term',
85 'parents:keys' => implode(', ', array($root_term->id(), $parent_term->id())),
87 $this->assertTokens('term', array('term' => $term), $tokens);
91 * Test the additional vocabulary tokens.
93 function testVocabularyTokens() {
94 $vocabulary = $this->vocab;
96 'machine-name' => 'tags',
97 'edit-url' => Url::fromRoute('entity.taxonomy_vocabulary.edit_form', ['taxonomy_vocabulary' => $vocabulary->id()], array('absolute' => TRUE))->toString(),
99 $this->assertTokens('vocabulary', array('vocabulary' => $vocabulary), $tokens);
102 function addVocabulary(array $vocabulary = array()) {
103 $vocabulary += array(
104 'name' => Unicode::strtolower($this->randomMachineName(5)),
105 'nodes' => array('article' => 'article'),
107 $vocabulary = entity_create('taxonomy_vocabulary', $vocabulary)->save();
111 function addTerm($vocabulary, array $term = array()) {
113 'name' => Unicode::strtolower($this->randomMachineName(5)),
114 'vid' => $vocabulary->id(),
116 $term = entity_create('taxonomy_term', $term);
122 * Test the multilingual terms.
124 function testMultilingualTerms() {
125 // Add a second language.
126 $language = ConfigurableLanguage::createFromLangcode('de');
129 // Create an english parent term and add a german translation for it.
130 $parent_term = $this->addTerm($this->vocab, [
131 'name' => 'english-parent-term',
134 $parent_term->addTranslation('de', [
135 'name' => 'german-parent-term',
138 // Create a term related to the parent term.
139 $child_term = $this->addTerm($this->vocab, [
140 'name' => 'english-child-term',
142 'parent' => $parent_term->id(),
144 $child_term->addTranslation('de', [
145 'name' => 'german-child-term',
148 // Expect the parent term to be in the specified language.
149 $this->assertTokens('term', array('term' => $child_term), ['parents' => 'german-parent-term'], ['langcode' => 'de']);