3 namespace Drupal\taxonomy\Tests;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
7 use Drupal\Core\Entity\Entity\EntityViewDisplay;
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
13 * Tests the autocomplete implementation of the taxonomy class.
17 class TermAutocompleteTest extends TaxonomyTestBase {
22 * @var \Drupal\taxonomy\Entity\Vocabulary
24 protected $vocabulary;
27 * The field to add to the content type for the taxonomy terms.
36 * @var \Drupal\user\Entity\User
41 * The autocomplete URL to call.
45 protected $autocompleteUrl;
48 * The term IDs indexed by term names.
57 protected function setUp() {
60 // Create a vocabulary.
61 $this->vocabulary = $this->createVocabulary();
63 // Create 11 terms, which have some sub-string in common, in a
64 // non-alphabetical order, so that we will have more than 10 matches later
65 // when we test the correct number of results is returned, and we can test
66 // the order of the results. The location of the sub-string to match varies
67 // also, since it should not be necessary to start with the sub-string to
68 // match it. Save term IDs to reuse later.
82 foreach ($termNames as $termName) {
83 $term = $this->createTerm($this->vocabulary, ['name' => $termName]);
84 $this->termIds[$termName] = $term->id();
87 // Create a taxonomy_term_reference field on the article Content Type that
88 // uses a taxonomy_autocomplete widget.
89 $this->fieldName = Unicode::strtolower($this->randomMachineName());
90 FieldStorageConfig::create([
91 'field_name' => $this->fieldName,
92 'entity_type' => 'node',
93 'type' => 'entity_reference',
94 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
96 'target_type' => 'taxonomy_term',
100 'field_name' => $this->fieldName,
101 'bundle' => 'article',
102 'entity_type' => 'node',
104 'handler' => 'default',
105 'handler_settings' => [
106 // Restrict selection of terms to a single vocabulary.
107 'target_bundles' => [
108 $this->vocabulary->id() => $this->vocabulary->id(),
113 EntityFormDisplay::load('node.article.default')
114 ->setComponent($this->fieldName, [
115 'type' => 'entity_reference_autocomplete',
118 EntityViewDisplay::load('node.article.default')
119 ->setComponent($this->fieldName, [
120 'type' => 'entity_reference_label',
124 // Create a user and then login.
125 $this->adminUser = $this->drupalCreateUser(['create article content']);
126 $this->drupalLogin($this->adminUser);
128 // Retrieve the autocomplete url.
129 $this->drupalGet('node/add/article');
130 $result = $this->xpath('//input[@name="' . $this->fieldName . '[0][target_id]"]');
131 $this->autocompleteUrl = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']);
135 * Tests that the autocomplete method returns the good number of results.
137 * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete()
139 public function testAutocompleteCountResults() {
140 // Test that no matching term found.
141 $data = $this->drupalGetJSON(
142 $this->autocompleteUrl,
143 ['query' => ['q' => 'zzz']]
145 $this->assertTrue(empty($data), 'Autocomplete returned no results');
147 // Test that only one matching term found, when only one matches.
148 $data = $this->drupalGetJSON(
149 $this->autocompleteUrl,
150 ['query' => ['q' => 'aaa 10']]
152 $this->assertEqual(1, count($data), 'Autocomplete returned 1 result');
154 // Test the correct number of matches when multiple are partial matches.
155 $data = $this->drupalGetJSON(
156 $this->autocompleteUrl,
157 ['query' => ['q' => 'aaa 1']]
159 $this->assertEqual(3, count($data), 'Autocomplete returned 3 results');
161 // Tests that only 10 results are returned, even if there are more than 10
163 $data = $this->drupalGetJSON(
164 $this->autocompleteUrl,
165 ['query' => ['q' => 'aaa']]
167 $this->assertEqual(10, count($data), 'Autocomplete returned only 10 results (for over 10 matches)');
171 * Tests that the autocomplete method returns properly ordered results.
173 * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete()
175 public function testAutocompleteOrderedResults() {
187 // Build $expected to match the autocomplete results.
189 foreach ($expectedResults as $termName) {
191 'value' => $termName . ' (' . $this->termIds[$termName] . ')',
196 $data = $this->drupalGetJSON(
197 $this->autocompleteUrl,
198 ['query' => ['q' => 'bbb']]
201 $this->assertIdentical($expected, $data);