You are here

function InactiveUserTest::testInactiveUserBlocking in Inactive User 7

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

Check inactive user blocking and notifications are working

File

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

Class

InactiveUserTest
Inactive user module testcase.

Code

function testInactiveUserBlocking() {

  // Change user and admin blocking options:
  // Notify user about being blocked after one week of inactivity.
  // Block user after two weeks of inactivity.
  // Notify both about blocking: user and admin
  $settings = array(
    'inactive_user_admin_email' => 'test@email.com',
    'inactive_user_auto_block_warn' => '604800',
    'inactive_user_auto_block' => '1209600',
    'inactive_user_notify_block' => '1',
    'inactive_user_notify_block_admin' => '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 blocked.
  $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 account will be blocked.'));

  // 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 blocked, but blocking 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 blocked.
  $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 account will be blocked.'));

  // Inactive_user keeps the time of blocking 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 blocked.
  db_update('inactive_users')
    ->fields(array(
    'warned_user_block_timestamp' => 604800 + 3600,
  ))
    ->condition('uid', $inactive->uid)
    ->execute();

  // 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 blocked 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 blocked account.'));

  // Verify the user has been blocked. user_load does not work with blocked
  // accounts.
  $status = db_select('users', 'u')
    ->fields('u', array(
    'status',
  ))
    ->condition('u.uid', $inactive->uid)
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($status, 0, t('Inactive user %name has been blocked.', array(
    '%name' => $inactive->name,
  )));
}