public function CommentTest::testPostIndividualSkipCommentApproval in JSON:API 8
Same name and namespace in other branches
- 8.2 tests/src/Functional/CommentTest.php \Drupal\Tests\jsonapi\Functional\CommentTest::testPostIndividualSkipCommentApproval()
Tests POSTing a comment with and without 'skip comment approval'.
File
- tests/src/ Functional/ CommentTest.php, line 370 
Class
- CommentTest
- JSON API integration test for the "Comment" content entity type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testPostIndividualSkipCommentApproval() {
  $this
    ->setUpAuthorization('POST');
  // Create request.
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = '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');
  // 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());
}