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