You are here

public function RestfulVariableTestCase::testRenderCache in RESTful 7.2

Same name and namespace in other branches
  1. 7 tests/RestfulVariableTestCase.test \RestfulVariableTestCase::testRenderCache()

Test the render cache.

File

tests/RestfulVariableTestCase.test, line 137
Contains RestfulVariableTestCase.

Class

RestfulVariableTestCase

Code

public function testRenderCache() {

  // Create a test variable.

  /* @var \Drupal\restful\Plugin\resource\Decorators\CacheDecoratedResource $handler */
  $handler = restful()
    ->getResourceManager()
    ->getPlugin('variables:1.0');
  $formatter = restful()
    ->getFormatterManager()
    ->negotiateFormatter(NULL, 'json');
  $formatter
    ->setResource($handler);
  $parsed_body = array(
    'variable_name' => 'test_variable_cache',
    'variable_value' => TRUE,
  );
  $handler
    ->doPost($parsed_body);
  $created = variable_get('test_variable_cache');
  $this
    ->assertNotNull($created, 'The cache variable has been created.');

  // Populate the cache entries.
  $account = $this
    ->drupalCreateUser();
  $handler
    ->setAccount($account);
  $formatter
    ->prepare($handler
    ->doGet('test_variable_cache'));

  // Get the cache value.
  $cache_fragments = $handler
    ->getDataProvider()
    ->getCacheFragments('test_variable_cache');
  $cache_fragments
    ->set('formatter', 'json');
  $render_cache = RenderCache::create($cache_fragments, $handler
    ->getCacheController());
  $cache_data = $render_cache
    ->get();
  $this
    ->assertNotNull($cache_data->data, 'Cache data is present.');
  $this
    ->assertEqual($cache_data->data['variable_name'], 'test_variable_cache', 'The variable name was retrieved from the cache.');
  $this
    ->assertEqual($cache_data->data['variable_value'], TRUE, 'The variable value was retrieved from the cache.');
}