You are here

protected function EntityResourceTestBase::assertNormalizationEdgeCases in Drupal 9

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

Asserts normalization-specific edge cases.

(Should be called before sending a well-formed request.)

Parameters

string $method: HTTP method.

\Drupal\Core\Url $url: URL to request.

array $request_options: Request options to apply.

Overrides ResourceTestBase::assertNormalizationEdgeCases

See also

\GuzzleHttp\ClientInterface::request()

5 calls to EntityResourceTestBase::assertNormalizationEdgeCases()
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.
EntityTestDateonlyTest::assertNormalizationEdgeCases in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDateonlyTest.php
Asserts normalization-specific edge cases.
EntityTestDateRangeTest::assertNormalizationEdgeCases in core/modules/datetime_range/tests/src/Functional/EntityResource/EntityTest/EntityTestDateRangeTest.php
Asserts normalization-specific edge cases.
EntityTestDatetimeTest::assertNormalizationEdgeCases in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php
Asserts normalization-specific edge cases.
3 methods override EntityResourceTestBase::assertNormalizationEdgeCases()
EntityTestDateonlyTest::assertNormalizationEdgeCases in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDateonlyTest.php
Asserts normalization-specific edge cases.
EntityTestDateRangeTest::assertNormalizationEdgeCases in core/modules/datetime_range/tests/src/Functional/EntityResource/EntityTest/EntityTestDateRangeTest.php
Asserts normalization-specific edge cases.
EntityTestDatetimeTest::assertNormalizationEdgeCases in core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDatetimeTest.php
Asserts normalization-specific edge cases.

File

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

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 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);
  }
}