function EntityResourceTestBase::assertNormalizationEdgeCases

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

File

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

Class

EntityResourceTestBase
Defines a base class for testing all entity resources.

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) {
  // \Drupal\serialization\Normalizer\EntityNormalizer::denormalize(): entity
  // types with bundles MUST send their bundle field to be denormalizable.
  $entity_type = $this->entity
    ->getEntityType();
  if ($entity_type->hasKey('bundle')) {
    $bundle_field_name = $this->entity
      ->getEntityType()
      ->getKey('bundle');
    $normalization = $this->getNormalizedPostEntity();
    // The bundle type itself can be validated only if there's a bundle entity
    // type.
    if ($entity_type->getBundleEntityType()) {
      $normalization[$bundle_field_name] = 'bad_bundle_name';
      $request_options[RequestOptions::BODY] = $this->serializer
        ->encode($normalization, static::$format);
      // DX: 422 when incorrect entity type bundle is specified.
      $response = $this->request($method, $url, $request_options);
      $this->assertResourceErrorResponse(422, '"bad_bundle_name" is not a valid bundle type for denormalization.', $response);
    }
    unset($normalization[$bundle_field_name]);
    $request_options[RequestOptions::BODY] = $this->serializer
      ->encode($normalization, static::$format);
    // DX: 422 when no entity type bundle is specified.
    $response = $this->request($method, $url, $request_options);
    $this->assertResourceErrorResponse(422, sprintf('Could not determine entity type bundle: "%s" field is missing.', $bundle_field_name), $response);
  }
}

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