Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View 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

View 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="!link_rel">link relations</a> between them, enabling API consumers to follow links to discover API functionality.', array('!link_rel' => 'http://en.wikipedia.org/wiki/Link_relation')) . '</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 <a href="!hal_do">the online documentation for the HAL module</a>.', array('!hal_do' => 'https://www.drupal.org/documentation/modules/hal')) . '</p>';
return $output;
}
}

View 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 }

View file

@ -0,0 +1,40 @@
<?php
/**
* @file
* Contains \Drupal\hal\Encoder\JsonEncoder.
*/
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';
/**
* Overrides \Symfony\Component\Serializer\Encoder\JsonEncoder::supportsEncoding()
*/
public function supportsEncoding($format) {
return $format == $this->format;
}
/**
* Overrides \Symfony\Component\Serializer\Encoder\JsonEncoder::supportsDecoding()
*/
public function supportsDecoding($format) {
return $format == $this->format;
}
}

View file

@ -0,0 +1,24 @@
<?php
/**
* @file
* Contains \Drupal\hal\EventSubscriber\ExceptionHalJsonSubscriber.
*/
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'];
}
}

View file

@ -0,0 +1,28 @@
<?php
/**
* @file
* Contains \Drupal\hal\HalServiceProvider.
*/
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']]);
}
}
}

View file

@ -0,0 +1,254 @@
<?php
/**
* @file
* Contains \Drupal\hal\Normalizer\ContentEntityNormalizer.
*/
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;
}
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
*/
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.
// Otherwise, output all field values except internal ID.
if (isset($context['included_fields'])) {
$fields = array();
foreach ($context['included_fields'] as $field_name) {
$fields[] = $entity->get($field_name);
}
}
else {
$fields = $entity->getFields();
}
// Ignore the entity ID and revision ID.
$exclude = array($entity->getEntityType()->getKey('id'), $entity->getEntityType()->getKey('revision'));
foreach ($fields as $field) {
// Continue if this is an excluded field or the current user does not have
// access to view it.
if (in_array($field->getFieldDefinition()->getName(), $exclude) || !$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.
*
* @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']);
$langcode_key = $entity_type->getKey('langcode');
$values = array();
// Figure out the language to use.
if (isset($data[$langcode_key])) {
$values[$langcode_key] = $data[$langcode_key][0]['value'];
// Remove the langcode so it does not get iterated over below.
unset($data[$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
* 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);
}
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;
}
}

View file

@ -0,0 +1,127 @@
<?php
/**
* @file
* Contains \Drupal\hal\Normalizer\EntityReferenceItemNormalizer.
*/
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;
}
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
*/
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),
),
);
}
/**
* Overrides \Drupal\hal\Normalizer\FieldItemNormalizer::constructValue().
*/
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;
}
/**
* Implements \Drupal\serialization\EntityResolver\UuidReferenceInterface::getUuid().
*/
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;
}
}
}

View file

@ -0,0 +1,115 @@
<?php
/**
* @file
* Contains \Drupal\hal\Normalizer\FieldItemNormalizer.
*/
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';
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
*/
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),
);
}
/**
* Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize()
*/
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_translation = $item->getEntity()->getTranslation($langcode);
$field_name = $item->getFieldDefinition()->getName();
return $entity_translation->get($field_name)->appendItem();
}
}

View file

@ -0,0 +1,109 @@
<?php
/**
* @file
* Contains \Drupal\hal\Normalizer\FieldNormalizer.
*/
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';
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
*/
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;
}
/**
* Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::denormalize()
*/
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;
}
}

View file

@ -0,0 +1,75 @@
<?php
/**
* @file
* Contains \Drupal\hal\Normalizer\FileEntityNormalizer.
*/
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 = $this->httpClient->get($data['uri'][0]['value'])->getBody(TRUE);
$path = 'temporary://' . drupal_basename($data['uri'][0]['value']);
$data['uri'] = file_unmanaged_save_data($file_data, $path);
return $this->entityManager->getStorage('file')->create($data);
}
}

View file

@ -0,0 +1,51 @@
<?php
/**
* @file
* Contains \Drupal\hal\Normalizer\NormalizerBase.
*/
namespace Drupal\hal\Normalizer;
use Drupal\serialization\EntityResolver\EntityResolverInterface;
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');
/**
* Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::supportsNormalization().
*/
public function supportsNormalization($data, $format = NULL) {
return in_array($format, $this->formats) && parent::supportsNormalization($data, $format);
}
/**
* Implements \Symfony\Component\Serializer\Normalizer\DenormalizerInterface::supportsDenormalization()
*/
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;
}
}

View file

@ -0,0 +1,211 @@
<?php
/**
* @file
* Contains \Drupal\hal\Tests\DenormalizeTest.
*/
namespace Drupal\hal\Tests;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
/**
* Tests that entities can be denormalized from HAL.
*
* @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.');
}
}
/**
* 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->default_value = 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);
}
/**
* Test that non-reference fields can be denormalized.
*/
public function testBasicFieldDenormalization() {
$data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
'uuid' => array(
array(
'value' => 'e5c9fb96-3acf-4a8d-9417-23de1b6c3311',
),
),
'field_test_text' => array(
array(
'value' => $this->randomMachineName(),
'format' => 'full_html',
),
),
'field_test_translatable_text' => array(
array(
'value' => $this->randomMachineName(),
'format' => 'full_html',
),
array(
'value' => $this->randomMachineName(),
'format' => 'filtered_html',
),
array(
'value' => $this->randomMachineName(),
'format' => 'filtered_html',
'lang' => 'de',
),
array(
'value' => $this->randomMachineName(),
'format' => 'full_html',
'lang' => 'de',
),
),
);
$expected_value_default = array(
array (
'value' => $data['field_test_translatable_text'][0]['value'],
'format' => 'full_html',
),
array (
'value' => $data['field_test_translatable_text'][1]['value'],
'format' => 'filtered_html',
),
);
$expected_value_de = array(
array (
'value' => $data['field_test_translatable_text'][2]['value'],
'format' => 'filtered_html',
),
array (
'value' => $data['field_test_translatable_text'][3]['value'],
'format' => 'full_html',
),
);
$denormalized = $this->serializer->denormalize($data, $this->entityClass, $this->format);
$this->assertEqual($data['uuid'], $denormalized->get('uuid')->getValue(), 'A preset value (e.g. UUID) is overridden by incoming data.');
$this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue(), 'A basic text field is denormalized.');
$this->assertEqual($expected_value_default, $denormalized->get('field_test_translatable_text')->getValue(), 'Values in the default language are properly handled for a translatable field.');
$this->assertEqual($expected_value_de, $denormalized->getTranslation('de')->get('field_test_translatable_text')->getValue(), 'Values in a translation language are properly handled for a translatable field.');
}
/**
* Verifies that the denormalized entity is correct in the PATCH context.
*/
public function testPatchDenormalization() {
$data = array(
'_links' => array(
'type' => array(
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
),
),
'field_test_text' => array(
array(
'value' => $this->randomMachineName(),
'format' => 'full_html',
),
),
);
$denormalized = $this->serializer->denormalize($data, $this->entityClass, $this->format, array('request_method' => 'patch'));
// Check that the one field got populated as expected.
$this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue());
// Check the custom property that contains the list of fields to merge.
$this->assertEqual($denormalized->_restSubmittedFields, ['field_test_text']);
}
}

View file

@ -0,0 +1,224 @@
<?php
/**
* @file
* Contains \Drupal\hal\Tests\EntityTest.
*/
namespace Drupal\hal\Tests;
use Drupal\comment\Tests\CommentTestTrait;
/**
* Tests that nodes and terms are correctly normalized and denormalized.
*
* @group hal
*/
class EntityTest extends NormalizerTestBase {
use CommentTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('node', 'taxonomy', 'comment');
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
\Drupal::service('router.builder')->rebuild();
$this->installSchema('system', array('sequences'));
$this->installSchema('comment', array('comment_entity_statistics'));
$this->installEntitySchema('taxonomy_term');
$this->installConfig(['node', 'comment']);
}
/**
* Tests the normalization of nodes.
*/
public function testNode() {
$node_type = entity_create('node_type', array('type' => 'example_type'));
$node_type->save();
$user = entity_create('user', array('name' => $this->randomMachineName()));
$user->save();
// Add comment type.
$this->container->get('entity.manager')->getStorage('comment_type')->create(array(
'id' => 'comment',
'label' => 'comment',
'target_entity_type_id' => 'node',
))->save();
$this->addDefaultCommentField('node', 'example_type');
$node = entity_create('node', array(
'title' => $this->randomMachineName(),
'uid' => $user->id(),
'type' => $node_type->id(),
'status' => NODE_PUBLISHED,
'promote' => 1,
'sticky' => 0,
'body' => array(
'value' => $this->randomMachineName(),
'format' => $this->randomMachineName(),
),
'revision_log' => $this->randomString(),
));
$node->save();
$original_values = $node->toArray();
unset($original_values['nid']);
unset($original_values['vid']);
$normalized = $this->serializer->normalize($node, $this->format);
$denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\node\Entity\Node', $this->format);
// Verify that the ID and revision ID were skipped by the normalizer.
$this->assertEqual(NULL, $denormalized_node->id());
$this->assertEqual(NULL, $denormalized_node->getRevisionId());
// Loop over the remaining fields and verify that they are identical.
foreach ($original_values as $field_name => $field_values) {
$this->assertEqual($field_values, $denormalized_node->get($field_name)->getValue());
}
}
/**
* Tests the normalization of terms.
*/
public function testTerm() {
$vocabulary = entity_create('taxonomy_vocabulary', array('vid' => 'example_vocabulary'));
$vocabulary->save();
$account = entity_create('user', array('name' => $this->randomMachineName()));
$account->save();
// @todo Until https://www.drupal.org/node/2327935 is fixed, if no parent is
// set, the test fails because target_id => 0 is reserialized to NULL.
$term_parent = entity_create('taxonomy_term', array(
'name' => $this->randomMachineName(),
'vid' => $vocabulary->id(),
));
$term_parent->save();
$term = entity_create('taxonomy_term', array(
'name' => $this->randomMachineName(),
'vid' => $vocabulary->id(),
'description' => array(
'value' => $this->randomMachineName(),
'format' => $this->randomMachineName(),
),
'parent' => $term_parent->id(),
));
$term->save();
$original_values = $term->toArray();
unset($original_values['tid']);
$normalized = $this->serializer->normalize($term, $this->format, ['account' => $account]);
$denormalized_term = $this->serializer->denormalize($normalized, 'Drupal\taxonomy\Entity\Term', $this->format, ['account' => $account]);
// Verify that the ID and revision ID were skipped by the normalizer.
$this->assertEqual(NULL, $denormalized_term->id());
// Loop over the remaining fields and verify that they are identical.
foreach ($original_values as $field_name => $field_values) {
$this->assertEqual($field_values, $denormalized_term->get($field_name)->getValue());
}
}
/**
* Tests the normalization of comments.
*/
public function testComment() {
$node_type = entity_create('node_type', array('type' => 'example_type'));
$node_type->save();
$account = entity_create('user', array('name' => $this->randomMachineName()));
$account->save();
// Add comment type.
$this->container->get('entity.manager')->getStorage('comment_type')->create(array(
'id' => 'comment',
'label' => 'comment',
'target_entity_type_id' => 'node',
))->save();
$this->addDefaultCommentField('node', 'example_type');
$node = entity_create('node', array(
'title' => $this->randomMachineName(),
'uid' => $account->id(),
'type' => $node_type->id(),
'status' => NODE_PUBLISHED,
'promote' => 1,
'sticky' => 0,
'body' => array(
'value' => $this->randomMachineName(),
'format' => $this->randomMachineName(),
)
));
$node->save();
$parent_comment = entity_create('comment', array(
'uid' => $account->id(),
'subject' => $this->randomMachineName(),
'comment_body' => [
'value' => $this->randomMachineName(),
'format' => NULL,
],
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
));
$parent_comment->save();
$comment = entity_create('comment', array(
'uid' => $account->id(),
'subject' => $this->randomMachineName(),
'comment_body' => [
'value' => $this->randomMachineName(),
'format' => NULL,
],
'entity_id' => $node->id(),
'entity_type' => 'node',
'field_name' => 'comment',
'pid' => $parent_comment->id(),
'mail' => 'dries@drupal.org',
'homepage' => 'http://buytaert.net',
));
$comment->save();
$original_values = $comment->toArray();
// cid will not exist and hostname will always be denied view access.
unset($original_values['cid'], $original_values['hostname']);
$normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]);
// Assert that the hostname field does not appear at all in the normalized
// data.
$this->assertFalse(array_key_exists('hostname', $normalized), 'Hostname was not found in normalized comment data.');
$denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\comment\Entity\Comment', $this->format, ['account' => $account]);
// Verify that the ID and revision ID were skipped by the normalizer.
$this->assertEqual(NULL, $denormalized_comment->id());
// Loop over the remaining fields and verify that they are identical.
foreach ($original_values as $field_name => $field_values) {
// The target field comes with revision id which is not set.
if (array_key_exists('revision_id', $field_values[0])) {
unset($field_values[0]['revision_id']);
}
$this->assertEqual($field_values, $denormalized_comment->get($field_name)->getValue());
}
}
}

View file

@ -0,0 +1,80 @@
<?php
/**
* @file
* Contains \Drupal\hal\Tests\FileDenormalizeTest.
*/
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 = entity_create('file', $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.');
}
}

View file

@ -0,0 +1,84 @@
<?php
/**
* @file
* Contains \Drupal\hal\Tests\FileNormalizeTest.
*/
namespace Drupal\hal\Tests;
use Drupal\Core\Cache\MemoryBackend;
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')), 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 = entity_create('file', $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.');
}
}

View file

@ -0,0 +1,184 @@
<?php
/**
* @file
* Contains \Drupal\hal\Tests\NormalizeTest.
*/
namespace Drupal\hal\Tests;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
/**
* Tests that entities can be normalized in HAL.
*
* @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 = entity_create('entity_test', (array('langcode' => 'de', 'field_test_entity_reference' => NULL)));
$target_entity_de->save();
$target_entity_en = entity_create('entity_test', (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 = entity_create('entity_test', $values);
$entity->save();
// Add an English value for name and entity reference properties.
$entity->getTranslation('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',
),
),
),
'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->assertFalse(isset($normalized['id']), 'Internal id is not 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();
}
}

View file

@ -0,0 +1,156 @@
<?php
/**
* @file
* Contains \Drupal\hal\Tests\NormalizerTestBase.
*/
namespace Drupal\hal\Tests;
use Drupal\Core\Cache\MemoryBackend;
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\simpletest\KernelTestBase;
use Symfony\Component\Serializer\Serializer;
/**
* Test the HAL normalizer.
*/
abstract class NormalizerTestBase extends KernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('entity_test', 'entity_reference', '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->installSchema('system', array('url_alias', 'router'));
$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.
entity_create('field_storage_config', array(
'field_name' => 'field_test_text',
'entity_type' => 'entity_test',
'type' => 'text',
))->save();
entity_create('field_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test_text',
'bundle' => 'entity_test',
'translatable' => FALSE,
))->save();
// Create the test translatable field.
entity_create('field_storage_config', array(
'field_name' => 'field_test_translatable_text',
'entity_type' => 'entity_test',
'type' => 'text',
))->save();
entity_create('field_config', array(
'entity_type' => 'entity_test',
'field_name' => 'field_test_translatable_text',
'bundle' => 'entity_test',
'translatable' => TRUE,
))->save();
// Create the test entity reference field.
entity_create('field_storage_config', array(
'field_name' => 'field_test_entity_reference',
'entity_type' => 'entity_test',
'type' => 'entity_reference',
'settings' => array(
'target_type' => 'entity_test',
),
))->save();
entity_create('field_config', array(
'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')), 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);
}
}

View file

@ -0,0 +1,34 @@
<?php
/**
* @file
* Contains \Drupal\Tests\hal\Unit\FieldItemNormalizerDenormalizeExceptionsUnitTest.
*/
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);
}
}

View file

@ -0,0 +1,34 @@
<?php
/**
* @file
* Contains \Drupal\Tests\hal\Unit\FieldNormalizerDenormalizeExceptionsUnitTest.
*/
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);
}
}

View file

@ -0,0 +1,36 @@
<?php
/**
* @file
* Contains \Drupal\Tests\hal\Unit\NormalizerDenormalizeExceptionsUnitTestBase.
*/
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)),
);
}
}