5 * Contains \Drupal\linkit\Tests\Matchers\TermMatcherTest.
8 namespace Drupal\linkit\Tests\Matchers;
10 use Drupal\Component\Utility\Unicode;
11 use Drupal\Core\Language\LanguageInterface;
12 use Drupal\linkit\Tests\LinkitTestBase;
13 use Drupal\taxonomy\Entity\Vocabulary;
20 class TermMatcherTest extends LinkitTestBase {
27 public static $modules = ['taxonomy'];
30 * The matcher manager.
32 * @var \Drupal\linkit\MatcherManager
37 * Creates and saves a vocabulary.
40 * The vocabulary name.
42 * @return Vocabulary The new vocabulary object.
43 * The new vocabulary object.
45 private function createVocabulary($name) {
46 $vocabularyStorage = \Drupal::entityTypeManager()->getStorage('taxonomy_vocabulary');
47 $vocabulary = $vocabularyStorage->create([
49 'description' => $name,
50 'vid' => Unicode::strtolower($name),
51 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
58 * Creates and saves a new term with in vocabulary $vid.
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.
66 * @return \Drupal\taxonomy\Entity\Term
67 * The new taxonomy term object.
69 private function createTerm(Vocabulary $vocabulary, $values = array()) {
70 $filter_formats = filter_formats();
71 $format = array_pop($filter_formats);
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(),
81 'vid' => $vocabulary->id(),
82 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
91 protected function setUp() {
93 $this->drupalLogin($this->adminUser);
94 $this->manager = $this->container->get('plugin.manager.linkit.matcher');
96 $testing_vocabulary_1 = $this->createVocabulary('testing_vocabulary_1');
97 $testing_vocabulary_2 = $this->createVocabulary('testing_vocabulary_2');
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']);
108 * Tests term matcher with default configuration.
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');
118 * Tests term matcher with bundle filer.
120 function testTermMatcherWidthBundleFiler() {
121 /** @var \Drupal\linkit\MatcherInterface $plugin */
122 $plugin = $this->manager->createInstance('entity:taxonomy_term', [
125 'testing_vocabulary_1' => 'testing_vocabulary_1'
130 $matches = $plugin->getMatches('foo');
131 $this->assertEqual(3, count($matches), 'Correct number of matches');