function CacheTest::testCacheData

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest::testCacheData()
  2. 8.9.x core/modules/views/tests/src/Kernel/Plugin/CacheTest.php \Drupal\Tests\views\Kernel\Plugin\CacheTest::testCacheData()
  3. 11.x 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 320

Class

CacheTest
Tests pluggable caching for views.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testCacheData() : void {
    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');
    }
}

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