Version 1
[yaffs-website] / web / modules / contrib / linkit / src / Tests / Matchers / TermMatcherTest.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Tests\Matchers\TermMatcherTest.
6  */
7
8 namespace Drupal\linkit\Tests\Matchers;
9
10 use Drupal\Component\Utility\Unicode;
11 use Drupal\Core\Language\LanguageInterface;
12 use Drupal\linkit\Tests\LinkitTestBase;
13 use Drupal\taxonomy\Entity\Vocabulary;
14
15 /**
16  * Tests term matcher.
17  *
18  * @group linkit
19  */
20 class TermMatcherTest extends LinkitTestBase {
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['taxonomy'];
28
29   /**
30    * The matcher manager.
31    *
32    * @var \Drupal\linkit\MatcherManager
33    */
34   protected $manager;
35
36   /**
37    * Creates and saves a vocabulary.
38    *
39    * @param string $name
40    *   The vocabulary name.
41    *
42    * @return Vocabulary The new vocabulary object.
43    * The new vocabulary object.
44    */
45   private function createVocabulary($name) {
46     $vocabularyStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary');
47     $vocabulary = $vocabularyStorage->create([
48       'name' => $name,
49       'description' => $name,
50       'vid' => Unicode::strtolower($name),
51       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
52     ]);
53     $vocabulary->save();
54     return $vocabulary;
55   }
56
57   /**
58    * Creates and saves a new term with in vocabulary $vid.
59    *
60    * @param \Drupal\taxonomy\Entity\Vocabulary $vocabulary
61    *   The vocabulary object.
62    * @param array $values
63    *   (optional) An array of values to set, keyed by property name. If the
64    *   entity type has bundles, the bundle key has to be specified.
65    *
66    * @return \Drupal\taxonomy\Entity\Term
67    *   The new taxonomy term object.
68    */
69   private function createTerm(Vocabulary $vocabulary, $values = array()) {
70     $filter_formats = filter_formats();
71     $format = array_pop($filter_formats);
72
73     $termStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
74     $term = $termStorage->create($values + array(
75         'name' => $this->randomMachineName(),
76         'description' => array(
77           'value' => $this->randomMachineName(),
78           // Use the first available text format.
79           'format' => $format->id(),
80         ),
81         'vid' => $vocabulary->id(),
82         'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
83       ));
84     $term->save();
85     return $term;
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   protected function setUp() {
92     parent::setUp();
93     $this->drupalLogin($this->adminUser);
94     $this->manager = $this->container->get('plugin.manager.linkit.matcher');
95
96     $testing_vocabulary_1 = $this->createVocabulary('testing_vocabulary_1');
97     $testing_vocabulary_2 = $this->createVocabulary('testing_vocabulary_2');
98
99     $this->createTerm($testing_vocabulary_1, ['name' => 'foo_bar']);
100     $this->createTerm($testing_vocabulary_1, ['name' => 'foo_baz']);
101     $this->createTerm($testing_vocabulary_1, ['name' => 'foo_foo']);
102     $this->createTerm($testing_vocabulary_1, ['name' => 'bar']);
103     $this->createTerm($testing_vocabulary_2, ['name' => 'foo_bar']);
104     $this->createTerm($testing_vocabulary_2, ['name' => 'foo_baz']);
105   }
106
107   /**
108    * Tests term matcher with default configuration.
109    */
110   function testTermMatcherWidthDefaultConfiguration() {
111     /** @var \Drupal\linkit\MatcherInterface $plugin */
112     $plugin = $this->manager->createInstance('entity:taxonomy_term', []);
113     $matches = $plugin->getMatches('foo');
114     $this->assertEqual(5, count($matches), 'Correct number of matches');
115   }
116
117   /**
118    * Tests term matcher with bundle filer.
119    */
120   function testTermMatcherWidthBundleFiler() {
121     /** @var \Drupal\linkit\MatcherInterface $plugin */
122     $plugin = $this->manager->createInstance('entity:taxonomy_term', [
123       'settings' => [
124         'bundles' => [
125           'testing_vocabulary_1' => 'testing_vocabulary_1'
126         ],
127       ],
128     ]);
129
130     $matches = $plugin->getMatches('foo');
131     $this->assertEqual(3, count($matches), 'Correct number of matches');
132   }
133
134 }