Move all files to 2017/
This commit is contained in:
parent
ac7370f67f
commit
2875863330
15717 changed files with 0 additions and 0 deletions
43
2017/web/core/modules/hal/tests/fixtures/update/drupal-8.hal-hal_update_8301.php
vendored
Normal file
43
2017/web/core/modules/hal/tests/fixtures/update/drupal-8.hal-hal_update_8301.php
vendored
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains database additions to drupal-8.bare.standard.php.gz for testing the
|
||||
* upgrade path of hal_update_8301().
|
||||
*/
|
||||
|
||||
use Drupal\Core\Database\Database;
|
||||
|
||||
$connection = Database::getConnection();
|
||||
|
||||
// Set the schema version.
|
||||
$connection->insert('key_value')
|
||||
->fields([
|
||||
'collection' => 'system.schema',
|
||||
'name' => 'hal',
|
||||
'value' => 'i:8000;',
|
||||
])
|
||||
->fields([
|
||||
'collection' => 'system.schema',
|
||||
'name' => 'serialization',
|
||||
'value' => 'i:8000;',
|
||||
])
|
||||
->execute();
|
||||
|
||||
// Update core.extension.
|
||||
$extensions = $connection->select('config')
|
||||
->fields('config', ['data'])
|
||||
->condition('collection', '')
|
||||
->condition('name', 'core.extension')
|
||||
->execute()
|
||||
->fetchField();
|
||||
$extensions = unserialize($extensions);
|
||||
$extensions['module']['hal'] = 0;
|
||||
$extensions['module']['serialization'] = 0;
|
||||
$connection->update('config')
|
||||
->fields([
|
||||
'data' => serialize($extensions),
|
||||
])
|
||||
->condition('collection', '')
|
||||
->condition('name', 'core.extension')
|
||||
->execute();
|
||||
BIN
2017/web/core/modules/hal/tests/fixtures/update/drupal-8.rest-hal_update_8301.php
vendored
Normal file
BIN
2017/web/core/modules/hal/tests/fixtures/update/drupal-8.rest-hal_update_8301.php
vendored
Normal file
Binary file not shown.
|
|
@ -0,0 +1,6 @@
|
|||
name: HAL test module
|
||||
type: module
|
||||
description: "Support module for HAL tests."
|
||||
package: Testing
|
||||
version: VERSION
|
||||
core: 8.x
|
||||
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains hook implementations for testing HAL module.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Implements hook_hal_type_uri_alter().
|
||||
*/
|
||||
function hal_test_hal_type_uri_alter(&$uri, $context = []) {
|
||||
if (!empty($context['hal_test'])) {
|
||||
$uri = 'hal_test_type';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_hal_relation_uri_alter().
|
||||
*/
|
||||
function hal_test_hal_relation_uri_alter(&$uri, $context = []) {
|
||||
if (!empty($context['hal_test'])) {
|
||||
$uri = 'hal_test_relation';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_rest_type_uri_alter().
|
||||
*
|
||||
* @deprecated Kept only for BC test coverage, see \Drupal\Tests\hal\Kernel\HalLinkManagerTest::testGetTypeUri().
|
||||
*
|
||||
* @see https://www.drupal.org/node/2830467
|
||||
*/
|
||||
function hal_test_rest_type_uri_alter(&$uri, $context = []) {
|
||||
if (!empty($context['rest_test'])) {
|
||||
$uri = 'rest_test_type';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_rest_relation_uri_alter().
|
||||
*
|
||||
* @deprecated Kept only for BC test coverage, see \Drupal\Tests\hal\Kernel\HalLinkManagerTest::testGetRelationUri().
|
||||
*
|
||||
* @see https://www.drupal.org/node/2830467
|
||||
*/
|
||||
function hal_test_rest_relation_uri_alter(&$uri, $context = []) {
|
||||
if (!empty($context['rest_test'])) {
|
||||
$uri = 'rest_test_relation';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Comment;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\CommentHalJsonTestBase is deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Instead, use Drupal\Tests\comment\Functional\Hal\CommentHalJsonTestBase. See https://www.drupal.org/node/2971931.', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\Tests\comment\Functional\Hal\CommentHalJsonTestBase as CommentHalJsonTestBaseReal;
|
||||
|
||||
/**
|
||||
* @deprecated in Drupal 8.6.x. Will be removed before Drupal 9.0.0. Use
|
||||
* Drupal\Tests\comment\Functional\Hal\CommentHalJsonTestBase instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2971931
|
||||
*/
|
||||
abstract class CommentHalJsonTestBase extends CommentHalJsonTestBaseReal {
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Feed;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\FeedHalJsonTestBase is deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Instead, use Drupal\Tests\aggregator\Functional\Hal\FeedHalJsonTestBase. See https://www.drupal.org/node/2971931.', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\Tests\aggregator\Functional\Hal\FeedHalJsonTestBase as FeedHalJsonTestBaseReal;
|
||||
|
||||
/**
|
||||
* @deprecated in Drupal 8.6.x. Will be removed before Drupal 9.0.0. Use
|
||||
* Drupal\Tests\aggregator\Functional\Hal\FeedHalJsonTestBase instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2971931
|
||||
*/
|
||||
abstract class FeedHalJsonTestBase extends FeedHalJsonTestBaseReal {
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\File;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\FileUploadHalJsonTestBase is deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Instead, use Drupal\Tests\file\Functional\Hal\FileUploadHalJsonTestBase. See https://www.drupal.org/node/2971931.', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\Tests\file\Functional\Hal\FileUploadHalJsonTestBase as FileUploadHalJsonTestBaseReal;
|
||||
|
||||
/**
|
||||
* @deprecated in Drupal 8.6.x. Will be removed before Drupal 9.0.0. Use
|
||||
* Drupal\Tests\file\Functional\Hal\FileUploadHalJsonTestBase instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2971931
|
||||
*/
|
||||
abstract class FileUploadHalJsonTestBase extends FileUploadHalJsonTestBaseReal {
|
||||
}
|
||||
|
|
@ -0,0 +1,98 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource;
|
||||
|
||||
use Drupal\Core\Entity\FieldableEntityInterface;
|
||||
use Drupal\Core\Field\EntityReferenceFieldItemListInterface;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Url;
|
||||
use GuzzleHttp\RequestOptions;
|
||||
|
||||
/**
|
||||
* Trait for EntityResourceTestBase subclasses testing formats using HAL.
|
||||
*/
|
||||
trait HalEntityNormalizationTrait {
|
||||
|
||||
/**
|
||||
* Applies the HAL entity field normalization to an entity normalization.
|
||||
*
|
||||
* The HAL normalization:
|
||||
* - adds a 'lang' attribute to every translatable field
|
||||
* - omits reference fields, since references are stored in _links & _embedded
|
||||
* - omits empty fields (fields without value)
|
||||
*
|
||||
* @param array $normalization
|
||||
* An entity normalization.
|
||||
*
|
||||
* @return array
|
||||
* The updated entity normalization.
|
||||
*/
|
||||
protected function applyHalFieldNormalization(array $normalization) {
|
||||
if (!$this->entity instanceof FieldableEntityInterface) {
|
||||
throw new \LogicException('This trait should only be used for fieldable entity types.');
|
||||
}
|
||||
|
||||
// In the HAL normalization, all translatable fields get a 'lang' attribute.
|
||||
$translatable_non_reference_fields = array_keys(array_filter($this->entity->getTranslatableFields(), function (FieldItemListInterface $field) {
|
||||
return !$field instanceof EntityReferenceFieldItemListInterface;
|
||||
}));
|
||||
foreach ($translatable_non_reference_fields as $field_name) {
|
||||
if (isset($normalization[$field_name])) {
|
||||
$normalization[$field_name][0]['lang'] = 'en';
|
||||
}
|
||||
}
|
||||
|
||||
// In the HAL normalization, reference fields are omitted, except for the
|
||||
// bundle field.
|
||||
$bundle_key = $this->entity->getEntityType()->getKey('bundle');
|
||||
$reference_fields = array_keys(array_filter($this->entity->getFields(), function (FieldItemListInterface $field) use ($bundle_key) {
|
||||
return $field instanceof EntityReferenceFieldItemListInterface && $field->getName() !== $bundle_key;
|
||||
}));
|
||||
foreach ($reference_fields as $field_name) {
|
||||
unset($normalization[$field_name]);
|
||||
}
|
||||
|
||||
// In the HAL normalization, the bundle field omits the 'target_type' and
|
||||
// 'target_uuid' properties, because it's encoded in the '_links' section.
|
||||
if ($bundle_key) {
|
||||
unset($normalization[$bundle_key][0]['target_type']);
|
||||
unset($normalization[$bundle_key][0]['target_uuid']);
|
||||
}
|
||||
|
||||
// In the HAL normalization, empty fields are omitted.
|
||||
$empty_fields = array_keys(array_filter($this->entity->getFields(), function (FieldItemListInterface $field) {
|
||||
return $field->isEmpty();
|
||||
}));
|
||||
foreach ($empty_fields as $field_name) {
|
||||
unset($normalization[$field_name]);
|
||||
}
|
||||
|
||||
return $normalization;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
|
||||
// \Drupal\hal\Normalizer\EntityNormalizer::denormalize(): entity
|
||||
// types with bundles MUST send their bundle field to be denormalizable.
|
||||
if ($this->entity->getEntityType()->hasKey('bundle')) {
|
||||
$normalization = $this->getNormalizedPostEntity();
|
||||
|
||||
$normalization['_links']['type'] = Url::fromUri('base:rest/type/' . static::$entityTypeId . '/bad_bundle_name');
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
|
||||
// DX: 422 when incorrect entity type bundle is specified.
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$this->assertResourceErrorResponse(422, 'No entity type(s) specified', $response);
|
||||
|
||||
unset($normalization['_links']['type']);
|
||||
$request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format);
|
||||
|
||||
// DX: 422 when no entity type bundle is specified.
|
||||
$response = $this->request($method, $url, $request_options);
|
||||
$this->assertResourceErrorResponse(422, 'The type link relation must be specified.', $response);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\EntityResource\Item;
|
||||
|
||||
@trigger_error('The ' . __NAMESPACE__ . '\ItemHalJsonTestBase is deprecated in Drupal 8.6.x and will be removed before Drupal 9.0.0. Instead, use Drupal\Tests\aggregator\Functional\Hal\ItemHalJsonTestBase. See https://www.drupal.org/node/2971931.', E_USER_DEPRECATED);
|
||||
|
||||
use Drupal\Tests\aggregator\Functional\Hal\ItemHalJsonTestBase as ItemHalJsonTestBaseReal;
|
||||
|
||||
/**
|
||||
* @deprecated in Drupal 8.6.x. Will be removed before Drupal 9.0.0. Use
|
||||
* Drupal\Tests\aggregator\Functional\Hal\ItemHalJsonTestBase instead.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2971931
|
||||
*/
|
||||
abstract class ItemHalJsonTestBase extends ItemHalJsonTestBaseReal {
|
||||
}
|
||||
|
|
@ -0,0 +1,43 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\Update;
|
||||
|
||||
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
|
||||
|
||||
/**
|
||||
* Tests that 'hal.settings' is created, to store 'link_domain'.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2758897
|
||||
*
|
||||
* @group hal
|
||||
* @group legacy
|
||||
*/
|
||||
class CreateHalSettingsForLinkDomainUpdateTest extends UpdatePathTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDatabaseDumpFiles() {
|
||||
$this->databaseDumpFiles = [
|
||||
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
|
||||
__DIR__ . '/../../../fixtures/update/drupal-8.hal-hal_update_8301.php',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests hal_update_8301().
|
||||
*/
|
||||
public function testHalSettingsCreated() {
|
||||
// Make sure we have the expected values before the update.
|
||||
$hal_settings = $this->config('hal.settings');
|
||||
$this->assertIdentical([], $hal_settings->getRawData());
|
||||
|
||||
$this->runUpdates();
|
||||
|
||||
// Make sure we have the expected values after the update.
|
||||
$hal_settings = \Drupal::configFactory()->get('hal.settings');
|
||||
$this->assertTrue(array_key_exists('link_domain', $hal_settings->getRawData()));
|
||||
$this->assertIdentical(NULL, $hal_settings->getRawData()['link_domain']);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Functional\Update;
|
||||
|
||||
use Drupal\FunctionalTests\Update\UpdatePathTestBase;
|
||||
|
||||
/**
|
||||
* 'link_domain' is migrated from 'rest.settings' to 'hal.settings'.
|
||||
*
|
||||
* @see https://www.drupal.org/node/2758897
|
||||
*
|
||||
* @group hal
|
||||
* @group legacy
|
||||
*/
|
||||
class MigrateLinkDomainSettingFromRestToHalUpdateTest extends UpdatePathTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setDatabaseDumpFiles() {
|
||||
$this->databaseDumpFiles = [
|
||||
__DIR__ . '/../../../../../system/tests/fixtures/update/drupal-8.bare.standard.php.gz',
|
||||
__DIR__ . '/../../../fixtures/update/drupal-8.hal-hal_update_8301.php',
|
||||
__DIR__ . '/../../../fixtures/update/drupal-8.rest-hal_update_8301.php',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests hal_update_8301().
|
||||
*/
|
||||
public function testLinkDomainMigratedFromRestSettingsToHalSettings() {
|
||||
// Make sure we have the expected values before the update.
|
||||
$hal_settings = $this->config('hal.settings');
|
||||
$this->assertIdentical([], $hal_settings->getRawData());
|
||||
$rest_settings = $this->config('rest.settings');
|
||||
$this->assertTrue(array_key_exists('link_domain', $rest_settings->getRawData()));
|
||||
$this->assertIdentical('http://example.com', $rest_settings->getRawData()['link_domain']);
|
||||
|
||||
$this->runUpdates();
|
||||
|
||||
// Make sure we have the expected values after the update.
|
||||
$hal_settings = \Drupal::configFactory()->get('hal.settings');
|
||||
$this->assertTrue(array_key_exists('link_domain', $hal_settings->getRawData()));
|
||||
$this->assertIdentical('http://example.com', $hal_settings->getRawData()['link_domain']);
|
||||
$rest_settings = $this->config('rest.settings');
|
||||
$this->assertFalse(array_key_exists('link_domain', $rest_settings->getRawData()));
|
||||
}
|
||||
|
||||
}
|
||||
142
2017/web/core/modules/hal/tests/src/Kernel/DenormalizeTest.php
Normal file
142
2017/web/core/modules/hal/tests/src/Kernel/DenormalizeTest.php
Normal file
|
|
@ -0,0 +1,142 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Tests HAL denormalization edge cases for EntityResource.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class DenormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Tests that the type link relation in incoming data is handled correctly.
|
||||
*/
|
||||
public function testTypeHandling() {
|
||||
// Valid type.
|
||||
$data_with_valid_type = [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['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 = [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
[
|
||||
'href' => Url::fromUri('base:rest/types/foo', ['absolute' => TRUE])->toString(),
|
||||
],
|
||||
[
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['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 = [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => Url::fromUri('base:rest/types/foo', ['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 = [
|
||||
'_links' => [],
|
||||
];
|
||||
try {
|
||||
$this->serializer->denormalize($data_with_no_type, $this->entityClass, $this->format);
|
||||
$this->fail('Exception should be thrown when no type is provided.');
|
||||
}
|
||||
catch (UnexpectedValueException $e) {
|
||||
$this->pass('Exception thrown when no type is provided.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests link relation handling with an invalid type.
|
||||
*/
|
||||
public function testTypeHandlingWithInvalidType() {
|
||||
$data_with_invalid_type = [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', ['absolute' => TRUE])->toString(),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$this->setExpectedException(UnexpectedValueException::class);
|
||||
$this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests link relation handling with no types.
|
||||
*/
|
||||
public function testTypeHandlingWithNoTypes() {
|
||||
$data_with_no_types = [
|
||||
'_links' => [
|
||||
'type' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$this->setExpectedException(UnexpectedValueException::class);
|
||||
$this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a field set to an empty array is different than an absent field.
|
||||
*/
|
||||
public function testMarkFieldForDeletion() {
|
||||
// Add a default value for a field.
|
||||
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
|
||||
$field->setDefaultValue([['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 = [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['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 = [
|
||||
'_links' => [
|
||||
'type' => [
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
|
||||
],
|
||||
],
|
||||
'field_test_text' => [],
|
||||
];
|
||||
$entity = $this->serializer->denormalize($data, get_class($entity), $this->format, ['target_instance' => $entity]);
|
||||
$this->assertEqual($entity->field_test_text->count(), 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,92 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\node\NodeInterface;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
|
||||
/**
|
||||
* Tests that translated nodes are correctly (de-)normalized.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class EntityTranslationNormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['node', 'content_translation'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installSchema('system', ['sequences']);
|
||||
$this->installConfig(['node', 'content_translation']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalization of node translations.
|
||||
*/
|
||||
public function testNodeTranslation() {
|
||||
$node_type = NodeType::create(['type' => 'example_type']);
|
||||
$node_type->save();
|
||||
$this->container->get('content_translation.manager')->setEnabled('node', 'example_type', TRUE);
|
||||
|
||||
$user = User::create(['name' => $this->randomMachineName()]);
|
||||
$user->save();
|
||||
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(),
|
||||
'uid' => (int) $user->id(),
|
||||
'type' => $node_type->id(),
|
||||
'status' => NodeInterface::PUBLISHED,
|
||||
'langcode' => 'en',
|
||||
'promote' => 1,
|
||||
'sticky' => 0,
|
||||
'body' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => $this->randomMachineName(),
|
||||
],
|
||||
'revision_log' => $this->randomString(),
|
||||
]);
|
||||
$node->addTranslation('de', [
|
||||
'title' => 'German title',
|
||||
'body' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => $this->randomMachineName(),
|
||||
],
|
||||
]);
|
||||
$node->save();
|
||||
|
||||
$original_values = $node->toArray();
|
||||
$translation = $node->getTranslation('de');
|
||||
$original_translation_values = $node->getTranslation('en')->toArray();
|
||||
|
||||
$normalized = $this->serializer->normalize($node, $this->format);
|
||||
|
||||
$this->assertContains(['lang' => 'en', 'value' => $node->getTitle()], $normalized['title'], 'Original language title has been normalized.');
|
||||
$this->assertContains(['lang' => 'de', 'value' => $translation->getTitle()], $normalized['title'], 'Translation language title has been normalized.');
|
||||
|
||||
/** @var \Drupal\node\NodeInterface $denormalized_node */
|
||||
$denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\node\Entity\Node', $this->format);
|
||||
|
||||
$this->assertSame($denormalized_node->language()->getId(), $denormalized_node->getUntranslated()->language()->getId(), 'Untranslated object is returned from serializer.');
|
||||
$this->assertSame('en', $denormalized_node->language()->getId());
|
||||
$this->assertTrue($denormalized_node->hasTranslation('de'));
|
||||
|
||||
$this->assertSame($node->getTitle(), $denormalized_node->getTitle());
|
||||
$this->assertSame($translation->getTitle(), $denormalized_node->getTranslation('de')->getTitle());
|
||||
|
||||
$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.');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\file\Entity\File;
|
||||
|
||||
/**
|
||||
* Tests that file entities can be normalized in HAL.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class FileNormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['file'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('file');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalize function.
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$file_params = [
|
||||
'filename' => 'test_1.txt',
|
||||
'uri' => 'public://test_1.txt',
|
||||
'filemime' => 'text/plain',
|
||||
'status' => FILE_STATUS_PERMANENT,
|
||||
];
|
||||
// Create a new file entity.
|
||||
$file = File::create($file_params);
|
||||
file_put_contents($file->getFileUri(), 'hello world');
|
||||
$file->save();
|
||||
|
||||
$expected_array = [
|
||||
'uri' => [
|
||||
[
|
||||
'value' => $file->getFileUri(),
|
||||
'url' => file_url_transform_relative(file_create_url($file->getFileUri())),
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$normalized = $this->serializer->normalize($file, $this->format);
|
||||
$this->assertEqual($normalized['uri'], $expected_array['uri'], 'URI is normalized.');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,278 @@
|
|||
<?php
|
||||
|
||||
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
|
||||
* @group hal
|
||||
* @group legacy
|
||||
*/
|
||||
class HalLinkManagerTest extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $modules = ['hal', 'hal_test', 'serialization', 'system', 'node', 'user', 'field'];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
$this->installEntitySchema('node');
|
||||
|
||||
NodeType::create([
|
||||
'type' => 'page',
|
||||
])->save();
|
||||
FieldStorageConfig::create([
|
||||
'entity_type' => 'node',
|
||||
'type' => 'entity_reference',
|
||||
'field_name' => 'field_ref',
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'field_name' => 'field_ref',
|
||||
])->save();
|
||||
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getTypeUri
|
||||
* @dataProvider providerTestGetTypeUri
|
||||
* @expectedDeprecation The deprecated alter hook hook_rest_type_uri_alter() is implemented in these functions: hal_test_rest_type_uri_alter. This hook is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Implement hook_hal_type_uri_alter() instead.
|
||||
*/
|
||||
public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) {
|
||||
$hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
|
||||
|
||||
if ($link_domain === NULL) {
|
||||
$hal_settings->clear('link_domain');
|
||||
}
|
||||
else {
|
||||
$hal_settings->set('link_domain', $link_domain)->save(TRUE);
|
||||
}
|
||||
|
||||
/* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */
|
||||
$type_manager = \Drupal::service('hal.link_manager.type');
|
||||
|
||||
$link = $type_manager->getTypeUri($entity_type, $bundle, $context);
|
||||
$this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
|
||||
$this->assertEquals($context, $expected_context);
|
||||
}
|
||||
|
||||
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',
|
||||
'bundle' => 'page',
|
||||
];
|
||||
|
||||
return [
|
||||
'site URL' => $base_test_case + [
|
||||
'context' => [],
|
||||
'link_domain' => NULL,
|
||||
'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',
|
||||
'bundle' => 'page',
|
||||
'context' => [],
|
||||
'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']),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getRelationUri
|
||||
* @dataProvider providerTestGetRelationUri
|
||||
* @expectedDeprecation The deprecated alter hook hook_rest_relation_uri_alter() is implemented in these functions: hal_test_rest_relation_uri_alter. This hook is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Implement hook_hal_relation_uri_alter() instead.
|
||||
*/
|
||||
public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) {
|
||||
$hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
|
||||
|
||||
if ($link_domain === NULL) {
|
||||
$hal_settings->clear('link_domain');
|
||||
}
|
||||
else {
|
||||
$hal_settings->set('link_domain', $link_domain)->save(TRUE);
|
||||
}
|
||||
|
||||
/* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
|
||||
$relation_manager = \Drupal::service('hal.link_manager.relation');
|
||||
|
||||
$link = $relation_manager->getRelationUri($entity_type, $bundle, $field_name, $context);
|
||||
$this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
|
||||
$this->assertEquals($context, $expected_context);
|
||||
}
|
||||
|
||||
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,
|
||||
'entity_type' => 'node',
|
||||
'bundle' => 'page',
|
||||
'field_name' => $field_name,
|
||||
];
|
||||
|
||||
return [
|
||||
'site URL' => $base_test_case + [
|
||||
'context' => [],
|
||||
'link_domain' => NULL,
|
||||
'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',
|
||||
'bundle' => 'page',
|
||||
'field_name' => $field_name,
|
||||
'context' => [],
|
||||
'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']),
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers ::getRelationInternalIds
|
||||
*/
|
||||
public function testGetRelationInternalIds() {
|
||||
/* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
|
||||
$relation_manager = \Drupal::service('hal.link_manager.relation');
|
||||
$link = $relation_manager->getRelationUri('node', 'page', 'field_ref');
|
||||
$internal_ids = $relation_manager->getRelationInternalIds($link);
|
||||
|
||||
$this->assertEquals([
|
||||
'entity_type_id' => 'node',
|
||||
'entity_type' => \Drupal::entityTypeManager()->getDefinition('node'),
|
||||
'bundle' => 'page',
|
||||
'field_name' => 'field_ref',
|
||||
], $internal_ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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', $serialization_context);
|
||||
$this->assertEqual($link, 'http://example.com/rest/type/node/page');
|
||||
$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]);
|
||||
}
|
||||
|
||||
}
|
||||
208
2017/web/core/modules/hal/tests/src/Kernel/NormalizeTest.php
Normal file
208
2017/web/core/modules/hal/tests/src/Kernel/NormalizeTest.php
Normal file
|
|
@ -0,0 +1,208 @@
|
|||
<?php
|
||||
|
||||
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.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class NormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalize function.
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$target_entity_de = EntityTest::create((['langcode' => 'de', 'field_test_entity_reference' => NULL]));
|
||||
$target_entity_de->save();
|
||||
$target_entity_en = EntityTest::create((['langcode' => 'en', 'field_test_entity_reference' => NULL]));
|
||||
$target_entity_en->save();
|
||||
|
||||
// Create a German entity.
|
||||
$values = [
|
||||
'langcode' => 'de',
|
||||
'name' => $this->randomMachineName(),
|
||||
'field_test_text' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'my_text_format',
|
||||
],
|
||||
'field_test_entity_reference' => [
|
||||
'target_id' => $target_entity_de->id(),
|
||||
],
|
||||
];
|
||||
// Array of translated values.
|
||||
$translation_values = [
|
||||
'name' => $this->randomMachineName(),
|
||||
'field_test_entity_reference' => [
|
||||
'target_id' => $target_entity_en->id(),
|
||||
],
|
||||
];
|
||||
|
||||
$entity = EntityTest::create($values);
|
||||
$entity->save();
|
||||
// Add an English value for name and entity reference properties.
|
||||
$entity->addTranslation('en')->set('name', [0 => ['value' => $translation_values['name']]]);
|
||||
$entity->getTranslation('en')->set('field_test_entity_reference', [0 => $translation_values['field_test_entity_reference']]);
|
||||
$entity->save();
|
||||
|
||||
$type_uri = Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString();
|
||||
$relation_uri = Url::fromUri('base:rest/relation/entity_test/entity_test/field_test_entity_reference', ['absolute' => TRUE])->toString();
|
||||
|
||||
$expected_array = [
|
||||
'_links' => [
|
||||
'curies' => [
|
||||
[
|
||||
'href' => '/relations',
|
||||
'name' => 'site',
|
||||
'templated' => TRUE,
|
||||
],
|
||||
],
|
||||
'self' => [
|
||||
'href' => $this->getEntityUri($entity),
|
||||
],
|
||||
'type' => [
|
||||
'href' => $type_uri,
|
||||
],
|
||||
$relation_uri => [
|
||||
[
|
||||
'href' => $this->getEntityUri($target_entity_de),
|
||||
'lang' => 'de',
|
||||
],
|
||||
[
|
||||
'href' => $this->getEntityUri($target_entity_en),
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
],
|
||||
'_embedded' => [
|
||||
$relation_uri => [
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->getEntityUri($target_entity_de),
|
||||
],
|
||||
'type' => [
|
||||
'href' => $type_uri,
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
[
|
||||
'value' => $target_entity_de->uuid(),
|
||||
],
|
||||
],
|
||||
'lang' => 'de',
|
||||
],
|
||||
[
|
||||
'_links' => [
|
||||
'self' => [
|
||||
'href' => $this->getEntityUri($target_entity_en),
|
||||
],
|
||||
'type' => [
|
||||
'href' => $type_uri,
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
[
|
||||
'value' => $target_entity_en->uuid(),
|
||||
],
|
||||
],
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
],
|
||||
'id' => [
|
||||
[
|
||||
'value' => $entity->id(),
|
||||
],
|
||||
],
|
||||
'uuid' => [
|
||||
[
|
||||
'value' => $entity->uuid(),
|
||||
],
|
||||
],
|
||||
'langcode' => [
|
||||
[
|
||||
'value' => 'de',
|
||||
],
|
||||
],
|
||||
'name' => [
|
||||
[
|
||||
'value' => $values['name'],
|
||||
'lang' => 'de',
|
||||
],
|
||||
[
|
||||
'value' => $translation_values['name'],
|
||||
'lang' => 'en',
|
||||
],
|
||||
],
|
||||
'field_test_text' => [
|
||||
[
|
||||
'value' => $values['field_test_text']['value'],
|
||||
'format' => $values['field_test_text']['format'],
|
||||
'processed' => "<p>{$values['field_test_text']['value']}</p>",
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
$normalized = $this->serializer->normalize($entity, $this->format);
|
||||
$this->assertEqual($normalized['_links']['self'], $expected_array['_links']['self'], 'self link placed correctly.');
|
||||
// @todo Test curies.
|
||||
// @todo Test type.
|
||||
$this->assertEqual($normalized['id'], $expected_array['id'], 'Internal id is exposed.');
|
||||
$this->assertEqual($normalized['uuid'], $expected_array['uuid'], 'Non-translatable fields is normalized.');
|
||||
$this->assertEqual($normalized['name'], $expected_array['name'], 'Translatable field with multiple language values is normalized.');
|
||||
$this->assertEqual($normalized['field_test_text'], $expected_array['field_test_text'], 'Field with properties is normalized.');
|
||||
$this->assertEqual($normalized['_embedded'][$relation_uri], $expected_array['_embedded'][$relation_uri], 'Entity reference field is normalized.');
|
||||
$this->assertEqual($normalized['_links'][$relation_uri], $expected_array['_links'][$relation_uri], 'Links are added for entity reference field.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the entity URI.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The entity.
|
||||
*
|
||||
* @return string
|
||||
* The entity URI.
|
||||
*/
|
||||
protected function getEntityUri(EntityInterface $entity) {
|
||||
$url = $entity->urlInfo('canonical', ['absolute' => TRUE]);
|
||||
return $url->setRouteParameter('_format', 'hal_json')->toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,123 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Test the HAL normalizer.
|
||||
*/
|
||||
abstract class NormalizerTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['entity_test', 'field', 'hal', 'language', 'serialization', 'system', 'text', 'user', 'filter'];
|
||||
|
||||
/**
|
||||
* The mock serializer.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The format being tested.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* The class name of the test class.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityClass = 'Drupal\entity_test\Entity\EntityTest';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('entity_test');
|
||||
// If the concrete test sub-class installs the Node or Comment modules,
|
||||
// ensure that the node and comment entity schema are created before the
|
||||
// field configurations are installed. This is because the entity tables
|
||||
// need to be created before the body field storage tables. This prevents
|
||||
// trying to create the body field tables twice.
|
||||
$class = get_class($this);
|
||||
while ($class) {
|
||||
if (property_exists($class, 'modules')) {
|
||||
// Only check the modules, if the $modules property was not inherited.
|
||||
$rp = new \ReflectionProperty($class, 'modules');
|
||||
if ($rp->class == $class) {
|
||||
foreach (array_intersect(['node', 'comment'], $class::$modules) as $module) {
|
||||
$this->installEntitySchema($module);
|
||||
}
|
||||
}
|
||||
}
|
||||
$class = get_parent_class($class);
|
||||
}
|
||||
$this->installConfig(['field', 'language']);
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
|
||||
// Add German as a language.
|
||||
ConfigurableLanguage::create([
|
||||
'id' => 'de',
|
||||
'label' => 'Deutsch',
|
||||
'weight' => -1,
|
||||
])->save();
|
||||
|
||||
// Create the test text field.
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => 'field_test_text',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'text',
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_text',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => FALSE,
|
||||
])->save();
|
||||
|
||||
// Create the test translatable field.
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => 'field_test_translatable_text',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'text',
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_translatable_text',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => TRUE,
|
||||
])->save();
|
||||
|
||||
// Create the test entity reference field.
|
||||
FieldStorageConfig::create([
|
||||
'field_name' => 'field_test_entity_reference',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'entity_reference',
|
||||
'settings' => [
|
||||
'target_type' => 'entity_test',
|
||||
],
|
||||
])->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_entity_reference',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => TRUE,
|
||||
])->save();
|
||||
|
||||
$this->serializer = $this->container->get('serializer');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\hal\Normalizer\FieldItemNormalizer;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
public function testFieldItemNormalizerDenormalizeExceptions($context) {
|
||||
$field_item_normalizer = new FieldItemNormalizer();
|
||||
$data = [];
|
||||
$class = [];
|
||||
$this->setExpectedException(InvalidArgumentException::class);
|
||||
$field_item_normalizer->denormalize($data, $class, NULL, $context);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\hal\Normalizer\FieldNormalizer;
|
||||
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
|
||||
|
||||
/**
|
||||
* @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
|
||||
*/
|
||||
public function testFieldNormalizerDenormalizeExceptions($context) {
|
||||
$field_item_normalizer = new FieldNormalizer();
|
||||
$data = [];
|
||||
$class = [];
|
||||
$this->setExpectedException(InvalidArgumentException::class);
|
||||
$field_item_normalizer->denormalize($data, $class, NULL, $context);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
||||
/**
|
||||
* Common ancestor for FieldItemNormalizerDenormalizeExceptionsUnitTest and
|
||||
* FieldNormalizerDenormalizeExceptionsUnitTest as they have the same
|
||||
* dataProvider.
|
||||
*/
|
||||
abstract class NormalizerDenormalizeExceptionsUnitTestBase extends UnitTestCase {
|
||||
|
||||
/**
|
||||
* Provides data for FieldItemNormalizerDenormalizeExceptionsUnitTest::testFieldItemNormalizerDenormalizeExceptions()
|
||||
* and for FieldNormalizerDenormalizeExceptionsUnitTest::testFieldNormalizerDenormalizeExceptions().
|
||||
*
|
||||
* @return array Test data.
|
||||
*/
|
||||
public function providerNormalizerDenormalizeExceptions() {
|
||||
$mock = $this->getMock('\Drupal\Core\Field\Plugin\DataType\FieldItem', ['getParent']);
|
||||
$mock->expects($this->any())
|
||||
->method('getParent')
|
||||
->will($this->returnValue(NULL));
|
||||
return [
|
||||
[[]],
|
||||
[['target_instance' => $mock]],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in a new issue