3 namespace Drupal\Tests\rest\Functional\EntityResource;
5 use Drupal\Core\Entity\FieldableEntityInterface;
6 use Drupal\Core\Field\Plugin\Field\FieldType\BooleanItem;
7 use Drupal\Core\Field\Plugin\Field\FieldType\ChangedItem;
8 use Drupal\Core\Field\Plugin\Field\FieldType\CreatedItem;
9 use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem;
10 use Drupal\Core\Field\Plugin\Field\FieldType\IntegerItem;
11 use Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem;
12 use Drupal\file\Plugin\Field\FieldType\FileItem;
13 use Drupal\image\Plugin\Field\FieldType\ImageItem;
14 use Drupal\options\Plugin\Field\FieldType\ListIntegerItem;
15 use Drupal\path\Plugin\Field\FieldType\PathItem;
16 use Drupal\Tests\rest\Functional\XmlNormalizationQuirksTrait;
19 * Trait for EntityResourceTestBase subclasses testing $format='xml'.
21 trait XmlEntityNormalizationQuirksTrait {
23 use XmlNormalizationQuirksTrait;
28 protected function getExpectedNormalizedEntity() {
29 $default_normalization = parent::getExpectedNormalizedEntity();
31 if ($this->entity instanceof FieldableEntityInterface) {
32 $normalization = $this->applyXmlFieldDecodingQuirks($default_normalization);
35 $normalization = $this->applyXmlConfigEntityDecodingQuirks($default_normalization);
37 $normalization = $this->applyXmlDecodingQuirks($normalization);
39 return $normalization;
43 * Applies the XML entity field encoding quirks that remain after decoding.
46 * - loses type data (int and bool become string)
48 * @param array $normalization
49 * An entity normalization.
52 * The updated fieldable entity normalization.
54 * @see \Symfony\Component\Serializer\Encoder\XmlEncoder
56 protected function applyXmlFieldDecodingQuirks(array $normalization) {
57 foreach ($this->entity->getFields(TRUE) as $field_name => $field) {
58 // Not every field is accessible.
59 if (!isset($normalization[$field_name])) {
63 for ($i = 0; $i < count($normalization[$field_name]); $i++) {
64 switch ($field->getItemDefinition()->getClass()) {
65 case BooleanItem::class:
66 $value = &$normalization[$field_name][$i]['value'];
67 $value = $value === TRUE ? '1' : '0';
69 case IntegerItem::class:
70 case ListIntegerItem::class:
71 $value = &$normalization[$field_name][$i]['value'];
72 $value = (string) $value;
75 $pid = &$normalization[$field_name][$i]['pid'];
78 case EntityReferenceItem::class:
80 $target_id = &$normalization[$field_name][$i]['target_id'];
81 $target_id = (string) $target_id;
83 case ChangedItem::class:
84 case CreatedItem::class:
85 case TimestampItem::class:
86 $value = &$normalization[$field_name][$i]['value'];
87 if (is_numeric($value)) {
88 $value = (string) $value;
91 case ImageItem::class:
92 $height = &$normalization[$field_name][$i]['height'];
93 $height = (string) $height;
94 $width = &$normalization[$field_name][$i]['width'];
95 $width = (string) $width;
96 $target_id = &$normalization[$field_name][$i]['target_id'];
97 $target_id = (string) $target_id;
102 if (!empty($normalization[$field_name])) {
103 $normalization[$field_name] = $normalization[$field_name][0];
107 return $normalization;
112 * Applies the XML config entity encoding quirks that remain after decoding.
115 * - loses type data (int and bool become string)
116 * - converts single-item arrays into single items (non-arrays)
118 * @param array $normalization
119 * An entity normalization.
122 * The updated config entity normalization.
124 * @see \Symfony\Component\Serializer\Encoder\XmlEncoder
126 protected function applyXmlConfigEntityDecodingQuirks(array $normalization) {
127 $normalization = static::castToString($normalization);
129 // When a single dependency is listed, it's not decoded into an array.
130 if (isset($normalization['dependencies'])) {
131 foreach ($normalization['dependencies'] as $dependency_type => $dependency_list) {
132 if (count($dependency_list) === 1) {
133 $normalization['dependencies'][$dependency_type] = $dependency_list[0];
138 return $normalization;
144 public function testPost() {
145 // Deserialization of the XML format is not supported.
146 $this->markTestSkipped();
152 public function testPatch() {
153 // Deserialization of the XML format is not supported.
154 $this->markTestSkipped();