You are here

public function StyleSerializerTest::testRestRenderCaching in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Tests/Views/StyleSerializerTest.php \Drupal\rest\Tests\Views\StyleSerializerTest::testRestRenderCaching()

Tests REST export with views render caching enabled.

File

core/modules/rest/src/Tests/Views/StyleSerializerTest.php, line 206
Contains \Drupal\rest\Tests\Views\StyleSerializerTest.

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\rest\Tests\Views

Code

public function testRestRenderCaching() {
  $this
    ->drupalLogin($this->adminUser);

  /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
  $render_cache = \Drupal::service('render_cache');

  // Enable render caching for the views.

  /** @var \Drupal\views\ViewEntityInterface $storage */
  $storage = View::load('test_serializer_display_entity');
  $options =& $storage
    ->getDisplay('default');
  $options['display_options']['cache'] = [
    'type' => 'tag',
  ];
  $storage
    ->save();
  $original = DisplayPluginBase::buildBasicRenderable('test_serializer_display_entity', 'rest_export_1');

  // Ensure that there is no corresponding render cache item yet.
  $original['#cache'] += [
    'contexts' => [],
  ];
  $original['#cache']['contexts'] = Cache::mergeContexts($original['#cache']['contexts'], $this->container
    ->getParameter('renderer.config')['required_cache_contexts']);
  $cache_tags = [
    'config:views.view.test_serializer_display_entity',
    'entity_test:1',
    'entity_test:10',
    'entity_test:2',
    'entity_test:3',
    'entity_test:4',
    'entity_test:5',
    'entity_test:6',
    'entity_test:7',
    'entity_test:8',
    'entity_test:9',
    'entity_test_list',
  ];
  $cache_contexts = [
    'entity_test_view_grants',
    'languages:language_interface',
    'theme',
    'request_format',
  ];
  $this
    ->assertFalse($render_cache
    ->get($original));

  // Request the page, once in XML and once in JSON to ensure that the caching
  // varies by it.
  $result1 = $this
    ->drupalGetJSON('test/serialize/entity');
  $this
    ->addRequestWithFormat('json');
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertCacheContexts($cache_contexts);
  $this
    ->assertCacheTags($cache_tags);
  $this
    ->assertTrue($render_cache
    ->get($original));
  $result_xml = $this
    ->drupalGetWithFormat('test/serialize/entity', 'xml');
  $this
    ->addRequestWithFormat('xml');
  $this
    ->assertHeader('content-type', 'text/xml; charset=UTF-8');
  $this
    ->assertCacheContexts($cache_contexts);
  $this
    ->assertCacheTags($cache_tags);
  $this
    ->assertTrue($render_cache
    ->get($original));

  // Ensure that the XML output is different from the JSON one.
  $this
    ->assertNotEqual($result1, $result_xml);

  // Ensure that the cached page works.
  $result2 = $this
    ->drupalGetJSON('test/serialize/entity');
  $this
    ->addRequestWithFormat('json');
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertEqual($result2, $result1);
  $this
    ->assertCacheContexts($cache_contexts);
  $this
    ->assertCacheTags($cache_tags);
  $this
    ->assertTrue($render_cache
    ->get($original));

  // Create a new entity and ensure that the cache tags are taken over.
  EntityTest::create([
    'name' => 'test_11',
    'user_id' => $this->adminUser
      ->id(),
  ])
    ->save();
  $result3 = $this
    ->drupalGetJSON('test/serialize/entity');
  $this
    ->addRequestWithFormat('json');
  $this
    ->assertHeader('content-type', 'application/json');
  $this
    ->assertNotEqual($result3, $result2);

  // Add the new entity cache tag and remove the first one, because we just
  // show 10 items in total.
  $cache_tags[] = 'entity_test:11';
  unset($cache_tags[array_search('entity_test:1', $cache_tags)]);
  $this
    ->assertCacheContexts($cache_contexts);
  $this
    ->assertCacheTags($cache_tags);
  $this
    ->assertTrue($render_cache
    ->get($original));
}