3 namespace Drupal\Tests\hal\Kernel;
6 use Drupal\field\Entity\FieldConfig;
7 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
10 * Tests HAL denormalization edge cases for EntityResource.
14 class DenormalizeTest extends NormalizerTestBase {
17 * Tests that the type link relation in incoming data is handled correctly.
19 public function testTypeHandling() {
21 $data_with_valid_type = [
24 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
28 $denormalized = $this->serializer->denormalize($data_with_valid_type, $this->entityClass, $this->format);
29 $this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with valid type results in creation of correct bundle.');
32 $data_with_multiple_types = [
36 'href' => Url::fromUri('base:rest/types/foo', ['absolute' => TRUE])->toString(),
39 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
44 $denormalized = $this->serializer->denormalize($data_with_multiple_types, $this->entityClass, $this->format);
45 $this->assertEqual(get_class($denormalized), $this->entityClass, 'Request with multiple types results in creation of correct bundle.');
48 $data_with_invalid_type = [
51 'href' => Url::fromUri('base:rest/types/foo', ['absolute' => TRUE])->toString(),
56 $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
57 $this->fail('Exception should be thrown when type is invalid.');
59 catch (UnexpectedValueException $e) {
60 $this->pass('Exception thrown when type is invalid.');
64 $data_with_no_type = [
69 $this->serializer->denormalize($data_with_no_type, $this->entityClass, $this->format);
70 $this->fail('Exception should be thrown when no type is provided.');
72 catch (UnexpectedValueException $e) {
73 $this->pass('Exception thrown when no type is provided.');
78 * Tests link relation handling with an invalid type.
80 public function testTypeHandlingWithInvalidType() {
81 $data_with_invalid_type = [
84 'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', ['absolute' => TRUE])->toString(),
89 $this->setExpectedException(UnexpectedValueException::class);
90 $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
94 * Tests link relation handling with no types.
96 public function testTypeHandlingWithNoTypes() {
97 $data_with_no_types = [
103 $this->setExpectedException(UnexpectedValueException::class);
104 $this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format);
108 * Test that a field set to an empty array is different than an absent field.
110 public function testMarkFieldForDeletion() {
111 // Add a default value for a field.
112 $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
113 $field->setDefaultValue([['value' => 'Llama']]);
116 // Denormalize data that contains no entry for the field, and check that
117 // the default value is present in the resulting entity.
121 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
125 $entity = $this->serializer->denormalize($data, $this->entityClass, $this->format);
126 $this->assertEqual($entity->field_test_text->count(), 1);
127 $this->assertEqual($entity->field_test_text->value, 'Llama');
129 // Denormalize data that contains an empty entry for the field, and check
130 // that the field is empty in the resulting entity.
134 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
137 'field_test_text' => [],
139 $entity = $this->serializer->denormalize($data, get_class($entity), $this->format, [ 'target_instance' => $entity ]);
140 $this->assertEqual($entity->field_test_text->count(), 0);