core/modules/search/search.module | 8 +++++++- core/modules/search/src/Controller/SearchController.php | 3 ++- core/modules/search/src/Plugin/SearchInterface.php | 7 ++++--- core/modules/search/src/Tests/SearchPageCacheTagsTest.php | 5 +++++ core/modules/simpletest/src/WebTestBase.php | 8 ++++---- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/core/modules/search/search.module b/core/modules/search/search.module index 3818879..4c3336d 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -154,10 +154,14 @@ function search_index_clear($type = NULL, $sid = NULL, $langcode = NULL) { $query_index->execute(); $query_dataset->execute(); - if ($type !== NULL) { + if ($type) { // Invalidate all render cache items that contain data from this index. Cache::invalidateTags(['search_index:' . $type]); } + else { + // Invalidate all render cache items that contain data from any index. + Cache::invalidateTags(['search_index']); + } } /** @@ -505,6 +509,8 @@ function search_index($type, $sid, $langcode, $text) { $tag = !$tag; } + // Remove the item $sid from the search index, and invalidate the relevant + // cache tags. search_index_clear($type, $sid, $langcode); // Insert cleaned up data into dataset diff --git a/core/modules/search/src/Controller/SearchController.php b/core/modules/search/src/Controller/SearchController.php index 3a14a7b..f51466a 100644 --- a/core/modules/search/src/Controller/SearchController.php +++ b/core/modules/search/src/Controller/SearchController.php @@ -131,7 +131,8 @@ public function view(Request $request, SearchPageInterface $entity) { // 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) { + if ($plugin->getType()) { + $build['search_results']['#cache']['tags'][] = 'search_index'; $build['search_results']['#cache']['tags'][] = 'search_index:' . $plugin->getType(); } diff --git a/core/modules/search/src/Plugin/SearchInterface.php b/core/modules/search/src/Plugin/SearchInterface.php index d43e74a..72007e6 100644 --- a/core/modules/search/src/Plugin/SearchInterface.php +++ b/core/modules/search/src/Plugin/SearchInterface.php @@ -65,12 +65,13 @@ public function getAttributes(); public function isSearchExecutable(); /** - * Returns the used search index type. + * Returns the search index type this plugin uses. * * @return string|null - * The plugin ID or other machine-readable type for the search index this - * search uses. NULL if no search index is used. + * The type used by this search plugin in the search index, or NULL if this + * plugin does not use the search index. * + * @see search_index() * @see search_index_clear() */ public function getType(); diff --git a/core/modules/search/src/Tests/SearchPageCacheTagsTest.php b/core/modules/search/src/Tests/SearchPageCacheTagsTest.php index 8f2e509..f54662f 100644 --- a/core/modules/search/src/Tests/SearchPageCacheTagsTest.php +++ b/core/modules/search/src/Tests/SearchPageCacheTagsTest.php @@ -72,6 +72,7 @@ function testSearchText() { $this->drupalPostForm('search/node', $edit, t('Search')); $this->assertText('bike shed shop'); $this->assertCacheTag('config:search.page.node_search'); + $this->assertCacheTag('search_index'); $this->assertCacheTag('search_index:node_search'); // Updating a node should invalidate the search plugin's index cache tag. @@ -80,6 +81,7 @@ function testSearchText() { $this->drupalPostForm('search/node', $edit, t('Search')); $this->assertText('bike shop'); $this->assertCacheTag('config:search.page.node_search'); + $this->assertCacheTag('search_index'); $this->assertCacheTag('search_index:node_search'); // Deleting a node should invalidate the search plugin's index cache tag. @@ -87,17 +89,20 @@ function testSearchText() { $this->drupalPostForm('search/node', $edit, t('Search')); $this->assertText('Your search yielded no results.'); $this->assertCacheTag('config:search.page.node_search'); + $this->assertCacheTag('search_index'); $this->assertCacheTag('search_index:node_search'); // Initial page for searching users. $this->drupalGet('search/user'); $this->assertCacheTag('config:search.page.user_search'); + $this->assertNoCacheTag('search_index'); $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->assertNoCacheTag('search_index'); $this->assertNoCacheTag('search_index:user_search'); } diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 3bb404d..c4f5cee 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -2765,12 +2765,12 @@ protected function assertCacheTag($expected_cache_tag) { /** * Asserts whether an expected cache tag was absent in the last response. * - * @param string $expected_cache_tag - * The expected cache tag. + * @param string $cache_tag + * The cache tag to check. */ - protected function assertNoCacheTag($expected_cache_tag) { + protected function assertNoCacheTag($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."); + $this->assertFalse(in_array($cache_tag, $cache_tags), "'" . $cache_tag . "' is absent in the X-Drupal-Cache-Tags header."); } }