Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\taxonomy\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Utility\Unicode;
|
Chris@0
|
6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
|
Chris@0
|
7 use Drupal\Core\Entity\Entity\EntityViewDisplay;
|
Chris@0
|
8 use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
Chris@0
|
9 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
10 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Tests the autocomplete implementation of the taxonomy class.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group taxonomy
|
Chris@0
|
16 */
|
Chris@0
|
17 class TermAutocompleteTest extends TaxonomyTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * The vocabulary.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\taxonomy\Entity\Vocabulary
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $vocabulary;
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * The field to add to the content type for the taxonomy terms.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var string
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $fieldName;
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * The admin user.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var \Drupal\user\Entity\User
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $adminUser;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * The autocomplete URL to call.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var string
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $autocompleteUrl;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * The term IDs indexed by term names.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @var array
|
Chris@0
|
51 */
|
Chris@0
|
52 protected $termIds;
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 protected function setUp() {
|
Chris@0
|
58 parent::setUp();
|
Chris@0
|
59
|
Chris@0
|
60 // Create a vocabulary.
|
Chris@0
|
61 $this->vocabulary = $this->createVocabulary();
|
Chris@0
|
62
|
Chris@0
|
63 // Create 11 terms, which have some sub-string in common, in a
|
Chris@0
|
64 // non-alphabetical order, so that we will have more than 10 matches later
|
Chris@0
|
65 // when we test the correct number of results is returned, and we can test
|
Chris@0
|
66 // the order of the results. The location of the sub-string to match varies
|
Chris@0
|
67 // also, since it should not be necessary to start with the sub-string to
|
Chris@0
|
68 // match it. Save term IDs to reuse later.
|
Chris@0
|
69 $termNames = [
|
Chris@0
|
70 'aaa 20 bbb',
|
Chris@0
|
71 'aaa 70 bbb',
|
Chris@0
|
72 'aaa 10 bbb',
|
Chris@0
|
73 'aaa 12 bbb',
|
Chris@0
|
74 'aaa 40 bbb',
|
Chris@0
|
75 'aaa 11 bbb',
|
Chris@0
|
76 'aaa 30 bbb',
|
Chris@0
|
77 'aaa 50 bbb',
|
Chris@0
|
78 'aaa 80',
|
Chris@0
|
79 'aaa 90',
|
Chris@0
|
80 'bbb 60 aaa',
|
Chris@0
|
81 ];
|
Chris@0
|
82 foreach ($termNames as $termName) {
|
Chris@0
|
83 $term = $this->createTerm($this->vocabulary, ['name' => $termName]);
|
Chris@0
|
84 $this->termIds[$termName] = $term->id();
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 // Create a taxonomy_term_reference field on the article Content Type that
|
Chris@0
|
88 // uses a taxonomy_autocomplete widget.
|
Chris@0
|
89 $this->fieldName = Unicode::strtolower($this->randomMachineName());
|
Chris@0
|
90 FieldStorageConfig::create([
|
Chris@0
|
91 'field_name' => $this->fieldName,
|
Chris@0
|
92 'entity_type' => 'node',
|
Chris@0
|
93 'type' => 'entity_reference',
|
Chris@0
|
94 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
|
Chris@0
|
95 'settings' => [
|
Chris@0
|
96 'target_type' => 'taxonomy_term',
|
Chris@0
|
97 ],
|
Chris@0
|
98 ])->save();
|
Chris@0
|
99 FieldConfig::create([
|
Chris@0
|
100 'field_name' => $this->fieldName,
|
Chris@0
|
101 'bundle' => 'article',
|
Chris@0
|
102 'entity_type' => 'node',
|
Chris@0
|
103 'settings' => [
|
Chris@0
|
104 'handler' => 'default',
|
Chris@0
|
105 'handler_settings' => [
|
Chris@0
|
106 // Restrict selection of terms to a single vocabulary.
|
Chris@0
|
107 'target_bundles' => [
|
Chris@0
|
108 $this->vocabulary->id() => $this->vocabulary->id(),
|
Chris@0
|
109 ],
|
Chris@0
|
110 ],
|
Chris@0
|
111 ],
|
Chris@0
|
112 ])->save();
|
Chris@0
|
113 EntityFormDisplay::load('node.article.default')
|
Chris@0
|
114 ->setComponent($this->fieldName, [
|
Chris@0
|
115 'type' => 'entity_reference_autocomplete',
|
Chris@0
|
116 ])
|
Chris@0
|
117 ->save();
|
Chris@0
|
118 EntityViewDisplay::load('node.article.default')
|
Chris@0
|
119 ->setComponent($this->fieldName, [
|
Chris@0
|
120 'type' => 'entity_reference_label',
|
Chris@0
|
121 ])
|
Chris@0
|
122 ->save();
|
Chris@0
|
123
|
Chris@0
|
124 // Create a user and then login.
|
Chris@0
|
125 $this->adminUser = $this->drupalCreateUser(['create article content']);
|
Chris@0
|
126 $this->drupalLogin($this->adminUser);
|
Chris@0
|
127
|
Chris@0
|
128 // Retrieve the autocomplete url.
|
Chris@0
|
129 $this->drupalGet('node/add/article');
|
Chris@0
|
130 $result = $this->xpath('//input[@name="' . $this->fieldName . '[0][target_id]"]');
|
Chris@0
|
131 $this->autocompleteUrl = $this->getAbsoluteUrl($result[0]['data-autocomplete-path']);
|
Chris@0
|
132 }
|
Chris@0
|
133
|
Chris@0
|
134 /**
|
Chris@0
|
135 * Tests that the autocomplete method returns the good number of results.
|
Chris@0
|
136 *
|
Chris@0
|
137 * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete()
|
Chris@0
|
138 */
|
Chris@0
|
139 public function testAutocompleteCountResults() {
|
Chris@0
|
140 // Test that no matching term found.
|
Chris@0
|
141 $data = $this->drupalGetJSON(
|
Chris@0
|
142 $this->autocompleteUrl,
|
Chris@0
|
143 ['query' => ['q' => 'zzz']]
|
Chris@0
|
144 );
|
Chris@0
|
145 $this->assertTrue(empty($data), 'Autocomplete returned no results');
|
Chris@0
|
146
|
Chris@0
|
147 // Test that only one matching term found, when only one matches.
|
Chris@0
|
148 $data = $this->drupalGetJSON(
|
Chris@0
|
149 $this->autocompleteUrl,
|
Chris@0
|
150 ['query' => ['q' => 'aaa 10']]
|
Chris@0
|
151 );
|
Chris@0
|
152 $this->assertEqual(1, count($data), 'Autocomplete returned 1 result');
|
Chris@0
|
153
|
Chris@0
|
154 // Test the correct number of matches when multiple are partial matches.
|
Chris@0
|
155 $data = $this->drupalGetJSON(
|
Chris@0
|
156 $this->autocompleteUrl,
|
Chris@0
|
157 ['query' => ['q' => 'aaa 1']]
|
Chris@0
|
158 );
|
Chris@0
|
159 $this->assertEqual(3, count($data), 'Autocomplete returned 3 results');
|
Chris@0
|
160
|
Chris@0
|
161 // Tests that only 10 results are returned, even if there are more than 10
|
Chris@0
|
162 // matches.
|
Chris@0
|
163 $data = $this->drupalGetJSON(
|
Chris@0
|
164 $this->autocompleteUrl,
|
Chris@0
|
165 ['query' => ['q' => 'aaa']]
|
Chris@0
|
166 );
|
Chris@0
|
167 $this->assertEqual(10, count($data), 'Autocomplete returned only 10 results (for over 10 matches)');
|
Chris@0
|
168 }
|
Chris@0
|
169
|
Chris@0
|
170 /**
|
Chris@0
|
171 * Tests that the autocomplete method returns properly ordered results.
|
Chris@0
|
172 *
|
Chris@0
|
173 * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete()
|
Chris@0
|
174 */
|
Chris@0
|
175 public function testAutocompleteOrderedResults() {
|
Chris@0
|
176 $expectedResults = [
|
Chris@0
|
177 'aaa 10 bbb',
|
Chris@0
|
178 'aaa 11 bbb',
|
Chris@0
|
179 'aaa 12 bbb',
|
Chris@0
|
180 'aaa 20 bbb',
|
Chris@0
|
181 'aaa 30 bbb',
|
Chris@0
|
182 'aaa 40 bbb',
|
Chris@0
|
183 'aaa 50 bbb',
|
Chris@0
|
184 'aaa 70 bbb',
|
Chris@0
|
185 'bbb 60 aaa',
|
Chris@0
|
186 ];
|
Chris@0
|
187 // Build $expected to match the autocomplete results.
|
Chris@0
|
188 $expected = [];
|
Chris@0
|
189 foreach ($expectedResults as $termName) {
|
Chris@0
|
190 $expected[] = [
|
Chris@0
|
191 'value' => $termName . ' (' . $this->termIds[$termName] . ')',
|
Chris@0
|
192 'label' => $termName
|
Chris@0
|
193 ];
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 $data = $this->drupalGetJSON(
|
Chris@0
|
197 $this->autocompleteUrl,
|
Chris@0
|
198 ['query' => ['q' => 'bbb']]
|
Chris@0
|
199 );
|
Chris@0
|
200
|
Chris@0
|
201 $this->assertIdentical($expected, $data);
|
Chris@0
|
202 }
|
Chris@0
|
203
|
Chris@0
|
204 }
|