comparison core/modules/search/src/Tests/SearchPreprocessLangcodeTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\search\Tests;
4
5 /**
6 * Tests that the search preprocessing uses the correct language code.
7 *
8 * @group search
9 */
10 class SearchPreprocessLangcodeTest extends SearchTestBase {
11
12 /**
13 * Modules to enable.
14 *
15 * @var array
16 */
17 public static $modules = ['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 $web_user = $this->drupalCreateUser([
30 'create page content',
31 'edit own page content',
32 'search content',
33 'use advanced search',
34 ]);
35 $this->drupalLogin($web_user);
36 }
37
38 /**
39 * Tests that hook_search_preprocess() returns the correct langcode.
40 */
41 public function testPreprocessLangcode() {
42 // Create a node.
43 $this->node = $this->drupalCreateNode(['body' => [[]], 'langcode' => 'en']);
44
45 // First update the index. This does the initial processing.
46 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
47
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();
52
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'));
58
59 // Checks if the langcode message has been set by hook_search_preprocess().
60 $this->assertText('Langcode Preprocess Test: en');
61 }
62
63 /**
64 * Tests stemming for hook_search_preprocess().
65 */
66 public function testPreprocessStemming() {
67 // Create a node.
68 $this->node = $this->drupalCreateNode([
69 'title' => 'we are testing',
70 'body' => [[]],
71 'langcode' => 'en',
72 ]);
73
74 // First update the index. This does the initial processing.
75 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
76
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();
81
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'));
85
86 // Check if the node has been found.
87 $this->assertText('Search results');
88 $this->assertText('we are testing');
89
90 // Search for the same node using a different query.
91 $edit = ['or' => 'test'];
92 $this->drupalPostForm('search/node', $edit, t('Advanced search'));
93
94 // Check if the node has been found.
95 $this->assertText('Search results');
96 $this->assertText('we are testing');
97 }
98
99 }