You are here

public function CacheTest::testCacheData 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::testCacheData()
  2. 10 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest::testCacheData()

Tests the data contained in cached items.

File

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

Class

CacheTest
Tests pluggable caching for views.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testCacheData() {
  for ($i = 1; $i <= 5; $i++) {
    Node::create([
      'title' => $this
        ->randomMachineName(8),
      'type' => 'page',
    ])
      ->save();
  }
  $view = Views::getView('test_display');
  $view
    ->setDisplay();
  $view->display_handler
    ->overrideOption('cache', [
    'type' => 'time',
    'options' => [
      'results_lifespan' => '3600',
      'output_lifespan' => '3600',
    ],
  ]);
  $this
    ->executeView($view);

  // Get the cache item.
  $cid = $view->display_handler
    ->getPlugin('cache')
    ->generateResultsKey();
  $cache = \Drupal::cache('data')
    ->get($cid);

  // Assert there are results, empty results would mean this test case would
  // pass otherwise.
  $this
    ->assertGreaterThan(0, count($cache->data['result']), 'Results saved in cached data.');

  // Assert each row doesn't contain '_entity' or '_relationship_entities'
  // items.
  foreach ($cache->data['result'] as $row) {
    $this
      ->assertNull($row->_entity, 'Cached row "_entity" property is NULL');
    $this
      ->assertSame([], $row->_relationship_entities, 'Cached row "_relationship_entities" property is empty');
  }
}