You are here

public function CommentResourceTestBase::testPostDxWithoutCriticalBaseFields in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php \Drupal\Tests\comment\Functional\Rest\CommentResourceTestBase::testPostDxWithoutCriticalBaseFields()
  2. 10 core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php \Drupal\Tests\comment\Functional\Rest\CommentResourceTestBase::testPostDxWithoutCriticalBaseFields()

Tests POSTing a comment without critical base fields.

Tests with the most minimal normalization possible: the one returned by ::getNormalizedPostEntity().

But Comment entities have some very special edge cases:

3 methods override CommentResourceTestBase::testPostDxWithoutCriticalBaseFields()
CommentXmlAnonTest::testPostDxWithoutCriticalBaseFields in core/modules/comment/tests/src/Functional/Rest/CommentXmlAnonTest.php
Tests POSTing a comment without critical base fields.
CommentXmlBasicAuthTest::testPostDxWithoutCriticalBaseFields in core/modules/comment/tests/src/Functional/Rest/CommentXmlBasicAuthTest.php
Tests POSTing a comment without critical base fields.
CommentXmlCookieTest::testPostDxWithoutCriticalBaseFields in core/modules/comment/tests/src/Functional/Rest/CommentXmlCookieTest.php
Tests POSTing a comment without critical base fields.

File

core/modules/comment/tests/src/Functional/Rest/CommentResourceTestBase.php, line 288

Class

CommentResourceTestBase

Namespace

Drupal\Tests\comment\Functional\Rest

Code

public function testPostDxWithoutCriticalBaseFields() {
  $this
    ->initAuthentication();
  $this
    ->provisionEntityResource();
  $this
    ->setUpAuthorization('POST');
  $url = $this
    ->getEntityResourcePostUrl()
    ->setOption('query', [
    '_format' => static::$format,
  ]);
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = static::$mimeType;
  $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType;
  $request_options = array_merge_recursive($request_options, $this
    ->getAuthenticationRequestOptions('POST'));

  // DX: 422 when missing 'entity_type' field.
  $request_options[RequestOptions::BODY] = $this->serializer
    ->encode(array_diff_key($this
    ->getNormalizedPostEntity(), [
    'entity_type' => TRUE,
  ]), static::$format);
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nentity_type: This value should not be null.\n", $response);

  // DX: 422 when missing 'entity_id' field.
  $request_options[RequestOptions::BODY] = $this->serializer
    ->encode(array_diff_key($this
    ->getNormalizedPostEntity(), [
    'entity_id' => TRUE,
  ]), static::$format);
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nentity_id: This value should not be null.\n", $response);

  // DX: 422 when missing 'field_name' field.
  $request_options[RequestOptions::BODY] = $this->serializer
    ->encode(array_diff_key($this
    ->getNormalizedPostEntity(), [
    'field_name' => TRUE,
  ]), static::$format);
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nfield_name: This value should not be null.\n", $response);
}