protected function EntityResourceTestBase::assertStoredEntityMatchesSentNormalization in Drupal 8
Same name and namespace in other branches
- 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::assertStoredEntityMatchesSentNormalization()
- 10 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 1591
Class
- EntityResourceTestBase
- Even though there is the generic EntityResource, it's necessary for every entity type to have its own test, because they each have different fields, validation constraints, et cetera. It's not because the generic case works, that every case…
Namespace
Drupal\Tests\rest\Functional\EntityResourceCode
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 (for example '_links' and '_embedded' in the HAL normalization).
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);
}
// Subset, not same, because we can e.g. send just the target_id for the
// bundle in a PATCH or POST request; the response will include more
// properties.
$this
->assertArraySubset($expected_stored_data, $modified_entity
->get($field_name)
->getValue(), TRUE);
}
}
}