Chris@0: 'entity_test_mulrev', Chris@0: 'field_name' => 'field_test_text_default', Chris@0: 'type' => 'text', Chris@0: 'cardinality' => 1, Chris@0: 'translatable' => FALSE, Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'entity_test_mulrev', Chris@0: 'field_name' => 'field_test_text_default', Chris@0: 'bundle' => 'entity_test_mulrev', Chris@0: 'label' => 'Test text-field with default', Chris@0: 'default_value' => [ Chris@0: [ Chris@0: 'value' => 'This is the default', Chris@0: 'format' => 'full_html', Chris@0: ], Chris@0: ], Chris@0: 'widget' => [ Chris@0: 'type' => 'text_textfield', Chris@0: 'weight' => 0, Chris@0: ], Chris@0: ])->save(); Chris@18: FieldStorageConfig::create([ Chris@18: 'entity_type' => 'entity_test_mulrev', Chris@18: 'field_name' => 'field_test_boolean', Chris@18: 'type' => 'boolean', Chris@18: 'cardinality' => 1, Chris@18: 'translatable' => FALSE, Chris@18: ])->save(); Chris@18: FieldConfig::create([ Chris@18: 'entity_type' => 'entity_test_mulrev', Chris@18: 'field_name' => 'field_test_boolean', Chris@18: 'bundle' => 'entity_test_mulrev', Chris@18: 'label' => 'Test boolean', Chris@18: ])->save(); Chris@0: Chris@0: // Create a test entity to serialize. Chris@0: $this->values = [ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'field_test_text' => [ Chris@0: 'value' => $this->randomMachineName(), Chris@0: 'format' => 'full_html', Chris@0: ], Chris@18: 'field_test_boolean' => [ Chris@18: 'value' => FALSE, Chris@18: ], Chris@0: ]; Chris@0: $this->entity = EntityTestMulRev::create($this->values); Chris@0: $this->entity->save(); Chris@0: Chris@0: $this->serializer = $this->container->get('serializer'); Chris@0: Chris@0: $this->installConfig(['field']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests normalizing and denormalizing an entity with field item normalizer. Chris@0: */ Chris@0: public function testFieldNormalizeDenormalize() { Chris@0: $normalized = $this->serializer->normalize($this->entity, 'json'); Chris@0: Chris@0: $expected_field_value = $this->entity->field_test_text[0]->getValue()['value'] . '::silly_suffix'; Chris@0: $this->assertEquals($expected_field_value, $normalized['field_test_text'][0]['value'], 'Text field item normalized'); Chris@0: $denormalized = $this->serializer->denormalize($normalized, $this->entityClass, 'json'); Chris@0: Chris@0: $this->assertEquals($denormalized->field_test_text[0]->getValue(), $this->entity->field_test_text[0]->getValue(), 'Text field item denormalized.'); Chris@0: $this->assertEquals($denormalized->field_test_text_default[0]->getValue(), $this->entity->field_test_text_default[0]->getValue(), 'Text field item with default denormalized.'); Chris@0: Chris@0: // Unset the values for text field that has a default value. Chris@0: unset($normalized['field_test_text_default']); Chris@0: $denormalized_without_all_fields = $this->serializer->denormalize($normalized, $this->entityClass, 'json'); Chris@0: // Check that denormalized entity is still the same even if not all fields Chris@0: // are not provided. Chris@0: $this->assertEquals($denormalized_without_all_fields->field_test_text[0]->getValue(), $this->entity->field_test_text[0]->getValue(), 'Text field item denormalized.'); Chris@0: // Even though field_test_text_default value was unset before Chris@0: // denormalization it should still have the default values for the field. Chris@0: $this->assertEquals($denormalized_without_all_fields->field_test_text_default[0]->getValue(), $this->entity->field_test_text_default[0]->getValue(), 'Text field item with default denormalized.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests denormalizing using a scalar field value. Chris@0: */ Chris@0: public function testFieldDenormalizeWithScalarValue() { Chris@0: $this->setExpectedException(UnexpectedValueException::class, 'Field values for "uuid" must use an array structure'); Chris@0: Chris@0: $normalized = $this->serializer->normalize($this->entity, 'json'); Chris@0: Chris@0: // Change the UUID value to use the UUID directly. No array structure. Chris@0: $normalized['uuid'] = $normalized['uuid'][0]['value']; Chris@0: Chris@0: $this->serializer->denormalize($normalized, $this->entityClass, 'json'); Chris@0: } Chris@0: Chris@18: /** Chris@18: * Tests a format-agnostic normalizer. Chris@18: * Chris@18: * @param string[] $test_modules Chris@18: * The test modules to install. Chris@18: * @param string $format Chris@18: * The format to test. (NULL results in the format-agnostic normalization.) Chris@18: * Chris@18: * @dataProvider providerTestCustomBooleanNormalization Chris@18: */ Chris@18: public function testCustomBooleanNormalization(array $test_modules, $format) { Chris@18: // Asserts the entity contains the value we set. Chris@18: $this->assertSame(FALSE, $this->entity->field_test_boolean->value); Chris@18: Chris@18: // Asserts normalizing the entity using core's 'serializer' service DOES Chris@18: // yield the value we set. Chris@18: $core_normalization = $this->container->get('serializer')->normalize($this->entity, $format); Chris@18: $this->assertSame(FALSE, $core_normalization['field_test_boolean'][0]['value']); Chris@18: Chris@18: $assert_denormalization = function (array $normalization) use ($format) { Chris@18: $denormalized_entity = $this->container->get('serializer')->denormalize($normalization, EntityTestMulRev::class, $format, []); Chris@18: $this->assertInstanceOf(EntityTestMulRev::class, $denormalized_entity); Chris@18: $this->assertSame(TRUE, $denormalized_entity->field_test_boolean->value); Chris@18: }; Chris@18: Chris@18: // Asserts denormalizing the entity DOES yield the value we set: Chris@18: // - when using the detailed representation Chris@18: $core_normalization['field_test_boolean'][0]['value'] = TRUE; Chris@18: $assert_denormalization($core_normalization); Chris@18: // - and when using the shorthand representation Chris@18: $core_normalization['field_test_boolean'][0] = TRUE; Chris@18: $assert_denormalization($core_normalization); Chris@18: Chris@18: // Install test module that contains a high-priority alternative normalizer. Chris@18: $this->enableModules($test_modules); Chris@18: Chris@18: // Asserts normalizing the entity DOES NOT ANYMORE yield the value we set. Chris@18: $core_normalization = $this->container->get('serializer')->normalize($this->entity, $format); Chris@18: $this->assertSame('👎', $core_normalization['field_test_boolean'][0]['value']); Chris@18: Chris@18: // Asserts denormalizing the entity DOES NOT ANYMORE yield the value we set: Chris@18: // - when using the detailed representation Chris@18: $core_normalization['field_test_boolean'][0]['value'] = '👍'; Chris@18: $assert_denormalization($core_normalization); Chris@18: // - and when using the shorthand representation Chris@18: $core_normalization['field_test_boolean'][0] = '👍'; Chris@18: $assert_denormalization($core_normalization); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Data provider. Chris@18: * Chris@18: * @return array Chris@18: * Test cases. Chris@18: */ Chris@18: public function providerTestCustomBooleanNormalization() { Chris@18: return [ Chris@18: 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [ Chris@18: ['test_fieldtype_boolean_emoji_normalizer'], Chris@18: NULL, Chris@18: ], Chris@18: 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [ Chris@18: ['test_datatype_boolean_emoji_normalizer'], Chris@18: NULL, Chris@18: ], Chris@18: 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the JSON normalization' => [ Chris@18: ['test_fieldtype_boolean_emoji_normalizer'], Chris@18: 'json', Chris@18: ], Chris@18: 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the JSON normalization' => [ Chris@18: ['test_datatype_boolean_emoji_normalizer'], Chris@18: 'json', Chris@18: ], Chris@18: 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [ Chris@18: ['test_fieldtype_boolean_emoji_normalizer'], Chris@18: 'hal_json', Chris@18: ], Chris@18: 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [ Chris@18: ['test_datatype_boolean_emoji_normalizer', 'hal'], Chris@18: 'hal_json', Chris@18: ], Chris@18: 'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the XML normalization' => [ Chris@18: ['test_fieldtype_boolean_emoji_normalizer'], Chris@18: 'xml', Chris@18: ], Chris@18: 'Format-agnostic @DataType-level normalizers SHOULD be able to affect the XML normalization' => [ Chris@18: ['test_datatype_boolean_emoji_normalizer', 'hal'], Chris@18: 'xml', Chris@18: ], Chris@18: ]; Chris@18: } Chris@18: Chris@0: }