3 namespace Drupal\search\Tests;
6 * Tests that the search preprocessing uses the correct language code.
10 class SearchPreprocessLangcodeTest extends SearchTestBase {
17 public static $modules = ['search_langcode_test'];
20 * Test node for searching.
22 * @var \Drupal\node\NodeInterface
26 protected function setUp() {
29 $web_user = $this->drupalCreateUser([
30 'create page content',
31 'edit own page content',
33 'use advanced search',
35 $this->drupalLogin($web_user);
39 * Tests that hook_search_preprocess() returns the correct langcode.
41 public function testPreprocessLangcode() {
43 $this->node = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'en']);
45 // First update the index. This does the initial processing.
46 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
48 // Then, run the shutdown function. Testing is a unique case where indexing
49 // and searching has to happen in the same request, so running the shutdown
50 // function manually is needed to finish the indexing process.
51 search_update_totals();
53 // Search for the additional text that is added by the preprocess
54 // function. If you search for text that is in the node, preprocess is
55 // not invoked on the node during the search excerpt generation.
56 $edit = ['or' => 'Additional text'];
57 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
59 // Checks if the langcode message has been set by hook_search_preprocess().
60 $this->assertText('Langcode Preprocess Test: en');
64 * Tests stemming for hook_search_preprocess().
66 public function testPreprocessStemming() {
68 $this->node = $this->drupalCreateNode([
69 'title' => 'we are testing',
74 // First update the index. This does the initial processing.
75 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
77 // Then, run the shutdown function. Testing is a unique case where indexing
78 // and searching has to happen in the same request, so running the shutdown
79 // function manually is needed to finish the indexing process.
80 search_update_totals();
82 // Search for the title of the node with a POST query.
83 $edit = ['or' => 'testing'];
84 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
86 // Check if the node has been found.
87 $this->assertText('Search results');
88 $this->assertText('we are testing');
90 // Search for the same node using a different query.
91 $edit = ['or' => 'test'];
92 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
94 // Check if the node has been found.
95 $this->assertText('Search results');
96 $this->assertText('we are testing');