Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / token / tests / src / Kernel / TaxonomyTest.php
1 <?php
2
3 namespace Drupal\Tests\token\Kernel;
4
5 use Drupal\taxonomy\Entity\Term;
6 use Drupal\taxonomy\Entity\Vocabulary;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\Core\Url;
9
10 /**
11  * Tests taxonomy tokens.
12  *
13  * @group token
14  */
15 class TaxonomyTest extends KernelTestBase {
16
17   protected $vocab;
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = ['taxonomy', 'text', 'language'];
25
26   /**
27    * {@inheritdoc}
28    */
29   public function setUp() {
30     parent::setUp();
31
32     $this->installEntitySchema('taxonomy_term');
33
34     // Create the default tags vocabulary.
35     $vocabulary = Vocabulary::create([
36       'name' => 'Tags',
37       'vid' => 'tags',
38     ]);
39     $vocabulary->save();
40     $this->vocab = $vocabulary;
41   }
42
43   /**
44    * Test the additional taxonomy term tokens.
45    */
46   function testTaxonomyTokens() {
47     $root_term = $this->addTerm($this->vocab, ['name' => 'Root term', 'path' => ['alias' => '/root-term']]);
48     $tokens = [
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(),
55       'parents' => NULL,
56       'parents:count' => NULL,
57       'parents:keys' => NULL,
58       'root' => NULL,
59       // Deprecated tokens
60       'url:alias' => '/root-term',
61     ];
62     $this->assertTokens('term', ['term' => $root_term], $tokens);
63
64     $parent_term = $this->addTerm($this->vocab, ['name' => 'Parent term', 'parent' => $root_term->id()]);
65     $tokens = [
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',
73       'parents:count' => 1,
74       'parents:keys' => $root_term->id(),
75       'root' => $root_term->label(),
76       'root:tid' => $root_term->id(),
77       // Deprecated tokens
78       'url:alias' => "/taxonomy/term/{$parent_term->id()}",
79     ];
80     $this->assertTokens('term', ['term' => $parent_term], $tokens);
81
82     $term = $this->addTerm($this->vocab, ['name' => 'Test term', 'parent' => $parent_term->id()]);
83     $tokens = [
84       'parents' => 'Root term, Parent term',
85       'parents:count' => 2,
86       'parents:keys' => implode(', ', [$root_term->id(), $parent_term->id()]),
87     ];
88     $this->assertTokens('term', ['term' => $term], $tokens);
89   }
90
91   /**
92    * Test the additional vocabulary tokens.
93    */
94   function testVocabularyTokens() {
95     $vocabulary = $this->vocab;
96     $tokens = [
97       'machine-name' => 'tags',
98       'edit-url' => Url::fromRoute('entity.taxonomy_vocabulary.edit_form', ['taxonomy_vocabulary' => $vocabulary->id()], ['absolute' => TRUE])->toString(),
99     ];
100     $this->assertTokens('vocabulary', ['vocabulary' => $vocabulary], $tokens);
101   }
102
103   function addVocabulary(array $vocabulary = []) {
104     $vocabulary += [
105       'name' => mb_strtolower($this->randomMachineName(5)),
106       'nodes' => ['article' => 'article'],
107     ];
108     $vocabulary = Vocabulary::create($vocabulary)->save();
109     return $vocabulary;
110   }
111
112   function addTerm($vocabulary, array $term = []) {
113     $term += [
114       'name' => mb_strtolower($this->randomMachineName(5)),
115       'vid' => $vocabulary->id(),
116     ];
117     $term = Term::create($term);
118     $term->save();
119     return $term;
120   }
121
122   /**
123    * Test the multilingual terms.
124    */
125   function testMultilingualTerms() {
126     // Add a second language.
127     $language = ConfigurableLanguage::createFromLangcode('de');
128     $language->save();
129
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',
133       'langcode' => 'en',
134     ]);
135     $parent_term->addTranslation('de', [
136       'name' => 'german-parent-term',
137     ])->save();
138
139     // Create a term related to the parent term.
140     $child_term = $this->addTerm($this->vocab, [
141       'name' => 'english-child-term',
142       'langcode' => 'en',
143       'parent' => $parent_term->id(),
144     ]);
145     $child_term->addTranslation('de', [
146       'name' => 'german-child-term',
147     ])->save();
148
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']);
152   }
153
154 }