function JsonApiRegressionTest::testPageCacheFromIssue3009596

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testPageCacheFromIssue3009596()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testPageCacheFromIssue3009596()
  3. 11.x 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 440

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPageCacheFromIssue3009596() : void {
    $anonymous_role = Role::load(RoleInterface::ANONYMOUS_ID);
    $anonymous_role->grantPermission('access content');
    $anonymous_role->trustData()
        ->save();
    NodeType::create([
        'type' => 'emu_fact',
        'name' => '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]);
}

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