3 namespace Drupal\search\Tests;
6 * Tests that the node search query can be altered via the query alter hook.
10 class SearchQueryAlterTest extends SearchTestBase {
16 public static $modules = ['search_query_alter'];
19 * Tests that the query alter works.
21 public function testQueryAlter() {
22 // Log in with sufficient privileges.
23 $this->drupalLogin($this->drupalCreateUser(['create page content', 'search content']));
25 // Create a node and an article with the same keyword. The query alter
26 // test module will alter the query so only articles should be returned.
29 'title' => 'test page',
30 'body' => [['value' => 'pizza']],
32 $this->drupalCreateNode($data);
34 $data['type'] = 'article';
35 $data['title'] = 'test article';
36 $this->drupalCreateNode($data);
38 // Update the search index.
39 $this->container->get('plugin.manager.search')->createInstance('node_search')->updateIndex();
40 search_update_totals();
42 // Search for the body keyword 'pizza'.
43 $this->drupalPostForm('search/node', ['keys' => 'pizza'], t('Search'));
44 // The article should be there but not the page.
45 $this->assertText('article', 'Article is in search results');
46 $this->assertNoText('page', 'Page is not in search results');