Version 1
[yaffs-website] / web / core / modules / hal / tests / src / Kernel / DenormalizeTest.php
1 <?php
2
3 namespace Drupal\Tests\hal\Kernel;
4
5 use Drupal\Core\Url;
6 use Drupal\field\Entity\FieldConfig;
7 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
8
9 /**
10  * Tests HAL denormalization edge cases for EntityResource.
11  *
12  * @group hal
13  */
14 class DenormalizeTest extends NormalizerTestBase {
15
16   /**
17    * Tests that the type link relation in incoming data is handled correctly.
18    */
19   public function testTypeHandling() {
20     // Valid type.
21     $data_with_valid_type = [
22       '_links' => [
23         'type' => [
24           'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
25         ],
26       ],
27     ];
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.');
30
31     // Multiple types.
32     $data_with_multiple_types = [
33       '_links' => [
34         'type' => [
35           [
36             'href' => Url::fromUri('base:rest/types/foo', ['absolute' => TRUE])->toString(),
37           ],
38           [
39             'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
40           ],
41         ],
42       ],
43     ];
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.');
46
47     // Invalid type.
48     $data_with_invalid_type = [
49       '_links' => [
50         'type' => [
51           'href' => Url::fromUri('base:rest/types/foo', ['absolute' => TRUE])->toString(),
52         ],
53       ],
54     ];
55     try {
56       $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
57       $this->fail('Exception should be thrown when type is invalid.');
58     }
59     catch (UnexpectedValueException $e) {
60       $this->pass('Exception thrown when type is invalid.');
61     }
62
63     // No type.
64     $data_with_no_type = [
65       '_links' => [
66       ],
67     ];
68     try {
69       $this->serializer->denormalize($data_with_no_type, $this->entityClass, $this->format);
70       $this->fail('Exception should be thrown when no type is provided.');
71     }
72     catch (UnexpectedValueException $e) {
73       $this->pass('Exception thrown when no type is provided.');
74     }
75   }
76
77   /**
78    * Tests link relation handling with an invalid type.
79    */
80   public function testTypeHandlingWithInvalidType() {
81     $data_with_invalid_type = [
82       '_links' => [
83         'type' => [
84           'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', ['absolute' => TRUE])->toString(),
85         ],
86       ],
87     ];
88
89     $this->setExpectedException(UnexpectedValueException::class);
90     $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
91   }
92
93   /**
94    * Tests link relation handling with no types.
95    */
96   public function testTypeHandlingWithNoTypes() {
97     $data_with_no_types = [
98       '_links' => [
99         'type' => [],
100       ],
101     ];
102
103     $this->setExpectedException(UnexpectedValueException::class);
104     $this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format);
105   }
106
107   /**
108    * Test that a field set to an empty array is different than an absent field.
109    */
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']]);
114     $field->save();
115
116     // Denormalize data that contains no entry for the field, and check that
117     // the default value is present in the resulting entity.
118     $data = [
119       '_links' => [
120         'type' => [
121           'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
122         ],
123       ],
124     ];
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');
128
129     // Denormalize data that contains an empty entry for the field, and check
130     // that the field is empty in the resulting entity.
131     $data = [
132       '_links' => [
133         'type' => [
134           'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
135         ],
136       ],
137       'field_test_text' => [],
138     ];
139     $entity = $this->serializer->denormalize($data, get_class($entity), $this->format, [ 'target_instance' => $entity ]);
140     $this->assertEqual($entity->field_test_text->count(), 0);
141   }
142
143 }