function UserTest::testDeleteRespectsUserCancelDelete

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/UserTest.php \Drupal\Tests\jsonapi\Functional\UserTest::testDeleteRespectsUserCancelDelete()
  2. 11.x core/modules/jsonapi/tests/src/Functional/UserTest.php \Drupal\Tests\jsonapi\Functional\UserTest::testDeleteRespectsUserCancelDelete()

Tests if JSON:API respects user.settings.cancel_method: user_cancel_delete.

File

core/modules/jsonapi/tests/src/Functional/UserTest.php, line 773

Class

UserTest
JSON:API integration test for the "User" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testDeleteRespectsUserCancelDelete() : void {
  $cancel_method = 'user_cancel_delete';
  $this->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  $this->config('user.settings')
    ->set('cancel_method', $cancel_method)
    ->save(TRUE);
  $account = $this->createAnotherEntity($cancel_method);
  $node = $this->drupalCreateNode([
    'uid' => $account->id(),
  ]);
  $url = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
    'entity' => $account->uuid(),
  ]);
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions());
  $this->setUpAuthorization('DELETE');
  $response = $this->request('DELETE', $url, $request_options);
  $this->assertResourceResponse(204, NULL, $response);
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $user_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('user');
  $user_storage->resetCache([
    $account->id(),
  ]);
  $account = $user_storage->load($account->id());
  $this->assertNull($account, 'User is deleted after JSON:API DELETE operation with user.settings.cancel_method: ' . $cancel_method);
  $node_storage->resetCache([
    $node->id(),
  ]);
  $test_node = $node_storage->load($node->id());
  $this->assertNull($test_node, 'Node of the user is deleted.');
}

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