diff --git a/core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.info.yml b/core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.info.yml new file mode 100644 index 0000000000..9a65a4be56 --- /dev/null +++ b/core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.info.yml @@ -0,0 +1,4 @@ +name: 'JSON API test non-cacheable methods' +type: module +package: Testing +core: 8.x diff --git a/core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.module b/core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.module new file mode 100644 index 0000000000..cd78294d78 --- /dev/null +++ b/core/modules/jsonapi/tests/modules/jsonapi_test_non_cacheable_methods/jsonapi_test_non_cacheable_methods.module @@ -0,0 +1,25 @@ +')->toString(); +} + +/** + * Implements hook_entity_predelete(). + */ +function jsonapi_test_non_cacheable_methods_entity_predelete(EntityInterface $entity) { + Url::fromRoute('')->toString(); +} diff --git a/core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php b/core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php index 2aec3d96a5..a59478d9dd 100644 --- a/core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php +++ b/core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php @@ -1240,4 +1240,67 @@ public function testAliasedFieldsWithVirtualRelationships() { $this->assertSame(200, $response->getStatusCode()); } + /** + * Tests that caching isn't happening for non-cacheable methods. + * + * @see https://www.drupal.org/project/drupal/issues/3072076 + */ + public function testNonCacheableMethods() { + $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', + 'PATCH', + 'DELETE', + ]; + $non_post_request_options = $base_request_options + [ + RequestOptions::JSON => [ + 'data' => [ + 'type' => 'node--article', + 'id' => $node->uuid(), + ], + ], + ]; + foreach ($methods as $method) { + $response = $this->request($method, Url::fromUri('internal:/jsonapi/node/article/' . $node->uuid()), $non_post_request_options); + $this->assertSame($method === 'DELETE' ? 204 : 200, $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()); + } + }