diff --git a/src/Plugin/views/cache/SearchApiCache.php b/src/Plugin/views/cache/SearchApiCache.php index 60051cf..b210676 100644 --- a/src/Plugin/views/cache/SearchApiCache.php +++ b/src/Plugin/views/cache/SearchApiCache.php @@ -100,10 +100,12 @@ class SearchApiCache extends Time { $build_info = $this->view->build_info; $key_data = ['build_info' => $build_info]; + + $display_handler_cache_contexts = $this->displayHandler + ->getCacheMetadata() + ->getCacheContexts(); $key_data += \Drupal::service('cache_contexts_manager') - ->convertTokensToKeys($this->displayHandler - ->getCacheMetadata() - ->getCacheContexts()) + ->convertTokensToKeys($display_handler_cache_contexts) ->getKeys(); $this->resultsKey = $this->view->storage->id() . ':' . $this->displayHandler->display['id'] . ':results:' . hash('sha256', serialize($key_data)); diff --git a/src/Tests/CacheabilityTest.php b/src/Tests/CacheabilityTest.php index 5fd9c6e..c90fc63 100644 --- a/src/Tests/CacheabilityTest.php +++ b/src/Tests/CacheabilityTest.php @@ -14,7 +14,7 @@ use Drupal\search_api\SearchApiException; use Drupal\search_api\Utility; /** - * Tests the cacheability settings of Search API. + * Tests the cacheability metadata of Search API. * * @group search_api */ @@ -48,8 +48,8 @@ class CacheabilityTest extends WebTestBase { $this->getTestServer(); $this->getTestIndex(); - // Setup example structure and content and populate the test index with that - // content. + // Set up example structure and content and populate the test index with + // that content. $this->setUpExampleStructure(); $this->insertExampleContent(); \Drupal::getContainer() @@ -67,14 +67,15 @@ class CacheabilityTest extends WebTestBase { $index_cachetag = 'config:search_api.index.database_search_index'; $server_cachetag = 'config:search_api.server.database_search_server'; $view_cachetag = 'config:views.view.search_api_test_view'; - $expected_tags = $index_cachetag . ' ' . $server_cachetag . ' ' . $view_cachetag . ' rendered'; // Test the cacheability headers of a backend that doesn't support cacheing. $this->drupalGet('search-api-test-fulltext'); $this->assertResponse(200); $this->assertHeader('x-drupal-dynamic-cache', 'UNCACHEABLE'); - $this->assertHeader('x-drupal-cache-tags', $expected_tags); - $this->assertHeader('cache-control', 'must-revalidate, no-cache, post-check=0, pre-check=0, private'); + $this->assertCacheTag($index_cachetag); + $this->assertCacheTag($server_cachetag); + $this->assertCacheTag($view_cachetag); + $this->assertTrue(strpos($this->drupalGetHeader('cache-control'), 'no-cache')); // Test the cacheability headers of a backend that supports cacheing. \Drupal::state()->set('search_api_test_backend.feature.cacheable_dependency', 100); @@ -82,8 +83,10 @@ class CacheabilityTest extends WebTestBase { $this->drupalGet('search-api-test-fulltext'); $this->assertResponse(200); $this->assertHeader('x-drupal-dynamic-cache', 'UNCACHEABLE'); - $this->assertHeader('x-drupal-cache-tags', $expected_tags); - $this->assertHeader('cache-control', 'must-revalidate, no-cache, post-check=0, pre-check=0, private'); + $this->assertCacheTag($index_cachetag); + $this->assertCacheTag($server_cachetag); + $this->assertCacheTag($view_cachetag); + $this->assertTrue(strpos($this->drupalGetHeader('cache-control'), 'no-cache')); } }