function StyleSerializerTest::testRestRenderCaching

Same name in other branches
  1. 8.9.x core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testRestRenderCaching()

Tests REST export with views render caching enabled.

File

core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php, line 289

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\Tests\rest\Functional\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 = Json::decode($this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'json',
        ],
    ]));
    $this->addRequestWithFormat('json');
    $this->assertSession()
        ->responseHeaderEquals('content-type', 'application/json');
    $this->assertCacheContexts($cache_contexts);
    $this->assertCacheTags($cache_tags);
    $this->assertNotEmpty($render_cache->get($original));
    $result_xml = $this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'xml',
        ],
    ]);
    $this->addRequestWithFormat('xml');
    $this->assertSession()
        ->responseHeaderEquals('content-type', 'text/xml; charset=UTF-8');
    $this->assertCacheContexts($cache_contexts);
    $this->assertCacheTags($cache_tags);
    $this->assertNotEmpty($render_cache->get($original));
    // Ensure that the XML output is different from the JSON one.
    $this->assertNotEquals($result1, $result_xml);
    // Ensure that the cached page works.
    $result2 = Json::decode($this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'json',
        ],
    ]));
    $this->addRequestWithFormat('json');
    $this->assertSession()
        ->responseHeaderEquals('content-type', 'application/json');
    $this->assertEquals($result1, $result2);
    $this->assertCacheContexts($cache_contexts);
    $this->assertCacheTags($cache_tags);
    $this->assertNotEmpty($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 = Json::decode($this->drupalGet('test/serialize/entity', [
        'query' => [
            '_format' => 'json',
        ],
    ]));
    $this->addRequestWithFormat('json');
    $this->assertSession()
        ->responseHeaderEquals('content-type', 'application/json');
    $this->assertNotEquals($result2, $result3);
    // 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->assertNotEmpty($render_cache->get($original));
}

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