updated core from 8.4 to 8.5 : bug with login_destination
This commit is contained in:
@@ -1,3 +1,11 @@
|
||||
# Set the domain for HAL type and relation links.
|
||||
# If left blank, the site's domain will be used.
|
||||
link_domain: ~
|
||||
# Before Drupal 8.5, the File entity 'uri' field value was overridden to return
|
||||
# the absolute file URL instead of the actual (stream wrapper) URI. The default
|
||||
# for new sites is now to return the actual URI as well as a root-relative file
|
||||
# URL. Enable this setting to use the previous behavior. For existing sites,
|
||||
# the previous behavior is kept by default.
|
||||
# @see hal_update_8501()
|
||||
# @see https://www.drupal.org/node/2925783
|
||||
bc_file_uri_as_url_normalizer: false
|
||||
|
||||
@@ -6,3 +6,6 @@ hal.settings:
|
||||
link_domain:
|
||||
type: string
|
||||
label: 'Domain of the relation'
|
||||
bc_file_uri_as_url_normalizer:
|
||||
type: boolean
|
||||
label: 'Whether to retain pre Drupal 8.5 behavior of normalizing the File entity "uri" field value to an absolute URL.'
|
||||
|
||||
@@ -7,8 +7,8 @@ package: Web services
|
||||
dependencies:
|
||||
- serialization
|
||||
|
||||
# Information added by Drupal.org packaging script on 2018-01-03
|
||||
version: '8.4.4'
|
||||
# Information added by Drupal.org packaging script on 2018-03-07
|
||||
version: '8.5.0'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1515021228
|
||||
datestamp: 1520457825
|
||||
|
||||
@@ -31,3 +31,15 @@ function hal_update_8301() {
|
||||
$hal_settings->set('link_domain', $link_domain);
|
||||
$hal_settings->save(TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add hal.settings::bc_file_uri_as_url_normalizer configuration.
|
||||
*/
|
||||
function hal_update_8501() {
|
||||
$config_factory = \Drupal::configFactory();
|
||||
$config_factory->getEditable('hal.settings')
|
||||
->set('bc_file_uri_as_url_normalizer', TRUE)
|
||||
->save(TRUE);
|
||||
|
||||
return t('Backwards compatibility mode has been enabled for File entities\' HAL normalization of the "uri" field. Like before, it will continue to return only the absolute file URL. If you want the new behavior, which returns both the stored URI and a root-relative file URL, <a href="https://www.drupal.org/node/2925783">read the change record to learn how to opt in.</a>');
|
||||
}
|
||||
|
||||
@@ -14,9 +14,10 @@ services:
|
||||
- { name: normalizer, priority: 10 }
|
||||
serializer.normalizer.file_entity.hal:
|
||||
class: Drupal\hal\Normalizer\FileEntityNormalizer
|
||||
deprecated: 'The "%service_id%" normalizer service is deprecated: it is obsolete, it only remains available for backwards compatibility.'
|
||||
arguments: ['@entity.manager', '@http_client', '@hal.link_manager', '@module_handler', '@config.factory']
|
||||
tags:
|
||||
- { name: normalizer, priority: 20 }
|
||||
arguments: ['@entity.manager', '@http_client', '@hal.link_manager', '@module_handler']
|
||||
serializer.normalizer.timestamp_item.hal:
|
||||
class: Drupal\hal\Normalizer\TimestampItemNormalizer
|
||||
tags:
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
namespace Drupal\hal\LinkManager;
|
||||
|
||||
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
|
||||
|
||||
/**
|
||||
* Defines an abstract base-class for HAL link manager objects.
|
||||
*/
|
||||
@@ -39,17 +41,32 @@ abstract class LinkManagerBase {
|
||||
/**
|
||||
* Gets the link domain.
|
||||
*
|
||||
* @param array $context
|
||||
* Normalization/serialization context.
|
||||
*
|
||||
* @return string
|
||||
* The link domain.
|
||||
*
|
||||
* @see \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize()
|
||||
* @see \Symfony\Component\Serializer\SerializerInterface::serialize()
|
||||
* @see \Drupal\serialization\Normalizer\CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY
|
||||
*/
|
||||
protected function getLinkDomain() {
|
||||
protected function getLinkDomain(array $context = []) {
|
||||
if (empty($this->linkDomain)) {
|
||||
if ($domain = $this->configFactory->get('hal.settings')->get('link_domain')) {
|
||||
$this->linkDomain = rtrim($domain, '/');
|
||||
// Bubble the appropriate cacheability metadata whenever possible.
|
||||
if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
|
||||
$context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheableDependency($this->configFactory->get('hal.settings'));
|
||||
}
|
||||
return rtrim($domain, '/');
|
||||
}
|
||||
else {
|
||||
// Bubble the relevant cacheability metadata whenever possible.
|
||||
if (isset($context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY])) {
|
||||
$context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]->addCacheContexts(['url.site']);
|
||||
}
|
||||
$request = $this->requestStack->getCurrentRequest();
|
||||
$this->linkDomain = $request->getSchemeAndHttpHost() . $request->getBasePath();
|
||||
return $request->getSchemeAndHttpHost() . $request->getBasePath();
|
||||
}
|
||||
}
|
||||
return $this->linkDomain;
|
||||
|
||||
@@ -68,7 +68,7 @@ class RelationLinkManager extends LinkManagerBase implements RelationLinkManager
|
||||
// module is installed that adds such content, but requires this URL to be
|
||||
// different (e.g., include a language prefix), then the module must also
|
||||
// override the RelationLinkManager class/service to return the desired URL.
|
||||
$uri = $this->getLinkDomain() . "/rest/relation/$entity_type/$bundle/$field_name";
|
||||
$uri = $this->getLinkDomain($context) . "/rest/relation/$entity_type/$bundle/$field_name";
|
||||
$this->moduleHandler->alter('hal_relation_uri', $uri, $context);
|
||||
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
|
||||
// hook is invoked to maintain backwards compatibility
|
||||
|
||||
@@ -69,7 +69,7 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
|
||||
// installed that adds such content, but requires this URL to be different
|
||||
// (e.g., include a language prefix), then the module must also override the
|
||||
// TypeLinkManager class/service to return the desired URL.
|
||||
$uri = $this->getLinkDomain() . "/rest/type/$entity_type/$bundle";
|
||||
$uri = $this->getLinkDomain($context) . "/rest/type/$entity_type/$bundle";
|
||||
$this->moduleHandler->alter('hal_type_uri', $uri, $context);
|
||||
// @deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. This
|
||||
// hook is invoked to maintain backwards compatibility
|
||||
|
||||
@@ -6,6 +6,7 @@ use Drupal\Component\Utility\NestedArray;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
|
||||
use Drupal\hal\LinkManager\LinkManagerInterface;
|
||||
use Drupal\serialization\Normalizer\FieldableEntityNormalizerTrait;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
@@ -72,15 +73,10 @@ class ContentEntityNormalizer extends NormalizerBase {
|
||||
],
|
||||
];
|
||||
|
||||
$field_items = TypedDataInternalPropertiesHelper::getNonInternalProperties($entity->getTypedData());
|
||||
// If the fields to use were specified, only output those field values.
|
||||
if (isset($context['included_fields'])) {
|
||||
$field_items = [];
|
||||
foreach ($context['included_fields'] as $field_name) {
|
||||
$field_items[] = $entity->get($field_name);
|
||||
}
|
||||
}
|
||||
else {
|
||||
$field_items = $entity->getFields();
|
||||
$field_items = array_intersect_key($field_items, array_flip($context['included_fields']));
|
||||
}
|
||||
foreach ($field_items as $field) {
|
||||
// Continue if the current user does not have access to view this field.
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Field\FieldItemInterface;
|
||||
use Drupal\Core\TypedData\TypedDataInternalPropertiesHelper;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
@@ -91,7 +92,7 @@ class FieldItemNormalizer extends NormalizerBase {
|
||||
// We normalize each individual property, so each can do their own casting,
|
||||
// if needed.
|
||||
/** @var \Drupal\Core\TypedData\TypedDataInterface $property */
|
||||
foreach ($field_item as $property_name => $property) {
|
||||
foreach (TypedDataInternalPropertiesHelper::getNonInternalProperties($field_item) as $property_name => $property) {
|
||||
$normalized[$property_name] = $this->serializer->normalize($property, $format, $context);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\hal\Normalizer;
|
||||
|
||||
use Drupal\Core\Config\ConfigFactoryInterface;
|
||||
use Drupal\Core\Entity\EntityManagerInterface;
|
||||
use Drupal\Core\Extension\ModuleHandlerInterface;
|
||||
use Drupal\hal\LinkManager\LinkManagerInterface;
|
||||
@@ -9,6 +10,8 @@ use GuzzleHttp\ClientInterface;
|
||||
|
||||
/**
|
||||
* Converts the Drupal entity object structure to a HAL array structure.
|
||||
*
|
||||
* @deprecated in Drupal 8.5.0, to be removed before Drupal 9.0.0.
|
||||
*/
|
||||
class FileEntityNormalizer extends ContentEntityNormalizer {
|
||||
|
||||
@@ -26,6 +29,13 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
|
||||
*/
|
||||
protected $httpClient;
|
||||
|
||||
/**
|
||||
* The HAL settings config.
|
||||
*
|
||||
* @var \Drupal\Core\Config\ImmutableConfig
|
||||
*/
|
||||
protected $halSettings;
|
||||
|
||||
/**
|
||||
* Constructs a FileEntityNormalizer object.
|
||||
*
|
||||
@@ -37,11 +47,14 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
|
||||
* The hypermedia link manager.
|
||||
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
|
||||
* The module handler.
|
||||
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
|
||||
* The config factory.
|
||||
*/
|
||||
public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler) {
|
||||
public function __construct(EntityManagerInterface $entity_manager, ClientInterface $http_client, LinkManagerInterface $link_manager, ModuleHandlerInterface $module_handler, ConfigFactoryInterface $config_factory) {
|
||||
parent::__construct($link_manager, $entity_manager, $module_handler);
|
||||
|
||||
$this->httpClient = $http_client;
|
||||
$this->halSettings = $config_factory->get('hal.settings');
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,8 +62,13 @@ class FileEntityNormalizer extends ContentEntityNormalizer {
|
||||
*/
|
||||
public function normalize($entity, $format = NULL, array $context = []) {
|
||||
$data = parent::normalize($entity, $format, $context);
|
||||
// Replace the file url with a full url for the file.
|
||||
$data['uri'][0]['value'] = $this->getEntityUri($entity);
|
||||
|
||||
$this->addCacheableDependency($context, $this->halSettings);
|
||||
|
||||
if ($this->halSettings->get('bc_file_uri_as_url_normalizer')) {
|
||||
// Replace the file url with a full url for the file.
|
||||
$data['uri'][0]['value'] = $this->getEntityUri($entity);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@ package: Testing
|
||||
# version: VERSION
|
||||
# core: 8.x
|
||||
|
||||
# Information added by Drupal.org packaging script on 2018-01-03
|
||||
version: '8.4.4'
|
||||
# Information added by Drupal.org packaging script on 2018-03-07
|
||||
version: '8.5.0'
|
||||
core: '8.x'
|
||||
project: 'drupal'
|
||||
datestamp: 1515021228
|
||||
datestamp: 1520457825
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\BlockContent;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\BlockContent\BlockContentResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class BlockContentHalJsonAnonTest extends BlockContentResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
|
||||
return $normalization + [
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . '/block/1?_format=hal_json',
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/block_content/basic',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/block_content/basic',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
// The 'url.site' cache context is added for '_links' in the response.
|
||||
return Cache::mergeTags(parent::getExpectedCacheContexts(), ['url.site']);
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\BlockContent;
|
||||
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class BlockContentHalJsonBasicAuthTest extends BlockContentHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\BlockContent;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class BlockContentHalJsonCookieTest extends BlockContentHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
||||
+1
-1
@@ -25,11 +25,11 @@ class CommentHalJsonAnonTest extends CommentHalJsonTestBase {
|
||||
* @see ::setUpAuthorization
|
||||
*/
|
||||
protected static $patchProtectedFieldNames = [
|
||||
'entity_id',
|
||||
'changed',
|
||||
'thread',
|
||||
'entity_type',
|
||||
'field_name',
|
||||
'entity_id',
|
||||
];
|
||||
|
||||
}
|
||||
|
||||
-19
@@ -26,25 +26,6 @@ abstract class CommentHalJsonTestBase extends CommentResourceTestBase {
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+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 = [
|
||||
'status',
|
||||
'created',
|
||||
'changed',
|
||||
'thread',
|
||||
'entity_type',
|
||||
'field_name',
|
||||
'entity_id',
|
||||
'uid',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\ConfigurableLanguage;
|
||||
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceWithInterfaceTranslationTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\ConfigurableLanguage\ConfigurableLanguageResourceTestBase;
|
||||
|
||||
/**
|
||||
@@ -10,7 +10,7 @@ use Drupal\Tests\rest\Functional\EntityResource\ConfigurableLanguage\Configurabl
|
||||
*/
|
||||
class ConfigurableLanguageHalJsonBasicAuthTest extends ConfigurableLanguageResourceTestBase {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
use BasicAuthResourceWithInterfaceTranslationTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
||||
+2
-2
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\ContentLanguageSettings;
|
||||
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceWithInterfaceTranslationTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\ContentLanguageSettings\ContentLanguageSettingsResourceTestBase;
|
||||
|
||||
/**
|
||||
@@ -10,7 +10,7 @@ use Drupal\Tests\rest\Functional\EntityResource\ContentLanguageSettings\ContentL
|
||||
*/
|
||||
class ContentLanguageSettingsHalJsonBasicAuthTest extends ContentLanguageSettingsResourceTestBase {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
use BasicAuthResourceWithInterfaceTranslationTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityFormDisplay;
|
||||
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\EntityFormDisplay\EntityFormDisplayResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityFormDisplayHalJsonAnonTest extends EntityFormDisplayResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityFormDisplay;
|
||||
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\EntityFormDisplay\EntityFormDisplayResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityFormDisplayHalJsonBasicAuthTest extends EntityFormDisplayResourceTestBase {
|
||||
|
||||
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 $auth = 'basic_auth';
|
||||
|
||||
}
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityFormDisplay;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\EntityFormDisplay\EntityFormDisplayResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityFormDisplayHalJsonCookieTest extends EntityFormDisplayResourceTestBase {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
||||
+2
@@ -5,6 +5,7 @@ namespace Drupal\Tests\hal\Functional\EntityResource\EntityTest;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\EntityTest\EntityTestResourceTestBase;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\FormatSpecificGetBcRouteTestTrait;
|
||||
use Drupal\user\Entity\User;
|
||||
|
||||
/**
|
||||
@@ -14,6 +15,7 @@ class EntityTestHalJsonAnonTest extends EntityTestResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
use FormatSpecificGetBcRouteTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityTest;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* Test that internal properties are not exposed in the 'hal_json' format.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class EntityTestHalJsonInternalPropertyNormalizerTest extends EntityTestHalJsonAnonTest {
|
||||
|
||||
use AnonResourceTestTrait, HalEntityNormalizationTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
// The 'internal_value' property in test field type will not be returned in
|
||||
// normalization because setInternal(FALSE) was not called for this
|
||||
// property.
|
||||
// @see \Drupal\entity_test\Plugin\Field\FieldType\InternalPropertyTestFieldItem::propertyDefinitions
|
||||
$normalization['field_test_internal'] = [
|
||||
[
|
||||
'value' => 'This value shall not be internal!',
|
||||
'non_internal_value' => 'Computed! This value shall not be internal!',
|
||||
],
|
||||
];
|
||||
return $normalization;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function createEntity() {
|
||||
if (!FieldStorageConfig::loadByName('entity_test', 'field_test_internal')) {
|
||||
FieldStorageConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_internal',
|
||||
'type' => 'internal_property_test',
|
||||
'cardinality' => 1,
|
||||
'translatable' => FALSE,
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_internal',
|
||||
'bundle' => 'entity_test',
|
||||
'label' => 'Test field with internal and non-internal properties',
|
||||
])->save();
|
||||
}
|
||||
|
||||
$entity = parent::createEntity();
|
||||
$entity->field_test_internal = [
|
||||
'value' => 'This value shall not be internal!',
|
||||
];
|
||||
$entity->save();
|
||||
return $entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'field_test_internal' => [
|
||||
[
|
||||
'value' => 'This value shall not be internal!',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
return Cache::mergeContexts(parent::getExpectedCacheContexts(), ['request_format']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheTags() {
|
||||
return Cache::mergeTags(parent::getExpectedCacheTags(), ['you_are_it', 'no_tag_backs']);
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityViewDisplay;
|
||||
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\EntityViewDisplay\EntityViewDisplayResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityViewDisplayHalJsonAnonTest extends EntityViewDisplayResourceTestBase {
|
||||
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityViewDisplay;
|
||||
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityViewDisplayHalJsonBasicAuthTest extends EntityViewDisplayHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\EntityViewDisplay;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class EntityViewDisplayHalJsonCookieTest extends EntityViewDisplayHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\File;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\File\FileResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class FileHalJsonAnonTest extends FileResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedNormalizedEntity() {
|
||||
$default_normalization = parent::getExpectedNormalizedEntity();
|
||||
|
||||
$normalization = $this->applyHalFieldNormalization($default_normalization);
|
||||
|
||||
$url = file_create_url($this->entity->getFileUri());
|
||||
// @see \Drupal\Tests\hal\Functional\EntityResource\File\FileHalJsonAnonTest::testGetBcUriField()
|
||||
if ($this->config('hal.settings')->get('bc_file_uri_as_url_normalizer')) {
|
||||
$normalization['uri'][0]['value'] = $url;
|
||||
}
|
||||
|
||||
$uid = $this->author->id();
|
||||
|
||||
return $normalization + [
|
||||
'_embedded' => [
|
||||
$this->baseUrl . '/rest/relation/file/file/uid' => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->baseUrl . "/user/$uid?_format=hal_json",
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/user/user',
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
[
|
||||
'value' => $this->author->uuid(),
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $url,
|
||||
],
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/file/file',
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/file/file/uid' => [
|
||||
[
|
||||
'href' => $this->baseUrl . "/user/$uid?_format=hal_json",
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/file/file',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheTags() {
|
||||
return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:hal.settings']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheContexts() {
|
||||
return [
|
||||
'url.site',
|
||||
'user.permissions',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @see hal_update_8501()
|
||||
*/
|
||||
public function testGetBcUriField() {
|
||||
$this->config('hal.settings')->set('bc_file_uri_as_url_normalizer', TRUE)->save(TRUE);
|
||||
|
||||
$this->initAuthentication();
|
||||
$url = $this->getEntityResourceUrl();
|
||||
$url->setOption('query', ['_format' => static::$format]);
|
||||
$request_options = $this->getAuthenticationRequestOptions('GET');
|
||||
$this->provisionEntityResource();
|
||||
$this->setUpAuthorization('GET');
|
||||
$response = $this->request('GET', $url, $request_options);
|
||||
$expected = $this->getExpectedNormalizedEntity();
|
||||
static::recursiveKSort($expected);
|
||||
$actual = $this->serializer->decode((string) $response->getBody(), static::$format);
|
||||
static::recursiveKSort($actual);
|
||||
$this->assertSame($expected, $actual);
|
||||
|
||||
// Explicitly assert that $file->uri->value is an absolute file URL, unlike
|
||||
// the default normalization.
|
||||
$this->assertSame($this->baseUrl . '/' . $this->siteDirectory . '/files/drupal.txt', $actual['uri'][0]['value']);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function testPatch() {
|
||||
// @todo https://www.drupal.org/node/1927648
|
||||
$this->markTestSkipped();
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\File;
|
||||
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class FileHalJsonBasicAuthTest extends FileHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\File;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class FileHalJsonCookieTest extends FileHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
||||
@@ -70,24 +70,6 @@ trait HalEntityNormalizationTrait {
|
||||
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}
|
||||
*/
|
||||
|
||||
+10
-12
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Media;
|
||||
|
||||
use Drupal\Core\Cache\Cache;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
@@ -50,7 +51,7 @@ class MediaHalJsonAnonTest extends MediaResourceTestBase {
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/media/camelids',
|
||||
],
|
||||
$this->baseUrl . '/rest/relation/media/camelids/field_media_file_1' => [
|
||||
$this->baseUrl . '/rest/relation/media/camelids/field_media_file' => [
|
||||
[
|
||||
'href' => $file->url(),
|
||||
'lang' => 'en',
|
||||
@@ -75,7 +76,7 @@ class MediaHalJsonAnonTest extends MediaResourceTestBase {
|
||||
],
|
||||
],
|
||||
'_embedded' => [
|
||||
$this->baseUrl . '/rest/relation/media/camelids/field_media_file_1' => [
|
||||
$this->baseUrl . '/rest/relation/media/camelids/field_media_file' => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
@@ -86,11 +87,6 @@ class MediaHalJsonAnonTest extends MediaResourceTestBase {
|
||||
],
|
||||
],
|
||||
'lang' => 'en',
|
||||
'uri' => [
|
||||
[
|
||||
'value' => $file->url(),
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
[
|
||||
'value' => $file->uuid(),
|
||||
@@ -126,11 +122,6 @@ class MediaHalJsonAnonTest extends MediaResourceTestBase {
|
||||
],
|
||||
],
|
||||
'lang' => 'en',
|
||||
'uri' => [
|
||||
[
|
||||
'value' => $thumbnail->url(),
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
[
|
||||
'value' => $thumbnail->uuid(),
|
||||
@@ -173,4 +164,11 @@ class MediaHalJsonAnonTest extends MediaResourceTestBase {
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getExpectedCacheTags() {
|
||||
return Cache::mergeTags(parent::getExpectedCacheTags(), ['config:hal.settings']);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Message;
|
||||
|
||||
use Drupal\Tests\hal\Functional\EntityResource\HalEntityNormalizationTrait;
|
||||
use Drupal\Tests\rest\Functional\AnonResourceTestTrait;
|
||||
use Drupal\Tests\rest\Functional\EntityResource\Message\MessageResourceTestBase;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class MessageHalJsonAnonTest extends MessageResourceTestBase {
|
||||
|
||||
use HalEntityNormalizationTrait;
|
||||
use AnonResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function getNormalizedPostEntity() {
|
||||
return parent::getNormalizedPostEntity() + [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => $this->baseUrl . '/rest/type/contact_message/camelids',
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Message;
|
||||
|
||||
use Drupal\Tests\rest\Functional\BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class MessageHalJsonBasicAuthTest extends MessageHalJsonAnonTest {
|
||||
|
||||
use BasicAuthResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['basic_auth'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'basic_auth';
|
||||
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Message;
|
||||
|
||||
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* @group hal
|
||||
*/
|
||||
class MessageHalJsonCookieTest extends MessageHalJsonAnonTest {
|
||||
|
||||
use CookieResourceTestTrait;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $auth = 'cookie';
|
||||
|
||||
}
|
||||
@@ -30,19 +30,6 @@ class NodeHalJsonAnonTest extends NodeResourceTestBase {
|
||||
*/
|
||||
protected static $mimeType = 'application/hal+json';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected static $patchProtectedFieldNames = [
|
||||
'revision_timestamp',
|
||||
'created',
|
||||
'changed',
|
||||
'promote',
|
||||
'sticky',
|
||||
'path',
|
||||
'revision_uid',
|
||||
];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
||||
@@ -20,6 +20,20 @@ class FileDenormalizeTest extends BrowserTestBase {
|
||||
*/
|
||||
public static $modules = ['hal', 'file', 'node'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
// @todo Remove this work-around in https://www.drupal.org/node/1927648.
|
||||
// @see hal_update_8501()
|
||||
\Drupal::configFactory()
|
||||
->getEditable('hal.settings')
|
||||
->set('bc_file_uri_as_url_normalizer', TRUE)
|
||||
->save(TRUE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests file entity denormalization.
|
||||
*/
|
||||
|
||||
@@ -83,6 +83,8 @@ class EntityTranslationNormalizeTest extends NormalizerTestBase {
|
||||
$this->assertSame($node->getTitle(), $denormalized_node->getTitle());
|
||||
$this->assertSame($translation->getTitle(), $denormalized_node->getTranslation('de')->getTitle());
|
||||
|
||||
$original_values['revision_default'] = [];
|
||||
$original_translation_values['revision_default'] = [];
|
||||
$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.');
|
||||
}
|
||||
|
||||
@@ -44,7 +44,10 @@ class FileNormalizeTest extends NormalizerTestBase {
|
||||
|
||||
$expected_array = [
|
||||
'uri' => [
|
||||
['value' => file_create_url($file->getFileUri())],
|
||||
[
|
||||
'value' => $file->getFileUri(),
|
||||
'url' => file_url_transform_relative(file_create_url($file->getFileUri())),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Cache\CacheableMetadata;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
|
||||
|
||||
/**
|
||||
* @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
|
||||
@@ -67,6 +69,13 @@ class HalLinkManagerTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
public function providerTestGetTypeUri() {
|
||||
$serialization_context_collecting_cacheability = [
|
||||
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
|
||||
];
|
||||
$expected_serialization_context_cacheability_url_site = [
|
||||
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
|
||||
];
|
||||
|
||||
$base_test_case = [
|
||||
'link_domain' => NULL,
|
||||
'entity_type' => 'node',
|
||||
@@ -80,18 +89,35 @@ class HalLinkManagerTest extends KernelTestBase {
|
||||
'expected return' => 'BASE_URL/rest/type/node/page',
|
||||
'expected context' => [],
|
||||
],
|
||||
'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
|
||||
'context' => $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'BASE_URL/rest/type/node/page',
|
||||
'expected context' => $expected_serialization_context_cacheability_url_site,
|
||||
],
|
||||
// Test hook_hal_type_uri_alter().
|
||||
'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [
|
||||
'context' => ['hal_test' => TRUE],
|
||||
'expected return' => 'hal_test_type',
|
||||
'expected context' => ['hal_test' => TRUE],
|
||||
],
|
||||
'site URL, with optional context, to test hook_hal_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
||||
'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'hal_test_type',
|
||||
// No cacheability metadata bubbled.
|
||||
'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
],
|
||||
// Test hook_rest_type_uri_alter() — for backwards compatibility.
|
||||
'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [
|
||||
'context' => ['rest_test' => TRUE],
|
||||
'expected return' => 'rest_test_type',
|
||||
'expected context' => ['rest_test' => TRUE],
|
||||
],
|
||||
'site URL, with optional context, to test hook_rest_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
||||
'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'rest_test_type',
|
||||
// No cacheability metadata bubbled.
|
||||
'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
],
|
||||
'configured URL' => [
|
||||
'link_domain' => 'http://llamas-rock.com/for-real/',
|
||||
'entity_type' => 'node',
|
||||
@@ -100,6 +126,16 @@ class HalLinkManagerTest extends KernelTestBase {
|
||||
'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
|
||||
'expected context' => [],
|
||||
],
|
||||
'configured URL, with optional context to collect cacheability metadata' => [
|
||||
'link_domain' => 'http://llamas-rock.com/for-real/',
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'context' => $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
|
||||
'expected context' => [
|
||||
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -126,6 +162,13 @@ class HalLinkManagerTest extends KernelTestBase {
|
||||
}
|
||||
|
||||
public function providerTestGetRelationUri() {
|
||||
$serialization_context_collecting_cacheability = [
|
||||
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
|
||||
];
|
||||
$expected_serialization_context_cacheability_url_site = [
|
||||
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
|
||||
];
|
||||
|
||||
$field_name = $this->randomMachineName();
|
||||
$base_test_case = [
|
||||
'link_domain' => NULL,
|
||||
@@ -141,18 +184,35 @@ class HalLinkManagerTest extends KernelTestBase {
|
||||
'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
|
||||
'expected context' => [],
|
||||
],
|
||||
'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
|
||||
'context' => $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
|
||||
'expected context' => $expected_serialization_context_cacheability_url_site,
|
||||
],
|
||||
// Test hook_hal_relation_uri_alter().
|
||||
'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [
|
||||
'context' => ['hal_test' => TRUE],
|
||||
'expected return' => 'hal_test_relation',
|
||||
'expected context' => ['hal_test' => TRUE],
|
||||
],
|
||||
'site URL, with optional context, to test hook_hal_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
||||
'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'hal_test_relation',
|
||||
// No cacheability metadata bubbled.
|
||||
'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
],
|
||||
// Test hook_rest_relation_uri_alter() — for backwards compatibility.
|
||||
'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [
|
||||
'context' => ['rest_test' => TRUE],
|
||||
'expected return' => 'rest_test_relation',
|
||||
'expected context' => ['rest_test' => TRUE],
|
||||
],
|
||||
'site URL, with optional context, to test hook_rest_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
||||
'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'rest_test_relation',
|
||||
// No cacheability metadata bubbled.
|
||||
'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
||||
],
|
||||
'configured URL' => [
|
||||
'link_domain' => 'http://llamas-rock.com/for-real/',
|
||||
'entity_type' => 'node',
|
||||
@@ -162,6 +222,17 @@ class HalLinkManagerTest extends KernelTestBase {
|
||||
'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
|
||||
'expected context' => [],
|
||||
],
|
||||
'configured URL, with optional context to collect cacheability metadata' => [
|
||||
'link_domain' => 'http://llamas-rock.com/for-real/',
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'field_name' => $field_name,
|
||||
'context' => $serialization_context_collecting_cacheability,
|
||||
'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
|
||||
'expected context' => [
|
||||
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@@ -186,13 +257,19 @@ class HalLinkManagerTest extends KernelTestBase {
|
||||
* @covers ::setLinkDomain
|
||||
*/
|
||||
public function testHalLinkManagersSetLinkDomain() {
|
||||
$serialization_context = [
|
||||
CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
|
||||
];
|
||||
|
||||
/* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
|
||||
$link_manager = \Drupal::service('hal.link_manager');
|
||||
$link_manager->setLinkDomain('http://example.com/');
|
||||
$link = $link_manager->getTypeUri('node', 'page');
|
||||
$link = $link_manager->getTypeUri('node', 'page', $serialization_context);
|
||||
$this->assertEqual($link, 'http://example.com/rest/type/node/page');
|
||||
$link = $link_manager->getRelationUri('node', 'page', 'field_ref');
|
||||
$this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
|
||||
$link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
|
||||
$this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref');
|
||||
$this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Drupal\Tests\hal\Kernel;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
use Drupal\filter\Entity\FilterFormat;
|
||||
|
||||
/**
|
||||
* Tests HAL normalization edge cases for EntityResource.
|
||||
@@ -19,6 +20,27 @@ class NormalizeTest extends NormalizerTestBase {
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
FilterFormat::create([
|
||||
'format' => 'my_text_format',
|
||||
'name' => 'My Text Format',
|
||||
'filters' => [
|
||||
'filter_html' => [
|
||||
'module' => 'filter',
|
||||
'status' => TRUE,
|
||||
'weight' => 10,
|
||||
'settings' => [
|
||||
'allowed_html' => '<p>',
|
||||
],
|
||||
],
|
||||
'filter_autop' => [
|
||||
'module' => 'filter',
|
||||
'status' => TRUE,
|
||||
'weight' => 10,
|
||||
'settings' => [],
|
||||
],
|
||||
],
|
||||
])->save();
|
||||
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
}
|
||||
|
||||
@@ -37,7 +59,7 @@ class NormalizeTest extends NormalizerTestBase {
|
||||
'name' => $this->randomMachineName(),
|
||||
'field_test_text' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
'format' => 'my_text_format',
|
||||
],
|
||||
'field_test_entity_reference' => [
|
||||
'target_id' => $target_entity_de->id(),
|
||||
@@ -152,6 +174,7 @@ class NormalizeTest extends NormalizerTestBase {
|
||||
[
|
||||
'value' => $values['field_test_text']['value'],
|
||||
'format' => $values['field_test_text']['format'],
|
||||
'processed' => "<p>{$values['field_test_text']['value']}</p>",
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
Reference in New Issue
Block a user