Update to Drupal 8.1.0. For more information, see https://www.drupal.org/drupal-8.1.0-release-notes
This commit is contained in:
parent
b11a755ba8
commit
c0a0d5a94c
6920 changed files with 64395 additions and 57312 deletions
206
core/modules/hal/tests/src/Kernel/DenormalizeTest.php
Normal file
206
core/modules/hal/tests/src/Kernel/DenormalizeTest.php
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
|
||||
|
||||
/**
|
||||
* Tests that entities can be denormalized from HAL.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class DenormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Tests that the type link relation in incoming data is handled correctly.
|
||||
*/
|
||||
public function testTypeHandling() {
|
||||
// Valid type.
|
||||
$data_with_valid_type = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
$denormalized = $this->serializer->denormalize($data_with_valid_type, $this->entityClass, $this->format);
|
||||
$this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with valid type results in creation of correct bundle.');
|
||||
|
||||
// Multiple types.
|
||||
$data_with_multiple_types = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
array(
|
||||
'href' => Url::fromUri('base:rest/types/foo', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
$denormalized = $this->serializer->denormalize($data_with_multiple_types, $this->entityClass, $this->format);
|
||||
$this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with multiple types results in creation of correct bundle.');
|
||||
|
||||
// Invalid type.
|
||||
$data_with_invalid_type = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/types/foo', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
try {
|
||||
$this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
|
||||
$this->fail('Exception should be thrown when type is invalid.');
|
||||
}
|
||||
catch (UnexpectedValueException $e) {
|
||||
$this->pass('Exception thrown when type is invalid.');
|
||||
}
|
||||
|
||||
// No type.
|
||||
$data_with_no_type = array(
|
||||
'_links' => array(
|
||||
),
|
||||
);
|
||||
try {
|
||||
$this->serializer->denormalize($data_with_no_type, $this->entityClass, $this->format);
|
||||
$this->fail('Exception should be thrown when no type is provided.');
|
||||
}
|
||||
catch (UnexpectedValueException $e) {
|
||||
$this->pass('Exception thrown when no type is provided.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that a field set to an empty array is different than an absent field.
|
||||
*/
|
||||
public function testMarkFieldForDeletion() {
|
||||
// Add a default value for a field.
|
||||
$field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
|
||||
$field->setDefaultValue(array(array('value' => 'Llama')));
|
||||
$field->save();
|
||||
|
||||
// Denormalize data that contains no entry for the field, and check that
|
||||
// the default value is present in the resulting entity.
|
||||
$data = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
);
|
||||
$entity = $this->serializer->denormalize($data, $this->entityClass, $this->format);
|
||||
$this->assertEqual($entity->field_test_text->count(), 1);
|
||||
$this->assertEqual($entity->field_test_text->value, 'Llama');
|
||||
|
||||
// Denormalize data that contains an empty entry for the field, and check
|
||||
// that the field is empty in the resulting entity.
|
||||
$data = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
'field_test_text' => array(),
|
||||
);
|
||||
$entity = $this->serializer->denormalize($data, get_class($entity), $this->format, [ 'target_instance' => $entity ]);
|
||||
$this->assertEqual($entity->field_test_text->count(), 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that non-reference fields can be denormalized.
|
||||
*/
|
||||
public function testBasicFieldDenormalization() {
|
||||
$data = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
'uuid' => array(
|
||||
array(
|
||||
'value' => 'e5c9fb96-3acf-4a8d-9417-23de1b6c3311',
|
||||
),
|
||||
),
|
||||
'field_test_text' => array(
|
||||
array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
),
|
||||
),
|
||||
'field_test_translatable_text' => array(
|
||||
array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
),
|
||||
array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'filtered_html',
|
||||
),
|
||||
array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'filtered_html',
|
||||
'lang' => 'de',
|
||||
),
|
||||
array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
'lang' => 'de',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$expected_value_default = array(
|
||||
array (
|
||||
'value' => $data['field_test_translatable_text'][0]['value'],
|
||||
'format' => 'full_html',
|
||||
),
|
||||
array (
|
||||
'value' => $data['field_test_translatable_text'][1]['value'],
|
||||
'format' => 'filtered_html',
|
||||
),
|
||||
);
|
||||
$expected_value_de = array(
|
||||
array (
|
||||
'value' => $data['field_test_translatable_text'][2]['value'],
|
||||
'format' => 'filtered_html',
|
||||
),
|
||||
array (
|
||||
'value' => $data['field_test_translatable_text'][3]['value'],
|
||||
'format' => 'full_html',
|
||||
),
|
||||
);
|
||||
$denormalized = $this->serializer->denormalize($data, $this->entityClass, $this->format);
|
||||
$this->assertEqual($data['uuid'], $denormalized->get('uuid')->getValue(), 'A preset value (e.g. UUID) is overridden by incoming data.');
|
||||
$this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue(), 'A basic text field is denormalized.');
|
||||
$this->assertEqual($expected_value_default, $denormalized->get('field_test_translatable_text')->getValue(), 'Values in the default language are properly handled for a translatable field.');
|
||||
$this->assertEqual($expected_value_de, $denormalized->getTranslation('de')->get('field_test_translatable_text')->getValue(), 'Values in a translation language are properly handled for a translatable field.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies that the denormalized entity is correct in the PATCH context.
|
||||
*/
|
||||
public function testPatchDenormalization() {
|
||||
$data = array(
|
||||
'_links' => array(
|
||||
'type' => array(
|
||||
'href' => Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString(),
|
||||
),
|
||||
),
|
||||
'field_test_text' => array(
|
||||
array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
),
|
||||
),
|
||||
);
|
||||
$denormalized = $this->serializer->denormalize($data, $this->entityClass, $this->format, array('request_method' => 'patch'));
|
||||
// Check that the one field got populated as expected.
|
||||
$this->assertEqual($data['field_test_text'], $denormalized->get('field_test_text')->getValue());
|
||||
// Check the custom property that contains the list of fields to merge.
|
||||
$this->assertEqual($denormalized->_restSubmittedFields, ['field_test_text']);
|
||||
}
|
||||
}
|
||||
206
core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php
Normal file
206
core/modules/hal/tests/src/Kernel/EntityNormalizeTest.php
Normal file
|
|
@ -0,0 +1,206 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\comment\Tests\CommentTestTrait;
|
||||
use Drupal\comment\Entity\Comment;
|
||||
use Drupal\node\Entity\Node;
|
||||
use Drupal\user\Entity\User;
|
||||
use Drupal\node\Entity\NodeType;
|
||||
use Drupal\taxonomy\Entity\Term;
|
||||
use Drupal\taxonomy\Entity\Vocabulary;
|
||||
|
||||
/**
|
||||
* Tests that nodes and terms are correctly normalized and denormalized.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class EntityNormalizeTest extends NormalizerTestBase {
|
||||
|
||||
use CommentTestTrait;
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('node', 'taxonomy', 'comment');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
$this->installSchema('system', array('sequences'));
|
||||
$this->installSchema('comment', array('comment_entity_statistics'));
|
||||
$this->installEntitySchema('taxonomy_term');
|
||||
$this->installConfig(['node', 'comment']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalization of nodes.
|
||||
*/
|
||||
public function testNode() {
|
||||
$node_type = NodeType::create(['type' => 'example_type']);
|
||||
$node_type->save();
|
||||
|
||||
$user = User::create(['name' => $this->randomMachineName()]);
|
||||
$user->save();
|
||||
|
||||
// Add comment type.
|
||||
$this->container->get('entity.manager')->getStorage('comment_type')->create(array(
|
||||
'id' => 'comment',
|
||||
'label' => 'comment',
|
||||
'target_entity_type_id' => 'node',
|
||||
))->save();
|
||||
|
||||
$this->addDefaultCommentField('node', 'example_type');
|
||||
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(),
|
||||
'uid' => $user->id(),
|
||||
'type' => $node_type->id(),
|
||||
'status' => NODE_PUBLISHED,
|
||||
'promote' => 1,
|
||||
'sticky' => 0,
|
||||
'body' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => $this->randomMachineName()
|
||||
],
|
||||
'revision_log' => $this->randomString(),
|
||||
]);
|
||||
$node->save();
|
||||
|
||||
$original_values = $node->toArray();
|
||||
|
||||
$normalized = $this->serializer->normalize($node, $this->format);
|
||||
|
||||
/** @var \Drupal\node\NodeInterface $denormalized_node */
|
||||
$denormalized_node = $this->serializer->denormalize($normalized, 'Drupal\node\Entity\Node', $this->format);
|
||||
|
||||
$this->assertEqual($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalization of terms.
|
||||
*/
|
||||
public function testTerm() {
|
||||
$vocabulary = Vocabulary::create(['vid' => 'example_vocabulary']);
|
||||
$vocabulary->save();
|
||||
|
||||
$account = User::create(['name' => $this->randomMachineName()]);
|
||||
$account->save();
|
||||
|
||||
// @todo Until https://www.drupal.org/node/2327935 is fixed, if no parent is
|
||||
// set, the test fails because target_id => 0 is reserialized to NULL.
|
||||
$term_parent = Term::create([
|
||||
'name' => $this->randomMachineName(),
|
||||
'vid' => $vocabulary->id(),
|
||||
]);
|
||||
$term_parent->save();
|
||||
$term = Term::create([
|
||||
'name' => $this->randomMachineName(),
|
||||
'vid' => $vocabulary->id(),
|
||||
'description' => array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => $this->randomMachineName(),
|
||||
),
|
||||
'parent' => $term_parent->id(),
|
||||
]);
|
||||
$term->save();
|
||||
|
||||
$original_values = $term->toArray();
|
||||
|
||||
$normalized = $this->serializer->normalize($term, $this->format, ['account' => $account]);
|
||||
|
||||
/** @var \Drupal\taxonomy\TermInterface $denormalized_term */
|
||||
$denormalized_term = $this->serializer->denormalize($normalized, 'Drupal\taxonomy\Entity\Term', $this->format, ['account' => $account]);
|
||||
|
||||
$this->assertEqual($original_values, $denormalized_term->toArray(), 'Term values are restored after normalizing and denormalizing.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalization of comments.
|
||||
*/
|
||||
public function testComment() {
|
||||
$node_type = NodeType::create(['type' => 'example_type']);
|
||||
$node_type->save();
|
||||
|
||||
$account = User::create(['name' => $this->randomMachineName()]);
|
||||
$account->save();
|
||||
|
||||
// Add comment type.
|
||||
$this->container->get('entity.manager')->getStorage('comment_type')->create(array(
|
||||
'id' => 'comment',
|
||||
'label' => 'comment',
|
||||
'target_entity_type_id' => 'node',
|
||||
))->save();
|
||||
|
||||
$this->addDefaultCommentField('node', 'example_type');
|
||||
|
||||
$node = Node::create([
|
||||
'title' => $this->randomMachineName(),
|
||||
'uid' => $account->id(),
|
||||
'type' => $node_type->id(),
|
||||
'status' => NODE_PUBLISHED,
|
||||
'promote' => 1,
|
||||
'sticky' => 0,
|
||||
'body' => [[
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => $this->randomMachineName()
|
||||
]],
|
||||
]);
|
||||
$node->save();
|
||||
|
||||
$parent_comment = Comment::create(array(
|
||||
'uid' => $account->id(),
|
||||
'subject' => $this->randomMachineName(),
|
||||
'comment_body' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => NULL,
|
||||
],
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
));
|
||||
$parent_comment->save();
|
||||
|
||||
$comment = Comment::create(array(
|
||||
'uid' => $account->id(),
|
||||
'subject' => $this->randomMachineName(),
|
||||
'comment_body' => [
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => NULL,
|
||||
],
|
||||
'entity_id' => $node->id(),
|
||||
'entity_type' => 'node',
|
||||
'field_name' => 'comment',
|
||||
'pid' => $parent_comment->id(),
|
||||
'mail' => 'dries@drupal.org',
|
||||
'homepage' => 'http://buytaert.net',
|
||||
));
|
||||
$comment->save();
|
||||
|
||||
$original_values = $comment->toArray();
|
||||
// Hostname will always be denied view access.
|
||||
// No value will exist for name as this is only for anonymous users.
|
||||
unset($original_values['hostname'], $original_values['name']);
|
||||
|
||||
$normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]);
|
||||
|
||||
// Assert that the hostname field does not appear at all in the normalized
|
||||
// data.
|
||||
$this->assertFalse(array_key_exists('hostname', $normalized), 'Hostname was not found in normalized comment data.');
|
||||
|
||||
/** @var \Drupal\comment\CommentInterface $denormalized_comment */
|
||||
$denormalized_comment = $this->serializer->denormalize($normalized, 'Drupal\comment\Entity\Comment', $this->format, ['account' => $account]);
|
||||
|
||||
// Before comparing, unset values that are expected to differ.
|
||||
$denormalized_comment_values = $denormalized_comment->toArray();
|
||||
unset($denormalized_comment_values['hostname'], $denormalized_comment_values['name']);
|
||||
$this->assertEqual($original_values, $denormalized_comment_values, 'The expected comment values are restored after normalizing and denormalizing.');
|
||||
}
|
||||
|
||||
}
|
||||
80
core/modules/hal/tests/src/Kernel/FileNormalizeTest.php
Normal file
80
core/modules/hal/tests/src/Kernel/FileNormalizeTest.php
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Cache\MemoryBackend;
|
||||
use Drupal\file\Entity\File;
|
||||
use Drupal\hal\Encoder\JsonEncoder;
|
||||
use Drupal\hal\Normalizer\FieldItemNormalizer;
|
||||
use Drupal\hal\Normalizer\FileEntityNormalizer;
|
||||
use Drupal\rest\LinkManager\LinkManager;
|
||||
use Drupal\rest\LinkManager\RelationLinkManager;
|
||||
use Drupal\rest\LinkManager\TypeLinkManager;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
|
||||
|
||||
/**
|
||||
* Tests that file entities can be normalized in HAL.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class FileNormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('file');
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('file');
|
||||
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default'), \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack')), new RelationLinkManager(new MemoryBackend('default'), $entity_manager, \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack')));
|
||||
|
||||
// Set up the mock serializer.
|
||||
$normalizers = array(
|
||||
new FieldItemNormalizer(),
|
||||
new FileEntityNormalizer($entity_manager, \Drupal::httpClient(), $link_manager, \Drupal::moduleHandler()),
|
||||
);
|
||||
|
||||
$encoders = array(
|
||||
new JsonEncoder(),
|
||||
);
|
||||
$this->serializer = new Serializer($normalizers, $encoders);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Tests the normalize function.
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$file_params = array(
|
||||
'filename' => 'test_1.txt',
|
||||
'uri' => 'public://test_1.txt',
|
||||
'filemime' => 'text/plain',
|
||||
'status' => FILE_STATUS_PERMANENT,
|
||||
);
|
||||
// Create a new file entity.
|
||||
$file = File::create($file_params);
|
||||
file_put_contents($file->getFileUri(), 'hello world');
|
||||
$file->save();
|
||||
|
||||
$expected_array = array(
|
||||
'uri' => array(
|
||||
array(
|
||||
'value' => file_create_url($file->getFileUri())),
|
||||
),
|
||||
);
|
||||
|
||||
$normalized = $this->serializer->normalize($file, $this->format);
|
||||
$this->assertEqual($normalized['uri'], $expected_array['uri'], 'URI is normalized.');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
185
core/modules/hal/tests/src/Kernel/NormalizeTest.php
Normal file
185
core/modules/hal/tests/src/Kernel/NormalizeTest.php
Normal file
|
|
@ -0,0 +1,185 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\entity_test\Entity\EntityTest;
|
||||
|
||||
/**
|
||||
* Tests that entities can be normalized in HAL.
|
||||
*
|
||||
* @group hal
|
||||
*/
|
||||
class NormalizeTest extends NormalizerTestBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests the normalize function.
|
||||
*/
|
||||
public function testNormalize() {
|
||||
$target_entity_de = EntityTest::create((array('langcode' => 'de', 'field_test_entity_reference' => NULL)));
|
||||
$target_entity_de->save();
|
||||
$target_entity_en = EntityTest::create((array('langcode' => 'en', 'field_test_entity_reference' => NULL)));
|
||||
$target_entity_en->save();
|
||||
|
||||
// Create a German entity.
|
||||
$values = array(
|
||||
'langcode' => 'de',
|
||||
'name' => $this->randomMachineName(),
|
||||
'field_test_text' => array(
|
||||
'value' => $this->randomMachineName(),
|
||||
'format' => 'full_html',
|
||||
),
|
||||
'field_test_entity_reference' => array(
|
||||
'target_id' => $target_entity_de->id(),
|
||||
),
|
||||
);
|
||||
// Array of translated values.
|
||||
$translation_values = array(
|
||||
'name' => $this->randomMachineName(),
|
||||
'field_test_entity_reference' => array(
|
||||
'target_id' => $target_entity_en->id(),
|
||||
)
|
||||
);
|
||||
|
||||
$entity = EntityTest::create($values);
|
||||
$entity->save();
|
||||
// Add an English value for name and entity reference properties.
|
||||
$entity->addTranslation('en')->set('name', array(0 => array('value' => $translation_values['name'])));
|
||||
$entity->getTranslation('en')->set('field_test_entity_reference', array(0 => $translation_values['field_test_entity_reference']));
|
||||
$entity->save();
|
||||
|
||||
$type_uri = Url::fromUri('base:rest/type/entity_test/entity_test', array('absolute' => TRUE))->toString();
|
||||
$relation_uri = Url::fromUri('base:rest/relation/entity_test/entity_test/field_test_entity_reference', array('absolute' => TRUE))->toString();
|
||||
|
||||
$expected_array = array(
|
||||
'_links' => array(
|
||||
'curies' => array(
|
||||
array(
|
||||
'href' => '/relations',
|
||||
'name' => 'site',
|
||||
'templated' => true,
|
||||
),
|
||||
),
|
||||
'self' => array(
|
||||
'href' => $this->getEntityUri($entity),
|
||||
),
|
||||
'type' => array(
|
||||
'href' => $type_uri,
|
||||
),
|
||||
$relation_uri => array(
|
||||
array(
|
||||
'href' => $this->getEntityUri($target_entity_de),
|
||||
'lang' => 'de',
|
||||
),
|
||||
array(
|
||||
'href' => $this->getEntityUri($target_entity_en),
|
||||
'lang' => 'en',
|
||||
),
|
||||
),
|
||||
),
|
||||
'_embedded' => array(
|
||||
$relation_uri => array(
|
||||
array(
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
'href' => $this->getEntityUri($target_entity_de),
|
||||
),
|
||||
'type' => array(
|
||||
'href' => $type_uri,
|
||||
),
|
||||
),
|
||||
'uuid' => array(
|
||||
array(
|
||||
'value' => $target_entity_de->uuid(),
|
||||
),
|
||||
),
|
||||
'lang' => 'de',
|
||||
),
|
||||
array(
|
||||
'_links' => array(
|
||||
'self' => array(
|
||||
'href' => $this->getEntityUri($target_entity_en),
|
||||
),
|
||||
'type' => array(
|
||||
'href' => $type_uri,
|
||||
),
|
||||
),
|
||||
'uuid' => array(
|
||||
array(
|
||||
'value' => $target_entity_en->uuid(),
|
||||
),
|
||||
),
|
||||
'lang' => 'en',
|
||||
),
|
||||
),
|
||||
),
|
||||
'id' => array(
|
||||
array(
|
||||
'value' => $entity->id(),
|
||||
),
|
||||
),
|
||||
'uuid' => array(
|
||||
array(
|
||||
'value' => $entity->uuid(),
|
||||
),
|
||||
),
|
||||
'langcode' => array(
|
||||
array(
|
||||
'value' => 'de',
|
||||
),
|
||||
),
|
||||
'name' => array(
|
||||
array(
|
||||
'value' => $values['name'],
|
||||
'lang' => 'de',
|
||||
),
|
||||
array(
|
||||
'value' => $translation_values['name'],
|
||||
'lang' => 'en',
|
||||
),
|
||||
),
|
||||
'field_test_text' => array(
|
||||
array(
|
||||
'value' => $values['field_test_text']['value'],
|
||||
'format' => $values['field_test_text']['format'],
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$normalized = $this->serializer->normalize($entity, $this->format);
|
||||
$this->assertEqual($normalized['_links']['self'], $expected_array['_links']['self'], 'self link placed correctly.');
|
||||
// @todo Test curies.
|
||||
// @todo Test type.
|
||||
$this->assertEqual($normalized['id'], $expected_array['id'], 'Internal id is exposed.');
|
||||
$this->assertEqual($normalized['uuid'], $expected_array['uuid'], 'Non-translatable fields is normalized.');
|
||||
$this->assertEqual($normalized['name'], $expected_array['name'], 'Translatable field with multiple language values is normalized.');
|
||||
$this->assertEqual($normalized['field_test_text'], $expected_array['field_test_text'], 'Field with properties is normalized.');
|
||||
$this->assertEqual($normalized['_embedded'][$relation_uri], $expected_array['_embedded'][$relation_uri], 'Entity reference field is normalized.');
|
||||
$this->assertEqual($normalized['_links'][$relation_uri], $expected_array['_links'][$relation_uri], 'Links are added for entity reference field.');
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the entity URI.
|
||||
*
|
||||
* @param \Drupal\Core\Entity\EntityInterface $entity
|
||||
* The entity.
|
||||
*
|
||||
* @return string
|
||||
* The entity URI.
|
||||
*/
|
||||
protected function getEntityUri(EntityInterface $entity) {
|
||||
$url = $entity->urlInfo('canonical', ['absolute' => TRUE]);
|
||||
return $url->setRouteParameter('_format', 'hal_json')->toString();
|
||||
}
|
||||
|
||||
}
|
||||
152
core/modules/hal/tests/src/Kernel/NormalizerTestBase.php
Normal file
152
core/modules/hal/tests/src/Kernel/NormalizerTestBase.php
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\Tests\hal\Kernel;
|
||||
|
||||
use Drupal\Core\Cache\MemoryBackend;
|
||||
use Drupal\field\Entity\FieldConfig;
|
||||
use Drupal\hal\Encoder\JsonEncoder;
|
||||
use Drupal\hal\Normalizer\ContentEntityNormalizer;
|
||||
use Drupal\hal\Normalizer\EntityReferenceItemNormalizer;
|
||||
use Drupal\hal\Normalizer\FieldItemNormalizer;
|
||||
use Drupal\hal\Normalizer\FieldNormalizer;
|
||||
use Drupal\language\Entity\ConfigurableLanguage;
|
||||
use Drupal\rest\LinkManager\LinkManager;
|
||||
use Drupal\rest\LinkManager\RelationLinkManager;
|
||||
use Drupal\rest\LinkManager\TypeLinkManager;
|
||||
use Drupal\serialization\EntityResolver\ChainEntityResolver;
|
||||
use Drupal\serialization\EntityResolver\TargetIdResolver;
|
||||
use Drupal\serialization\EntityResolver\UuidResolver;
|
||||
use Drupal\KernelTests\KernelTestBase;
|
||||
use Symfony\Component\Serializer\Serializer;
|
||||
use Drupal\field\Entity\FieldStorageConfig;
|
||||
|
||||
/**
|
||||
* Test the HAL normalizer.
|
||||
*/
|
||||
abstract class NormalizerTestBase extends KernelTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = ['entity_test', 'field', 'hal', 'language', 'rest', 'serialization', 'system', 'text', 'user', 'filter'];
|
||||
|
||||
/**
|
||||
* The mock serializer.
|
||||
*
|
||||
* @var \Symfony\Component\Serializer\Serializer
|
||||
*/
|
||||
protected $serializer;
|
||||
|
||||
/**
|
||||
* The format being tested.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $format = 'hal_json';
|
||||
|
||||
/**
|
||||
* The class name of the test class.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $entityClass = 'Drupal\entity_test\Entity\EntityTest';
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function setUp() {
|
||||
parent::setUp();
|
||||
$this->installEntitySchema('user');
|
||||
$this->installEntitySchema('entity_test');
|
||||
// If the concrete test sub-class installs the Node or Comment modules,
|
||||
// ensure that the node and comment entity schema are created before the
|
||||
// field configurations are installed. This is because the entity tables
|
||||
// need to be created before the body field storage tables. This prevents
|
||||
// trying to create the body field tables twice.
|
||||
$class = get_class($this);
|
||||
while ($class) {
|
||||
if (property_exists($class, 'modules')) {
|
||||
// Only check the modules, if the $modules property was not inherited.
|
||||
$rp = new \ReflectionProperty($class, 'modules');
|
||||
if ($rp->class == $class) {
|
||||
foreach (array_intersect(array('node', 'comment'), $class::$modules) as $module) {
|
||||
$this->installEntitySchema($module);
|
||||
}
|
||||
}
|
||||
}
|
||||
$class = get_parent_class($class);
|
||||
}
|
||||
$this->installConfig(array('field', 'language'));
|
||||
\Drupal::service('router.builder')->rebuild();
|
||||
|
||||
// Add German as a language.
|
||||
ConfigurableLanguage::create(array(
|
||||
'id' => 'de',
|
||||
'label' => 'Deutsch',
|
||||
'weight' => -1,
|
||||
))->save();
|
||||
|
||||
// Create the test text field.
|
||||
FieldStorageConfig::create(array(
|
||||
'field_name' => 'field_test_text',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'text',
|
||||
))->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_text',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => FALSE,
|
||||
])->save();
|
||||
|
||||
// Create the test translatable field.
|
||||
FieldStorageConfig::create(array(
|
||||
'field_name' => 'field_test_translatable_text',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'text',
|
||||
))->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_translatable_text',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => TRUE,
|
||||
])->save();
|
||||
|
||||
// Create the test entity reference field.
|
||||
FieldStorageConfig::create(array(
|
||||
'field_name' => 'field_test_entity_reference',
|
||||
'entity_type' => 'entity_test',
|
||||
'type' => 'entity_reference',
|
||||
'settings' => array(
|
||||
'target_type' => 'entity_test',
|
||||
),
|
||||
))->save();
|
||||
FieldConfig::create([
|
||||
'entity_type' => 'entity_test',
|
||||
'field_name' => 'field_test_entity_reference',
|
||||
'bundle' => 'entity_test',
|
||||
'translatable' => TRUE,
|
||||
])->save();
|
||||
|
||||
$entity_manager = \Drupal::entityManager();
|
||||
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default'), \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack')), new RelationLinkManager(new MemoryBackend('default'), $entity_manager, \Drupal::moduleHandler(), \Drupal::service('config.factory'), \Drupal::service('request_stack')));
|
||||
|
||||
$chain_resolver = new ChainEntityResolver(array(new UuidResolver($entity_manager), new TargetIdResolver()));
|
||||
|
||||
// Set up the mock serializer.
|
||||
$normalizers = array(
|
||||
new ContentEntityNormalizer($link_manager, $entity_manager, \Drupal::moduleHandler()),
|
||||
new EntityReferenceItemNormalizer($link_manager, $chain_resolver),
|
||||
new FieldItemNormalizer(),
|
||||
new FieldNormalizer(),
|
||||
);
|
||||
|
||||
$encoders = array(
|
||||
new JsonEncoder(),
|
||||
);
|
||||
$this->serializer = new Serializer($normalizers, $encoders);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\hal\Unit\FieldItemNormalizerDenormalizeExceptionsUnitTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\hal\Normalizer\FieldItemNormalizer;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\hal\Unit\FieldNormalizerDenormalizeExceptionsUnitTest.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\hal\Normalizer\FieldNormalizer;
|
||||
|
|
|
|||
|
|
@ -1,10 +1,5 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Tests\hal\Unit\NormalizerDenormalizeExceptionsUnitTestBase.
|
||||
*/
|
||||
|
||||
namespace Drupal\Tests\hal\Unit;
|
||||
|
||||
use Drupal\Tests\UnitTestCase;
|
||||
|
|
|
|||
Reference in a new issue