Commit 4b8acaab authored by catch's avatar catch

Issue #2304849 by Arla, clemens.tolboom, effulgentsia, kaa4ever: Stop...

Issue #2304849 by Arla, clemens.tolboom, effulgentsia, kaa4ever: Stop excluding local ID and revision fields from HAL output
parent 6862b53e
......@@ -83,7 +83,6 @@ public function normalize($entity, $format = NULL, array $context = array()) {
);
// 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 @@ public function normalize($entity, $format = NULL, array $context = array()) {
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;
}
......
......@@ -76,21 +76,13 @@ public function testNode() {
$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 @@ public function testTerm() {
$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 @@ public function testComment() {
$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 @@ public function testComment() {
// 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.');
}
}
......@@ -128,6 +128,11 @@ public function testNormalize() {
),
),
),
'id' => array(
array(
'value' => $entity->id(),
),
),
'uuid' => array(
array(
'value' => $entity->uuid(),
......@@ -160,7 +165,7 @@ public function testNormalize() {
$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.');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment