Issue #2304849 by Arla, clemens.tolboom, effulgentsia, kaa4ever: Stop excluding local ID and revision fields from HAL output

8.1.x
Nathaniel Catchpole 2016-02-22 12:26:32 +09:00
parent 6862b53e83
commit 4b8acaab95
3 changed files with 19 additions and 38 deletions

View File

@ -83,7 +83,6 @@ class ContentEntityNormalizer extends NormalizerBase {
);
// If the fields to use were specified, only output those field values.
// Otherwise, output all field values except internal ID.
if (isset($context['included_fields'])) {
$fields = array();
foreach ($context['included_fields'] as $field_name) {
@ -93,12 +92,9 @@ class ContentEntityNormalizer extends NormalizerBase {
else {
$fields = $entity->getFields();
}
// Ignore the entity ID and revision ID.
$exclude = array($entity->getEntityType()->getKey('id'), $entity->getEntityType()->getKey('revision'));
foreach ($fields as $field) {
// Continue if this is an excluded field or the current user does not have
// access to view it.
if (in_array($field->getFieldDefinition()->getName(), $exclude) || !$field->access('view', $context['account'])) {
// Continue if the current user does not have access to view this field.
if (!$field->access('view', $context['account'])) {
continue;
}

View File

@ -76,21 +76,13 @@ class EntityTest extends NormalizerTestBase {
$node->save();
$original_values = $node->toArray();
unset($original_values['nid']);
unset($original_values['vid']);
$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);
// Verify that the ID and revision ID were skipped by the normalizer.
$this->assertEqual(NULL, $denormalized_node->id());
$this->assertEqual(NULL, $denormalized_node->getRevisionId());
// Loop over the remaining fields and verify that they are identical.
foreach ($original_values as $field_name => $field_values) {
$this->assertEqual($field_values, $denormalized_node->get($field_name)->getValue());
}
$this->assertEqual($original_values, $denormalized_node->toArray(), 'Node values are restored after normalizing and denormalizing.');
}
/**
@ -122,19 +114,13 @@ class EntityTest extends NormalizerTestBase {
$term->save();
$original_values = $term->toArray();
unset($original_values['tid']);
$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]);
// Verify that the ID and revision ID were skipped by the normalizer.
$this->assertEqual(NULL, $denormalized_term->id());
// Loop over the remaining fields and verify that they are identical.
foreach ($original_values as $field_name => $field_values) {
$this->assertEqual($field_values, $denormalized_term->get($field_name)->getValue());
}
$this->assertEqual($original_values, $denormalized_term->toArray(), 'Term values are restored after normalizing and denormalizing.');
}
/**
@ -200,9 +186,9 @@ class EntityTest extends NormalizerTestBase {
$comment->save();
$original_values = $comment->toArray();
// cid will not exist and hostname will always be denied view access.
// Hostname will always be denied view access.
// No value will exist for name as this is only for anonymous users.
unset($original_values['cid'], $original_values['hostname'], $original_values['name']);
unset($original_values['hostname'], $original_values['name']);
$normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]);
@ -210,19 +196,13 @@ class EntityTest extends NormalizerTestBase {
// 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]);
// Verify that the ID and revision ID were skipped by the normalizer.
$this->assertEqual(NULL, $denormalized_comment->id());
// Loop over the remaining fields and verify that they are identical.
foreach ($original_values as $field_name => $field_values) {
// The target field comes with revision id which is not set.
if (array_key_exists('revision_id', $field_values[0])) {
unset($field_values[0]['revision_id']);
}
$this->assertEqual($field_values, $denormalized_comment->get($field_name)->getValue());
}
// 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.');
}
}

View File

@ -128,6 +128,11 @@ class NormalizeTest extends NormalizerTestBase {
),
),
),
'id' => array(
array(
'value' => $entity->id(),
),
),
'uuid' => array(
array(
'value' => $entity->uuid(),
@ -160,7 +165,7 @@ class NormalizeTest extends NormalizerTestBase {
$this->assertEqual($normalized['_links']['self'], $expected_array['_links']['self'], 'self link placed correctly.');
// @todo Test curies.
// @todo Test type.
$this->assertFalse(isset($normalized['id']), 'Internal id is not exposed.');
$this->assertEqual($normalized['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.');