Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
9
web/core/modules/hal/hal.info.yml
Normal file
9
web/core/modules/hal/hal.info.yml
Normal file
|
@ -0,0 +1,9 @@
|
|||
name: 'HAL'
|
||||
type: module
|
||||
description: 'Serializes entities using Hypertext Application Language.'
|
||||
package: Web services
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
dependencies:
|
||||
- rest
|
||||
- serialization
|
23
web/core/modules/hal/hal.module
Normal file
23
web/core/modules/hal/hal.module
Normal file
|
@ -0,0 +1,23 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Adds support for serializing entities to Hypertext Application Language.
|
||||
*/
|
||||
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
function hal_help($route_name, RouteMatchInterface $route_match) {
|
||||
switch ($route_name) {
|
||||
case 'help.page.hal':
|
||||
$output = '';
|
||||
$output .= '<h3>' . t('About') . '</h3>';
|
||||
$output .= '<p>' . t('<a href=":hal_spec">Hypertext Application Language (HAL)</a> is a format that supports the linking required for hypermedia APIs.', array(':hal_spec' => 'http://stateless.co/hal_specification.html')) . '</p>';
|
||||
$output .= '<p>' . t('Hypermedia APIs are a style of Web API that uses URIs to identify resources and the <a href="http://wikipedia.org/wiki/Link_relation">link relations</a> between them, enabling API consumers to follow links to discover API functionality.') . '</p>';
|
||||
$output .= '<p>' . t('This module adds support for serializing entities (such as content items, taxonomy terms, etc.) to the JSON version of HAL. For more information, see the <a href=":hal_do">online documentation for the HAL module</a>.', array(':hal_do' => 'https://www.drupal.org/documentation/modules/hal')) . '</p>';
|
||||
return $output;
|
||||
}
|
||||
}
|
32
web/core/modules/hal/hal.services.yml
Normal file
32
web/core/modules/hal/hal.services.yml
Normal file
|
@ -0,0 +1,32 @@
|
|||
services:
|
||||
serializer.normalizer.entity_reference_item.hal:
|
||||
class: Drupal\hal\Normalizer\EntityReferenceItemNormalizer
|
||||
arguments: ['@rest.link_manager', '@serializer.entity_resolver']
|
||||
tags:
|
||||
- { name: normalizer, priority: 10 }
|
||||
serializer.normalizer.field_item.hal:
|
||||
class: Drupal\hal\Normalizer\FieldItemNormalizer
|
||||
tags:
|
||||
- { name: normalizer, priority: 10 }
|
||||
serializer.normalizer.field.hal:
|
||||
class: Drupal\hal\Normalizer\FieldNormalizer
|
||||
tags:
|
||||
- { name: normalizer, priority: 10 }
|
||||
serializer.normalizer.file_entity.hal:
|
||||
class: Drupal\hal\Normalizer\FileEntityNormalizer
|
||||
tags:
|
||||
- { name: normalizer, priority: 20 }
|
||||
arguments: ['@entity.manager', '@http_client', '@rest.link_manager', '@module_handler']
|
||||
serializer.normalizer.entity.hal:
|
||||
class: Drupal\hal\Normalizer\ContentEntityNormalizer
|
||||
arguments: ['@rest.link_manager', '@entity.manager', '@module_handler']
|
||||
tags:
|
||||
- { name: normalizer, priority: 10 }
|
||||
serializer.encoder.hal:
|
||||
class: Drupal\hal\Encoder\JsonEncoder
|
||||
tags:
|
||||
- { name: encoder, priority: 10, format: hal_json }
|
||||
exception.default_json:
|
||||
class: Drupal\hal\EventSubscriber\ExceptionHalJsonSubscriber
|
||||
tags:
|
||||
- { name: event_subscriber }
|
35
web/core/modules/hal/src/Encoder/JsonEncoder.php
Normal file
35
web/core/modules/hal/src/Encoder/JsonEncoder.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Encoder;
|
||||
|
||||
use Symfony\Component\Serializer\Encoder\JsonEncoder as SymfonyJsonEncoder;
|
||||
|
||||
/**
|
||||
* Encodes HAL data in JSON.
|
||||
*
|
||||
* Simply respond to hal_json format requests using the JSON encoder.
|
||||
*/
|
||||
class JsonEncoder extends SymfonyJsonEncoder {
|
||||
|
||||
/**
|
||||
* The formats that this Encoder supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsEncoding($format) {
|
||||
return $format == $this->format;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDecoding($format) {
|
||||
return $format == $this->format;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\EventSubscriber;
|
||||
|
||||
use Drupal\Core\EventSubscriber\ExceptionJsonSubscriber;
|
||||
|
||||
/**
|
||||
* Handle HAL JSON exceptions the same as JSON exceptions.
|
||||
*/
|
||||
class ExceptionHalJsonSubscriber extends ExceptionJsonSubscriber {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getHandledFormats() {
|
||||
return ['hal_json'];
|
||||
}
|
||||
|
||||
}
|
22
web/core/modules/hal/src/HalServiceProvider.php
Normal file
22
web/core/modules/hal/src/HalServiceProvider.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal;
|
||||
|
||||
use Drupal\Core\DependencyInjection\ContainerBuilder;
|
||||
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
|
||||
|
||||
/**
|
||||
* Adds hal+json as known format.
|
||||
*/
|
||||
class HalServiceProvider implements ServiceModifierInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function alter(ContainerBuilder $container) {
|
||||
if ($container->has('http_middleware.negotiation') && is_a($container->getDefinition('http_middleware.negotiation')->getClass(), '\Drupal\Core\StackMiddleware\NegotiationMiddleware', TRUE)) {
|
||||
$container->getDefinition('http_middleware.negotiation')->addMethodCall('registerFormat', ['hal_json', ['application/hal+json']]);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
262
web/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php
Normal file
262
web/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php
Normal file
|
@ -0,0 +1,262 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Component\Utility\NestedArray;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\rest\LinkManager\LinkManagerInterface;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Converts the Drupal entity object structure to a HAL array structure.
|
||||
*/
|
||||
class ContentEntityNormalizer extends NormalizerBase {
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = 'Drupal\Core\Entity\ContentEntityInterface';
|
||||
|
||||
/**
|
||||
* The hypermedia link manager.
|
||||
*
|
||||
* @var \Drupal\rest\LinkManager\LinkManagerInterface
|
||||
*/
|
||||
protected $linkManager;
|
||||
|
||||
/**
|
||||
* The entity manager.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\EntityManagerInterface
|
||||
*/
|
||||
protected $entityManager;
|
||||
|
||||
/**
|
||||
* The module handler.
|
||||
*
|
||||
* @var \Drupal\Core\Extension\ModuleHandlerInterface
|
||||
*/
|
||||
protected $moduleHandler;
|
||||
|
||||
|
||||
/**
|
||||
* Constructs an ContentEntityNormalizer object.
|
||||
*
|
||||
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
|
||||
* The hypermedia link manager.
|
||||
*/
|
||||
public function __construct(LinkManagerInterface $link_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler) {
|
||||
$this->linkManager = $link_manager;
|
||||
$this->entityManager = $entity_manager;
|
||||
$this->moduleHandler = $module_handler;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($entity, $format = NULL, array $context = array()) {
|
||||
$context += array(
|
||||
'account' => NULL,
|
||||
'included_fields' => NULL,
|
||||
);
|
||||
|
||||
// Create the array of normalized fields, starting with the URI.
|
||||
/** @var $entity \Drupal\Core\Entity\ContentEntityInterface */
|
||||
$normalized = array(
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
'href' => $this->getEntityUri($entity),
|
||||
),
|
||||
'type' => array(
|
||||
'href' => $this->linkManager->getTypeUri($entity->getEntityTypeId(), $entity->bundle(), $context),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
// If the fields to use were specified, only output those field values.
|
||||
if (isset($context['included_fields'])) {
|
||||
$fields = array();
|
||||
foreach ($context['included_fields'] as $field_name) {
|
||||
$fields[] = $entity->get($field_name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$fields = $entity->getFields();
|
||||
}
|
||||
foreach ($fields as $field) {
|
||||
// Continue if the current user does not have access to view this field.
|
||||
if (!$field->access('view', $context['account'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$normalized_property = $this->serializer->normalize($field, $format, $context);
|
||||
$normalized = NestedArray::mergeDeep($normalized, $normalized_property);
|
||||
}
|
||||
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize().
|
||||
*
|
||||
* @param array $data
|
||||
* Entity data to restore.
|
||||
* @param string $class
|
||||
* Unused, entity_create() is used to instantiate entity objects.
|
||||
* @param string $format
|
||||
* Format the given data was extracted from.
|
||||
* @param array $context
|
||||
* Options available to the denormalizer. Keys that can be used:
|
||||
* - request_method: if set to "patch" the denormalization will clear out
|
||||
* all default values for entity fields before applying $data to the
|
||||
* entity.
|
||||
*
|
||||
* @return \Drupal\Core\Entity\EntityInterface
|
||||
* An unserialized entity object containing the data in $data.
|
||||
*
|
||||
* @throws \Symfony\Component\Serializer\Exception\UnexpectedValueException
|
||||
*/
|
||||
public function denormalize($data, $class, $format = NULL, array $context = array()) {
|
||||
// Get type, necessary for determining which bundle to create.
|
||||
if (!isset($data['_links']['type'])) {
|
||||
throw new UnexpectedValueException('The type link relation must be specified.');
|
||||
}
|
||||
|
||||
// Create the entity.
|
||||
$typed_data_ids = $this->getTypedDataIds($data['_links']['type'], $context);
|
||||
$entity_type = $this->entityManager->getDefinition($typed_data_ids['entity_type']);
|
||||
$default_langcode_key = $entity_type->getKey('default_langcode');
|
||||
$langcode_key = $entity_type->getKey('langcode');
|
||||
$values = array();
|
||||
|
||||
// Figure out the language to use.
|
||||
if (isset($data[$default_langcode_key])) {
|
||||
// Find the field item for which the default_lancode value is set to 1 and
|
||||
// set the langcode the right default language.
|
||||
foreach ($data[$default_langcode_key] as $item) {
|
||||
if (!empty($item['value']) && isset($item['lang'])) {
|
||||
$values[$langcode_key] = $item['lang'];
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Remove the default langcode so it does not get iterated over below.
|
||||
unset($data[$default_langcode_key]);
|
||||
}
|
||||
|
||||
if ($entity_type->hasKey('bundle')) {
|
||||
$bundle_key = $entity_type->getKey('bundle');
|
||||
$values[$bundle_key] = $typed_data_ids['bundle'];
|
||||
// Unset the bundle key from data, if it's there.
|
||||
unset($data[$bundle_key]);
|
||||
}
|
||||
|
||||
$entity = $this->entityManager->getStorage($typed_data_ids['entity_type'])->create($values);
|
||||
|
||||
// Remove links from data array.
|
||||
unset($data['_links']);
|
||||
// Get embedded resources and remove from data array.
|
||||
$embedded = array();
|
||||
if (isset($data['_embedded'])) {
|
||||
$embedded = $data['_embedded'];
|
||||
unset($data['_embedded']);
|
||||
}
|
||||
|
||||
// Flatten the embedded values.
|
||||
foreach ($embedded as $relation => $field) {
|
||||
$field_ids = $this->linkManager->getRelationInternalIds($relation);
|
||||
if (!empty($field_ids)) {
|
||||
$field_name = $field_ids['field_name'];
|
||||
$data[$field_name] = $field;
|
||||
}
|
||||
}
|
||||
|
||||
// Pass the names of the fields whose values can be merged.
|
||||
$entity->_restSubmittedFields = array_keys($data);
|
||||
|
||||
// Iterate through remaining items in data array. These should all
|
||||
// correspond to fields.
|
||||
foreach ($data as $field_name => $field_data) {
|
||||
$items = $entity->get($field_name);
|
||||
// Remove any values that were set as a part of entity creation (e.g
|
||||
// uuid). If the incoming field data is set to an empty array, this will
|
||||
// also have the effect of emptying the field in REST module.
|
||||
$items->setValue(array());
|
||||
if ($field_data) {
|
||||
// Denormalize the field data into the FieldItemList object.
|
||||
$context['target_instance'] = $items;
|
||||
$this->serializer->denormalize($field_data, get_class($items), $format, $context);
|
||||
}
|
||||
}
|
||||
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the entity URI.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The entity.
|
||||
* @return string
|
||||
* The entity URI.
|
||||
*/
|
||||
protected function getEntityUri(EntityInterface $entity) {
|
||||
// Some entity types don't provide a canonical link template, at least call
|
||||
// out to ->url().
|
||||
if ($entity->isNew() || !$entity->hasLinkTemplate('canonical')) {
|
||||
return $entity->url('canonical', []);
|
||||
}
|
||||
$url = $entity->urlInfo('canonical', ['absolute' => TRUE]);
|
||||
return $url->setRouteParameter('_format', 'hal_json')->toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the typed data IDs for a type URI.
|
||||
*
|
||||
* @param array $types
|
||||
* The type array(s) (value of the 'type' attribute of the incoming data).
|
||||
* @param array $context
|
||||
* Context from the normalizer/serializer operation.
|
||||
*
|
||||
* @return array
|
||||
* The typed data IDs.
|
||||
*/
|
||||
protected function getTypedDataIds($types, $context = array()) {
|
||||
// The 'type' can potentially contain an array of type objects. By default,
|
||||
// Drupal only uses a single type in serializing, but allows for multiple
|
||||
// types when deserializing.
|
||||
if (isset($types['href'])) {
|
||||
$types = array($types);
|
||||
}
|
||||
|
||||
if (empty($types)) {
|
||||
throw new UnexpectedValueException('No entity type(s) specified');
|
||||
}
|
||||
|
||||
foreach ($types as $type) {
|
||||
if (!isset($type['href'])) {
|
||||
throw new UnexpectedValueException('Type must contain an \'href\' attribute.');
|
||||
}
|
||||
|
||||
$type_uri = $type['href'];
|
||||
|
||||
// Check whether the URI corresponds to a known type on this site. Break
|
||||
// once one does.
|
||||
if ($typed_data_ids = $this->linkManager->getTypeInternalIds($type['href'], $context)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If none of the URIs correspond to an entity type on this site, no entity
|
||||
// can be created. Throw an exception.
|
||||
if (empty($typed_data_ids)) {
|
||||
throw new UnexpectedValueException(sprintf('Type %s does not correspond to an entity on this site.', $type_uri));
|
||||
}
|
||||
|
||||
return $typed_data_ids;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,122 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\rest\LinkManager\LinkManagerInterface;
|
||||
use Drupal\serialization\EntityResolver\EntityResolverInterface;
|
||||
use Drupal\serialization\EntityResolver\UuidReferenceInterface;
|
||||
|
||||
/**
|
||||
* Converts the Drupal entity reference item object to HAL array structure.
|
||||
*/
|
||||
class EntityReferenceItemNormalizer extends FieldItemNormalizer implements UuidReferenceInterface {
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = 'Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem';
|
||||
|
||||
/**
|
||||
* The hypermedia link manager.
|
||||
*
|
||||
* @var \Drupal\rest\LinkManager\LinkManagerInterface
|
||||
*/
|
||||
protected $linkManager;
|
||||
|
||||
/**
|
||||
* The entity resolver.
|
||||
*
|
||||
* @var \Drupal\serialization\EntityResolver\EntityResolverInterface
|
||||
*/
|
||||
protected $entityResolver;
|
||||
|
||||
/**
|
||||
* Constructs an EntityReferenceItemNormalizer object.
|
||||
*
|
||||
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
|
||||
* The hypermedia link manager.
|
||||
* @param \Drupal\serialization\EntityResolver\EntityResolverInterface $entity_Resolver
|
||||
* The entity resolver.
|
||||
*/
|
||||
public function __construct(LinkManagerInterface $link_manager, EntityResolverInterface $entity_Resolver) {
|
||||
$this->linkManager = $link_manager;
|
||||
$this->entityResolver = $entity_Resolver;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = array()) {
|
||||
/** @var $field_item \Drupal\Core\Field\FieldItemInterface */
|
||||
$target_entity = $field_item->get('entity')->getValue();
|
||||
|
||||
// If this is not a content entity, let the parent implementation handle it,
|
||||
// only content entities are supported as embedded resources.
|
||||
if (!($target_entity instanceof FieldableEntityInterface)) {
|
||||
return parent::normalize($field_item, $format, $context);
|
||||
}
|
||||
|
||||
// If the parent entity passed in a langcode, unset it before normalizing
|
||||
// the target entity. Otherwise, untranslatable fields of the target entity
|
||||
// will include the langcode.
|
||||
$langcode = isset($context['langcode']) ? $context['langcode'] : NULL;
|
||||
unset($context['langcode']);
|
||||
$context['included_fields'] = array('uuid');
|
||||
|
||||
// Normalize the target entity.
|
||||
$embedded = $this->serializer->normalize($target_entity, $format, $context);
|
||||
$link = $embedded['_links']['self'];
|
||||
// If the field is translatable, add the langcode to the link relation
|
||||
// object. This does not indicate the language of the target entity.
|
||||
if ($langcode) {
|
||||
$embedded['lang'] = $link['lang'] = $langcode;
|
||||
}
|
||||
|
||||
// The returned structure will be recursively merged into the normalized
|
||||
// entity so that the items are properly added to the _links and _embedded
|
||||
// objects.
|
||||
$field_name = $field_item->getParent()->getName();
|
||||
$entity = $field_item->getEntity();
|
||||
$field_uri = $this->linkManager->getRelationUri($entity->getEntityTypeId(), $entity->bundle(), $field_name, $context);
|
||||
return array(
|
||||
'_links' => array(
|
||||
$field_uri => array($link),
|
||||
),
|
||||
'_embedded' => array(
|
||||
$field_uri => array($embedded),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function constructValue($data, $context) {
|
||||
$field_item = $context['target_instance'];
|
||||
$field_definition = $field_item->getFieldDefinition();
|
||||
$target_type = $field_definition->getSetting('target_type');
|
||||
$id = $this->entityResolver->resolve($this, $data, $target_type);
|
||||
if (isset($id)) {
|
||||
return array('target_id' => $id);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getUuid($data) {
|
||||
if (isset($data['uuid'])) {
|
||||
$uuid = $data['uuid'];
|
||||
// The value may be a nested array like $uuid[0]['value'].
|
||||
if (is_array($uuid) && isset($uuid[0]['value'])) {
|
||||
$uuid = $uuid[0]['value'];
|
||||
}
|
||||
return $uuid;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
111
web/core/modules/hal/src/Normalizer/FieldItemNormalizer.php
Normal file
111
web/core/modules/hal/src/Normalizer/FieldItemNormalizer.php
Normal file
|
@ -0,0 +1,111 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Converts the Drupal field item object structure to HAL array structure.
|
||||
*/
|
||||
class FieldItemNormalizer extends NormalizerBase {
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = 'Drupal\Core\Field\FieldItemInterface';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field_item, $format = NULL, array $context = array()) {
|
||||
$values = $field_item->toArray();
|
||||
if (isset($context['langcode'])) {
|
||||
$values['lang'] = $context['langcode'];
|
||||
}
|
||||
|
||||
// The values are wrapped in an array, and then wrapped in another array
|
||||
// keyed by field name so that field items can be merged by the
|
||||
// FieldNormalizer. This is necessary for the EntityReferenceItemNormalizer
|
||||
// to be able to place values in the '_links' array.
|
||||
$field = $field_item->getParent();
|
||||
return array(
|
||||
$field->getName() => array($values),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = NULL, array $context = array()) {
|
||||
if (!isset($context['target_instance'])) {
|
||||
throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldItemNormalizer');
|
||||
}
|
||||
if ($context['target_instance']->getParent() == NULL) {
|
||||
throw new InvalidArgumentException('The field item passed in via $context[\'target_instance\'] must have a parent set.');
|
||||
}
|
||||
|
||||
$field_item = $context['target_instance'];
|
||||
|
||||
// If this field is translatable, we need to create a translated instance.
|
||||
if (isset($data['lang'])) {
|
||||
$langcode = $data['lang'];
|
||||
unset($data['lang']);
|
||||
$field_definition = $field_item->getFieldDefinition();
|
||||
if ($field_definition->isTranslatable()) {
|
||||
$field_item = $this->createTranslatedInstance($field_item, $langcode);
|
||||
}
|
||||
}
|
||||
|
||||
$field_item->setValue($this->constructValue($data, $context));
|
||||
return $field_item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the field item value using the incoming data.
|
||||
*
|
||||
* @param $data
|
||||
* The incoming data for this field item.
|
||||
* @param $context
|
||||
* The context passed into the Normalizer.
|
||||
*
|
||||
* @return mixed
|
||||
* The value to use in Entity::setValue().
|
||||
*/
|
||||
protected function constructValue($data, $context) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a translated version of the field item instance.
|
||||
*
|
||||
* To indicate that a field item applies to one translation of an entity and
|
||||
* not another, the property path must originate with a translation of the
|
||||
* entity. This is the reason for using target_instances, from which the
|
||||
* property path can be traversed up to the root.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemInterface $field_item
|
||||
* The untranslated field item instance.
|
||||
* @param $langcode
|
||||
* The langcode.
|
||||
*
|
||||
* @return \Drupal\Core\Field\FieldItemInterface
|
||||
* The translated field item instance.
|
||||
*/
|
||||
protected function createTranslatedInstance(FieldItemInterface $item, $langcode) {
|
||||
// Remove the untranslated item that was created for the default language
|
||||
// by FieldNormalizer::denormalize().
|
||||
$items = $item->getParent();
|
||||
$delta = $item->getName();
|
||||
unset($items[$delta]);
|
||||
|
||||
// Instead, create a new item for the entity in the requested language.
|
||||
$entity = $item->getEntity();
|
||||
$entity_translation = $entity->hasTranslation($langcode) ? $entity->getTranslation($langcode) : $entity->addTranslation($langcode);
|
||||
$field_name = $item->getFieldDefinition()->getName();
|
||||
return $entity_translation->get($field_name)->appendItem();
|
||||
}
|
||||
|
||||
}
|
104
web/core/modules/hal/src/Normalizer/FieldNormalizer.php
Normal file
104
web/core/modules/hal/src/Normalizer/FieldNormalizer.php
Normal file
|
@ -0,0 +1,104 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Component\Utility\NestedArray;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* Converts the Drupal field structure to HAL array structure.
|
||||
*/
|
||||
class FieldNormalizer extends NormalizerBase {
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = 'Drupal\Core\Field\FieldItemListInterface';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($field, $format = NULL, array $context = array()) {
|
||||
$normalized_field_items = array();
|
||||
|
||||
// Get the field definition.
|
||||
$entity = $field->getEntity();
|
||||
$field_name = $field->getName();
|
||||
$field_definition = $field->getFieldDefinition();
|
||||
|
||||
// If this field is not translatable, it can simply be normalized without
|
||||
// separating it into different translations.
|
||||
if (!$field_definition->isTranslatable()) {
|
||||
$normalized_field_items = $this->normalizeFieldItems($field, $format, $context);
|
||||
}
|
||||
// Otherwise, the languages have to be extracted from the entity and passed
|
||||
// in to the field item normalizer in the context. The langcode is appended
|
||||
// to the field item values.
|
||||
else {
|
||||
foreach ($entity->getTranslationLanguages() as $language) {
|
||||
$context['langcode'] = $language->getId();
|
||||
$translation = $entity->getTranslation($language->getId());
|
||||
$translated_field = $translation->get($field_name);
|
||||
$normalized_field_items = array_merge($normalized_field_items, $this->normalizeFieldItems($translated_field, $format, $context));
|
||||
}
|
||||
}
|
||||
|
||||
// Merge deep so that links set in entity reference normalizers are merged
|
||||
// into the links property.
|
||||
$normalized = NestedArray::mergeDeepArray($normalized_field_items);
|
||||
return $normalized;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = NULL, array $context = array()) {
|
||||
if (!isset($context['target_instance'])) {
|
||||
throw new InvalidArgumentException('$context[\'target_instance\'] must be set to denormalize with the FieldNormalizer');
|
||||
}
|
||||
if ($context['target_instance']->getParent() == NULL) {
|
||||
throw new InvalidArgumentException('The field passed in via $context[\'target_instance\'] must have a parent set.');
|
||||
}
|
||||
|
||||
$items = $context['target_instance'];
|
||||
$item_class = $items->getItemDefinition()->getClass();
|
||||
foreach ($data as $item_data) {
|
||||
// Create a new item and pass it as the target for the unserialization of
|
||||
// $item_data. Note: if $item_data is about a different language than the
|
||||
// default, FieldItemNormalizer::denormalize() will dismiss this item and
|
||||
// create a new one for the right language.
|
||||
$context['target_instance'] = $items->appendItem();
|
||||
$this->serializer->denormalize($item_data, $item_class, $format, $context);
|
||||
}
|
||||
|
||||
return $items;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to normalize field items.
|
||||
*
|
||||
* @param \Drupal\Core\Field\FieldItemListInterface $field
|
||||
* The field object.
|
||||
* @param string $format
|
||||
* The format.
|
||||
* @param array $context
|
||||
* The context array.
|
||||
*
|
||||
* @return array
|
||||
* The array of normalized field items.
|
||||
*/
|
||||
protected function normalizeFieldItems($field, $format, $context) {
|
||||
$normalized_field_items = array();
|
||||
if (!$field->isEmpty()) {
|
||||
foreach ($field as $field_item) {
|
||||
$normalized_field_items[] = $this->serializer->normalize($field_item, $format, $context);
|
||||
}
|
||||
}
|
||||
return $normalized_field_items;
|
||||
}
|
||||
|
||||
}
|
70
web/core/modules/hal/src/Normalizer/FileEntityNormalizer.php
Normal file
70
web/core/modules/hal/src/Normalizer/FileEntityNormalizer.php
Normal file
|
@ -0,0 +1,70 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\rest\LinkManager\LinkManagerInterface;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
|
||||
/**
|
||||
* Converts the Drupal entity object structure to a HAL array structure.
|
||||
*/
|
||||
class FileEntityNormalizer extends ContentEntityNormalizer {
|
||||
|
||||
/**
|
||||
* The interface or class that this Normalizer supports.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $supportedInterfaceOrClass = 'Drupal\file\FileInterface';
|
||||
|
||||
/**
|
||||
* The HTTP client.
|
||||
*
|
||||
* @var \GuzzleHttp\ClientInterface
|
||||
*/
|
||||
protected $httpClient;
|
||||
|
||||
/**
|
||||
* Constructs a FileEntityNormalizer object.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
|
||||
* The entity manager.
|
||||
* @param \GuzzleHttp\ClientInterface $http_client
|
||||
* The HTTP Client.
|
||||
* @param \Drupal\rest\LinkManager\LinkManagerInterface $link_manager
|
||||
* The hypermedia link manager.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler) {
|
||||
parent::__construct($link_manager, $entity_manager, $module_handler);
|
||||
|
||||
$this->httpClient = $http_client;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function normalize($entity, $format = NULL, array $context = array()) {
|
||||
$data = parent::normalize($entity, $format, $context);
|
||||
// Replace the file url with a full url for the file.
|
||||
$data['uri'][0]['value'] = $this->getEntityUri($entity);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function denormalize($data, $class, $format = NULL, array $context = array()) {
|
||||
$file_data = (string) $this->httpClient->get($data['uri'][0]['value'])->getBody();
|
||||
|
||||
$path = 'temporary://' . drupal_basename($data['uri'][0]['value']);
|
||||
$data['uri'] = file_unmanaged_save_data($file_data, $path);
|
||||
|
||||
return $this->entityManager->getStorage('file')->create($data);
|
||||
}
|
||||
|
||||
}
|
45
web/core/modules/hal/src/Normalizer/NormalizerBase.php
Normal file
45
web/core/modules/hal/src/Normalizer/NormalizerBase.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\serialization\Normalizer\NormalizerBase as SerializationNormalizerBase;
|
||||
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
|
||||
|
||||
/**
|
||||
* Base class for Normalizers.
|
||||
*/
|
||||
abstract class NormalizerBase extends SerializationNormalizerBase implements DenormalizerInterface {
|
||||
|
||||
/**
|
||||
* The formats that the Normalizer can handle.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $formats = array('hal_json');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsNormalization($data, $format = NULL) {
|
||||
return in_array($format, $this->formats) && parent::supportsNormalization($data, $format);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsDenormalization($data, $type, $format = NULL) {
|
||||
if (in_array($format, $this->formats) && (class_exists($this->supportedInterfaceOrClass) || interface_exists($this->supportedInterfaceOrClass))) {
|
||||
$target = new \ReflectionClass($type);
|
||||
$supported = new \ReflectionClass($this->supportedInterfaceOrClass);
|
||||
if ($supported->isInterface()) {
|
||||
return $target->implementsInterface($this->supportedInterfaceOrClass);
|
||||
}
|
||||
else {
|
||||
return ($target->getName() == $this->supportedInterfaceOrClass || $target->isSubclassOf($this->supportedInterfaceOrClass));
|
||||
}
|
||||
}
|
||||
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
}
|
75
web/core/modules/hal/src/Tests/FileDenormalizeTest.php
Normal file
75
web/core/modules/hal/src/Tests/FileDenormalizeTest.php
Normal file
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\hal\Tests;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests that file entities can be denormalized in HAL.
|
||||
*
|
||||
* @group hal
|
||||
* @see \Drupal\hal\Normalizer\FileEntityNormalizer
|
||||
*/
|
||||
class FileDenormalizeTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('hal', 'file', 'node');
|
||||
|
||||
/**
|
||||
* Tests file entity denormalization.
|
||||
*/
|
||||
public function testFileDenormalize() {
|
||||
$file_params = array(
|
||||
'filename' => 'test_1.txt',
|
||||
'uri' => 'public://test_1.txt',
|
||||
'filemime' => 'text/plain',
|
||||
'status' => FILE_STATUS_PERMANENT,
|
||||
);
|
||||
// Create a new file entity.
|
||||
$file = File::create($file_params);
|
||||
file_put_contents($file->getFileUri(), 'hello world');
|
||||
$file->save();
|
||||
|
||||
$serializer = \Drupal::service('serializer');
|
||||
$normalized_data = $serializer->normalize($file, 'hal_json');
|
||||
$denormalized = $serializer->denormalize($normalized_data, 'Drupal\file\Entity\File', 'hal_json');
|
||||
|
||||
$this->assertTrue($denormalized instanceof File, 'A File instance was created.');
|
||||
|
||||
$this->assertIdentical('temporary://' . $file->getFilename(), $denormalized->getFileUri(), 'The expected file URI was found.');
|
||||
$this->assertTrue(file_exists($denormalized->getFileUri()), 'The temporary file was found.');
|
||||
|
||||
$this->assertIdentical($file->uuid(), $denormalized->uuid(), 'The expected UUID was found');
|
||||
$this->assertIdentical($file->getMimeType(), $denormalized->getMimeType(), 'The expected MIME type was found.');
|
||||
$this->assertIdentical($file->getFilename(), $denormalized->getFilename(), 'The expected filename was found.');
|
||||
$this->assertTrue($denormalized->isPermanent(), 'The file has a permanent status.');
|
||||
|
||||
// Try to denormalize with the file uri only.
|
||||
$file_name = 'test_2.txt';
|
||||
$file_path = 'public://' . $file_name;
|
||||
|
||||
file_put_contents($file_path, 'hello world');
|
||||
$file_uri = file_create_url($file_path);
|
||||
|
||||
$data = array(
|
||||
'uri' => array(
|
||||
array('value' => $file_uri),
|
||||
),
|
||||
);
|
||||
|
||||
$denormalized = $serializer->denormalize($data, 'Drupal\file\Entity\File', 'hal_json');
|
||||
|
||||
$this->assertIdentical('temporary://' . $file_name, $denormalized->getFileUri(), 'The expected file URI was found.');
|
||||
$this->assertTrue(file_exists($denormalized->getFileUri()), 'The temporary file was found.');
|
||||
|
||||
$this->assertIdentical('text/plain', $denormalized->getMimeType(), 'The expected MIME type was found.');
|
||||
$this->assertIdentical($file_name, $denormalized->getFilename(), 'The expected filename was found.');
|
||||
$this->assertFalse($denormalized->isPermanent(), 'The file has a permanent status.');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Block;
|
||||
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Block\BlockResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class BlockHalJsonAnonTest extends BlockResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Block;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Block\BlockResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class BlockHalJsonBasicAuthTest extends BlockResourceTestBase {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal', 'basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Block;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Block\BlockResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class BlockHalJsonCookieTest extends BlockResourceTestBase {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Comment;
|
||||
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class CommentHalJsonAnonTest extends CommentHalJsonTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Anononymous users cannot edit their own comments.
|
||||
*
|
||||
* @see \Drupal\comment\CommentAccessControlHandler::checkAccess
|
||||
*
|
||||
* Therefore we grant them the 'administer comments' permission for the
|
||||
* purpose of this test. Then they are able to edit their own comments, but
|
||||
* some fields are still not editable, even with that permission.
|
||||
*
|
||||
* @see ::setUpAuthorization
|
||||
*/
|
||||
protected static $patchProtectedFieldNames = [
|
||||
'changed',
|
||||
'thread',
|
||||
'entity_type',
|
||||
'field_name',
|
||||
'entity_id',
|
||||
];
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Comment;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class CommentHalJsonBasicAuthTest extends CommentHalJsonTestBase {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Comment;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class CommentHalJsonCookieTest extends CommentHalJsonTestBase {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,144 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Comment;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Comment\CommentResourceTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
abstract class CommentHalJsonTestBase extends CommentResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* The HAL+JSON format causes different PATCH-protected fields. For some
|
||||
* reason, the 'pid' and 'homepage' fields are NOT PATCH-protected, even
|
||||
* though they are for non-HAL+JSON serializations.
|
||||
*
|
||||
* @todo fix in https://www.drupal.org/node/2824271
|
||||
*/
|
||||
protected static $patchProtectedFieldNames = [
|
||||
'created',
|
||||
'changed',
|
||||
'status',
|
||||
'thread',
|
||||
'entity_type',
|
||||
'field_name',
|
||||
'entity_id',
|
||||
'uid',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
|
||||
// Because \Drupal\comment\Entity\Comment::getOwner() generates an in-memory
|
||||
// User entity without a UUID, we cannot use it.
|
||||
$author = User::load($this->entity->getOwnerId());
|
||||
$commented_entity = EntityTest::load(1);
|
||||
return $normalization + [
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/comment/1?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/comment/comment',
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/comment/comment/entity_id' => [
|
||||
[
|
||||
'href' => $this->baseUrl . '/entity_test/1?_format=hal_json',
|
||||
],
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/comment/comment/uid' => [
|
||||
[
|
||||
'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
],
|
||||
'_embedded' => [
|
||||
$this->baseUrl . '/rest/relation/comment/comment/entity_id' => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/entity_test/1?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/entity_test/bar',
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
['value' => $commented_entity->uuid()]
|
||||
],
|
||||
],
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/comment/comment/uid' => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/user/user',
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
['value' => $author->uuid()]
|
||||
],
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/comment/comment',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
// The 'url.site' cache context is added for '_links' in the response.
|
||||
return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\ConfigTest;
|
||||
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\ConfigTest\ConfigTestResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class ConfigTestHalJsonAnonTest extends ConfigTestResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\ConfigTest;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\ConfigTest\ConfigTestResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class ConfigTestHalJsonBasicAuthTest extends ConfigTestResourceTestBase {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal', 'basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\ConfigTest;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\ConfigTest\ConfigTestResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class ConfigTestHalJsonCookieTest extends ConfigTestResourceTestBase {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityTest;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\EntityTest\EntityTestResourceTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityTestHalJsonAnonTest extends EntityTestResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
|
||||
$author = User::load(0);
|
||||
return $normalization + [
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/entity_test/1?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/entity_test/entity_test',
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/entity_test/entity_test/user_id' => [
|
||||
[
|
||||
'href' => $this->baseUrl . '/user/0?_format=hal_json',
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
],
|
||||
'_embedded' => [
|
||||
$this->baseUrl . '/rest/relation/entity_test/entity_test/user_id' => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/user/0?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/user/user',
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
['value' => $author->uuid()]
|
||||
],
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/entity_test/entity_test',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
// The 'url.site' cache context is added for '_links' in the response.
|
||||
return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityTest;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityTestHalJsonBasicAuthTest extends EntityTestHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityTest;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityTestHalJsonCookieTest extends EntityTestHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,127 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource;
|
||||
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Url;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
/**
|
||||
* Trait for EntityResourceTestBase subclasses testing formats using HAL.
|
||||
*/
|
||||
trait HalEntityNormalizationTrait {
|
||||
|
||||
/**
|
||||
* Applies the HAL entity field normalization to an entity normalization.
|
||||
*
|
||||
* The HAL normalization:
|
||||
* - adds a 'lang' attribute to every translatable field
|
||||
* - omits reference fields, since references are stored in _links & _embedded
|
||||
* - omits empty fields (fields without value)
|
||||
*
|
||||
* @param array $normalization
|
||||
* An entity normalization.
|
||||
*
|
||||
* @return array
|
||||
* The updated entity normalization.
|
||||
*/
|
||||
protected function applyHalFieldNormalization(array $normalization) {
|
||||
if (!$this->entity instanceof FieldableEntityInterface) {
|
||||
throw new \LogicException('This trait should only be used for fieldable entity types.');
|
||||
}
|
||||
|
||||
// In the HAL normalization, all translatable fields get a 'lang' attribute.
|
||||
$translatable_non_reference_fields = array_keys(array_filter($this->entity->getTranslatableFields(), function (FieldItemListInterface $field) {
|
||||
return !$field instanceof EntityReferenceFieldItemListInterface;
|
||||
}));
|
||||
foreach ($translatable_non_reference_fields as $field_name) {
|
||||
if (isset($normalization[$field_name])) {
|
||||
$normalization[$field_name][0]['lang'] = 'en';
|
||||
}
|
||||
}
|
||||
|
||||
// In the HAL normalization, reference fields are omitted, except for the
|
||||
// bundle field.
|
||||
$bundle_key = $this->entity->getEntityType()->getKey('bundle');
|
||||
$reference_fields = array_keys(array_filter($this->entity->getFields(), function (FieldItemListInterface $field) use ($bundle_key) {
|
||||
return $field instanceof EntityReferenceFieldItemListInterface && $field->getName() !== $bundle_key;
|
||||
}));
|
||||
foreach ($reference_fields as $field_name) {
|
||||
unset($normalization[$field_name]);
|
||||
}
|
||||
|
||||
// In the HAL normalization, the bundle field omits the 'target_type' and
|
||||
// 'target_uuid' properties, because it's encoded in the '_links' section.
|
||||
if ($bundle_key) {
|
||||
unset($normalization[$bundle_key][0]['target_type']);
|
||||
unset($normalization[$bundle_key][0]['target_uuid']);
|
||||
}
|
||||
|
||||
// In the HAL normalization, empty fields are omitted.
|
||||
$empty_fields = array_keys(array_filter($this->entity->getFields(), function (FieldItemListInterface $field) {
|
||||
return $field->isEmpty();
|
||||
}));
|
||||
foreach ($empty_fields as $field_name) {
|
||||
unset($normalization[$field_name]);
|
||||
}
|
||||
|
||||
return $normalization;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function removeFieldsFromNormalization(array $normalization, $field_names) {
|
||||
$normalization = parent::removeFieldsFromNormalization($normalization, $field_names);
|
||||
foreach ($field_names as $field_name) {
|
||||
$relation_url = Url::fromUri('base:rest/relation/' . static::$entityTypeId . '/' . $this->entity->bundle() . '/' . $field_name)
|
||||
->setAbsolute(TRUE)
|
||||
->toString();
|
||||
$normalization['_links'] = array_diff_key($normalization['_links'], [$relation_url => TRUE]);
|
||||
if (isset($normalization['_embedded'])) {
|
||||
$normalization['_embedded'] = array_diff_key($normalization['_embedded'], [$relation_url => TRUE]);
|
||||
}
|
||||
}
|
||||
|
||||
return array_diff_key($normalization, array_flip($field_names));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
|
||||
// \Drupal\serialization\Normalizer\EntityNormalizer::denormalize(): entity
|
||||
// types with bundles MUST send their bundle field to be denormalizable.
|
||||
if ($this->entity->getEntityType()->hasKey('bundle')) {
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
|
||||
|
||||
$normalization['_links']['type'] = Url::fromUri('base:rest/type/' . static::$entityTypeId . '/bad_bundle_name');
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
|
||||
// DX: 400 when incorrect entity type bundle is specified.
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
// @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813853.
|
||||
// $this->assertResourceErrorResponse(400, 'No entity type(s) specified', $response);
|
||||
$this->assertSame(400, $response->getStatusCode());
|
||||
$this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
|
||||
$this->assertSame($this->serializer->encode(['error' => 'No entity type(s) specified'], static::$format), (string) $response->getBody());
|
||||
|
||||
|
||||
unset($normalization['_links']['type']);
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
|
||||
|
||||
// DX: 400 when no entity type bundle is specified.
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
// @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813853.
|
||||
// $this->assertResourceErrorResponse(400, 'The type link relation must be specified.', $response);
|
||||
$this->assertSame(400, $response->getStatusCode());
|
||||
$this->assertSame([static::$mimeType], $response->getHeader('Content-Type'));
|
||||
$this->assertSame($this->serializer->encode(['error' => 'The type link relation must be specified.'], static::$format), (string) $response->getBody());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,137 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Node;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Node\NodeResourceTestBase;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class NodeHalJsonAnonTest extends NodeResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $patchProtectedFieldNames = [
|
||||
'created',
|
||||
'changed',
|
||||
'promote',
|
||||
'sticky',
|
||||
'revision_timestamp',
|
||||
'revision_uid',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
|
||||
$author = User::load($this->entity->getOwnerId());
|
||||
return $normalization + [
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/node/1?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/node/camelids',
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/node/camelids/uid' => [
|
||||
[
|
||||
'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/node/camelids/revision_uid' => [
|
||||
[
|
||||
'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
|
||||
],
|
||||
],
|
||||
],
|
||||
'_embedded' => [
|
||||
$this->baseUrl . '/rest/relation/node/camelids/uid' => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/user/user',
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
['value' => $author->uuid()]
|
||||
],
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/node/camelids/revision_uid' => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/user/user',
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
['value' => $author->uuid()]
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/node/camelids',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
// The 'url.site' cache context is added for '_links' in the response.
|
||||
return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['url.site']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Node;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class NodeHalJsonBasicAuthTest extends NodeHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Node;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class NodeHalJsonCookieTest extends NodeHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Role;
|
||||
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Role\RoleResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class RoleHalJsonAnonTest extends RoleResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Role;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Role\RoleResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class RoleHalJsonBasicAuthTest extends RoleResourceTestBase {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal', 'basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Role;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Role\RoleResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class RoleHalJsonCookieTest extends RoleResourceTestBase {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Term;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Term\TermResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class TermHalJsonAnonTest extends TermResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
|
||||
return $normalization + [
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/taxonomy/term/1?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
// The 'url.site' cache context is added for '_links' in the response.
|
||||
return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['url.site']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Term;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class TermHalJsonBasicAuthTest extends TermHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Term;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class TermHalJsonCookieTest extends TermHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\User;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\User\UserResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class UserHalJsonAnonTest extends UserResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
|
||||
return $normalization + [
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/user/3?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/user/user',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/user/user',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
// The 'url.site' cache context is added for '_links' in the response.
|
||||
return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['url.site']);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\User;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class UserHalJsonBasicAuthTest extends UserHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\User;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class UserHalJsonCookieTest extends UserHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Vocabulary;
|
||||
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Vocabulary\VocabularyResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class VocabularyHalJsonAnonTest extends VocabularyResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* @todo Remove this override in https://www.drupal.org/node/2805281.
|
||||
*/
|
||||
public function testGet() {
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Vocabulary;
|
||||
|
||||
use Drupal\Tests\hal\Functional\HalJsonBasicAuthWorkaroundFor2805281Trait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Vocabulary\VocabularyResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class VocabularyHalJsonBasicAuthTest extends VocabularyResourceTestBase {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal', 'basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
// @todo Fix in https://www.drupal.org/node/2805281: remove this trait usage.
|
||||
use HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
HalJsonBasicAuthWorkaroundFor2805281Trait::assertResponseWhenMissingAuthentication insteadof BasicAuthResourceTestTrait;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Vocabulary;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Vocabulary\VocabularyResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class VocabularyHalJsonCookieTest extends VocabularyResourceTestBase {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $expectedErrorMimeType = 'application/json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
|
@ -0,0 +1,26 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional;
|
||||
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
trait HalJsonBasicAuthWorkaroundFor2805281Trait {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Note how the response claims it contains a application/hal+json body, but
|
||||
* in reality it contains a text/plain body! Also, the correct error MIME type
|
||||
* is application/json.
|
||||
*
|
||||
* @todo Fix in https://www.drupal.org/node/2805281: remove this trait.
|
||||
*/
|
||||
protected function assertResponseWhenMissingAuthentication(ResponseInterface $response) {
|
||||
$this->assertSame(401, $response->getStatusCode());
|
||||
// @todo this works fine locally, but on testbot it comes back with
|
||||
// 'text/plain; charset=UTF-8'. WTF.
|
||||
// $this->assertSame(['application/hal+json'], $response->getHeader('Content-Type'));
|
||||
$this->assertSame('No authentication credentials provided.', (string) $response->getBody());
|
||||
}
|
||||
|
||||
}
|
143
web/core/modules/hal/tests/src/Kernel/DenormalizeTest.php
Normal file
143
web/core/modules/hal/tests/src/Kernel/DenormalizeTest.php
Normal file
|
@ -0,0 +1,143 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Tests HAL denormalization edge cases for EntityResource.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class DenormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Tests that the type link relation in incoming data is handled correctly.
|
||||
*/
|
||||
public function testTypeHandling() {
|
||||
// Valid type.
|
||||
$data_with_valid_type = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
$denormalized = $this->serializer->denormalize($data_with_valid_type, $this->entityClass, $this->format);
|
||||
$this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with valid type results in creation of correct bundle.');
|
||||
|
||||
// Multiple types.
|
||||
$data_with_multiple_types = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
array(
|
||||
'href' => Url::fromUri('base:rest/types/foo', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
$denormalized = $this->serializer->denormalize($data_with_multiple_types, $this->entityClass, $this->format);
|
||||
$this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with multiple types results in creation of correct bundle.');
|
||||
|
||||
// Invalid type.
|
||||
$data_with_invalid_type = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/types/foo', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
try {
|
||||
$this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
|
||||
$this->fail('Exception should be thrown when type is invalid.');
|
||||
}
|
||||
catch (UnexpectedValueException $e) {
|
||||
$this->pass('Exception thrown when type is invalid.');
|
||||
}
|
||||
|
||||
// No type.
|
||||
$data_with_no_type = array(
|
||||
'_links' => array(
|
||||
),
|
||||
);
|
||||
try {
|
||||
$this->serializer->denormalize($data_with_no_type, $this->entityClass, $this->format);
|
||||
$this->fail('Exception should be thrown when no type is provided.');
|
||||
}
|
||||
catch (UnexpectedValueException $e) {
|
||||
$this->pass('Exception thrown when no type is provided.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests link relation handling with an invalid type.
|
||||
*/
|
||||
public function testTypeHandlingWithInvalidType() {
|
||||
$data_with_invalid_type = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->setExpectedException(UnexpectedValueException::class);
|
||||
$this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests link relation handling with no types.
|
||||
*/
|
||||
public function testTypeHandlingWithNoTypes() {
|
||||
$data_with_no_types = array(
|
||||
'_links' => array(
|
||||
'type' => array(),
|
||||
),
|
||||
);
|
||||
|
||||
$this->setExpectedException(UnexpectedValueException::class);
|
||||
$this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a field set to an empty array is different than an absent field.
|
||||
*/
|
||||
public function testMarkFieldForDeletion() {
|
||||
// Add a default value for a field.
|
||||
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
|
||||
$field->setDefaultValue(array(array('value' => 'Llama')));
|
||||
$field->save();
|
||||
|
||||
// Denormalize data that contains no entry for the field, and check that
|
||||
// the default value is present in the resulting entity.
|
||||
$data = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
$entity = $this->serializer->denormalize($data, $this->entityClass, $this->format);
|
||||
$this->assertEqual($entity->field_test_text->count(), 1);
|
||||
$this->assertEqual($entity->field_test_text->value, 'Llama');
|
||||
|
||||
// Denormalize data that contains an empty entry for the field, and check
|
||||
// that the field is empty in the resulting entity.
|
||||
$data = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
'field_test_text' => array(),
|
||||
);
|
||||
$entity = $this->serializer->denormalize($data, get_class($entity), $this->format, [ 'target_instance' => $entity ]);
|
||||
$this->assertEqual($entity->field_test_text->count(), 0);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
|
||||
/**
|
||||
* Tests that translated nodes are correctly (de-)normalized.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class EntityTranslationNormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'content_translation');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('system', array('sequences'));
|
||||
$this->installConfig(['node', 'content_translation']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalization of node translations.
|
||||
*/
|
||||
public function testNodeTranslation() {
|
||||
$node_type = NodeType::create(['type' => 'example_type']);
|
||||
$node_type->save();
|
||||
$this->container->get('content_translation.manager')->setEnabled('node', 'example_type', TRUE);
|
||||
|
||||
$user = User::create(['name' => $this->randomMachineName()]);
|
||||
$user->save();
|
||||
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(),
|
||||
'uid' => $user->id(),
|
||||
'type' => $node_type->id(),
|
||||
'status' => NODE_PUBLISHED,
|
||||
'langcode' => 'en',
|
||||
'promote' => 1,
|
||||
'sticky' => 0,
|
||||
'body' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => $this->randomMachineName()
|
||||
],
|
||||
'revision_log' => $this->randomString(),
|
||||
]);
|
||||
$node->addTranslation('de', [
|
||||
'title' => 'German title',
|
||||
'body' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => $this->randomMachineName()
|
||||
],
|
||||
]);
|
||||
$node->save();
|
||||
|
||||
$original_values = $node->toArray();
|
||||
$translation = $node->getTranslation('de');
|
||||
$original_translation_values = $node->getTranslation('en')->toArray();
|
||||
|
||||
$normalized = $this->serializer->normalize($node, $this->format);
|
||||
|
||||
$this->assertContains(['lang' => 'en', 'value' => $node->getTitle()], $normalized['title'], 'Original language title has been normalized.');
|
||||
$this->assertContains(['lang' => 'de', 'value' => $translation->getTitle()], $normalized['title'], 'Translation language title has been normalized.');
|
||||
|
||||
/** @var \Drupal\node\NodeInterface $denormalized_node */
|
||||
$denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\node\Entity\Node', $this->format);
|
||||
|
||||
$this->assertSame($denormalized_node->language()->getId(), $denormalized_node->getUntranslated()->language()->getId(), 'Untranslated object is returned from serializer.');
|
||||
$this->assertSame('en', $denormalized_node->language()->getId());
|
||||
$this->assertTrue($denormalized_node->hasTranslation('de'));
|
||||
|
||||
$this->assertSame($node->getTitle(), $denormalized_node->getTitle());
|
||||
$this->assertSame($translation->getTitle(), $denormalized_node->getTranslation('de')->getTitle());
|
||||
|
||||
$this->assertEquals($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.');
|
||||
$this->assertEquals($original_translation_values, $denormalized_node->getTranslation('en')->toArray(), 'Node values are restored after normalizing and denormalizing.');
|
||||
}
|
||||
|
||||
}
|
80
web/core/modules/hal/tests/src/Kernel/FileNormalizeTest.php
Normal file
80
web/core/modules/hal/tests/src/Kernel/FileNormalizeTest.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Cache\MemoryBackend;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\hal\Encoder\JsonEncoder;
|
||||
use Drupal\hal\Normalizer\FieldItemNormalizer;
|
||||
use Drupal\hal\Normalizer\FileEntityNormalizer;
|
||||
use Drupal\rest\LinkManager\LinkManager;
|
||||
use Drupal\rest\LinkManager\RelationLinkManager;
|
||||
use Drupal\rest\LinkManager\TypeLinkManager;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
|
||||
/**
|
||||
* Tests that file entities can be normalized in HAL.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class FileNormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('file');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('file');
|
||||
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default'), \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack'), \Drupal::service('entity_type.bundle.info')), new RelationLinkManager(new MemoryBackend('default'), $entity_manager, \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack')));
|
||||
|
||||
// Set up the mock serializer.
|
||||
$normalizers = array(
|
||||
new FieldItemNormalizer(),
|
||||
new FileEntityNormalizer($entity_manager, \Drupal::httpClient(), $link_manager, \Drupal::moduleHandler()),
|
||||
);
|
||||
|
||||
$encoders = array(
|
||||
new JsonEncoder(),
|
||||
);
|
||||
$this->serializer = new Serializer($normalizers, $encoders);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests the normalize function.
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$file_params = array(
|
||||
'filename' => 'test_1.txt',
|
||||
'uri' => 'public://test_1.txt',
|
||||
'filemime' => 'text/plain',
|
||||
'status' => FILE_STATUS_PERMANENT,
|
||||
);
|
||||
// Create a new file entity.
|
||||
$file = File::create($file_params);
|
||||
file_put_contents($file->getFileUri(), 'hello world');
|
||||
$file->save();
|
||||
|
||||
$expected_array = array(
|
||||
'uri' => array(
|
||||
array(
|
||||
'value' => file_create_url($file->getFileUri())),
|
||||
),
|
||||
);
|
||||
|
||||
$normalized = $this->serializer->normalize($file, $this->format);
|
||||
$this->assertEqual($normalized['uri'], $expected_array['uri'], 'URI is normalized.');
|
||||
|
||||
}
|
||||
|
||||
}
|
185
web/core/modules/hal/tests/src/Kernel/NormalizeTest.php
Normal file
185
web/core/modules/hal/tests/src/Kernel/NormalizeTest.php
Normal file
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
|
||||
/**
|
||||
* Tests HAL normalization edge cases for EntityResource.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class NormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalize function.
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$target_entity_de = EntityTest::create((array('langcode' => 'de', 'field_test_entity_reference' => NULL)));
|
||||
$target_entity_de->save();
|
||||
$target_entity_en = EntityTest::create((array('langcode' => 'en', 'field_test_entity_reference' => NULL)));
|
||||
$target_entity_en->save();
|
||||
|
||||
// Create a German entity.
|
||||
$values = array(
|
||||
'langcode' => 'de',
|
||||
'name' => $this->randomMachineName(),
|
||||
'field_test_text' => array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
),
|
||||
'field_test_entity_reference' => array(
|
||||
'target_id' => $target_entity_de->id(),
|
||||
),
|
||||
);
|
||||
// Array of translated values.
|
||||
$translation_values = array(
|
||||
'name' => $this->randomMachineName(),
|
||||
'field_test_entity_reference' => array(
|
||||
'target_id' => $target_entity_en->id(),
|
||||
)
|
||||
);
|
||||
|
||||
$entity = EntityTest::create($values);
|
||||
$entity->save();
|
||||
// Add an English value for name and entity reference properties.
|
||||
$entity->addTranslation('en')->set('name', array(0 => array('value' => $translation_values['name'])));
|
||||
$entity->getTranslation('en')->set('field_test_entity_reference', array(0 => $translation_values['field_test_entity_reference']));
|
||||
$entity->save();
|
||||
|
||||
$type_uri = Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString();
|
||||
$relation_uri = Url::fromUri('base:rest/relation/entity_test/entity_test/field_test_entity_reference', array('absolute' => TRUE))->toString();
|
||||
|
||||
$expected_array = array(
|
||||
'_links' => array(
|
||||
'curies' => array(
|
||||
array(
|
||||
'href' => '/relations',
|
||||
'name' => 'site',
|
||||
'templated' => TRUE,
|
||||
),
|
||||
),
|
||||
'self' => array(
|
||||
'href' => $this->getEntityUri($entity),
|
||||
),
|
||||
'type' => array(
|
||||
'href' => $type_uri,
|
||||
),
|
||||
$relation_uri => array(
|
||||
array(
|
||||
'href' => $this->getEntityUri($target_entity_de),
|
||||
'lang' => 'de',
|
||||
),
|
||||
array(
|
||||
'href' => $this->getEntityUri($target_entity_en),
|
||||
'lang' => 'en',
|
||||
),
|
||||
),
|
||||
),
|
||||
'_embedded' => array(
|
||||
$relation_uri => array(
|
||||
array(
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
'href' => $this->getEntityUri($target_entity_de),
|
||||
),
|
||||
'type' => array(
|
||||
'href' => $type_uri,
|
||||
),
|
||||
),
|
||||
'uuid' => array(
|
||||
array(
|
||||
'value' => $target_entity_de->uuid(),
|
||||
),
|
||||
),
|
||||
'lang' => 'de',
|
||||
),
|
||||
array(
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
'href' => $this->getEntityUri($target_entity_en),
|
||||
),
|
||||
'type' => array(
|
||||
'href' => $type_uri,
|
||||
),
|
||||
),
|
||||
'uuid' => array(
|
||||
array(
|
||||
'value' => $target_entity_en->uuid(),
|
||||
),
|
||||
),
|
||||
'lang' => 'en',
|
||||
),
|
||||
),
|
||||
),
|
||||
'id' => array(
|
||||
array(
|
||||
'value' => $entity->id(),
|
||||
),
|
||||
),
|
||||
'uuid' => array(
|
||||
array(
|
||||
'value' => $entity->uuid(),
|
||||
),
|
||||
),
|
||||
'langcode' => array(
|
||||
array(
|
||||
'value' => 'de',
|
||||
),
|
||||
),
|
||||
'name' => array(
|
||||
array(
|
||||
'value' => $values['name'],
|
||||
'lang' => 'de',
|
||||
),
|
||||
array(
|
||||
'value' => $translation_values['name'],
|
||||
'lang' => 'en',
|
||||
),
|
||||
),
|
||||
'field_test_text' => array(
|
||||
array(
|
||||
'value' => $values['field_test_text']['value'],
|
||||
'format' => $values['field_test_text']['format'],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$normalized = $this->serializer->normalize($entity, $this->format);
|
||||
$this->assertEqual($normalized['_links']['self'], $expected_array['_links']['self'], 'self link placed correctly.');
|
||||
// @todo Test curies.
|
||||
// @todo Test type.
|
||||
$this->assertEqual($normalized['id'], $expected_array['id'], 'Internal id is exposed.');
|
||||
$this->assertEqual($normalized['uuid'], $expected_array['uuid'], 'Non-translatable fields is normalized.');
|
||||
$this->assertEqual($normalized['name'], $expected_array['name'], 'Translatable field with multiple language values is normalized.');
|
||||
$this->assertEqual($normalized['field_test_text'], $expected_array['field_test_text'], 'Field with properties is normalized.');
|
||||
$this->assertEqual($normalized['_embedded'][$relation_uri], $expected_array['_embedded'][$relation_uri], 'Entity reference field is normalized.');
|
||||
$this->assertEqual($normalized['_links'][$relation_uri], $expected_array['_links'][$relation_uri], 'Links are added for entity reference field.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the entity URI.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The entity.
|
||||
*
|
||||
* @return string
|
||||
* The entity URI.
|
||||
*/
|
||||
protected function getEntityUri(EntityInterface $entity) {
|
||||
$url = $entity->urlInfo('canonical', ['absolute' => TRUE]);
|
||||
return $url->setRouteParameter('_format', 'hal_json')->toString();
|
||||
}
|
||||
|
||||
}
|
152
web/core/modules/hal/tests/src/Kernel/NormalizerTestBase.php
Normal file
152
web/core/modules/hal/tests/src/Kernel/NormalizerTestBase.php
Normal file
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Cache\MemoryBackend;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\hal\Encoder\JsonEncoder;
|
||||
use Drupal\hal\Normalizer\ContentEntityNormalizer;
|
||||
use Drupal\hal\Normalizer\EntityReferenceItemNormalizer;
|
||||
use Drupal\hal\Normalizer\FieldItemNormalizer;
|
||||
use Drupal\hal\Normalizer\FieldNormalizer;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\rest\LinkManager\LinkManager;
|
||||
use Drupal\rest\LinkManager\RelationLinkManager;
|
||||
use Drupal\rest\LinkManager\TypeLinkManager;
|
||||
use Drupal\serialization\EntityResolver\ChainEntityResolver;
|
||||
use Drupal\serialization\EntityResolver\TargetIdResolver;
|
||||
use Drupal\serialization\EntityResolver\UuidResolver;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Test the HAL normalizer.
|
||||
*/
|
||||
abstract class NormalizerTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['entity_test', 'field', 'hal', 'language', 'rest', 'serialization', 'system', 'text', 'user', 'filter'];
|
||||
|
||||
/**
|
||||
* The mock serializer.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The format being tested.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* The class name of the test class.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityClass = 'Drupal\entity_test\Entity\EntityTest';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('entity_test');
|
||||
// If the concrete test sub-class installs the Node or Comment modules,
|
||||
// ensure that the node and comment entity schema are created before the
|
||||
// field configurations are installed. This is because the entity tables
|
||||
// need to be created before the body field storage tables. This prevents
|
||||
// trying to create the body field tables twice.
|
||||
$class = get_class($this);
|
||||
while ($class) {
|
||||
if (property_exists($class, 'modules')) {
|
||||
// Only check the modules, if the $modules property was not inherited.
|
||||
$rp = new \ReflectionProperty($class, 'modules');
|
||||
if ($rp->class == $class) {
|
||||
foreach (array_intersect(array('node', 'comment'), $class::$modules) as $module) {
|
||||
$this->installEntitySchema($module);
|
||||
}
|
||||
}
|
||||
}
|
||||
$class = get_parent_class($class);
|
||||
}
|
||||
$this->installConfig(array('field', 'language'));
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
|
||||
// Add German as a language.
|
||||
ConfigurableLanguage::create(array(
|
||||
'id' => 'de',
|
||||
'label' => 'Deutsch',
|
||||
'weight' => -1,
|
||||
))->save();
|
||||
|
||||
// Create the test text field.
|
||||
FieldStorageConfig::create(array(
|
||||
'field_name' => 'field_test_text',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'text',
|
||||
))->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_text',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => FALSE,
|
||||
])->save();
|
||||
|
||||
// Create the test translatable field.
|
||||
FieldStorageConfig::create(array(
|
||||
'field_name' => 'field_test_translatable_text',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'text',
|
||||
))->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_translatable_text',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => TRUE,
|
||||
])->save();
|
||||
|
||||
// Create the test entity reference field.
|
||||
FieldStorageConfig::create(array(
|
||||
'field_name' => 'field_test_entity_reference',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'entity_reference',
|
||||
'settings' => array(
|
||||
'target_type' => 'entity_test',
|
||||
),
|
||||
))->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_entity_reference',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => TRUE,
|
||||
])->save();
|
||||
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default'), \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack'), \Drupal::service('entity_type.bundle.info')), new RelationLinkManager(new MemoryBackend('default'), $entity_manager, \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack')));
|
||||
|
||||
$chain_resolver = new ChainEntityResolver(array(new UuidResolver($entity_manager), new TargetIdResolver()));
|
||||
|
||||
// Set up the mock serializer.
|
||||
$normalizers = array(
|
||||
new ContentEntityNormalizer($link_manager, $entity_manager, \Drupal::moduleHandler()),
|
||||
new EntityReferenceItemNormalizer($link_manager, $chain_resolver),
|
||||
new FieldItemNormalizer(),
|
||||
new FieldNormalizer(),
|
||||
);
|
||||
|
||||
$encoders = array(
|
||||
new JsonEncoder(),
|
||||
);
|
||||
$this->serializer = new Serializer($normalizers, $encoders);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\hal\Normalizer\FieldItemNormalizer;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\hal\Normalizer\FieldItemNormalizer
|
||||
* @group hal
|
||||
*/
|
||||
class FieldItemNormalizerDenormalizeExceptionsUnitTest extends NormalizerDenormalizeExceptionsUnitTestBase {
|
||||
|
||||
/**
|
||||
* Tests that the FieldItemNormalizer::denormalize() throws proper exceptions.
|
||||
*
|
||||
* @param array $context
|
||||
* Context for FieldItemNormalizer::denormalize().
|
||||
*
|
||||
* @dataProvider providerNormalizerDenormalizeExceptions
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testFieldItemNormalizerDenormalizeExceptions($context) {
|
||||
$field_item_normalizer = new FieldItemNormalizer();
|
||||
$data = array();
|
||||
$class = array();
|
||||
$field_item_normalizer->denormalize($data, $class, NULL, $context);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\hal\Normalizer\FieldNormalizer;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\hal\Normalizer\FieldNormalizer
|
||||
* @group hal
|
||||
*/
|
||||
class FieldNormalizerDenormalizeExceptionsUnitTest extends NormalizerDenormalizeExceptionsUnitTestBase {
|
||||
|
||||
/**
|
||||
* Tests that the FieldNormalizer::denormalize() throws proper exceptions.
|
||||
*
|
||||
* @param array $context
|
||||
* Context for FieldNormalizer::denormalize().
|
||||
*
|
||||
* @dataProvider providerNormalizerDenormalizeExceptions
|
||||
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
|
||||
*/
|
||||
public function testFieldNormalizerDenormalizeExceptions($context) {
|
||||
$field_item_normalizer = new FieldNormalizer();
|
||||
$data = array();
|
||||
$class = array();
|
||||
$field_item_normalizer->denormalize($data, $class, NULL, $context);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* Common ancestor for FieldItemNormalizerDenormalizeExceptionsUnitTest and
|
||||
* FieldNormalizerDenormalizeExceptionsUnitTest as they have the same
|
||||
* dataProvider.
|
||||
*/
|
||||
abstract class NormalizerDenormalizeExceptionsUnitTestBase extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Provides data for FieldItemNormalizerDenormalizeExceptionsUnitTest::testFieldItemNormalizerDenormalizeExceptions()
|
||||
* and for FieldNormalizerDenormalizeExceptionsUnitTest::testFieldNormalizerDenormalizeExceptions().
|
||||
*
|
||||
* @return array Test data.
|
||||
*/
|
||||
public function providerNormalizerDenormalizeExceptions() {
|
||||
$mock = $this->getMock('\Drupal\Core\Field\Plugin\DataType\FieldItem', array('getParent'));
|
||||
$mock->expects($this->any())
|
||||
->method('getParent')
|
||||
->will($this->returnValue(NULL));
|
||||
return array(
|
||||
array(array()),
|
||||
array(array('target_instance' => $mock)),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue