You are here

protected function EntityTestDateRangeTest::assertNormalizationEdgeCases in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/datetime_range/tests/src/Functional/EntityResource/EntityTest/EntityTestDateRangeTest.php \Drupal\Tests\datetime_range\Functional\EntityResource\EntityTest\EntityTestDateRangeTest::assertNormalizationEdgeCases()
  2. 9 core/modules/datetime_range/tests/src/Functional/EntityResource/EntityTest/EntityTestDateRangeTest.php \Drupal\Tests\datetime_range\Functional\EntityResource\EntityTest\EntityTestDateRangeTest::assertNormalizationEdgeCases()

File

core/modules/datetime_range/tests/src/Functional/EntityResource/EntityTest/EntityTestDateRangeTest.php, line 121

Class

EntityTestDateRangeTest
Tests the 'daterange' field's normalization.

Namespace

Drupal\Tests\datetime_range\Functional\EntityResource\EntityTest

Code

protected function assertNormalizationEdgeCases($method, Url $url, array $request_options) : void {
  parent::assertNormalizationEdgeCases($method, $url, $request_options);
  if ($this->entity
    ->getEntityType()
    ->hasKey('bundle')) {
    $fieldName = static::$fieldName;

    // DX: 422 when 'value' data type is incorrect.
    $normalization = $this
      ->getNormalizedPostEntity();
    $normalization[static::$fieldName][0]['value'] = [
      '2017',
      '03',
      '01',
      '21',
      '53',
      '00',
    ];
    $request_options[RequestOptions::BODY] = $this->serializer
      ->encode($normalization, static::$format);
    $response = $this
      ->request($method, $url, $request_options);
    $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
    $this
      ->assertResourceErrorResponse(422, $message, $response);

    // DX: 422 when 'end_value' is not specified.
    $normalization = $this
      ->getNormalizedPostEntity();
    unset($normalization[static::$fieldName][0]['end_value']);
    $request_options[RequestOptions::BODY] = $this->serializer
      ->encode($normalization, static::$format);
    $response = $this
      ->request($method, $url, $request_options);
    $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0.end_value: This value should not be null.\n";
    $this
      ->assertResourceErrorResponse(422, $message, $response);

    // DX: 422 when 'end_value' data type is incorrect.
    $normalization = $this
      ->getNormalizedPostEntity();
    $normalization[static::$fieldName][0]['end_value'] = [
      '2017',
      '03',
      '01',
      '21',
      '53',
      '00',
    ];
    $request_options[RequestOptions::BODY] = $this->serializer
      ->encode($normalization, static::$format);
    $response = $this
      ->request($method, $url, $request_options);
    $message = "Unprocessable Entity: validation failed.\n{$fieldName}.0.end_value: This value should be of the correct primitive type.\n";
    $this
      ->assertResourceErrorResponse(422, $message, $response);

    // DX: 422 when end date value is invalid.
    $normalization = $this
      ->getNormalizedPostEntity();
    $value = '2017-13-55T20:02:00+00:00';
    $normalization[static::$fieldName][0]['end_value'] = $value;
    $request_options[RequestOptions::BODY] = $this->serializer
      ->encode($normalization, static::$format);
    $response = $this
      ->request($method, $url, $request_options);
    $message = "The specified date \"{$value}\" is not in an accepted format: \"Y-m-d\\TH:i:sP\" (RFC 3339), \"Y-m-d\\TH:i:sO\" (ISO 8601).";
    $this
      ->assertResourceErrorResponse(422, $message, $response);

    // @todo Expand in https://www.drupal.org/project/drupal/issues/2847041.
  }
}