.../Comment/CommentResourceTestBase.php | 53 ++++++++++++++++------ .../rest/tests/src/Functional/ResourceTestBase.php | 4 ++ 2 files changed, 42 insertions(+), 15 deletions(-) diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php index cb50160..c03d45e 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Comment/CommentResourceTestBase.php @@ -8,6 +8,7 @@ use Drupal\entity_test\Entity\EntityTest; use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase; use Drupal\user\Entity\User; +use GuzzleHttp\RequestOptions; abstract class CommentResourceTestBase extends EntityResourceTestBase { @@ -41,21 +42,6 @@ protected function setUpAuthorization($method) { * {@inheritdoc} */ protected function createEntity() { -// // Create a "Camelids" node type. -// NodeType::create([ -// 'name' => 'Camelids', -// 'type' => 'camelids', -// ])->save(); -// -// // Create a "Llama" node. -// $node = Node::create(['type' => 'camelids']); -// $node->setTitle('Llama') -// ->setPublished(TRUE) -// ->setCreatedTime(123456789) -// ->setChangedTime(123456789) -// ->setRevisionCreationTime(123456789) -// ->save(); - // Create a "bar" bundle for the "entity_test" entity type and create. $bundle = 'bar'; entity_test_create_bundle($bundle, NULL, 'entity_test'); @@ -221,4 +207,41 @@ protected function getNormalizedEntityToCreate() { ]; } + public function testPostDxWithoutCriticalBaseFields() { + $this->provisionEntityResource(); + $this->setUpAuthorization('POST'); + + $url = $this->getPostUrl()->setOption('query', ['_format' => static::$format]); + $request_options = []; + $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType; + $request_options = array_merge_recursive($request_options, $this->getAuthenticationRequestOptions()); + + // DX: 422 when missing 'entity_type' field. + $request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedEntityToCreate(), ['entity_type' => TRUE]), static::$format); + $response = $this->request('POST', $url, $request_options); + // @todo Remove the first line in favor of the commented line in https://www.drupal.org/node/2820364. + $this->assertResourceErrorResponse(500, 'A fatal error occurred: Internal Server Error', $response); + //$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->getNormalizedEntityToCreate(), ['entity_id' => TRUE]), static::$format); + // @todo Remove the try/catch in favor of the two commented lines in https://www.drupal.org/node/2820364. + try { + $this->request('POST', $url, $request_options); + $this->assertSame(TRUE, FALSE); + } + 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()); + } + //$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_type' field. + $request_options[RequestOptions::BODY] = $this->serializer->encode(array_diff_key($this->getNormalizedEntityToCreate(), ['field_name' => TRUE]), static::$format); + $response = $this->request('POST', $url, $request_options); + // @todo Remove the first line in favor of the commented line in https://www.drupal.org/node/2820364. + $this->assertResourceErrorResponse(500, 'A fatal error occurred: Field is unknown.', $response); + //$this->assertResourceErrorResponse(422, "Unprocessable Entity: validation failed.\nfield_name: This value should not be null.\n", $response); + } + } diff --git a/core/modules/rest/tests/src/Functional/ResourceTestBase.php b/core/modules/rest/tests/src/Functional/ResourceTestBase.php index 1c212bb..36a4a5a 100644 --- a/core/modules/rest/tests/src/Functional/ResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/ResourceTestBase.php @@ -8,6 +8,7 @@ use Drupal\user\Entity\Role; use Drupal\user\RoleInterface; use GuzzleHttp\Exception\ClientException; +use GuzzleHttp\Exception\ServerException; use Psr\Http\Message\ResponseInterface; /** @@ -250,6 +251,9 @@ protected function request($method, Url $url, array $request_options) { catch (ClientException $e) { $response = $e->getResponse(); } + catch (ServerException $e) { + $response = $e->getResponse(); + } return $response; }