You are here

public function JsonApiRegressionTest::testPageCacheFromIssue3009596 in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testPageCacheFromIssue3009596()
  2. 10 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testPageCacheFromIssue3009596()

Ensures that Drupal's page cache is effective.

See also

https://www.drupal.org/project/drupal/issues/3009596

File

core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php, line 594

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPageCacheFromIssue3009596() {
  $anonymous_role = Role::load(RoleInterface::ANONYMOUS_ID);
  $anonymous_role
    ->grantPermission('access content');
  $anonymous_role
    ->trustData()
    ->save();
  NodeType::create([
    'type' => 'emu_fact',
  ])
    ->save();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $node = Node::create([
    'type' => 'emu_fact',
    'title' => "Emus don't say moo!",
  ]);
  $node
    ->save();
  $request_options = [
    RequestOptions::HEADERS => [
      'Accept' => 'application/vnd.api+json',
    ],
  ];
  $node_url = Url::fromUri('internal:/jsonapi/node/emu_fact/' . $node
    ->uuid());

  // The first request should be a cache MISS.
  $response = $this
    ->request('GET', $node_url, $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->assertSame('MISS', $response
    ->getHeader('X-Drupal-Cache')[0]);

  // The second request should be a cache HIT.
  $response = $this
    ->request('GET', $node_url, $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->assertSame('HIT', $response
    ->getHeader('X-Drupal-Cache')[0]);
}