You are here

protected static function ResourceResponseTestTrait::getAccessDeniedResponse in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ResourceResponseTestTrait.php \Drupal\Tests\jsonapi\Functional\ResourceResponseTestTrait::getAccessDeniedResponse()

Gets a generic forbidden response.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to generate the forbidden response.

\Drupal\Core\Access\AccessResultInterface $access: The denied AccessResult. This can carry a reason and cacheability data.

string|null $relationship_field_name: (optional) The field name to which the forbidden result applies. Useful for testing related/relationship routes and includes.

string|null $detail: (optional) Details for the JSON API error object.

string|null $pointer: (optional) Document pointer for the JSON API error object.

string|null $id: (optional) ID for the JSON API error object.

Return value

\Drupal\jsonapi\ResourceResponse The forbidden ResourceResponse.

2 calls to ResourceResponseTestTrait::getAccessDeniedResponse()
ResourceResponseTestTrait::getExpectedIncludedResourceResponse in tests/src/Functional/ResourceResponseTestTrait.php
Gets an array of expected ResourceResponses for the given include paths.
ResourceTestBase::getExpectedGetRelationshipResponse in tests/src/Functional/ResourceTestBase.php
Gets an expected ResourceResponse for the given relationship.

File

tests/src/Functional/ResourceResponseTestTrait.php, line 473

Class

ResourceResponseTestTrait
Utility methods for handling resource responses.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected static function getAccessDeniedResponse(EntityInterface $entity, AccessResultInterface $access, $relationship_field_name = NULL, $detail = NULL, $pointer = NULL, $id = NULL) {
  $detail = $detail ? $detail : 'The current user is not allowed to GET the selected resource.';
  if ($access instanceof AccessResultReasonInterface && ($reason = $access
    ->getReason())) {
    $detail .= ' ' . $reason;
  }
  $resource_identifier = static::toResourceIdentifier($entity);
  $error = [
    'status' => 403,
    'title' => 'Forbidden',
    'detail' => $detail,
    'links' => [
      'info' => HttpExceptionNormalizer::getInfoUrl(403),
    ],
    'code' => 0,
  ];
  if (!is_null($id)) {
    $error['id'] = $id;
  }
  if ($relationship_field_name || $pointer) {
    $error['source']['pointer'] = $pointer ? $pointer : $relationship_field_name;
  }
  return (new ResourceResponse([
    'errors' => [
      $error,
    ],
  ], 403))
    ->addCacheableDependency($access);
}