You are here

public function ResourceTestBase::testDeleteIndividual in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testDeleteIndividual()
  2. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::testDeleteIndividual()

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

3 methods override ResourceTestBase::testDeleteIndividual()
FileUploadTest::testDeleteIndividual in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
@requires module irrelevant_for_this_test
ItemTest::testDeleteIndividual in core/modules/jsonapi/tests/src/Functional/ItemTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.
MessageTest::testDeleteIndividual in core/modules/jsonapi/tests/src/Functional/MessageTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 2403

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/drupal/issues/2878463.
  $url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
    'entity' => $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: 405 when read-only mode is enabled.
  $response = $this
    ->request('DELETE', $url, $request_options);
  $this
    ->assertResourceErrorResponse(405, sprintf("JSON:API is configured to accept only read operations. Site administrators can configure this at %s.", Url::fromUri('base:/admin/config/services/jsonapi')
    ->setAbsolute()
    ->toString(TRUE)
    ->getGeneratedUrl()), $url, $response);
  $this
    ->assertSame([
    'GET',
  ], $response
    ->getHeader('Allow'));
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);

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

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

  // DX: 404 when non-existent.
  $response = $this
    ->request('DELETE', $url, $request_options);
  $this
    ->assertSame(404, $response
    ->getStatusCode());
}