You are here

public function ResourceTestBase::testDeleteIndividual in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testDeleteIndividual()

Tests DELETEing an individual resource, plus edge cases to ensure good DX.

1 call to ResourceTestBase::testDeleteIndividual()
BlockContentTest::testDeleteIndividual in tests/src/Functional/BlockContentTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.
3 methods override ResourceTestBase::testDeleteIndividual()
BlockContentTest::testDeleteIndividual in tests/src/Functional/BlockContentTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.
ItemTest::testDeleteIndividual in tests/src/Functional/ItemTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.
MessageTest::testDeleteIndividual in tests/src/Functional/MessageTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.

File

tests/src/Functional/ResourceTestBase.php, line 2336

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testDeleteIndividual() {

  // @todo Remove this in https://www.drupal.org/node/2300677.
  if ($this->entity instanceof ConfigEntityInterface) {
    $this
      ->assertTrue(TRUE, 'DELETEing config entities is not yet supported.');
    return;
  }

  // The URL and Guzzle request options that will be used in this test. The
  // request options will be modified/expanded throughout this test:
  // - to first test all mistakes a developer might make, and assert that the
  //   error responses provide a good DX
  // - to eventually result in a well-formed request that succeeds.
  // @todo Remove line below in favor of commented line in https://www.drupal.org/project/jsonapi/issues/2878463.
  $url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
    static::$entityTypeId => $this->entity
      ->uuid(),
  ]);

  /* $url = $this->entity->toUrl('jsonapi'); */
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions());

  // DX: 403 when unauthorized.
  $response = $this
    ->request('DELETE', $url, $request_options);
  $reason = $this
    ->getExpectedUnauthorizedAccessMessage('DELETE');

  // @todo Remove $expected + assertResourceResponse() in favor of the commented line below once https://www.drupal.org/project/jsonapi/issues/2943176 lands.
  $expected_document = [
    'errors' => [
      [
        'title' => 'Forbidden',
        'status' => 403,
        'detail' => "The current user is not allowed to DELETE the selected resource." . (strlen($reason) ? ' ' . $reason : ''),
        'links' => [
          'info' => HttpExceptionNormalizer::getInfoUrl(403),
        ],
        'code' => 0,
        'id' => '/' . static::$resourceTypeName . '/' . $this->entity
          ->uuid(),
        'source' => [
          'pointer' => '/data',
        ],
      ],
    ],
  ];
  $this
    ->assertResourceResponse(403, $expected_document, $response);

  /* $this->assertResourceErrorResponse(403, "The current user is not allowed to DELETE the selected resource." . (strlen($reason) ? ' ' . $reason : ''), $response, '/data'); */
  $this
    ->setUpAuthorization('DELETE');

  // 204 for well-formed request.
  $response = $this
    ->request('DELETE', $url, $request_options);
  $this
    ->assertResourceResponse(204, NULL, $response);
}