Security update for Core, with self-updated composer
[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     try {
68       $this->serializer->denormalize($data_with_no_type, $this->entityClass, $this->format);
69       $this->fail('Exception should be thrown when no type is provided.');
70     }
71     catch (UnexpectedValueException $e) {
72       $this->pass('Exception thrown when no type is provided.');
73     }
74   }
75
76   /**
77    * Tests link relation handling with an invalid type.
78    */
79   public function testTypeHandlingWithInvalidType() {
80     $data_with_invalid_type = [
81       '_links' => [
82         'type' => [
83           'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', ['absolute' => TRUE])->toString(),
84         ],
85       ],
86     ];
87
88     $this->setExpectedException(UnexpectedValueException::class);
89     $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format);
90   }
91
92   /**
93    * Tests link relation handling with no types.
94    */
95   public function testTypeHandlingWithNoTypes() {
96     $data_with_no_types = [
97       '_links' => [
98         'type' => [],
99       ],
100     ];
101
102     $this->setExpectedException(UnexpectedValueException::class);
103     $this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format);
104   }
105
106   /**
107    * Test that a field set to an empty array is different than an absent field.
108    */
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']]);
113     $field->save();
114
115     // Denormalize data that contains no entry for the field, and check that
116     // the default value is present in the resulting entity.
117     $data = [
118       '_links' => [
119         'type' => [
120           'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
121         ],
122       ],
123     ];
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');
127
128     // Denormalize data that contains an empty entry for the field, and check
129     // that the field is empty in the resulting entity.
130     $data = [
131       '_links' => [
132         'type' => [
133           'href' => Url::fromUri('base:rest/type/entity_test/entity_test', ['absolute' => TRUE])->toString(),
134         ],
135       ],
136       'field_test_text' => [],
137     ];
138     $entity = $this->serializer->denormalize($data, get_class($entity), $this->format, ['target_instance' => $entity]);
139     $this->assertEqual($entity->field_test_text->count(), 0);
140   }
141
142 }