public function CommentTest::testPostIndividualDxWithoutCriticalBaseFields in JSON:API 8
Same name and namespace in other branches
- 8.2 tests/src/Functional/CommentTest.php \Drupal\Tests\jsonapi\Functional\CommentTest::testPostIndividualDxWithoutCriticalBaseFields()
Tests POSTing a comment without critical base fields.
Note that testPostIndividual() is testing with the most minimal normalization possible: the one returned by ::getNormalizedPostEntity().
But Comment entities have some very special edge cases:
- base fields that are not marked as required in \Drupal\comment\Entity\Comment::baseFieldDefinitions() yet in fact are required.
- base fields that are marked as required, but yet can still result in validation errors other than "missing required field".
File
- tests/
src/ Functional/ CommentTest.php, line 322
Class
- CommentTest
- JSON API integration test for the "Comment" content entity type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testPostIndividualDxWithoutCriticalBaseFields() {
$this
->setUpAuthorization('POST');
$url = Url::fromRoute(sprintf('jsonapi.%s.collection', static::$resourceTypeName));
$request_options = [];
$request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
$request_options = NestedArray::mergeDeep($request_options, $this
->getAuthenticationRequestOptions());
$remove_field = function (array $normalization, $type, $attribute_name) {
unset($normalization['data'][$type][$attribute_name]);
return $normalization;
};
// DX: 422 when missing 'entity_type' field.
$request_options[RequestOptions::BODY] = Json::encode($remove_field($this
->getPostDocument(), 'attributes', 'entity_type'));
$response = $this
->request('POST', $url, $request_options);
if (floatval(\Drupal::VERSION) >= 8.699999999999999) {
$this
->assertResourceErrorResponse(422, 'entity_type: This value should not be null.', $response, '/data/attributes/entity_type');
}
else {
$this
->assertResourceErrorResponse(500, 'The "" entity type does not exist.', $response, FALSE);
}
// DX: 422 when missing 'entity_id' field.
$request_options[RequestOptions::BODY] = Json::encode($remove_field($this
->getPostDocument(), 'relationships', 'entity_id'));
// @todo Remove the try/catch in https://www.drupal.org/node/2820364.
try {
$response = $this
->request('POST', $url, $request_options);
$this
->assertResourceErrorResponse(422, 'entity_id: This value should not be null.', $response, '/data/attributes/entity_id');
} catch (\Exception $e) {
$this
->assertSame("Error: Call to a member function get() on null\nDrupal\\comment\\Plugin\\Validation\\Constraint\\CommentNameConstraintValidator->getAnonymousContactDetailsSetting()() (Line: 96)\n", $e
->getMessage());
}
// DX: 422 when missing 'field_name' field.
$request_options[RequestOptions::BODY] = Json::encode($remove_field($this
->getPostDocument(), 'attributes', 'field_name'));
$response = $this
->request('POST', $url, $request_options);
if (floatval(\Drupal::VERSION) >= 8.699999999999999) {
$this
->assertResourceErrorResponse(422, 'field_name: This value should not be null.', $response, '/data/attributes/field_name');
}
else {
$this
->assertSame(500, $response
->getStatusCode());
}
}