You are here

public function CacheTest::testCacheContextIntegration in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest::testCacheContextIntegration()
  2. 10 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest::testCacheContextIntegration()

Tests the cache context integration for views result cache.

File

core/modules/views/tests/src/Kernel/Plugin/CacheTest.php, line 357

Class

CacheTest
Tests pluggable caching for views.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testCacheContextIntegration() {
  $view = Views::getView('test_cache');
  $view
    ->setDisplay('page_2');
  \Drupal::state()
    ->set('views_test_cache_context', 'George');
  $this
    ->executeView($view);
  $map = [
    'views_test_data_name' => 'name',
  ];
  $this
    ->assertIdenticalResultset($view, [
    [
      'name' => 'George',
    ],
  ], $map);

  // Update the entry in the DB to ensure that result caching works.
  \Drupal::database()
    ->update('views_test_data')
    ->condition('name', 'George')
    ->fields([
    'name' => 'egroeG',
  ])
    ->execute();
  $view = Views::getView('test_cache');
  $view
    ->setDisplay('page_2');
  $this
    ->executeView($view);
  $this
    ->assertIdenticalResultset($view, [
    [
      'name' => 'George',
    ],
  ], $map);

  // Now change the cache context value, a different query should be executed.
  $view = Views::getView('test_cache');
  $view
    ->setDisplay('page_2');
  \Drupal::state()
    ->set('views_test_cache_context', 'Paul');
  $this
    ->executeView($view);
  $this
    ->assertIdenticalResultset($view, [
    [
      'name' => 'Paul',
    ],
  ], $map);
}