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 = [
68 $this->serializer->denormalize($data_with_no_type, $this->entityClass, $this->format);
69 $this->fail('Exception should be thrown when no type is provided.');
71 catch (UnexpectedValueException $e) {
72 $this->pass('Exception thrown when no type is provided.');
77 * Tests link relation handling with an invalid type.
79 public function testTypeHandlingWithInvalidType() {
80 $data_with_invalid_type = [
83 'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', ['absolute' => TRUE])->toString(),
88 $this->setExpectedException(UnexpectedValueException::class);
89 $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
93 * Tests link relation handling with no types.
95 public function testTypeHandlingWithNoTypes() {
96 $data_with_no_types = [
102 $this->setExpectedException(UnexpectedValueException::class);
103 $this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format);
107 * Test that a field set to an empty array is different than an absent field.
109 public function testMarkFieldForDeletion() {
110 // Add a default value for a field.
111 $field = FieldConfig::loadByName('entity_test', 'entity_test', 'field_test_text');
112 $field->setDefaultValue([['value' => 'Llama']]);
115 // Denormalize data that contains no entry for the field, and check that
116 // the default value is present in the resulting entity.
120 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
124 $entity = $this->serializer->denormalize($data, $this->entityClass, $this->format);
125 $this->assertEqual($entity->field_test_text->count(), 1);
126 $this->assertEqual($entity->field_test_text->value, 'Llama');
128 // Denormalize data that contains an empty entry for the field, and check
129 // that the field is empty in the resulting entity.
133 'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
136 'field_test_text' => [],
138 $entity = $this->serializer->denormalize($data, get_class($entity), $this->format, ['target_instance' => $entity]);
139 $this->assertEqual($entity->field_test_text->count(), 0);