You are here

public function EntityResourceTestBase::testDelete in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testDelete()
  2. 10 core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php \Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase::testDelete()

Tests a DELETE request for an entity, plus edge cases to ensure good DX.

2 methods override EntityResourceTestBase::testDelete()
EntityTestComputedFieldNormalizerTest::testDelete in core/modules/system/tests/modules/entity_test/tests/src/Functional/Rest/EntityTestComputedFieldNormalizerTest.php
Tests a DELETE request for an entity, plus edge cases to ensure good DX.
MessageResourceTestBase::testDelete in core/modules/contact/tests/src/Functional/Rest/MessageResourceTestBase.php
Tests a DELETE request for an entity, plus edge cases to ensure good DX.

File

core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php, line 1105

Class

EntityResourceTestBase
Even though there is the generic EntityResource, it's necessary for every entity type to have its own test, because they each have different fields, validation constraints, et cetera. It's not because the generic case works, that every case…

Namespace

Drupal\Tests\rest\Functional\EntityResource

Code

public function testDelete() {

  // @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;
  }
  $this
    ->initAuthentication();
  $has_canonical_url = $this->entity
    ->hasLinkTemplate('canonical');

  // 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.
  $url = $this
    ->getEntityResourceUrl();
  $request_options = [];

  // DX: 404 when resource not provisioned, but 405 if canonical route. Plain
  // text  or HTML response because missing ?_format query string.
  $response = $this
    ->request('DELETE', $url, $request_options);
  if ($has_canonical_url) {
    $this
      ->assertSame(405, $response
      ->getStatusCode());
    $this
      ->assertSame([
      'GET, POST, HEAD',
    ], $response
      ->getHeader('Allow'));
    $this
      ->assertSame([
      'text/html; charset=UTF-8',
    ], $response
      ->getHeader('Content-Type'));
    $this
      ->assertStringContainsString('A client error happened', (string) $response
      ->getBody());
  }
  else {
    $this
      ->assertSame(404, $response
      ->getStatusCode());
    $this
      ->assertSame([
      'text/html; charset=UTF-8',
    ], $response
      ->getHeader('Content-Type'));
  }
  $url
    ->setOption('query', [
    '_format' => static::$format,
  ]);

  // DX: 404 when resource not provisioned, 405 if canonical route.
  $response = $this
    ->request('DELETE', $url, $request_options);
  if ($has_canonical_url) {
    $this
      ->assertSame([
      'GET, POST, HEAD',
    ], $response
      ->getHeader('Allow'));
    $this
      ->assertResourceErrorResponse(405, 'No route found for "DELETE ' . str_replace($this->baseUrl, '', $this
      ->getEntityResourceUrl()
      ->setAbsolute()
      ->toString()) . '": Method Not Allowed (Allow: GET, POST, HEAD)', $response);
  }
  else {
    $this
      ->assertResourceErrorResponse(404, 'No route found for "DELETE ' . str_replace($this->baseUrl, '', $this
      ->getEntityResourceUrl()
      ->setAbsolute()
      ->toString()) . '"', $response);
  }
  $this
    ->provisionEntityResource();
  if (static::$auth) {

    // DX: forgetting authentication: authentication provider-specific error
    // response.
    $response = $this
      ->request('DELETE', $url, $request_options);
    $this
      ->assertResponseWhenMissingAuthentication('DELETE', $response);
  }
  $request_options = NestedArray::mergeDeep($request_options, $this
    ->getAuthenticationRequestOptions('PATCH'));

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

  // Before sending a well-formed request, allow the authentication provider's
  // edge cases to also be tested.
  $this
    ->assertAuthenticationEdgeCases('DELETE', $url, $request_options);

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