3 namespace Drupal\Tests\serialization\Kernel;
5 use Drupal\entity_test\Entity\EntityTestMulRev;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Symfony\Component\Serializer\Exception\UnexpectedValueException;
11 * Test field level normalization process.
13 * @group serialization
15 class FieldItemSerializationTest extends NormalizerTestBase {
20 public static $modules = ['serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user', 'field_normalization_test'];
23 * The class name of the test class.
27 protected $entityClass = 'Drupal\entity_test\Entity\EntityTestMulRev';
39 * @var \Drupal\Core\Entity\ContentEntityBase
44 * The serializer service.
46 * @var \Symfony\Component\Serializer\Serializer
48 protected $serializer;
53 protected function setUp() {
56 // Auto-create a field for testing default field values.
57 FieldStorageConfig::create([
58 'entity_type' => 'entity_test_mulrev',
59 'field_name' => 'field_test_text_default',
62 'translatable' => FALSE,
65 'entity_type' => 'entity_test_mulrev',
66 'field_name' => 'field_test_text_default',
67 'bundle' => 'entity_test_mulrev',
68 'label' => 'Test text-field with default',
71 'value' => 'This is the default',
72 'format' => 'full_html',
76 'type' => 'text_textfield',
81 // Create a test entity to serialize.
83 'name' => $this->randomMachineName(),
84 'field_test_text' => [
85 'value' => $this->randomMachineName(),
86 'format' => 'full_html',
89 $this->entity = EntityTestMulRev::create($this->values);
90 $this->entity->save();
92 $this->serializer = $this->container->get('serializer');
94 $this->installConfig(['field']);
98 * Tests normalizing and denormalizing an entity with field item normalizer.
100 public function testFieldNormalizeDenormalize() {
101 $normalized = $this->serializer->normalize($this->entity, 'json');
103 $expected_field_value = $this->entity->field_test_text[0]->getValue()['value'] . '::silly_suffix';
104 $this->assertEquals($expected_field_value, $normalized['field_test_text'][0]['value'], 'Text field item normalized');
105 $denormalized = $this->serializer->denormalize($normalized, $this->entityClass, 'json');
107 $this->assertEquals($denormalized->field_test_text[0]->getValue(), $this->entity->field_test_text[0]->getValue(), 'Text field item denormalized.');
108 $this->assertEquals($denormalized->field_test_text_default[0]->getValue(), $this->entity->field_test_text_default[0]->getValue(), 'Text field item with default denormalized.');
110 // Unset the values for text field that has a default value.
111 unset($normalized['field_test_text_default']);
112 $denormalized_without_all_fields = $this->serializer->denormalize($normalized, $this->entityClass, 'json');
113 // Check that denormalized entity is still the same even if not all fields
115 $this->assertEquals($denormalized_without_all_fields->field_test_text[0]->getValue(), $this->entity->field_test_text[0]->getValue(), 'Text field item denormalized.');
116 // Even though field_test_text_default value was unset before
117 // denormalization it should still have the default values for the field.
118 $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.');
122 * Tests denormalizing using a scalar field value.
124 public function testFieldDenormalizeWithScalarValue() {
125 $this->setExpectedException(UnexpectedValueException::class, 'Field values for "uuid" must use an array structure');
127 $normalized = $this->serializer->normalize($this->entity, 'json');
129 // Change the UUID value to use the UUID directly. No array structure.
130 $normalized['uuid'] = $normalized['uuid'][0]['value'];
132 $this->serializer->denormalize($normalized, $this->entityClass, 'json');