403f41ab2bc49ad766410d313f8cc43fe90ab59f
[yaffs-website] / web / core / modules / serialization / tests / src / Kernel / FieldItemSerializationTest.php
1 <?php
2
3 namespace Drupal\Tests\serialization\Kernel;
4
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;
9
10 /**
11  * Test field level normalization process.
12  *
13  * @group serialization
14  */
15 class FieldItemSerializationTest extends NormalizerTestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user', 'field_normalization_test'];
21
22   /**
23    * The class name of the test class.
24    *
25    * @var string
26    */
27   protected $entityClass = 'Drupal\entity_test\Entity\EntityTestMulRev';
28
29   /**
30    * The test values.
31    *
32    * @var array
33    */
34   protected $values;
35
36   /**
37    * The test entity.
38    *
39    * @var \Drupal\Core\Entity\ContentEntityBase
40    */
41   protected $entity;
42
43   /**
44    * The serializer service.
45    *
46    * @var \Symfony\Component\Serializer\Serializer
47    */
48   protected $serializer;
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function setUp() {
54     parent::setUp();
55
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',
60       'type' => 'text',
61       'cardinality' => 1,
62       'translatable' => FALSE,
63     ])->save();
64     FieldConfig::create([
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',
69       'default_value' => [
70         [
71           'value' => 'This is the default',
72           'format' => 'full_html',
73         ],
74       ],
75       'widget' => [
76         'type' => 'text_textfield',
77         'weight' => 0,
78       ],
79     ])->save();
80
81     // Create a test entity to serialize.
82     $this->values = [
83       'name' => $this->randomMachineName(),
84       'field_test_text' => [
85         'value' => $this->randomMachineName(),
86         'format' => 'full_html',
87       ],
88     ];
89     $this->entity = EntityTestMulRev::create($this->values);
90     $this->entity->save();
91
92     $this->serializer = $this->container->get('serializer');
93
94     $this->installConfig(['field']);
95   }
96
97   /**
98    * Tests normalizing and denormalizing an entity with field item normalizer.
99    */
100   public function testFieldNormalizeDenormalize() {
101     $normalized = $this->serializer->normalize($this->entity, 'json');
102
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');
106
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.');
109
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
114     // are not provided.
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.');
119   }
120
121   /**
122    * Tests denormalizing using a scalar field value.
123    */
124   public function testFieldDenormalizeWithScalarValue() {
125     $this->setExpectedException(UnexpectedValueException::class, 'Field values for "uuid" must use an array structure');
126
127     $normalized = $this->serializer->normalize($this->entity, 'json');
128
129     // Change the UUID value to use the UUID directly. No array structure.
130     $normalized['uuid'] = $normalized['uuid'][0]['value'];
131
132     $this->serializer->denormalize($normalized, $this->entityClass, 'json');
133   }
134
135 }