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