3 namespace Drupal\Tests\search\Functional;
5 use Drupal\Tests\BrowserTestBase;
8 * Tests that the search preprocessing uses the correct language code.
12 class SearchPreprocessLangcodeTest extends BrowserTestBase {
17 protected static $modules = ['node', 'search', 'search_langcode_test'];
20 * Test node for searching.
22 * @var \Drupal\node\NodeInterface
26 protected function setUp() {
29 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
31 $web_user = $this->drupalCreateUser([
32 'create page content',
33 'edit own page content',
35 'use advanced search',
37 $this->drupalLogin($web_user);
41 * Tests that hook_search_preprocess() returns the correct langcode.
43 public function testPreprocessLangcode() {
45 $this->node = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'en']);
47 // First update the index. This does the initial processing.
48 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
50 // Then, run the shutdown function. Testing is a unique case where indexing
51 // and searching has to happen in the same request, so running the shutdown
52 // function manually is needed to finish the indexing process.
53 search_update_totals();
55 // Search for the additional text that is added by the preprocess
56 // function. If you search for text that is in the node, preprocess is
57 // not invoked on the node during the search excerpt generation.
58 $edit = ['or' => 'Additional text'];
59 $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
61 // Checks if the langcode message has been set by hook_search_preprocess().
62 $this->assertText('Langcode Preprocess Test: en');
66 * Tests stemming for hook_search_preprocess().
68 public function testPreprocessStemming() {
70 $this->node = $this->drupalCreateNode([
71 'title' => 'we are testing',
76 // First update the index. This does the initial processing.
77 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
79 // Then, run the shutdown function. Testing is a unique case where indexing
80 // and searching has to happen in the same request, so running the shutdown
81 // function manually is needed to finish the indexing process.
82 search_update_totals();
84 // Search for the title of the node with a POST query.
85 $edit = ['or' => 'testing'];
86 $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
88 // Check if the node has been found.
89 $this->assertText('Search results');
90 $this->assertText('we are testing');
92 // Search for the same node using a different query.
93 $edit = ['or' => 'test'];
94 $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
96 // Check if the node has been found.
97 $this->assertText('Search results');
98 $this->assertText('we are testing');