function CacheTest::testCacheContextIntegration

Same name and namespace in other branches
  1. 8.9.x 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()
  3. 11.x 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' => 'notGeorge',
    ])
        ->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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.