Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\search\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 /**
|
Chris@0
|
6 * Tests that the search preprocessing uses the correct language code.
|
Chris@0
|
7 *
|
Chris@0
|
8 * @group search
|
Chris@0
|
9 */
|
Chris@0
|
10 class SearchPreprocessLangcodeTest extends SearchTestBase {
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Modules to enable.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @var array
|
Chris@0
|
16 */
|
Chris@0
|
17 public static $modules = ['search_langcode_test'];
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Test node for searching.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var \Drupal\node\NodeInterface
|
Chris@0
|
23 */
|
Chris@0
|
24 protected $node;
|
Chris@0
|
25
|
Chris@0
|
26 protected function setUp() {
|
Chris@0
|
27 parent::setUp();
|
Chris@0
|
28
|
Chris@0
|
29 $web_user = $this->drupalCreateUser([
|
Chris@0
|
30 'create page content',
|
Chris@0
|
31 'edit own page content',
|
Chris@0
|
32 'search content',
|
Chris@0
|
33 'use advanced search',
|
Chris@0
|
34 ]);
|
Chris@0
|
35 $this->drupalLogin($web_user);
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Tests that hook_search_preprocess() returns the correct langcode.
|
Chris@0
|
40 */
|
Chris@0
|
41 public function testPreprocessLangcode() {
|
Chris@0
|
42 // Create a node.
|
Chris@0
|
43 $this->node = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'en']);
|
Chris@0
|
44
|
Chris@0
|
45 // First update the index. This does the initial processing.
|
Chris@0
|
46 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
|
Chris@0
|
47
|
Chris@0
|
48 // Then, run the shutdown function. Testing is a unique case where indexing
|
Chris@0
|
49 // and searching has to happen in the same request, so running the shutdown
|
Chris@0
|
50 // function manually is needed to finish the indexing process.
|
Chris@0
|
51 search_update_totals();
|
Chris@0
|
52
|
Chris@0
|
53 // Search for the additional text that is added by the preprocess
|
Chris@0
|
54 // function. If you search for text that is in the node, preprocess is
|
Chris@0
|
55 // not invoked on the node during the search excerpt generation.
|
Chris@0
|
56 $edit = ['or' => 'Additional text'];
|
Chris@0
|
57 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
|
Chris@0
|
58
|
Chris@0
|
59 // Checks if the langcode message has been set by hook_search_preprocess().
|
Chris@0
|
60 $this->assertText('Langcode Preprocess Test: en');
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /**
|
Chris@0
|
64 * Tests stemming for hook_search_preprocess().
|
Chris@0
|
65 */
|
Chris@0
|
66 public function testPreprocessStemming() {
|
Chris@0
|
67 // Create a node.
|
Chris@0
|
68 $this->node = $this->drupalCreateNode([
|
Chris@0
|
69 'title' => 'we are testing',
|
Chris@0
|
70 'body' => [[]],
|
Chris@0
|
71 'langcode' => 'en',
|
Chris@0
|
72 ]);
|
Chris@0
|
73
|
Chris@0
|
74 // First update the index. This does the initial processing.
|
Chris@0
|
75 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
|
Chris@0
|
76
|
Chris@0
|
77 // Then, run the shutdown function. Testing is a unique case where indexing
|
Chris@0
|
78 // and searching has to happen in the same request, so running the shutdown
|
Chris@0
|
79 // function manually is needed to finish the indexing process.
|
Chris@0
|
80 search_update_totals();
|
Chris@0
|
81
|
Chris@0
|
82 // Search for the title of the node with a POST query.
|
Chris@0
|
83 $edit = ['or' => 'testing'];
|
Chris@0
|
84 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
|
Chris@0
|
85
|
Chris@0
|
86 // Check if the node has been found.
|
Chris@0
|
87 $this->assertText('Search results');
|
Chris@0
|
88 $this->assertText('we are testing');
|
Chris@0
|
89
|
Chris@0
|
90 // Search for the same node using a different query.
|
Chris@0
|
91 $edit = ['or' => 'test'];
|
Chris@0
|
92 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
|
Chris@0
|
93
|
Chris@0
|
94 // Check if the node has been found.
|
Chris@0
|
95 $this->assertText('Search results');
|
Chris@0
|
96 $this->assertText('we are testing');
|
Chris@0
|
97 }
|
Chris@0
|
98
|
Chris@0
|
99 }
|