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