Mercurial > hg > isophonics-drupal-site
comparison core/modules/search/tests/src/Functional/SearchMultilingualEntityTest.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 129ea1e6d783 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Tests\search\Functional; | |
4 | |
5 use Drupal\field\Entity\FieldStorageConfig; | |
6 use Drupal\language\Entity\ConfigurableLanguage; | |
7 | |
8 /** | |
9 * Tests entities with multilingual fields. | |
10 * | |
11 * @group search | |
12 */ | |
13 class SearchMultilingualEntityTest extends SearchTestBase { | |
14 | |
15 /** | |
16 * List of searchable nodes. | |
17 * | |
18 * @var \Drupal\node\NodeInterface[] | |
19 */ | |
20 protected $searchableNodes = []; | |
21 | |
22 /** | |
23 * Node search plugin. | |
24 * | |
25 * @var \Drupal\node\Plugin\Search\NodeSearch | |
26 */ | |
27 protected $plugin; | |
28 | |
29 public static $modules = ['language', 'locale', 'comment']; | |
30 | |
31 protected function setUp() { | |
32 parent::setUp(); | |
33 | |
34 // Create a user who can administer search, do searches, see the status | |
35 // report, and administer cron. Log in. | |
36 $user = $this->drupalCreateUser(['administer search', 'search content', 'use advanced search', 'access content', 'access site reports', 'administer site configuration']); | |
37 $this->drupalLogin($user); | |
38 | |
39 // Set up the search plugin. | |
40 $this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); | |
41 | |
42 // Check indexing counts before adding any nodes. | |
43 $this->assertIndexCounts(0, 0, 'before adding nodes'); | |
44 $this->assertDatabaseCounts(0, 0, 'before adding nodes'); | |
45 | |
46 // Add two new languages. | |
47 ConfigurableLanguage::createFromLangcode('hu')->save(); | |
48 ConfigurableLanguage::createFromLangcode('sv')->save(); | |
49 | |
50 // Make the body field translatable. The title is already translatable by | |
51 // definition. The parent class has already created the article and page | |
52 // content types. | |
53 $field_storage = FieldStorageConfig::loadByName('node', 'body'); | |
54 $field_storage->setTranslatable(TRUE); | |
55 $field_storage->save(); | |
56 | |
57 // Create a few page nodes with multilingual body values. | |
58 $default_format = filter_default_format(); | |
59 $nodes = [ | |
60 [ | |
61 'title' => 'First node en', | |
62 'type' => 'page', | |
63 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]], | |
64 'langcode' => 'en', | |
65 ], | |
66 [ | |
67 'title' => 'Second node this is the English title', | |
68 'type' => 'page', | |
69 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]], | |
70 'langcode' => 'en', | |
71 ], | |
72 [ | |
73 'title' => 'Third node en', | |
74 'type' => 'page', | |
75 'body' => [['value' => $this->randomMachineName(32), 'format' => $default_format]], | |
76 'langcode' => 'en', | |
77 ], | |
78 // After the third node, we don't care what the settings are. But we | |
79 // need to have at least 5 to make sure the throttling is working | |
80 // correctly. So, let's make 8 total. | |
81 [], | |
82 [], | |
83 [], | |
84 [], | |
85 [], | |
86 ]; | |
87 $this->searchableNodes = []; | |
88 foreach ($nodes as $setting) { | |
89 $this->searchableNodes[] = $this->drupalCreateNode($setting); | |
90 } | |
91 | |
92 // Add a single translation to the second node. | |
93 $translation = $this->searchableNodes[1]->addTranslation('hu', ['title' => 'Second node hu']); | |
94 $translation->body->value = $this->randomMachineName(32); | |
95 $this->searchableNodes[1]->save(); | |
96 | |
97 // Add two translations to the third node. | |
98 $translation = $this->searchableNodes[2]->addTranslation('hu', ['title' => 'Third node this is the Hungarian title']); | |
99 $translation->body->value = $this->randomMachineName(32); | |
100 $translation = $this->searchableNodes[2]->addTranslation('sv', ['title' => 'Third node sv']); | |
101 $translation->body->value = $this->randomMachineName(32); | |
102 $this->searchableNodes[2]->save(); | |
103 | |
104 // Verify that we have 8 nodes left to do. | |
105 $this->assertIndexCounts(8, 8, 'before updating the search index'); | |
106 $this->assertDatabaseCounts(0, 0, 'before updating the search index'); | |
107 } | |
108 | |
109 /** | |
110 * Tests the indexing throttle and search results with multilingual nodes. | |
111 */ | |
112 public function testMultilingualSearch() { | |
113 // Index only 2 nodes per cron run. We cannot do this setting in the UI, | |
114 // because it doesn't go this low. | |
115 $this->config('search.settings')->set('index.cron_limit', 2)->save(); | |
116 // Get a new search plugin, to make sure it has this setting. | |
117 $this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); | |
118 | |
119 // Update the index. This does the initial processing. | |
120 $this->plugin->updateIndex(); | |
121 // Run the shutdown function. Testing is a unique case where indexing | |
122 // and searching has to happen in the same request, so running the shutdown | |
123 // function manually is needed to finish the indexing process. | |
124 search_update_totals(); | |
125 $this->assertIndexCounts(6, 8, 'after updating partially'); | |
126 $this->assertDatabaseCounts(2, 0, 'after updating partially'); | |
127 | |
128 // Now index the rest of the nodes. | |
129 // Make sure index throttle is high enough, via the UI. | |
130 $this->drupalPostForm('admin/config/search/pages', ['cron_limit' => 20], t('Save configuration')); | |
131 $this->assertEqual(20, $this->config('search.settings')->get('index.cron_limit', 100), 'Config setting was saved correctly'); | |
132 // Get a new search plugin, to make sure it has this setting. | |
133 $this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); | |
134 | |
135 $this->plugin->updateIndex(); | |
136 search_update_totals(); | |
137 $this->assertIndexCounts(0, 8, 'after updating fully'); | |
138 $this->assertDatabaseCounts(8, 0, 'after updating fully'); | |
139 | |
140 // Click the reindex button on the admin page, verify counts, and reindex. | |
141 $this->drupalPostForm('admin/config/search/pages', [], t('Re-index site')); | |
142 $this->drupalPostForm(NULL, [], t('Re-index site')); | |
143 $this->assertIndexCounts(8, 8, 'after reindex'); | |
144 $this->assertDatabaseCounts(8, 0, 'after reindex'); | |
145 $this->plugin->updateIndex(); | |
146 search_update_totals(); | |
147 | |
148 // Test search results. | |
149 | |
150 // This should find two results for the second and third node. | |
151 $this->plugin->setSearch('English OR Hungarian', [], []); | |
152 $search_result = $this->plugin->execute(); | |
153 $this->assertEqual(count($search_result), 2, 'Found two results.'); | |
154 // Nodes are saved directly after each other and have the same created time | |
155 // so testing for the order is not possible. | |
156 $results = [$search_result[0]['title'], $search_result[1]['title']]; | |
157 $this->assertTrue(in_array('Third node this is the Hungarian title', $results), 'The search finds the correct Hungarian title.'); | |
158 $this->assertTrue(in_array('Second node this is the English title', $results), 'The search finds the correct English title.'); | |
159 | |
160 // Now filter for Hungarian results only. | |
161 $this->plugin->setSearch('English OR Hungarian', ['f' => ['language:hu']], []); | |
162 $search_result = $this->plugin->execute(); | |
163 | |
164 $this->assertEqual(count($search_result), 1, 'The search found only one result'); | |
165 $this->assertEqual($search_result[0]['title'], 'Third node this is the Hungarian title', 'The search finds the correct Hungarian title.'); | |
166 | |
167 // Test for search with common key word across multiple languages. | |
168 $this->plugin->setSearch('node', [], []); | |
169 $search_result = $this->plugin->execute(); | |
170 | |
171 $this->assertEqual(count($search_result), 6, 'The search found total six results'); | |
172 | |
173 // Test with language filters and common key word. | |
174 $this->plugin->setSearch('node', ['f' => ['language:hu']], []); | |
175 $search_result = $this->plugin->execute(); | |
176 | |
177 $this->assertEqual(count($search_result), 2, 'The search found 2 results'); | |
178 | |
179 // Test to check for the language of result items. | |
180 foreach ($search_result as $result) { | |
181 $this->assertEqual($result['langcode'], 'hu', 'The search found the correct Hungarian result'); | |
182 } | |
183 | |
184 // Mark one of the nodes for reindexing, using the API function, and | |
185 // verify indexing status. | |
186 search_mark_for_reindex('node_search', $this->searchableNodes[0]->id()); | |
187 $this->assertIndexCounts(1, 8, 'after marking one node to reindex via API function'); | |
188 | |
189 // Update the index and verify the totals again. | |
190 $this->plugin = $this->container->get('plugin.manager.search')->createInstance('node_search'); | |
191 $this->plugin->updateIndex(); | |
192 search_update_totals(); | |
193 $this->assertIndexCounts(0, 8, 'after indexing again'); | |
194 | |
195 // Mark one node for reindexing by saving it, and verify indexing status. | |
196 $this->searchableNodes[1]->save(); | |
197 $this->assertIndexCounts(1, 8, 'after marking one node to reindex via save'); | |
198 | |
199 // The request time is always the same throughout test runs. Update the | |
200 // request time to a previous time, to simulate it having been marked | |
201 // previously. | |
202 $current = REQUEST_TIME; | |
203 $old = $current - 10; | |
204 db_update('search_dataset') | |
205 ->fields(['reindex' => $old]) | |
206 ->condition('reindex', $current, '>=') | |
207 ->execute(); | |
208 | |
209 // Save the node again. Verify that the request time on it is not updated. | |
210 $this->searchableNodes[1]->save(); | |
211 $result = db_select('search_dataset', 'd') | |
212 ->fields('d', ['reindex']) | |
213 ->condition('type', 'node_search') | |
214 ->condition('sid', $this->searchableNodes[1]->id()) | |
215 ->execute() | |
216 ->fetchField(); | |
217 $this->assertEqual($result, $old, 'Reindex time was not updated if node was already marked'); | |
218 | |
219 // Add a bogus entry to the search index table using a different search | |
220 // type. This will not appear in the index status, because it is not | |
221 // managed by a plugin. | |
222 search_index('foo', $this->searchableNodes[0]->id(), 'en', 'some text'); | |
223 $this->assertIndexCounts(1, 8, 'after adding a different index item'); | |
224 | |
225 // Mark just this "foo" index for reindexing. | |
226 search_mark_for_reindex('foo'); | |
227 $this->assertIndexCounts(1, 8, 'after reindexing the other search type'); | |
228 | |
229 // Mark everything for reindexing. | |
230 search_mark_for_reindex(); | |
231 $this->assertIndexCounts(8, 8, 'after reindexing everything'); | |
232 | |
233 // Clear one item from the index, but with wrong language. | |
234 $this->assertDatabaseCounts(8, 1, 'before clear'); | |
235 search_index_clear('node_search', $this->searchableNodes[0]->id(), 'hu'); | |
236 $this->assertDatabaseCounts(8, 1, 'after clear with wrong language'); | |
237 // Clear using correct language. | |
238 search_index_clear('node_search', $this->searchableNodes[0]->id(), 'en'); | |
239 $this->assertDatabaseCounts(7, 1, 'after clear with right language'); | |
240 // Don't specify language. | |
241 search_index_clear('node_search', $this->searchableNodes[1]->id()); | |
242 $this->assertDatabaseCounts(6, 1, 'unspecified language clear'); | |
243 // Clear everything in 'foo'. | |
244 search_index_clear('foo'); | |
245 $this->assertDatabaseCounts(6, 0, 'other index clear'); | |
246 // Clear everything. | |
247 search_index_clear(); | |
248 $this->assertDatabaseCounts(0, 0, 'complete clear'); | |
249 } | |
250 | |
251 /** | |
252 * Verifies the indexing status counts. | |
253 * | |
254 * @param int $remaining | |
255 * Count of remaining items to verify. | |
256 * @param int $total | |
257 * Count of total items to verify. | |
258 * @param string $message | |
259 * Message to use, something like "after updating the search index". | |
260 */ | |
261 protected function assertIndexCounts($remaining, $total, $message) { | |
262 // Check status via plugin method call. | |
263 $status = $this->plugin->indexStatus(); | |
264 $this->assertEqual($status['remaining'], $remaining, 'Remaining items ' . $message . ' is ' . $remaining); | |
265 $this->assertEqual($status['total'], $total, 'Total items ' . $message . ' is ' . $total); | |
266 | |
267 // Check text in progress section of Search settings page. Note that this | |
268 // test avoids using | |
269 // \Drupal\Core\StringTranslation\TranslationInterface::formatPlural(), so | |
270 // it tests for fragments of text. | |
271 $indexed = $total - $remaining; | |
272 $percent = ($total > 0) ? floor(100 * $indexed / $total) : 100; | |
273 $this->drupalGet('admin/config/search/pages'); | |
274 $this->assertText($percent . '% of the site has been indexed.', 'Progress percent text at top of Search settings page is correct at: ' . $message); | |
275 $this->assertText($remaining . ' item', 'Remaining text at top of Search settings page is correct at: ' . $message); | |
276 | |
277 // Check text in pages section of Search settings page. | |
278 $this->assertText($indexed . ' of ' . $total . ' indexed', 'Progress text in pages section of Search settings page is correct at: ' . $message); | |
279 | |
280 // Check text on status report page. | |
281 $this->drupalGet('admin/reports/status'); | |
282 $this->assertText('Search index progress', 'Search status section header is present on status report page'); | |
283 $this->assertText($percent . '%', 'Correct percentage is shown on status report page at: ' . $message); | |
284 $this->assertText('(' . $remaining . ' remaining)', 'Correct remaining value is shown on status report page at: ' . $message); | |
285 } | |
286 | |
287 /** | |
288 * Checks actual database counts of items in the search index. | |
289 * | |
290 * @param int $count_node | |
291 * Count of node items to assert. | |
292 * @param int $count_foo | |
293 * Count of "foo" items to assert. | |
294 * @param string $message | |
295 * Message suffix to use. | |
296 */ | |
297 protected function assertDatabaseCounts($count_node, $count_foo, $message) { | |
298 // Count number of distinct nodes by ID. | |
299 $results = db_select('search_dataset', 'i') | |
300 ->fields('i', ['sid']) | |
301 ->condition('type', 'node_search') | |
302 ->groupBy('sid') | |
303 ->execute() | |
304 ->fetchCol(); | |
305 $this->assertEqual($count_node, count($results), 'Node count was ' . $count_node . ' for ' . $message); | |
306 | |
307 // Count number of "foo" records. | |
308 $results = db_select('search_dataset', 'i') | |
309 ->fields('i', ['sid']) | |
310 ->condition('type', 'foo') | |
311 ->execute() | |
312 ->fetchCol(); | |
313 $this->assertEqual($count_foo, count($results), 'Foo count was ' . $count_foo . ' for ' . $message); | |
314 | |
315 } | |
316 | |
317 } |