You are here

public function CommentTest::testPostIndividualSkipCommentApproval in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/CommentTest.php \Drupal\Tests\jsonapi\Functional\CommentTest::testPostIndividualSkipCommentApproval()
  2. 10 core/modules/jsonapi/tests/src/Functional/CommentTest.php \Drupal\Tests\jsonapi\Functional\CommentTest::testPostIndividualSkipCommentApproval()

Tests POSTing a comment with and without 'skip comment approval'.

File

core/modules/jsonapi/tests/src/Functional/CommentTest.php, line 360

Class

CommentTest
JSON:API integration test for the "Comment" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostIndividualSkipCommentApproval() {
  $this
    ->setUpAuthorization('POST');
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);

  // Create request.
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions());
  $request_options[RequestOptions::BODY] = Json::encode($this
    ->getPostDocument());
  $url = Url::fromRoute('jsonapi.comment--comment.collection.post');

  // Status should be FALSE when posting as anonymous.
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceResponse(201, FALSE, $response);
  $this
    ->assertFalse(Json::decode((string) $response
    ->getBody())['data']['attributes']['status']);
  $this
    ->assertFalse($this->entityStorage
    ->loadUnchanged(2)
    ->isPublished());

  // Grant anonymous permission to skip comment approval.
  $this
    ->grantPermissionsToTestedRole([
    'skip comment approval',
  ]);

  // Status must be TRUE when posting as anonymous and skip comment approval.
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertResourceResponse(201, FALSE, $response);
  $this
    ->assertTrue(Json::decode((string) $response
    ->getBody())['data']['attributes']['status']);
  $this
    ->assertTrue($this->entityStorage
    ->loadUnchanged(3)
    ->isPublished());
}