function SearchQueryAlterTest::testQueryAlter

Same name in other branches
  1. 9 core/modules/search/tests/src/Functional/SearchQueryAlterTest.php \Drupal\Tests\search\Functional\SearchQueryAlterTest::testQueryAlter()
  2. 8.9.x core/modules/search/tests/src/Functional/SearchQueryAlterTest.php \Drupal\Tests\search\Functional\SearchQueryAlterTest::testQueryAlter()
  3. 11.x core/modules/search/tests/src/Functional/SearchQueryAlterTest.php \Drupal\Tests\search\Functional\SearchQueryAlterTest::testQueryAlter()

Tests that the query alter works.

File

core/modules/search/tests/src/Functional/SearchQueryAlterTest.php, line 29

Class

SearchQueryAlterTest
Tests that the node search query can be altered via the query alter hook.

Namespace

Drupal\Tests\search\Functional

Code

public function testQueryAlter() : void {
    $this->drupalCreateContentType([
        'type' => 'page',
        'name' => 'Basic page',
    ]);
    $this->drupalCreateContentType([
        'type' => 'article',
        'name' => 'Article',
    ]);
    // Log in with sufficient privileges.
    $this->drupalLogin($this->drupalCreateUser([
        'create page content',
        'search content',
    ]));
    // Create a node and an article with the same keyword. The query alter
    // test module will alter the query so only articles should be returned.
    $data = [
        'type' => 'page',
        'title' => 'test page',
        'body' => [
            [
                'value' => 'pizza',
            ],
        ],
    ];
    $this->drupalCreateNode($data);
    $data['type'] = 'article';
    $data['title'] = 'test article';
    $this->drupalCreateNode($data);
    // Update the search index.
    $this->container
        ->get('plugin.manager.search')
        ->createInstance('node_search')
        ->updateIndex();
    // Search for the body keyword 'pizza'.
    $this->drupalGet('search/node');
    $this->submitForm([
        'keys' => 'pizza',
    ], 'Search');
    // The article should be there but not the page.
    $this->assertSession()
        ->pageTextContains('article');
    $this->assertSession()
        ->pageTextNotContains('page');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.