1 user.test UserCancelTestCase::testMassUserCancelByAdmin()

Create an administrative user and mass-delete other users.

File

core/modules/user/tests/user.test, line 1025
Tests for user.module.

Class

UserCancelTestCase
Test cancelling a user.

Code

function testMassUserCancelByAdmin() {
  config_set('system.core', 'user_cancel_method', 'user_cancel_reassign');
  // Enable account cancellation notification.
  config_set('system.core', 'user_mail_status_canceled_notify', TRUE);

  // Create administrative user.
  $admin_user = $this->backdropCreateUser(array('administer users'));
  $this->backdropLogin($admin_user);

  // Create some users.
  $users = array();
  for ($i = 0; $i < 3; $i++) {
    $account = $this->backdropCreateUser(array());
    $users[$account->uid] = $account;
  }

  // Cancel user accounts, including own one.
  $edit = array();
  $edit['action'] = 'user_cancel_user_action';
  $edit['bulk_form[0]'] = TRUE; // Try to cancel uid 1.
  $edit['bulk_form[1]'] = TRUE; // Cancel the created admin user.
  $position = 1;
  foreach ($users as $account) {
    $position++;
    $edit['bulk_form[' . $position . ']'] = TRUE;
  }
  $this->backdropPost('admin/people', $edit, t('Execute'));
  $this->assertText(t('Are you sure you want to cancel these user accounts?'), 'Confirmation form to cancel accounts displayed.');
  $this->assertText(t('When cancelling these accounts'), 'Allows to select account cancellation method.');
  $this->assertText(t('Require email confirmation to cancel account.'), 'Allows to send confirmation mail.');
  $this->assertText(t('Notify user when account is canceled.'), 'Allows to send notification mail.');

  // Confirm deletion.
  $this->backdropPost(NULL, NULL, t('Cancel accounts'));
  $status = TRUE;
  foreach ($users as $account) {
    $status = $status && (strpos($this->content, t('%name has been deleted.', array('%name' => $account->name))) !== FALSE);
    $status = $status && !user_load($account->uid, TRUE);
  }
  $this->assertTrue($status, 'Users deleted and not found in the database.');

  // Ensure that admin account was not cancelled.
  $this->assertText(t('A confirmation request to cancel your account has been sent to your email address.'), 'Account cancellation request mailed message displayed.');
  $admin_user = user_load($admin_user->uid);
  $this->assertTrue($admin_user->status == 1, 'Administrative user is found in the database and enabled.');

  // Verify that uid 1's account was not cancelled.
  $user1 = user_load(1, TRUE);
  $this->assertEqual($user1->status, 1, 'User #1 still exists and is not blocked.');
}