function SearchPageCacheTagsTest::testSearchText

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

Tests the presence of the expected cache tag in various situations.

File

core/modules/search/tests/src/Functional/SearchPageCacheTagsTest.php, line 70

Class

SearchPageCacheTagsTest
Tests the search_page entity cache tags on the search results pages.

Namespace

Drupal\Tests\search\Functional

Code

public function testSearchText() : void {
    $this->drupalLogin($this->searchingUser);
    // Initial page for searching nodes.
    $this->drupalGet('search/node');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
    // Node search results.
    $edit = [];
    $edit['keys'] = 'bike shed';
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->pageTextContains('bike shed shop');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node:1');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'user:2');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'rendered');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'http_response');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
    // Updating a node should invalidate the search plugin's index cache tag.
    $this->node->title = 'bike shop';
    $this->node
        ->save();
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->pageTextContains('bike shop');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node:1');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'user:2');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'rendered');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'http_response');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
    // Deleting a node should invalidate the search plugin's index cache tag.
    $this->node
        ->delete();
    $this->drupalGet('search/node');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->pageTextContains('Your search yielded no results.');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'search_index:node_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'node_list');
    // Initial page for searching users.
    $this->drupalGet('search/user');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.user_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'user_list');
    $this->assertSession()
        ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
    $this->assertSession()
        ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
    // User search results.
    $edit['keys'] = $this->searchingUser
        ->getAccountName();
    $this->drupalGet('search/user');
    $this->submitForm($edit, 'Search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'config:search.page.user_search');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'user_list');
    $this->assertSession()
        ->responseHeaderContains('X-Drupal-Cache-Tags', 'user:2');
    $this->assertSession()
        ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index');
    $this->assertSession()
        ->responseHeaderNotContains('X-Drupal-Cache-Tags', 'search_index:user_search');
}

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