View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Access\AccessResultReasonInterface;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\jsonapi\Normalizer\HttpExceptionNormalizer;
use Drupal\jsonapi\ResourceResponse;
use Psr\Http\Message\ResponseInterface;
trait ResourceResponseTestTrait {
protected static function toCollectionResourceResponse(array $responses, $self_link, $is_multiple) {
assert(count($responses) > 0);
$merged_document = [];
$merged_cacheability = new CacheableMetadata();
foreach ($responses as $response) {
$response_document = $response
->getResponseData();
$merge_errors = function ($errors) use (&$merged_document, $is_multiple) {
foreach ($errors as $error) {
if ($is_multiple) {
$merged_document['meta']['errors'][] = $error;
}
else {
$merged_document['errors'][] = $error;
}
}
};
if (!empty($response_document['errors'])) {
$merge_errors($response_document['errors']);
}
if (!empty($response_document['meta']['errors'])) {
$merge_errors($response_document['meta']['errors']);
}
elseif (isset($response_document['data'])) {
$response_data = $response_document['data'];
if (!isset($merged_document['data'])) {
$merged_document['data'] = static::isResourceIdentifier($response_data) && $is_multiple ? [
$response_data,
] : $response_data;
}
else {
$response_resources = static::isResourceIdentifier($response_data) ? [
$response_data,
] : $response_data;
foreach ($response_resources as $response_resource) {
$merged_document['data'][] = $response_resource;
}
}
}
$merged_cacheability
->addCacheableDependency($response
->getCacheableMetadata());
}
if (isset($merged_document['errors'])) {
unset($merged_document['links']);
}
else {
$merged_document['links'] = [
'self' => $self_link,
];
$merged_document['jsonapi'] = [
'meta' => [
'links' => [
'self' => 'http://jsonapi.org/format/1.0/',
],
],
'version' => '1.0',
];
}
$merged_response_code = array_reduce($responses, function ($merged_response_code, $response) {
$response_code = $response
->getStatusCode();
assert($response_code >= 200 && $response_code < 500, 'Responses must be valid and complete to be merged.');
assert(!($response_code >= 300 && $response_code < 400), 'Redirect responses cannot be merged.');
if (is_null($merged_response_code)) {
return $response_code;
}
elseif ($merged_response_code === $response_code) {
return $merged_response_code;
}
elseif ($response_code >= 200 && $response_code < 300 || $merged_response_code >= 200 && $merged_response_code < 300) {
return 200;
}
else {
return 400;
}
}, NULL);
return (new ResourceResponse($merged_document, $merged_response_code))
->addCacheableDependency($merged_cacheability);
}
protected function getExpectedIncludedResourceResponse(array $include_paths, array $request_options) {
$resource_data = array_reduce($include_paths, function ($data, $path) use ($request_options) {
$field_names = explode('.', $path);
$entity = $this->entity;
foreach ($field_names as $field_name) {
$collected_responses = [];
$field_access = static::entityFieldAccess($entity, $field_name, 'view', $this->account);
if (!$field_access
->isAllowed()) {
$collected_responses[] = static::getAccessDeniedResponse($entity, $field_access, $field_name, 'The current user is not allowed to view this relationship.');
break;
}
if ($target_entity = $entity->{$field_name}->entity) {
$target_access = static::entityAccess($target_entity, 'view', $this->account);
if (!$target_access
->isAllowed()) {
$resource_identifier = static::toResourceIdentifier($target_entity);
if (!static::collectionHasResourceIdentifier($resource_identifier, $data['already_checked'])) {
$data['already_checked'][] = $resource_identifier;
$error_id = '/' . $resource_identifier['type'] . '/' . $resource_identifier['id'];
$collected_responses[] = static::getAccessDeniedResponse($entity, $target_access, NULL, NULL, '/data', $error_id);
}
break;
}
}
$psr_responses = $this
->getResponses([
static::getRelatedLink(static::toResourceIdentifier($entity), $field_name),
], $request_options);
$collected_responses[] = static::toCollectionResourceResponse(static::toResourceResponses($psr_responses), NULL, TRUE);
$entity = $entity->{$field_name}->entity;
}
if (!empty($collected_responses)) {
$data['responses'][$path] = static::toCollectionResourceResponse($collected_responses, NULL, TRUE);
}
return $data;
}, [
'responses' => [],
'already_checked' => [],
]);
return static::toCollectionResourceResponse($resource_data['responses'], NULL, TRUE);
}
protected static function toResourceResponses(array $responses) {
return array_map([
self::class,
'toResourceResponse',
], $responses);
}
protected static function toResourceResponse(ResponseInterface $response) {
$cacheability = new CacheableMetadata();
if ($cache_tags = $response
->getHeader('X-Drupal-Cache-Tags')) {
$cacheability
->addCacheTags(explode(' ', $cache_tags[0]));
}
if ($cache_contexts = $response
->getHeader('X-Drupal-Cache-Contexts')) {
$cacheability
->addCacheContexts(explode(' ', $cache_contexts[0]));
}
if ($dynamic_cache = $response
->getHeader('X-Drupal-Dynamic-Cache')) {
$cacheability
->setCacheMaxAge($dynamic_cache[0] === 'UNCACHEABLE' ? 0 : Cache::PERMANENT);
}
$related_document = Json::decode($response
->getBody());
$resource_response = new ResourceResponse($related_document, $response
->getStatusCode());
return $resource_response
->addCacheableDependency($cacheability);
}
protected static function toResourceIdentifier(EntityInterface $entity) {
return [
'type' => $entity
->getEntityTypeId() . '--' . $entity
->bundle(),
'id' => $entity
->uuid(),
];
}
protected static function isResourceIdentifier(array $data) {
return array_key_exists('type', $data) && array_key_exists('id', $data);
}
protected static function sortResourceCollection(array &$resources) {
usort($resources, function ($a, $b) {
return strcmp("{$a['type']}:{$a['id']}", "{$b['type']}:{$b['id']}");
});
}
protected static function collectionHasResourceIdentifier(array $needle, array $haystack) {
foreach ($haystack as $resource) {
if ($resource['type'] == $needle['type'] && $resource['id'] == $needle['id']) {
return TRUE;
}
}
return FALSE;
}
protected static function getLinkPaths(array $relationship_field_names, $type) {
assert($type === 'relationship' || $type === 'related');
return array_reduce($relationship_field_names, function ($link_paths, $relationship_field_name) use ($type) {
$tail = $type === 'relationship' ? 'self' : $type;
$link_paths[$relationship_field_name] = "data.relationships.{$relationship_field_name}.links.{$tail}";
return $link_paths;
}, []);
}
protected static function extractLinks(array $link_paths, array $document) {
return array_map(function ($link_path) use ($document) {
$link = array_reduce(explode('.', $link_path), 'array_column', [
$document,
]);
return $link ? reset($link) : NULL;
}, $link_paths);
}
protected static function getResourceLinks(array $resource_identifiers) {
return array_map([
static::class,
'getResourceLink',
], $resource_identifiers);
}
protected static function getResourceLink(array $resource_identifier) {
assert(static::isResourceIdentifier($resource_identifier));
$resource_type = $resource_identifier['type'];
$resource_id = $resource_identifier['id'];
$entity_type_id = explode('--', $resource_type)[0];
$url = Url::fromRoute(sprintf('jsonapi.%s.individual', $resource_type), [
$entity_type_id => $resource_id,
]);
return $url
->setAbsolute()
->toString();
}
protected static function getRelationshipLink(array $resource_identifier, $relationship_field_name) {
return static::getResourceLink($resource_identifier) . "/relationships/{$relationship_field_name}";
}
protected static function getRelatedLink(array $resource_identifier, $relationship_field_name) {
return static::getResourceLink($resource_identifier) . "/{$relationship_field_name}";
}
protected function getRelatedResponses(array $relationship_field_names, array $request_options, EntityInterface $entity = NULL) {
$entity = $entity ?: $this->entity;
$links = array_map(function ($relationship_field_name) use ($entity) {
return static::getRelatedLink(static::toResourceIdentifier($entity), $relationship_field_name);
}, array_combine($relationship_field_names, $relationship_field_names));
return $this
->getResponses($links, $request_options);
}
protected function getRelationshipResponses(array $relationship_field_names, array $request_options) {
$links = array_map(function ($relationship_field_name) {
return static::getRelationshipLink(static::toResourceIdentifier($this->entity), $relationship_field_name);
}, array_combine($relationship_field_names, $relationship_field_names));
return $this
->getResponses($links, $request_options);
}
protected function getResponses(array $links, array $request_options) {
return array_reduce(array_keys($links), function ($related_responses, $key) use ($links, $request_options) {
$related_responses[$key] = $this
->request('GET', Url::fromUri($links[$key]), $request_options);
return $related_responses;
}, []);
}
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);
}
}