You are here

function InactiveUserTest::testInactiveUserDeleting in Inactive User 6

Same name and namespace in other branches
  1. 7 inactive_user.test \InactiveUserTest::testInactiveUserDeleting()

Check inactive user deleting and notifications are working

File

./inactive_user.test, line 219
Test the basic functions of the Inactive User module.

Class

InactiveUserTest
Inactive user module testcase.

Code

function testInactiveUserDeleting() {

  // Change user and admin deleting options:
  // Notify user about being deleted after one week of inactivity.
  // Delete user after two weeks of inactivity.
  // Notify both about deleting: user and admin
  // Do not delete users with content.
  $settings = array(
    'inactive_user_admin_email' => 'test@email.com',
    'inactive_user_auto_delete_warn' => '604800',
    'inactive_user_auto_delete' => '1209600',
    'inactive_user_notify_delete' => '1',
    'inactive_user_notify_delete_admin' => '1',
    'inactive_user_preserve_content' => '1',
  );
  $this
    ->inactiveUserSettings($settings);

  // Create an inactive user for more than a week.
  $inactive = $this
    ->drupalCreateInactiveUser(604800 + 3600);

  // Perform inactive validations
  $this
    ->checkInactiveAccounts();

  // One email should have been sent to the inactive user about being deleted.
  $emails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(count($emails), 1, t('Only one email has been sent for this inactivity validation.'));

  // Get the last email, and verify it was sent to user's email address
  $notification = array_pop($emails);
  $this
    ->assertEqual($notification['to'], $inactive->mail, t('User has been notified that its accout will be deleted.'));

  // Create an inactive user for more than two weeks
  $inactive = $this
    ->drupalCreateInactiveUser(1209600 + 3600);

  // Perform inactive validations
  $this
    ->checkInactiveAccounts();

  // One more email should have been sent, notifying the user its account will
  // be deleted, but deleting operation will not happen because the user was
  // notified at the time of 'notify'. The notification period should last for
  // a whole week before the account is deleted.
  $emails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(count($emails), 2, t('One new email have been sent for this inactivity validation.'));

  // Get the last email, and verify it was sent to user's email address
  $notification = array_pop($emails);
  $this
    ->assertEqual($notification['to'], $inactive->mail, t('User has been notified that its accout will be deleted.'));

  // Inactive_user keeps the time of deleting notification in a separate table
  // as the timestamp of the operation, need to be moved back in time. We are
  // modifying the notification period so on next validation the account will
  // be deleted.
  db_query("UPDATE {inactive_users} SET warned_user_delete_timestamp = %d WHERE uid = %d", 604800 + 3600, $inactive->uid);

  // Perform inactive validations again
  $this
    ->checkInactiveAccounts();

  // Now, two more emails have been sent.
  $emails = $this
    ->drupalGetMails();
  $this
    ->assertEqual(count($emails), 4, t('Two new emails have been sent for this inactivity validation.'));

  // Get the other email, and verify it was sent to admin's email address
  $notification = array_pop($emails);
  $this
    ->assertEqual($notification['to'], $settings['inactive_user_admin_email'], t('Administrator has been notified of deleted accounts.'));

  // Admin has been notified that its account has been blocked.
  $notification = array_pop($emails);
  $this
    ->assertEqual($notification['to'], $inactive->mail, t('User has been notified of its deleted account.'));

  // Verify the user has been deleted.
  $status = db_result(db_query('SELECT uid FROM {users} WHERE uid = %d', $inactive->uid));
  $this
    ->assertEqual($status, NULL, t('Inactive user %name has been deleted.', array(
    '%name' => $inactive->name,
  )));
}