.../EntityResource/EntityResourceTestBase.php | 83 +++++++++++++--------- 1 file changed, 50 insertions(+), 33 deletions(-) diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index c8240d2..5ef55ee 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -7,6 +7,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheableResponseInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface; +use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\FieldableEntityInterface; use Drupal\Core\Field\Plugin\Field\FieldType\BooleanItem; use Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem; @@ -1025,39 +1026,9 @@ public function testPatch() { // DX: 403 when sending PATCH request with updated read-only fields. - // Clone the entity, modifies all PATCH-protected fields. Then performs a - $modified_entity = clone $this->entity; - $original_values = []; - foreach (static::$patchProtectedFieldNames as $field_name) { - $field = $modified_entity->get($field_name); - $original_values[$field_name] = $field->getValue(); - switch ($field->getItemDefinition()->getClass()) { - case EntityReferenceItem::class: - // EntityReferenceItem::generateSampleValue() picks one of the last 50 - // entities of the supported type & bundle. We don't care if the value - // is valid, we only care that it's different. - $field->setValue(['target_id' => 99999]); - break; - case BooleanItem::class: - // BooleanItem::generateSampleValue() picks either 0 or 1. So a 50% - // chance of not picking a different value. - $field->value = ((int) $field->value) === 1 ? '0' : '1'; - break; - case PathItem::class: - // PathItem::generateSampleValue() doesn't set a PID, which causes - // PathItem::postSave() to fail. Keep the PID (and other properties), - // just modify the alias. - $value = $field->getValue(); - $value['alias'] = str_replace(' ', '-', strtolower((new Random())->sentences(3))); - $field->setValue($value); - break; - default: - $field->generateSampleItems(); - break; - } - } - // Send PATCH request by serializing the cloned entity, assert the error - // response, change the cloned entity field that caused the error response + list($modified_entity, $original_values) = static::getModifiedEntityForPatchTesting($this->entity); + // Send PATCH request by serializing the modified entity, assert the error + // response, change the modified entity field that caused the error response // back to its original value, repeat. for ($i = 0; $i < count(static::$patchProtectedFieldNames); $i++) { $patch_protected_field_name = static::$patchProtectedFieldNames[$i]; @@ -1293,6 +1264,52 @@ protected function getEntityResourcePostUrl() { } /** + * Clones the given entity and modifies all PATCH-protected fields. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity being tested and to modify. + * + * @return array + * Contains two items: + * 1. The modified entity object. + * 2. The original field values, keyed by field name. + */ + protected static function getModifiedEntityForPatchTesting(EntityInterface $entity) { + $modified_entity = clone $entity; + $original_values = []; + foreach (static::$patchProtectedFieldNames as $field_name) { + $field = $modified_entity->get($field_name); + $original_values[$field_name] = $field->getValue(); + switch ($field->getItemDefinition()->getClass()) { + case EntityReferenceItem::class: + // EntityReferenceItem::generateSampleValue() picks one of the last 50 + // entities of the supported type & bundle. We don't care if the value + // is valid, we only care that it's different. + $field->setValue(['target_id' => 99999]); + break; + case BooleanItem::class: + // BooleanItem::generateSampleValue() picks either 0 or 1. So a 50% + // chance of not picking a different value. + $field->value = ((int) $field->value) === 1 ? '0' : '1'; + break; + case PathItem::class: + // PathItem::generateSampleValue() doesn't set a PID, which causes + // PathItem::postSave() to fail. Keep the PID (and other properties), + // just modify the alias. + $value = $field->getValue(); + $value['alias'] = str_replace(' ', '-', strtolower((new Random())->sentences(3))); + $field->setValue($value); + break; + default: + $field->generateSampleItems(); + break; + } + } + + return [$modified_entity, $original_values]; + } + + /** * Makes the given entity normalization invalid. * * @param array $normalization