You are here

protected function EntityTestDateonlyTest::assertNormalizationEdgeCases in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDateonlyTest.php \Drupal\Tests\datetime\Functional\EntityResource\EntityTest\EntityTestDateonlyTest::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 EntityResourceTestBase::assertNormalizationEdgeCases

See also

\GuzzleHttp\ClientInterface::request()

File

core/modules/datetime/tests/src/Functional/EntityResource/EntityTest/EntityTestDateonlyTest.php, line 120

Class

EntityTestDateonlyTest
Tests the datetime field constraint with 'date' items.

Namespace

Drupal\Tests\datetime\Functional\EntityResource\EntityTest

Code

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

    // DX: 422 when date type is incorrect.
    $normalization = $this
      ->getNormalizedPostEntity();
    $normalization[static::$fieldName][0]['value'] = [
      '2017',
      '03',
      '01',
    ];
    $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: The datetime value must be a string.\n{$fieldName}.0.value: This value should be of the correct primitive type.\n";
    $this
      ->assertResourceErrorResponse(422, $message, $response);

    // DX: 422 when date format is incorrect.
    $normalization = $this
      ->getNormalizedPostEntity();
    $value = '2017-03-01T01:02:03';
    $normalization[static::$fieldName][0]['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\" (date-only).";
    $this
      ->assertResourceErrorResponse(422, $message, $response);

    // DX: 422 when value is not a valid date.
    $normalization = $this
      ->getNormalizedPostEntity();
    $value = '2017-13-55';
    $normalization[static::$fieldName][0]['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\" (date-only).";
    $this
      ->assertResourceErrorResponse(422, $message, $response);
  }
}