Mercurial > hg > isophonics-drupal-site
comparison core/modules/taxonomy/src/Tests/TermAutocompleteTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\taxonomy\Tests; | |
4 | |
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; | |
11 | |
12 /** | |
13 * Tests the autocomplete implementation of the taxonomy class. | |
14 * | |
15 * @group taxonomy | |
16 */ | |
17 class TermAutocompleteTest extends TaxonomyTestBase { | |
18 | |
19 /** | |
20 * The vocabulary. | |
21 * | |
22 * @var \Drupal\taxonomy\Entity\Vocabulary | |
23 */ | |
24 protected $vocabulary; | |
25 | |
26 /** | |
27 * The field to add to the content type for the taxonomy terms. | |
28 * | |
29 * @var string | |
30 */ | |
31 protected $fieldName; | |
32 | |
33 /** | |
34 * The admin user. | |
35 * | |
36 * @var \Drupal\user\Entity\User | |
37 */ | |
38 protected $adminUser; | |
39 | |
40 /** | |
41 * The autocomplete URL to call. | |
42 * | |
43 * @var string | |
44 */ | |
45 protected $autocompleteUrl; | |
46 | |
47 /** | |
48 * The term IDs indexed by term names. | |
49 * | |
50 * @var array | |
51 */ | |
52 protected $termIds; | |
53 | |
54 /** | |
55 * {@inheritdoc} | |
56 */ | |
57 protected function setUp() { | |
58 parent::setUp(); | |
59 | |
60 // Create a vocabulary. | |
61 $this->vocabulary = $this->createVocabulary(); | |
62 | |
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. | |
69 $termNames = [ | |
70 'aaa 20 bbb', | |
71 'aaa 70 bbb', | |
72 'aaa 10 bbb', | |
73 'aaa 12 bbb', | |
74 'aaa 40 bbb', | |
75 'aaa 11 bbb', | |
76 'aaa 30 bbb', | |
77 'aaa 50 bbb', | |
78 'aaa 80', | |
79 'aaa 90', | |
80 'bbb 60 aaa', | |
81 ]; | |
82 foreach ($termNames as $termName) { | |
83 $term = $this->createTerm($this->vocabulary, ['name' => $termName]); | |
84 $this->termIds[$termName] = $term->id(); | |
85 } | |
86 | |
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, | |
95 'settings' => [ | |
96 'target_type' => 'taxonomy_term', | |
97 ], | |
98 ])->save(); | |
99 FieldConfig::create([ | |
100 'field_name' => $this->fieldName, | |
101 'bundle' => 'article', | |
102 'entity_type' => 'node', | |
103 'settings' => [ | |
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(), | |
109 ], | |
110 ], | |
111 ], | |
112 ])->save(); | |
113 EntityFormDisplay::load('node.article.default') | |
114 ->setComponent($this->fieldName, [ | |
115 'type' => 'entity_reference_autocomplete', | |
116 ]) | |
117 ->save(); | |
118 EntityViewDisplay::load('node.article.default') | |
119 ->setComponent($this->fieldName, [ | |
120 'type' => 'entity_reference_label', | |
121 ]) | |
122 ->save(); | |
123 | |
124 // Create a user and then login. | |
125 $this->adminUser = $this->drupalCreateUser(['create article content']); | |
126 $this->drupalLogin($this->adminUser); | |
127 | |
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']); | |
132 } | |
133 | |
134 /** | |
135 * Tests that the autocomplete method returns the good number of results. | |
136 * | |
137 * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete() | |
138 */ | |
139 public function testAutocompleteCountResults() { | |
140 // Test that no matching term found. | |
141 $data = $this->drupalGetJSON( | |
142 $this->autocompleteUrl, | |
143 ['query' => ['q' => 'zzz']] | |
144 ); | |
145 $this->assertTrue(empty($data), 'Autocomplete returned no results'); | |
146 | |
147 // Test that only one matching term found, when only one matches. | |
148 $data = $this->drupalGetJSON( | |
149 $this->autocompleteUrl, | |
150 ['query' => ['q' => 'aaa 10']] | |
151 ); | |
152 $this->assertEqual(1, count($data), 'Autocomplete returned 1 result'); | |
153 | |
154 // Test the correct number of matches when multiple are partial matches. | |
155 $data = $this->drupalGetJSON( | |
156 $this->autocompleteUrl, | |
157 ['query' => ['q' => 'aaa 1']] | |
158 ); | |
159 $this->assertEqual(3, count($data), 'Autocomplete returned 3 results'); | |
160 | |
161 // Tests that only 10 results are returned, even if there are more than 10 | |
162 // matches. | |
163 $data = $this->drupalGetJSON( | |
164 $this->autocompleteUrl, | |
165 ['query' => ['q' => 'aaa']] | |
166 ); | |
167 $this->assertEqual(10, count($data), 'Autocomplete returned only 10 results (for over 10 matches)'); | |
168 } | |
169 | |
170 /** | |
171 * Tests that the autocomplete method returns properly ordered results. | |
172 * | |
173 * @see \Drupal\taxonomy\Controller\TermAutocompleteController::autocomplete() | |
174 */ | |
175 public function testAutocompleteOrderedResults() { | |
176 $expectedResults = [ | |
177 'aaa 10 bbb', | |
178 'aaa 11 bbb', | |
179 'aaa 12 bbb', | |
180 'aaa 20 bbb', | |
181 'aaa 30 bbb', | |
182 'aaa 40 bbb', | |
183 'aaa 50 bbb', | |
184 'aaa 70 bbb', | |
185 'bbb 60 aaa', | |
186 ]; | |
187 // Build $expected to match the autocomplete results. | |
188 $expected = []; | |
189 foreach ($expectedResults as $termName) { | |
190 $expected[] = [ | |
191 'value' => $termName . ' (' . $this->termIds[$termName] . ')', | |
192 'label' => $termName | |
193 ]; | |
194 } | |
195 | |
196 $data = $this->drupalGetJSON( | |
197 $this->autocompleteUrl, | |
198 ['query' => ['q' => 'bbb']] | |
199 ); | |
200 | |
201 $this->assertIdentical($expected, $data); | |
202 } | |
203 | |
204 } |