You are here

public function PageCacheTest::testConfigChangePageCache in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/rest/src/Tests/PageCacheTest.php \Drupal\rest\Tests\PageCacheTest::testConfigChangePageCache()

Tests that configuration changes also clear the page cache.

File

core/modules/rest/src/Tests/PageCacheTest.php, line 27
Contains \Drupal\rest\Tests\PageCacheTest.

Class

PageCacheTest
Tests page caching for REST GET requests.

Namespace

Drupal\rest\Tests

Code

public function testConfigChangePageCache() {
  $this
    ->enableService('entity:entity_test', 'GET');

  // Allow anonymous users to issue GET requests.
  $permissions = $this
    ->entityPermissions('entity_test', 'view');
  $permissions[] = 'restful get entity:entity_test';
  user_role_grant_permissions('anonymous', $permissions);

  // Create an entity programmatically.
  $entity = $this
    ->entityCreate('entity_test');
  $entity
    ->save();

  // Read it over the REST API.
  $this
    ->httpRequest($entity
    ->urlInfo()
    ->setRouteParameter('_format', $this->defaultFormat), 'GET', NULL, $this->defaultMimeType);
  $this
    ->assertResponse(200, 'HTTP response code is correct.');
  $this
    ->assertHeader('x-drupal-cache', 'MISS');
  $this
    ->assertCacheTag('config:rest.settings');
  $this
    ->assertCacheTag('entity_test:1');

  // Read it again, should be page-cached now.
  $this
    ->httpRequest($entity
    ->urlInfo()
    ->setRouteParameter('_format', $this->defaultFormat), 'GET', NULL, $this->defaultMimeType);
  $this
    ->assertResponse(200, 'HTTP response code is correct.');
  $this
    ->assertHeader('x-drupal-cache', 'HIT');
  $this
    ->assertCacheTag('config:rest.settings');
  $this
    ->assertCacheTag('entity_test:1');

  // Trigger a config save which should clear the page cache, so we should get
  // a cache miss now for the same request.
  $this
    ->config('rest.settings')
    ->save();
  $this
    ->httpRequest($entity
    ->urlInfo()
    ->setRouteParameter('_format', $this->defaultFormat), 'GET', NULL, $this->defaultMimeType);
  $this
    ->assertResponse(200, 'HTTP response code is correct.');
  $this
    ->assertHeader('x-drupal-cache', 'MISS');
  $this
    ->assertCacheTag('config:rest.settings');
  $this
    ->assertCacheTag('entity_test:1');
}