You are here

public function CacheTest::testCacheData in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/views/src/Tests/Plugin/CacheTest.php \Drupal\views\Tests\Plugin\CacheTest::testCacheData()

Tests the data contained in cached items.

File

core/modules/views/src/Tests/Plugin/CacheTest.php, line 324
Contains \Drupal\views\Tests\Plugin\CacheTest.

Class

CacheTest
Tests pluggable caching for views.

Namespace

Drupal\views\Tests\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', array(
    'type' => 'time',
    'options' => array(
      '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
    ->assertTrue(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
      ->assertIdentical($row->_entity, NULL, 'Cached row "_entity" property is NULL');
    $this
      ->assertIdentical($row->_relationship_entities, [], 'Cached row "_relationship_entities" property is empty');
  }
}