962d8e9d2ef470f2c466d81260a04076062bd9db
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / XmlEntityNormalizationQuirksTrait.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource;
4
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;
17
18 /**
19  * Trait for EntityResourceTestBase subclasses testing $format='xml'.
20  */
21 trait XmlEntityNormalizationQuirksTrait {
22
23   use XmlNormalizationQuirksTrait;
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function getExpectedNormalizedEntity() {
29     $default_normalization = parent::getExpectedNormalizedEntity();
30
31     if ($this->entity instanceof FieldableEntityInterface) {
32       $normalization = $this->applyXmlFieldDecodingQuirks($default_normalization);
33     }
34     else {
35       $normalization = $this->applyXmlConfigEntityDecodingQuirks($default_normalization);
36     }
37     $normalization = $this->applyXmlDecodingQuirks($normalization);
38
39     return $normalization;
40   }
41
42   /**
43    * Applies the XML entity field encoding quirks that remain after decoding.
44    *
45    * The XML encoding:
46    * - loses type data (int and bool become string)
47    *
48    * @param array $normalization
49    *   An entity normalization.
50    *
51    * @return array
52    *   The updated fieldable entity normalization.
53    *
54    * @see \Symfony\Component\Serializer\Encoder\XmlEncoder
55    */
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])) {
60         continue;
61       }
62
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';
68             break;
69           case IntegerItem::class:
70           case ListIntegerItem::class:
71             $value = &$normalization[$field_name][$i]['value'];
72             $value = (string) $value;
73             break;
74           case PathItem::class:
75             $pid = &$normalization[$field_name][$i]['pid'];
76             $pid = (string) $pid;
77             break;
78           case EntityReferenceItem::class:
79           case FileItem::class:
80             $target_id = &$normalization[$field_name][$i]['target_id'];
81             $target_id = (string) $target_id;
82             break;
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;
89             }
90             break;
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;
98             break;
99         }
100       }
101
102       if (!empty($normalization[$field_name])) {
103         $normalization[$field_name] = $normalization[$field_name][0];
104       }
105     }
106
107     return $normalization;
108   }
109
110
111   /**
112    * Applies the XML config entity encoding quirks that remain after decoding.
113    *
114    * The XML encoding:
115    * - loses type data (int and bool become string)
116    * - converts single-item arrays into single items (non-arrays)
117    *
118    * @param array $normalization
119    *   An entity normalization.
120    *
121    * @return array
122    *   The updated config entity normalization.
123    *
124    * @see \Symfony\Component\Serializer\Encoder\XmlEncoder
125    */
126   protected function applyXmlConfigEntityDecodingQuirks(array $normalization) {
127     $normalization = static::castToString($normalization);
128
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];
134         }
135       }
136     }
137
138     return $normalization;
139   }
140
141   /**
142    * {@inheritdoc}
143    */
144   public function testPost() {
145     // Deserialization of the XML format is not supported.
146     $this->markTestSkipped();
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function testPatch() {
153     // Deserialization of the XML format is not supported.
154     $this->markTestSkipped();
155   }
156
157 }