Chris@4
|
1 <?php
|
Chris@4
|
2
|
Chris@4
|
3 namespace Drupal\Tests\search\Functional;
|
Chris@4
|
4
|
Chris@4
|
5 use Drupal\Tests\BrowserTestBase;
|
Chris@4
|
6
|
Chris@4
|
7 /**
|
Chris@4
|
8 * Tests that the node search query can be altered via the query alter hook.
|
Chris@4
|
9 *
|
Chris@4
|
10 * @group search
|
Chris@4
|
11 */
|
Chris@4
|
12 class SearchQueryAlterTest extends BrowserTestBase {
|
Chris@4
|
13
|
Chris@4
|
14 /**
|
Chris@4
|
15 * {@inheritdoc}
|
Chris@4
|
16 */
|
Chris@4
|
17 protected static $modules = ['node', 'search', 'search_query_alter'];
|
Chris@4
|
18
|
Chris@4
|
19 /**
|
Chris@4
|
20 * Tests that the query alter works.
|
Chris@4
|
21 */
|
Chris@4
|
22 public function testQueryAlter() {
|
Chris@4
|
23 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
|
Chris@4
|
24 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
|
Chris@4
|
25
|
Chris@4
|
26 // Log in with sufficient privileges.
|
Chris@4
|
27 $this->drupalLogin($this->drupalCreateUser(['create page content', 'search content']));
|
Chris@4
|
28
|
Chris@4
|
29 // Create a node and an article with the same keyword. The query alter
|
Chris@4
|
30 // test module will alter the query so only articles should be returned.
|
Chris@4
|
31 $data = [
|
Chris@4
|
32 'type' => 'page',
|
Chris@4
|
33 'title' => 'test page',
|
Chris@4
|
34 'body' => [['value' => 'pizza']],
|
Chris@4
|
35 ];
|
Chris@4
|
36 $this->drupalCreateNode($data);
|
Chris@4
|
37
|
Chris@4
|
38 $data['type'] = 'article';
|
Chris@4
|
39 $data['title'] = 'test article';
|
Chris@4
|
40 $this->drupalCreateNode($data);
|
Chris@4
|
41
|
Chris@4
|
42 // Update the search index.
|
Chris@4
|
43 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
|
Chris@4
|
44 search_update_totals();
|
Chris@4
|
45
|
Chris@4
|
46 // Search for the body keyword 'pizza'.
|
Chris@4
|
47 $this->drupalPostForm('search/node', ['keys' => 'pizza'], t('Search'));
|
Chris@4
|
48 // The article should be there but not the page.
|
Chris@4
|
49 $this->assertText('article', 'Article is in search results');
|
Chris@4
|
50 $this->assertNoText('page', 'Page is not in search results');
|
Chris@4
|
51 }
|
Chris@4
|
52
|
Chris@4
|
53 }
|