trait XmlEntityNormalizationQuirksTrait

Same name in other branches
  1. 8.9.x core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait
  2. 10 core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait
  3. 11.x core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php \Drupal\Tests\rest\Functional\EntityResource\XmlEntityNormalizationQuirksTrait

Trait for EntityResourceTestBase subclasses testing $format='xml'.

Hierarchy

153 files declare their use of XmlEntityNormalizationQuirksTrait
ActionXmlAnonTest.php in core/modules/system/tests/src/Functional/Rest/ActionXmlAnonTest.php
ActionXmlBasicAuthTest.php in core/modules/system/tests/src/Functional/Rest/ActionXmlBasicAuthTest.php
ActionXmlCookieTest.php in core/modules/system/tests/src/Functional/Rest/ActionXmlCookieTest.php
BaseFieldOverrideXmlAnonTest.php in core/tests/Drupal/FunctionalTests/Rest/BaseFieldOverrideXmlAnonTest.php
BaseFieldOverrideXmlBasicAuthTest.php in core/tests/Drupal/FunctionalTests/Rest/BaseFieldOverrideXmlBasicAuthTest.php

... See full list

File

core/modules/rest/tests/src/Functional/EntityResource/XmlEntityNormalizationQuirksTrait.php, line 22

Namespace

Drupal\Tests\rest\Functional\EntityResource
View source
trait XmlEntityNormalizationQuirksTrait {
    use XmlNormalizationQuirksTrait;
    
    /**
     * {@inheritdoc}
     */
    protected function getExpectedNormalizedEntity() {
        $default_normalization = parent::getExpectedNormalizedEntity();
        if ($this->entity instanceof FieldableEntityInterface) {
            $normalization = $this->applyXmlFieldDecodingQuirks($default_normalization);
        }
        else {
            $normalization = $this->applyXmlConfigEntityDecodingQuirks($default_normalization);
        }
        $normalization = $this->applyXmlDecodingQuirks($normalization);
        return $normalization;
    }
    
    /**
     * Applies the XML entity field encoding quirks that remain after decoding.
     *
     * The XML encoding:
     * - loses type data (int and bool become string)
     *
     * @param array $normalization
     *   An entity normalization.
     *
     * @return array
     *   The updated fieldable entity normalization.
     *
     * @see \Symfony\Component\Serializer\Encoder\XmlEncoder
     */
    protected function applyXmlFieldDecodingQuirks(array $normalization) {
        foreach ($this->entity
            ->getFields(TRUE) as $field_name => $field) {
            // Not every field is accessible.
            if (!isset($normalization[$field_name])) {
                continue;
            }
            for ($i = 0; $i < count($normalization[$field_name]); $i++) {
                switch ($field->getItemDefinition()
                    ->getClass()) {
                    case BooleanItem::class:
                    case StatusItem::class:
                        // @todo Remove the StatusItem case in
                        //   https://www.drupal.org/project/drupal/issues/2936864.
                        $value =& $normalization[$field_name][$i]['value'];
                        $value = $value === TRUE ? '1' : '0';
                        break;
                    case IntegerItem::class:
                    case ListIntegerItem::class:
                        $value =& $normalization[$field_name][$i]['value'];
                        $value = (string) $value;
                        break;
                    case PathItem::class:
                        $pid =& $normalization[$field_name][$i]['pid'];
                        $pid = (string) $pid;
                        break;
                    case EntityReferenceItem::class:
                    case FileItem::class:
                        $target_id =& $normalization[$field_name][$i]['target_id'];
                        $target_id = (string) $target_id;
                        break;
                    case ChangedItem::class:
                    case CreatedItem::class:
                    case TimestampItem::class:
                        $value =& $normalization[$field_name][$i]['value'];
                        if (is_numeric($value)) {
                            $value = (string) $value;
                        }
                        break;
                    case ImageItem::class:
                        $height =& $normalization[$field_name][$i]['height'];
                        $height = (string) $height;
                        $width =& $normalization[$field_name][$i]['width'];
                        $width = (string) $width;
                        $target_id =& $normalization[$field_name][$i]['target_id'];
                        $target_id = (string) $target_id;
                        break;
                }
            }
            if (count($normalization[$field_name]) === 1) {
                $normalization[$field_name] = $normalization[$field_name][0];
            }
        }
        return $normalization;
    }
    
    /**
     * Applies the XML config entity encoding quirks that remain after decoding.
     *
     * The XML encoding:
     * - loses type data (int and bool become string)
     * - converts single-item arrays into single items (non-arrays)
     *
     * @param array $normalization
     *   An entity normalization.
     *
     * @return array
     *   The updated config entity normalization.
     *
     * @see \Symfony\Component\Serializer\Encoder\XmlEncoder
     */
    protected function applyXmlConfigEntityDecodingQuirks(array $normalization) {
        $normalization = static::castToString($normalization);
        // When a single dependency is listed, it's not decoded into an array.
        if (isset($normalization['dependencies'])) {
            foreach ($normalization['dependencies'] as $dependency_type => $dependency_list) {
                if (count($dependency_list) === 1) {
                    $normalization['dependencies'][$dependency_type] = $dependency_list[0];
                }
            }
        }
        return $normalization;
    }
    
    /**
     * {@inheritdoc}
     */
    public function testPost() {
        // Deserialization of the XML format is not supported.
        $this->markTestSkipped();
    }
    
    /**
     * {@inheritdoc}
     */
    public function testPatch() {
        // Deserialization of the XML format is not supported.
        $this->markTestSkipped();
    }

}

Members

Title Sort descending Modifiers Object type Summary
XmlEntityNormalizationQuirksTrait::applyXmlConfigEntityDecodingQuirks protected function Applies the XML config entity encoding quirks that remain after decoding.
XmlEntityNormalizationQuirksTrait::applyXmlFieldDecodingQuirks protected function Applies the XML entity field encoding quirks that remain after decoding.
XmlEntityNormalizationQuirksTrait::getExpectedNormalizedEntity protected function
XmlEntityNormalizationQuirksTrait::testPatch public function
XmlEntityNormalizationQuirksTrait::testPost public function
XmlNormalizationQuirksTrait::applyXmlDecodingQuirks protected function Applies the XML encoding quirks that remain after decoding.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.