function EntityResourceTestBase::assertStoredEntityMatchesSentNormalization

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assertStoredEntityMatchesSentNormalization()
  2. 8.9.x core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assertStoredEntityMatchesSentNormalization()
  3. 11.x core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assertStoredEntityMatchesSentNormalization()

Asserts that the stored entity matches the sent normalization.

Parameters

array $sent_normalization: An entity normalization.

\Drupal\Core\Entity\FieldableEntityInterface $modified_entity: The entity object of the modified (PATCHed or POSTed) entity.

2 calls to EntityResourceTestBase::assertStoredEntityMatchesSentNormalization()
EntityResourceTestBase::testPatch in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Tests a PATCH request for an entity, plus edge cases to ensure good DX.
EntityResourceTestBase::testPost in core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php
Tests a POST request for an entity, plus edge cases to ensure good DX.

File

core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php, line 1398

Class

EntityResourceTestBase
Defines a base class for testing all entity resources.

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

protected function assertStoredEntityMatchesSentNormalization(array $sent_normalization, FieldableEntityInterface $modified_entity) {
  foreach ($sent_normalization as $field_name => $field_normalization) {
    // Some top-level keys in the normalization may not be fields on the
    // entity.
    if ($modified_entity->hasField($field_name)) {
      $field_definition = $modified_entity->get($field_name)
        ->getFieldDefinition();
      $property_definitions = $field_definition->getItemDefinition()
        ->getPropertyDefinitions();
      $expected_stored_data = [];
      // Some fields don't have any property definitions, so there's nothing
      // to denormalize.
      if (empty($property_definitions)) {
        $expected_stored_data = $field_normalization;
      }
      else {
        // Denormalize every sent field item property to make it possible to
        // compare against the stored value.
        $denormalization_context = [
          'field_definition' => $field_definition,
        ];
        foreach ($field_normalization as $delta => $expected_field_item_normalization) {
          foreach ($property_definitions as $property_name => $property_definition) {
            // Not every property is required to be sent.
            if (!array_key_exists($property_name, $field_normalization[$delta])) {
              continue;
            }
            // Computed properties are not stored.
            if ($property_definition->isComputed()) {
              continue;
            }
            $property_value = $field_normalization[$delta][$property_name];
            $property_value_class = $property_definitions[$property_name]->getClass();
            $expected_stored_data[$delta][$property_name] = $this->serializer
              ->supportsDenormalization($property_value, $property_value_class, NULL, $denormalization_context) ? $this->serializer
              ->denormalize($property_value, $property_value_class, NULL, $denormalization_context) : $property_value;
          }
        }
        // Fields are stored in the database, when read they are represented
        // as strings in PHP memory.
        $expected_stored_data = static::castToString($expected_stored_data);
      }
      $this->assertEntityArraySubset($expected_stored_data, $modified_entity->get($field_name)
        ->getValue());
    }
  }
}

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