459c1baccfd0eb097ef31d7d394176b574fb6c69
[yaffs-website] / web / core / modules / search / tests / src / Functional / SearchPreprocessLangcodeTest.php
1 <?php
2
3 namespace Drupal\Tests\search\Functional;
4
5 use Drupal\Tests\BrowserTestBase;
6
7 /**
8  * Tests that the search preprocessing uses the correct language code.
9  *
10  * @group search
11  */
12 class SearchPreprocessLangcodeTest extends BrowserTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   protected static $modules = ['node', 'search', 'search_langcode_test'];
18
19   /**
20    * Test node for searching.
21    *
22    * @var \Drupal\node\NodeInterface
23    */
24   protected $node;
25
26   protected function setUp() {
27     parent::setUp();
28
29     $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
30
31     $web_user = $this->drupalCreateUser([
32       'create page content',
33       'edit own page content',
34       'search content',
35       'use advanced search',
36     ]);
37     $this->drupalLogin($web_user);
38   }
39
40   /**
41    * Tests that hook_search_preprocess() returns the correct langcode.
42    */
43   public function testPreprocessLangcode() {
44     // Create a node.
45     $this->node = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'en']);
46
47     // First update the index. This does the initial processing.
48     $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
49
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();
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     // 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();
83
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');
87
88     // Check if the node has been found.
89     $this->assertText('Search results');
90     $this->assertText('we are testing');
91
92     // Search for the same node using a different query.
93     $edit = ['or' => 'test'];
94     $this->drupalPostForm('search/node', $edit, 'edit-submit--2');
95
96     // Check if the node has been found.
97     $this->assertText('Search results');
98     $this->assertText('we are testing');
99   }
100
101 }