function CommentExtrasTest::testPostIndividualSkipCommentApproval

Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/tests/src/Functional/CommentExtrasTest.php \Drupal\Tests\jsonapi\Functional\CommentExtrasTest::testPostIndividualSkipCommentApproval()

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

File

core/modules/jsonapi/tests/src/Functional/CommentExtrasTest.php, line 81

Class

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

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostIndividualSkipCommentApproval() : void {
  $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);
  $document = $this->getDocumentFromResponse($response);
  $this->assertResourceResponse(201, FALSE, $response);
  $this->assertFalse($document['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);
  $document = $this->getDocumentFromResponse($response);
  $this->assertResourceResponse(201, FALSE, $response);
  $this->assertTrue($document['data']['attributes']['status']);
  $this->assertTrue($this->entityStorage
    ->loadUnchanged(3)
    ->isPublished());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.