View source
<?php
namespace Drupal\Tests\serialization\Kernel;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
class FieldItemSerializationTest extends NormalizerTestBase {
public static $modules = [
'serialization',
'system',
'field',
'entity_test',
'text',
'filter',
'user',
'field_normalization_test',
];
protected $entityClass = 'Drupal\\entity_test\\Entity\\EntityTestMulRev';
protected $values;
protected $entity;
protected $serializer;
protected function setUp() {
parent::setUp();
FieldStorageConfig::create([
'entity_type' => 'entity_test_mulrev',
'field_name' => 'field_test_text_default',
'type' => 'text',
'cardinality' => 1,
'translatable' => FALSE,
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test_mulrev',
'field_name' => 'field_test_text_default',
'bundle' => 'entity_test_mulrev',
'label' => 'Test text-field with default',
'default_value' => [
[
'value' => 'This is the default',
'format' => 'full_html',
],
],
'widget' => [
'type' => 'text_textfield',
'weight' => 0,
],
])
->save();
FieldStorageConfig::create([
'entity_type' => 'entity_test_mulrev',
'field_name' => 'field_test_boolean',
'type' => 'boolean',
'cardinality' => 1,
'translatable' => FALSE,
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test_mulrev',
'field_name' => 'field_test_boolean',
'bundle' => 'entity_test_mulrev',
'label' => 'Test boolean',
])
->save();
$this->values = [
'name' => $this
->randomMachineName(),
'field_test_text' => [
'value' => $this
->randomMachineName(),
'format' => 'full_html',
],
'field_test_boolean' => [
'value' => FALSE,
],
];
$this->entity = EntityTestMulRev::create($this->values);
$this->entity
->save();
$this->serializer = $this->container
->get('serializer');
$this
->installConfig([
'field',
]);
}
public function testFieldNormalizeDenormalize() {
$normalized = $this->serializer
->normalize($this->entity, 'json');
$expected_field_value = $this->entity->field_test_text[0]
->getValue()['value'] . '::silly_suffix';
$this
->assertEquals($expected_field_value, $normalized['field_test_text'][0]['value'], 'Text field item normalized');
$denormalized = $this->serializer
->denormalize($normalized, $this->entityClass, 'json');
$this
->assertEquals($denormalized->field_test_text[0]
->getValue(), $this->entity->field_test_text[0]
->getValue(), 'Text field item denormalized.');
$this
->assertEquals($denormalized->field_test_text_default[0]
->getValue(), $this->entity->field_test_text_default[0]
->getValue(), 'Text field item with default denormalized.');
unset($normalized['field_test_text_default']);
$denormalized_without_all_fields = $this->serializer
->denormalize($normalized, $this->entityClass, 'json');
$this
->assertEquals($denormalized_without_all_fields->field_test_text[0]
->getValue(), $this->entity->field_test_text[0]
->getValue(), 'Text field item denormalized.');
$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.');
}
public function testFieldDenormalizeWithScalarValue() {
$this
->expectException(UnexpectedValueException::class);
$this
->expectExceptionMessage('Field values for "uuid" must use an array structure');
$normalized = $this->serializer
->normalize($this->entity, 'json');
$normalized['uuid'] = $normalized['uuid'][0]['value'];
$this->serializer
->denormalize($normalized, $this->entityClass, 'json');
}
public function testCustomBooleanNormalization(array $test_modules, $format) {
$this
->assertSame(FALSE, $this->entity->field_test_boolean->value);
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity, $format);
$this
->assertSame(FALSE, $core_normalization['field_test_boolean'][0]['value']);
$assert_denormalization = function (array $normalization) use ($format) {
$denormalized_entity = $this->container
->get('serializer')
->denormalize($normalization, EntityTestMulRev::class, $format, []);
$this
->assertInstanceOf(EntityTestMulRev::class, $denormalized_entity);
$this
->assertSame(TRUE, $denormalized_entity->field_test_boolean->value);
};
$core_normalization['field_test_boolean'][0]['value'] = TRUE;
$assert_denormalization($core_normalization);
$core_normalization['field_test_boolean'][0] = TRUE;
$assert_denormalization($core_normalization);
$this
->enableModules($test_modules);
$core_normalization = $this->container
->get('serializer')
->normalize($this->entity, $format);
$this
->assertSame('๐', $core_normalization['field_test_boolean'][0]['value']);
$core_normalization['field_test_boolean'][0]['value'] = '๐';
$assert_denormalization($core_normalization);
$core_normalization['field_test_boolean'][0] = '๐';
$assert_denormalization($core_normalization);
}
public function providerTestCustomBooleanNormalization() {
return [
'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
[
'test_fieldtype_boolean_emoji_normalizer',
],
NULL,
],
'Format-agnostic @DataType-level normalizers SHOULD be able to affect the format-agnostic normalization' => [
[
'test_datatype_boolean_emoji_normalizer',
],
NULL,
],
'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the JSON normalization' => [
[
'test_fieldtype_boolean_emoji_normalizer',
],
'json',
],
'Format-agnostic @DataType-level normalizers SHOULD be able to affect the JSON normalization' => [
[
'test_datatype_boolean_emoji_normalizer',
],
'json',
],
'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
[
'test_fieldtype_boolean_emoji_normalizer',
],
'hal_json',
],
'Format-agnostic @DataType-level normalizers SHOULD be able to affect the HAL+JSON normalization' => [
[
'test_datatype_boolean_emoji_normalizer',
'hal',
],
'hal_json',
],
'Format-agnostic @FieldType-level normalizers SHOULD be able to affect the XML normalization' => [
[
'test_fieldtype_boolean_emoji_normalizer',
],
'xml',
],
'Format-agnostic @DataType-level normalizers SHOULD be able to affect the XML normalization' => [
[
'test_datatype_boolean_emoji_normalizer',
'hal',
],
'xml',
],
];
}
}