function JsonApiRegressionTest::testNonCacheableMethods

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testNonCacheableMethods()
  2. 11.x core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testNonCacheableMethods()

Tests that caching isn't happening for non-cacheable methods.

See also

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

File

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

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testNonCacheableMethods() : void {
    $this->container
        ->get('module_installer')
        ->install([
        'jsonapi_test_non_cacheable_methods',
    ], TRUE);
    $this->config('jsonapi.settings')
        ->set('read_only', FALSE)
        ->save(TRUE);
    $node = Node::create([
        'type' => 'article',
        'title' => 'Llama non-cacheable',
    ]);
    $node->save();
    $user = $this->drupalCreateUser([
        'access content',
        'create article content',
        'edit any article content',
        'delete any article content',
    ]);
    $base_request_options = [
        RequestOptions::HEADERS => [
            'Content-Type' => 'application/vnd.api+json',
            'Accept' => 'application/vnd.api+json',
        ],
        RequestOptions::AUTH => [
            $user->getAccountName(),
            $user->pass_raw,
        ],
    ];
    $methods = [
        'HEAD',
        'GET',
    ];
    foreach ($methods as $method) {
        $response = $this->request($method, Url::fromUri('internal:/jsonapi/node/article/' . $node->uuid()), $base_request_options);
        $this->assertSame(200, $response->getStatusCode());
    }
    $patch_request_options = $base_request_options + [
        RequestOptions::JSON => [
            'data' => [
                'type' => 'node--article',
                'id' => $node->uuid(),
            ],
        ],
    ];
    $response = $this->request('PATCH', Url::fromUri('internal:/jsonapi/node/article/' . $node->uuid()), $patch_request_options);
    $this->assertSame(200, $response->getStatusCode());
    $response = $this->request('DELETE', Url::fromUri('internal:/jsonapi/node/article/' . $node->uuid()), $base_request_options);
    $this->assertSame(204, $response->getStatusCode());
    $post_request_options = $base_request_options + [
        RequestOptions::JSON => [
            'data' => [
                'type' => 'node--article',
                'attributes' => [
                    'title' => 'Llama non-cacheable',
                ],
            ],
        ],
    ];
    $response = $this->request('POST', Url::fromUri('internal:/jsonapi/node/article'), $post_request_options);
    $this->assertSame(201, $response->getStatusCode());
}

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