You are here

protected function ViewsDisplayCachingTest::assertCacheContexts in Search API 8

Checks if the cache contexts in the given render array are as expected.

Parameters

string[] $expected_contexts: An array of cache contexts that are expected to be present in the given render array.

array $render_array: The render array of which to check the cache contexts.

Overrides AssertPageCacheContextsAndTagsTrait::assertCacheContexts

1 call to ViewsDisplayCachingTest::assertCacheContexts()
ViewsDisplayCachingTest::assertViewsCacheability in tests/src/Kernel/Views/ViewsDisplayCachingTest.php
Checks the cacheability metadata of the given view.

File

tests/src/Kernel/Views/ViewsDisplayCachingTest.php, line 266

Class

ViewsDisplayCachingTest
Tests the Search API caching plugins for Views.

Namespace

Drupal\Tests\search_api\Kernel\Views

Code

protected function assertCacheContexts(array $expected_contexts, array $render_array) {

  // Merge in the default cache contexts in the expected contexts.
  $default_contexts = [
    // Default contexts that are always provided by core.
    'languages:' . LanguageInterface::TYPE_INTERFACE,
    'theme',
    'user.permissions',
    // We are showing translatable content, so it varies by content language.
    'languages:' . LanguageInterface::TYPE_CONTENT,
    // Our view has a pager, so we vary by query arguments.
    // @see \Drupal\views\Plugin\views\pager\SqlBase::getCacheContexts()
    'url.query_args',
    // Since our view has exposed filters, it varies by url.
    // @see \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginBase::getCacheContexts()
    'url',
  ];
  $expected_contexts = Cache::mergeContexts($expected_contexts, $default_contexts);
  $actual_contexts = CacheableMetadata::createFromRenderArray($render_array)
    ->getCacheContexts();
  sort($expected_contexts);
  sort($actual_contexts);
  $this
    ->assertEquals($expected_contexts, $actual_contexts);
}