You are here

public function JsonApiTest::testApiResourceDelete in Entity Reference Integrity 8

Test deleting the referenced node via the API.

File

modules/entity_reference_integrity_enforce/tests/src/Functional/JsonApiTest.php, line 135

Class

JsonApiTest
Tests referential integrity via JSONAPI.

Namespace

Drupal\Tests\entity_reference_integrity_enforce\Functional

Code

public function testApiResourceDelete() {

  // Setup the request.
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
  $request_options[RequestOptions::HEADERS]['Authorization'] = 'Basic ' . base64_encode($this->testUser->name->value . ':' . $this->testUser->passRaw);

  // Build a url for the individual JSONAPI resource.
  $resource_type_name = $this->referencedNode
    ->getEntityTypeId() . '--' . $this->referencedNode
    ->bundle();
  $node_url = Url::fromRoute(sprintf('jsonapi.%s.individual', $resource_type_name), [
    'entity' => $this->referencedNode
      ->uuid(),
  ])
    ->setAbsolute();

  // Assert access denied when the node has dependents.
  $this
    ->assertTrue($this->referenceIntegrityHandler
    ->hasDependents($this->referencedNode));
  $response = $this
    ->request('DELETE', $node_url, $request_options);
  $this
    ->assertEquals(403, $response
    ->getStatusCode());

  // Assert valid response body. Should be a JSONAPI response with errors.
  $response_data = Json::decode((string) $response
    ->getBody());
  $this
    ->assertArrayHasKey('errors', $response_data);
  $this
    ->assertEquals(1, sizeof($response_data['errors']));
  $this
    ->assertArrayHasKey('status', $response_data['errors'][0]);
  $this
    ->assertEquals('403', $response_data['errors'][0]['status']);

  // Assert that the correct reason is provided. This ensures the 403 is
  // given for the right reason.
  $reason = EntityReferenceIntegrityEntityHandler::getAccessDeniedReason($this->referencedNode, FALSE);
  $this
    ->assertArrayHasKey('detail', $response_data['errors'][0]);
  $this
    ->assertEquals($reason, $response_data['errors'][0]['detail']);

  // Unset the node reference.
  $this->testNode->test_reference_field = [];
  $this->testNode
    ->save();

  // Assert access allowed when the node has no dependents.
  $this
    ->assertFalse($this->referenceIntegrityHandler
    ->hasDependents($this->referencedNode));
  $response = $this
    ->request('DELETE', $node_url, $request_options);
  $this
    ->assertEquals(204, $response
    ->getStatusCode());

  // Test that the unreferenced node can be deleted.
  $node_url = Url::fromRoute(sprintf('jsonapi.%s.individual', $resource_type_name), [
    'entity' => $this->testNode
      ->uuid(),
  ])
    ->setAbsolute();
  $this
    ->assertFalse($this->referenceIntegrityHandler
    ->hasDependents($this->referencedNode));
  $response = $this
    ->request('DELETE', $node_url, $request_options);
  $this
    ->assertEquals(204, $response
    ->getStatusCode());
}