core/modules/search/src/Controller/SearchController.php | 9 ++++++++- core/modules/search/src/Tests/SearchPageCacheTagsTest.php | 4 ++-- core/modules/simpletest/src/WebTestBase.php | 11 +++++++++++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/core/modules/search/src/Controller/SearchController.php b/core/modules/search/src/Controller/SearchController.php index d49173a..3a14a7b 100644 --- a/core/modules/search/src/Controller/SearchController.php +++ b/core/modules/search/src/Controller/SearchController.php @@ -124,10 +124,17 @@ public function view(Request $request, SearchPageInterface $entity) { ), ), '#cache' => array( - 'tags' => Cache::mergeTags($entity->getCacheTags(), ['search_index:' . $plugin->getPluginId()]), + 'tags' => $entity->getCacheTags(), ), ); + // If this plugin uses a search index, then also add the cache tag tracking + // that search index, so that cached search result pages are invalidated + // when necessary. + if ($plugin->getType() !== NULL) { + $build['search_results']['#cache']['tags'][] = 'search_index:' . $plugin->getType(); + } + $build['pager'] = array( '#type' => 'pager', ); diff --git a/core/modules/search/src/Tests/SearchPageCacheTagsTest.php b/core/modules/search/src/Tests/SearchPageCacheTagsTest.php index eb33827..8f2e509 100644 --- a/core/modules/search/src/Tests/SearchPageCacheTagsTest.php +++ b/core/modules/search/src/Tests/SearchPageCacheTagsTest.php @@ -92,13 +92,13 @@ function testSearchText() { // Initial page for searching users. $this->drupalGet('search/user'); $this->assertCacheTag('config:search.page.user_search'); - $this->assertCacheTag('search_index:user_search'); + $this->assertNoCacheTag('search_index:user_search'); // User search results. $edit['keys'] = $this->searchingUser->getUsername(); $this->drupalPostForm('search/user', $edit, t('Search')); $this->assertCacheTag('config:search.page.user_search'); - $this->assertCacheTag('search_index:user_search'); + $this->assertNoCacheTag('search_index:user_search'); } } diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index e79dff9..3bb404d 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -2762,4 +2762,15 @@ protected function assertCacheTag($expected_cache_tag) { $this->assertTrue(in_array($expected_cache_tag, $cache_tags), "'" . $expected_cache_tag . "' is present in the X-Drupal-Cache-Tags header."); } + /** + * Asserts whether an expected cache tag was absent in the last response. + * + * @param string $expected_cache_tag + * The expected cache tag. + */ + protected function assertNoCacheTag($expected_cache_tag) { + $cache_tags = explode(' ', $this->drupalGetHeader('X-Drupal-Cache-Tags')); + $this->assertFalse(in_array($expected_cache_tag, $cache_tags), "'" . $expected_cache_tag . "' is absent in the X-Drupal-Cache-Tags header."); + } + }