You are here

public function UserCancelTest::testUserAnonymizeBatch in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserAnonymizeBatch()
  2. 9 core/modules/user/tests/src/Functional/UserCancelTest.php \Drupal\Tests\user\Functional\UserCancelTest::testUserAnonymizeBatch()

Delete account and anonymize all content using a batch process.

File

core/modules/user/tests/src/Functional/UserCancelTest.php, line 412

Class

UserCancelTest
Ensure that account cancellation methods work as expected.

Namespace

Drupal\Tests\user\Functional

Code

public function testUserAnonymizeBatch() {
  $node_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('node');
  $this
    ->config('user.settings')
    ->set('cancel_method', 'user_cancel_reassign')
    ->save();
  $user_storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('user');

  // Create a user.
  $account = $this
    ->drupalCreateUser([
    'cancel account',
  ]);
  $this
    ->drupalLogin($account);

  // Load a real user object.
  $user_storage
    ->resetCache([
    $account
      ->id(),
  ]);
  $account = $user_storage
    ->load($account
    ->id());

  // Create 11 nodes in order to trigger batch processing in
  // node_mass_update().
  $nodes = [];
  for ($i = 0; $i < 11; $i++) {
    $node = $this
      ->drupalCreateNode([
      'uid' => $account
        ->id(),
    ]);
    $nodes[$node
      ->id()] = $node;
  }

  // Attempt to cancel account.
  $this
    ->drupalGet('user/' . $account
    ->id() . '/cancel');
  $this
    ->assertSession()
    ->pageTextContains('Are you sure you want to cancel your account?');
  $this
    ->assertSession()
    ->pageTextContains("Your account will be removed and all account information deleted. All of your content will be assigned to the {$this->config('user.settings')->get('anonymous')} user.");

  // Confirm account cancellation.
  $timestamp = time();
  $this
    ->submitForm([], 'Confirm');
  $this
    ->assertSession()
    ->pageTextContains('A confirmation request to cancel your account has been sent to your email address.');

  // Confirm account cancellation request.
  $this
    ->drupalGet("user/" . $account
    ->id() . "/cancel/confirm/{$timestamp}/" . user_pass_rehash($account, $timestamp));
  $user_storage
    ->resetCache([
    $account
      ->id(),
  ]);
  $this
    ->assertNull($user_storage
    ->load($account
    ->id()), 'User is not found in the database.');

  // Confirm that user's content has been attributed to anonymous user.
  $node_storage
    ->resetCache(array_keys($nodes));
  $test_nodes = $node_storage
    ->loadMultiple(array_keys($nodes));
  foreach ($test_nodes as $test_node) {
    $this
      ->assertEquals(0, $test_node
      ->getOwnerId(), 'Node ' . $test_node
      ->id() . ' of the user has been attributed to anonymous user.');
    $this
      ->assertTrue($test_node
      ->isPublished());
  }
}