View source
<?php
namespace Drupal\linkit\Tests\Matchers;
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\linkit\Tests\LinkitTestBase;
use Drupal\taxonomy\Entity\Vocabulary;
class TermMatcherTest extends LinkitTestBase {
public static $modules = [
'taxonomy',
];
protected $manager;
private function createVocabulary($name) {
$vocabularyStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_vocabulary');
$vocabulary = $vocabularyStorage
->create([
'name' => $name,
'description' => $name,
'vid' => Unicode::strtolower($name),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$vocabulary
->save();
return $vocabulary;
}
private function createTerm(Vocabulary $vocabulary, $values = array()) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$termStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$term = $termStorage
->create($values + array(
'name' => $this
->randomMachineName(),
'description' => array(
'value' => $this
->randomMachineName(),
'format' => $format
->id(),
),
'vid' => $vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
));
$term
->save();
return $term;
}
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this->adminUser);
$this->manager = $this->container
->get('plugin.manager.linkit.matcher');
$testing_vocabulary_1 = $this
->createVocabulary('testing_vocabulary_1');
$testing_vocabulary_2 = $this
->createVocabulary('testing_vocabulary_2');
$this
->createTerm($testing_vocabulary_1, [
'name' => 'foo_bar',
]);
$this
->createTerm($testing_vocabulary_1, [
'name' => 'foo_baz',
]);
$this
->createTerm($testing_vocabulary_1, [
'name' => 'foo_foo',
]);
$this
->createTerm($testing_vocabulary_1, [
'name' => 'bar',
]);
$this
->createTerm($testing_vocabulary_2, [
'name' => 'foo_bar',
]);
$this
->createTerm($testing_vocabulary_2, [
'name' => 'foo_baz',
]);
}
function testTermMatcherWidthDefaultConfiguration() {
$plugin = $this->manager
->createInstance('entity:taxonomy_term', []);
$matches = $plugin
->getMatches('foo');
$this
->assertEqual(5, count($matches), 'Correct number of matches');
}
function testTermMatcherWidthBundleFiler() {
$plugin = $this->manager
->createInstance('entity:taxonomy_term', [
'settings' => [
'bundles' => [
'testing_vocabulary_1' => 'testing_vocabulary_1',
],
],
]);
$matches = $plugin
->getMatches('foo');
$this
->assertEqual(3, count($matches), 'Correct number of matches');
}
}