You are here

protected function EntityResourceTestBase::getModifiedEntityForPostTesting in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::getModifiedEntityForPostTesting()
  2. 10 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::getModifiedEntityForPostTesting()

Gets the normalized POST entity with random values for its unique fields.

Return value

array An array structure as returned by ::getNormalizedPostEntity().

See also

::testPost

::getNormalizedPostEntity

1 call to EntityResourceTestBase::getModifiedEntityForPostTesting()
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 327

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\EntityResource

Code

protected function getModifiedEntityForPostTesting() {
  $normalized_entity = $this
    ->getNormalizedPostEntity();

  // Ensure that all the unique fields of the entity type get a new random
  // value.
  foreach (static::$uniqueFieldNames as $field_name) {
    $field_definition = $this->entity
      ->getFieldDefinition($field_name);
    $field_type_class = $field_definition
      ->getItemDefinition()
      ->getClass();
    $normalized_entity[$field_name] = $field_type_class::generateSampleValue($field_definition);
  }
  return $normalized_entity;
}