public function NodeTest::testGetIndividual in JSON:API 8
Same name and namespace in other branches
- 8.2 tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testGetIndividual()
Tests GETting an individual resource, plus edge cases to ensure good DX.
Overrides ResourceTestBase::testGetIndividual
File
- tests/
src/ Functional/ NodeTest.php, line 299
Class
- NodeTest
- JSON API integration test for the "Node" content entity type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testGetIndividual() {
parent::testGetIndividual();
// Unpublish node.
$this->entity
->setUnpublished()
->save();
// @todo Remove line below in favor of commented line in https://www.drupal.org/project/jsonapi/issues/2878463.
$url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
static::$entityTypeId => $this->entity
->uuid(),
]);
/* $url = $this->entity->toUrl('jsonapi'); */
$request_options = $this
->getAuthenticationRequestOptions();
// 403 when accessing own unpublished node.
$response = $this
->request('GET', $url, $request_options);
// @todo Remove $expected + assertResourceResponse() in favor of the commented line below once https://www.drupal.org/project/jsonapi/issues/2943176 lands.
$expected_document = [
'errors' => [
[
'title' => 'Forbidden',
'status' => 403,
'detail' => 'The current user is not allowed to GET the selected resource.',
'links' => [
'info' => HttpExceptionNormalizer::getInfoUrl(403),
],
'code' => 0,
'id' => '/node--camelids/' . $this->entity
->uuid(),
'source' => [
'pointer' => '/data',
],
],
],
];
$this
->assertResourceResponse(403, $expected_document, $response);
/* $this->assertResourceErrorResponse(403, 'The current user is not allowed to GET the selected resource.', $response, '/data'); */
// 200 after granting permission.
$this
->grantPermissionsToTestedRole([
'view own unpublished content',
]);
$response = $this
->request('GET', $url, $request_options);
// The response varies by 'user', causing the 'user.permissions' cache
// context to be optimized away.
$expected_cache_contexts = Cache::mergeContexts($this
->getExpectedCacheContexts(), [
'user',
]);
$expected_cache_contexts = array_diff($expected_cache_contexts, [
'user.permissions',
]);
$this
->assertResourceResponse(200, FALSE, $response, $this
->getExpectedCacheTags(), $expected_cache_contexts, FALSE, 'UNCACHEABLE');
}