diff --git a/src/Plugin/views/cache/SearchApiCache.php b/src/Plugin/views/cache/SearchApiCache.php index 9308676..60051cf 100644 --- a/src/Plugin/views/cache/SearchApiCache.php +++ b/src/Plugin/views/cache/SearchApiCache.php @@ -22,7 +22,6 @@ use Drupal\views\Plugin\views\cache\Time; * help = @Translation("Cache Search API views. (Other methods probably won't work with search views.)") * ) */ -// @todo Limit to Search API base tables. class SearchApiCache extends Time { /** @@ -92,28 +91,20 @@ class SearchApiCache extends Time { /** * {@inheritdoc} - * - * @todo This is completely wrong. This is inherited from the Drupal 7 port, - * or from old Drupal 8 Views code. Look at the parent's implementation - * (i.e. in Drupal 8 core). */ public function generateResultsKey() { if (!isset($this->resultsKey)) { $query = $this->getQuery()->getSearchApiQuery(); $query->preExecute(); - $user = \Drupal::currentUser(); - $key_data = array( - 'query' => $query, - 'roles' => $user->getRoles(), - 'super-user' => $user->id() == 1, // special caching for super user. - 'langcode' => \Drupal::languageManager()->getCurrentLanguage()->getId(), - 'base_url' => $GLOBALS['base_url'], - ); - foreach (array('exposed_info', 'page', 'sort', 'order', 'items_per_page', 'offset') as $key) { - if ($this->view->getRequest()->query->has($key)) { - $key_data[$key] = $this->view->getRequest()->query->get($key); - } - } + + $build_info = $this->view->build_info; + + $key_data = ['build_info' => $build_info]; + $key_data += \Drupal::service('cache_contexts_manager') + ->convertTokensToKeys($this->displayHandler + ->getCacheMetadata() + ->getCacheContexts()) + ->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 new file mode 100644 index 0000000..dd336b7 --- /dev/null +++ b/src/Tests/CacheabilityTest.php @@ -0,0 +1,95 @@ +getTestServer(); + $this->getTestIndex(); + + // Setup example structure and content and populate the test index with that + // content. + $this->setUpExampleStructure(); + $this->insertExampleContent(); + \Drupal::getContainer() + ->get('search_api.index_task_manager') + ->addItemsAll(Index::load($this->indexId)); + $this->indexItems($this->indexId); + } + + /** + * Tests the cacheability settings of Search API. + */ + public function testFramework() { + $this->drupalLogin($this->adminUser); + + $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'); + + // Test the cacheability headers of a backend that supports cacheing. + \Drupal::state()->set('search_api_test_backend.feature.cacheable_dependency', 100); + + $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->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'); + } + +} diff --git a/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php b/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php index 5032989..a762c3e 100644 --- a/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php +++ b/tests/search_api_test_backend/src/Plugin/search_api/backend/TestBackend.php @@ -46,7 +46,15 @@ class TestBackend extends BackendPluginBase { * {@inheritdoc} */ public function supportsFeature($feature) { - return $feature == 'search_api_mlt'; + if (\Drupal::state()->get('search_api_test_backend.feature.cacheable_dependency', FALSE) !== FALSE) { + return TRUE; + } + if ($feature === 'search_api_mlt') { + return TRUE; + } + + // By default, we should return false. + return FALSE; } /** @@ -192,6 +200,10 @@ class TestBackend extends BackendPluginBase { return $remove; } + public function getCacheMaxAge() { + return \Drupal::state()->get('search_api_test_backend.feature.cacheable_dependency', 0); + } + /** * Throws an exception if set in the Drupal state for the given method. *