You are here

public function CacheTest::testTimeResultCachingWithFilter in Drupal 8

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

Tests result caching with filters.

See also

views_plugin_cache_time

File

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

Class

CacheTest
Tests pluggable caching for views.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testTimeResultCachingWithFilter() {

  // Check that we can find the test filter plugin.
  $plugin = $this->container
    ->get('plugin.manager.views.filter')
    ->createInstance('test_filter');
  $this
    ->assertInstanceOf(FilterPlugin::class, $plugin);
  $view = Views::getView('test_filter');
  $view
    ->initDisplay();
  $view->display_handler
    ->overrideOption('cache', [
    'type' => 'time',
    'options' => [
      'results_lifespan' => '3600',
      'output_lifespan' => '3600',
    ],
  ]);

  // Change the filtering.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'test_filter' => [
      'id' => 'test_filter',
      'table' => 'views_test_data',
      'field' => 'name',
      'operator' => '=',
      'value' => 'John',
      'group' => 0,
    ],
  ]);
  $this
    ->executeView($view);

  // Get the cache item.
  $cid1 = $view->display_handler
    ->getPlugin('cache')
    ->generateResultsKey();

  // Build the expected result.
  $dataset = [
    [
      'name' => 'John',
    ],
  ];

  // Verify the result.
  $this
    ->assertCount(1, $view->result, 'The number of returned rows match.');
  $this
    ->assertIdenticalResultSet($view, $dataset, [
    'views_test_data_name' => 'name',
  ]);
  $view
    ->destroy();
  $view
    ->initDisplay();

  // Change the filtering.
  $view->displayHandlers
    ->get('default')
    ->overrideOption('filters', [
    'test_filter' => [
      'id' => 'test_filter',
      'table' => 'views_test_data',
      'field' => 'name',
      'operator' => '=',
      'value' => 'Ringo',
      'group' => 0,
    ],
  ]);
  $this
    ->executeView($view);

  // Get the cache item.
  $cid2 = $view->display_handler
    ->getPlugin('cache')
    ->generateResultsKey();
  $this
    ->assertNotEqual($cid1, $cid2, "Results keys are different.");

  // Build the expected result.
  $dataset = [
    [
      'name' => 'Ringo',
    ],
  ];

  // Verify the result.
  $this
    ->assertCount(1, $view->result, 'The number of returned rows match.');
  $this
    ->assertIdenticalResultSet($view, $dataset, [
    'views_test_data_name' => 'name',
  ]);
}