comparison core/modules/serialization/tests/src/Kernel/FieldItemSerializationTest.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
comparison
equal deleted inserted replaced
4:a9cd425dd02b 5:12f9dff5fda9
75 'widget' => [ 75 'widget' => [
76 'type' => 'text_textfield', 76 'type' => 'text_textfield',
77 'weight' => 0, 77 'weight' => 0,
78 ], 78 ],
79 ])->save(); 79 ])->save();
80 FieldStorageConfig::create([
81 'entity_type' => 'entity_test_mulrev',
82 'field_name' => 'field_test_boolean',
83 'type' => 'boolean',
84 'cardinality' => 1,
85 'translatable' => FALSE,
86 ])->save();
87 FieldConfig::create([
88 'entity_type' => 'entity_test_mulrev',
89 'field_name' => 'field_test_boolean',
90 'bundle' => 'entity_test_mulrev',
91 'label' => 'Test boolean',
92 ])->save();
80 93
81 // Create a test entity to serialize. 94 // Create a test entity to serialize.
82 $this->values = [ 95 $this->values = [
83 'name' => $this->randomMachineName(), 96 'name' => $this->randomMachineName(),
84 'field_test_text' => [ 97 'field_test_text' => [
85 'value' => $this->randomMachineName(), 98 'value' => $this->randomMachineName(),
86 'format' => 'full_html', 99 'format' => 'full_html',
100 ],
101 'field_test_boolean' => [
102 'value' => FALSE,
87 ], 103 ],
88 ]; 104 ];
89 $this->entity = EntityTestMulRev::create($this->values); 105 $this->entity = EntityTestMulRev::create($this->values);
90 $this->entity->save(); 106 $this->entity->save();
91 107
130 $normalized['uuid'] = $normalized['uuid'][0]['value']; 146 $normalized['uuid'] = $normalized['uuid'][0]['value'];
131 147
132 $this->serializer->denormalize($normalized, $this->entityClass, 'json'); 148 $this->serializer->denormalize($normalized, $this->entityClass, 'json');
133 } 149 }
134 150
151 /**
152 * Tests a format-agnostic normalizer.
153 *
154 * @param string[] $test_modules
155 * The test modules to install.
156 * @param string $format
157 * The format to test. (NULL results in the format-agnostic normalization.)
158 *
159 * @dataProvider providerTestCustomBooleanNormalization
160 */
161 public function testCustomBooleanNormalization(array $test_modules, $format) {
162 // Asserts the entity contains the value we set.
163 $this->assertSame(FALSE, $this->entity->field_test_boolean->value);
164
165 // Asserts normalizing the entity using core's 'serializer' service DOES
166 // yield the value we set.
167 $core_normalization = $this->container->get('serializer')->normalize($this->entity, $format);
168 $this->assertSame(FALSE, $core_normalization['field_test_boolean'][0]['value']);
169
170 $assert_denormalization = function (array $normalization) use ($format) {
171 $denormalized_entity = $this->container->get('serializer')->denormalize($normalization, EntityTestMulRev::class, $format, []);
172 $this->assertInstanceOf(EntityTestMulRev::class, $denormalized_entity);
173 $this->assertSame(TRUE, $denormalized_entity->field_test_boolean->value);
174 };
175
176 // Asserts denormalizing the entity DOES yield the value we set:
177 // - when using the detailed representation
178 $core_normalization['field_test_boolean'][0]['value'] = TRUE;
179 $assert_denormalization($core_normalization);
180 // - and when using the shorthand representation
181 $core_normalization['field_test_boolean'][0] = TRUE;
182 $assert_denormalization($core_normalization);
183
184 // Install test module that contains a high-priority alternative normalizer.
185 $this->enableModules($test_modules);
186
187 // Asserts normalizing the entity DOES NOT ANYMORE yield the value we set.
188 $core_normalization = $this->container->get('serializer')->normalize($this->entity, $format);
189 $this->assertSame('👎', $core_normalization['field_test_boolean'][0]['value']);
190
191 // Asserts denormalizing the entity DOES NOT ANYMORE yield the value we set:
192 // - when using the detailed representation
193 $core_normalization['field_test_boolean'][0]['value'] = '👍';
194 $assert_denormalization($core_normalization);
195 // - and when using the shorthand representation
196 $core_normalization['field_test_boolean'][0] = '👍';
197 $assert_denormalization($core_normalization);
198 }
199
200 /**
201 * Data provider.
202 *
203 * @return array
204 * Test cases.
205 */
206 public function providerTestCustomBooleanNormalization() {
207 return [
208 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
209 ['test_fieldtype_boolean_emoji_normalizer'],
210 NULL,
211 ],
212 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
213 ['test_datatype_boolean_emoji_normalizer'],
214 NULL,
215 ],
216 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the JSON normalization' => [
217 ['test_fieldtype_boolean_emoji_normalizer'],
218 'json',
219 ],
220 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the JSON normalization' => [
221 ['test_datatype_boolean_emoji_normalizer'],
222 'json',
223 ],
224 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
225 ['test_fieldtype_boolean_emoji_normalizer'],
226 'hal_json',
227 ],
228 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
229 ['test_datatype_boolean_emoji_normalizer', 'hal'],
230 'hal_json',
231 ],
232 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the XML normalization' => [
233 ['test_fieldtype_boolean_emoji_normalizer'],
234 'xml',
235 ],
236 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the XML normalization' => [
237 ['test_datatype_boolean_emoji_normalizer', 'hal'],
238 'xml',
239 ],
240 ];
241 }
242
135 } 243 }