View source
<?php
namespace Drupal\Tests\linkit\Kernel\Matchers;
use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase;
class TermMatcherTest extends LinkitKernelTestBase {
public static $modules = [
'taxonomy',
];
protected $manager;
protected function setUp() {
parent::setUp();
$this
->createUser();
\Drupal::currentUser()
->setAccount($this
->createUser([], [
'access content',
]));
$this
->installEntitySchema('taxonomy_term');
$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',
]);
}
public function testTermMatcherWidthDefaultConfiguration() {
$plugin = $this->manager
->createInstance('entity:taxonomy_term', []);
$suggestions = $plugin
->execute('foo');
$this
->assertEquals(5, count($suggestions
->getSuggestions()), 'Correct number of suggestions');
}
public function testTermMatcherWidthBundleFiler() {
$plugin = $this->manager
->createInstance('entity:taxonomy_term', [
'settings' => [
'bundles' => [
'testing_vocabulary_1' => 'testing_vocabulary_1',
],
],
]);
$suggestions = $plugin
->execute('foo');
$this
->assertEquals(3, count($suggestions
->getSuggestions()), 'Correct number of suggestions');
}
public function testTermMatcherWidthMetadataTokens() {
$plugin = $this->manager
->createInstance('entity:taxonomy_term', [
'settings' => [
'metadata' => '[term:tid] [term:field_with_no_value]',
],
]);
$suggestionCollection = $plugin
->execute('Lorem');
$suggestions = $suggestionCollection
->getSuggestions();
foreach ($suggestions as $suggestion) {
$this
->assertStringNotContainsString('[term:nid]', $suggestion
->getDescription(), 'Raw token "[term:nid]" is not present in the description');
$this
->assertStringNotContainsString('[term:field_with_no_value]', $suggestion
->getDescription(), 'Raw token "[term:field_with_no_value]" is not present in the description');
}
}
private function createVocabulary($name) {
$vocabularyStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_vocabulary');
$vocabulary = $vocabularyStorage
->create([
'name' => $name,
'description' => $name,
'vid' => mb_strtolower($name),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$vocabulary
->save();
return $vocabulary;
}
private function createTerm(VocabularyInterface $vocabulary, array $values = []) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$termStorage = \Drupal::entityTypeManager()
->getStorage('taxonomy_term');
$term = $termStorage
->create($values + [
'name' => $this
->randomMachineName(),
'description' => [
'value' => $this
->randomMachineName(),
'format' => $format
->id(),
],
'vid' => $vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
]);
$term
->save();
return $term;
}
}