You are here

function InactiveUserTest::testInactiveUserWithContentDeleting in Inactive User 7

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

Check inactive user (with content) deleting and notifications are working

File

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

Class

InactiveUserTest
Inactive user module testcase.

Code

function testInactiveUserWithContentDeleting() {

  // 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);

  // Create a node for this user.
  $this
    ->drupalCreateNode(array(
    'uid' => $inactive->uid,
  ));

  // 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), 0, t('Users with content should not be advised about deleting their account.'));

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

  // Create a node for this user.
  $this
    ->drupalCreateNode(array(
    'uid' => $inactive->uid,
  ));

  // 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), 0, t('Users with content should not be advised about deleting their account.'));

  // 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_update('inactive_users')
    ->fields(array(
    'warned_user_delete_timestamp' => 604800 + 3600,
  ))
    ->condition('uid', $inactive->uid)
    ->execute();

  // Verify the user is protected.
  $protected = db_select('inactive_users', 'ia')
    ->fields('ia', array(
    'protected',
  ))
    ->condition('uid', $inactive->uid)
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($protected, 1, t('Inactive user %name is protected because it owns content..', array(
    '%name' => $inactive->name,
  )));

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

  // Now, two more emails have been sent.
  $emails = $this
    ->drupalGetMails();
  $this
    ->pass(count($emails));
  $this
    ->assertEqual(count($emails), 0, t('Inactive users with content are protected and are not deleted, neither generates notifications.'));

  // Verify the user has not been deleted.
  $status = db_select('users', 'u')
    ->fields('u', array(
    'uid',
  ))
    ->condition('u.uid', $inactive->uid)
    ->execute()
    ->fetchField();
  $this
    ->assertEqual($status, $inactive->uid, t('Inactive user %name owning content has not been deleted.', array(
    '%name' => $inactive->name,
  )));
}