function ResourceTestBase::doTestDeleteIndividual

Same name in other branches
  1. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::doTestDeleteIndividual()

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

2 calls to ResourceTestBase::doTestDeleteIndividual()
FileTest::testIndividual in core/modules/jsonapi/tests/src/Functional/FileTest.php
Tests POST/PATCH/DELETE for an individual resource.
ResourceTestBase::testIndividual in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests POST/PATCH/DELETE for an individual resource.
2 methods override ResourceTestBase::doTestDeleteIndividual()
MessageTest::doTestDeleteIndividual in core/modules/jsonapi/tests/src/Functional/MessageTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.
UserTest::doTestDeleteIndividual in core/modules/jsonapi/tests/src/Functional/UserTest.php
Tests DELETEing an individual resource, plus edge cases to ensure good DX.

File

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

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function doTestDeleteIndividual() : void {
    // @todo Remove this in https://www.drupal.org/node/2300677.
    if ($this->entity instanceof ConfigEntityInterface) {
        $this->markTestSkipped('DELETEing config entities is not yet supported.');
    }
    // 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());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.