You are here

function UserExpireTestCase::testUserExpire in User Expire 7

File

./user_expire.test, line 37
Tests for User expire module.

Class

UserExpireTestCase
@file Tests for User expire module.

Code

function testUserExpire() {
  $this
    ->assertTrue($this->basic_account->status, t('User account is currently enabled.'));
  user_expire_set_expiration($this->basic_account, REQUEST_TIME - 1);
  user_expire_expire_users(array(
    $this->basic_account,
  ));
  $this
    ->assertMailString('subject', 'Account expired', 1);
  $this
    ->assertMail('to', $this->basic_account->mail, 'Account expiration notification Mail sent.');
  $this
    ->assertFalse($this->basic_account->status, t('User account has been successfully disabled.'));

  // Admin sets expiry, it's saved properly.
  $this
    ->drupalLogin($this->admin_user);

  // Ensure the report is clear.
  $this
    ->drupalGet('admin/reports/expiring-users');
  $this
    ->assertNoText('0 sec from now', t('Processed expiration does not show in Expiring users report'));

  // Make them active again.
  $edit = array();
  $edit['status'] = 1;

  // And set the expiration to something passed.
  $edit['user_expiration'] = 1;
  $edit['user_expiration_date[month]'] = 8;
  $edit['user_expiration_date[day]'] = 18;
  $edit['user_expiration_date[year]'] = 2002;
  $this
    ->drupalPost("user/{$this->basic_account->uid}/edit", $edit, t('Save'));

  // Ensure it was re-activated.
  $this
    ->assertRaw('type="radio" id="edit-status-1" name="status" value="1" checked="checked" class="form-radio"', t('User account is currently enabled.'));

  // And the expiration was really really saved.
  $this
    ->assertRaw('expiration date is set to Sun, 08/18/2002 - 00:00.');
  $this
    ->drupalGet('admin/reports/expiring-users');
  $this
    ->assertRaw('0 sec from now', 'Expiration shows in Expiring users report');
  $this
    ->drupalLogout($this->admin_user);

  // User edits account, expiry is still set.
  $this
    ->drupalLogin($this->basic_account);
  $edit = array();
  $edit['pass[pass1]'] = $new_pass = $this
    ->randomName();
  $edit['pass[pass2]'] = $new_pass;
  $edit['current_pass'] = $this->basic_account->pass_raw;
  $this
    ->drupalPost("user/{$this->basic_account->uid}/edit", $edit, t('Save'));
  $this
    ->assertRaw(t("The changes have been saved."));
  $this
    ->drupalLogout($this->basic_account);

  // Admin looks again and expiry is still set.
  $this
    ->drupalLogin($this->admin_user);
  $this
    ->drupalGet('admin/reports/expiring-users');
  $this
    ->assertRaw('0 sec from now', 'Expiration shows in Expiring users report');

  // Cron runs, account is locked, removed from expiry.
  user_expire_cron();
  $this
    ->drupalGet('admin/reports/expiring-users');
  $this
    ->assertNoText('0 sec from now', t('Processed expiration does not show in Expiring users report'));
  $this
    ->drupalGet("user/{$this->basic_account->uid}/edit");
  $this
    ->assertRaw('type="radio" id="edit-status-0" name="status" value="0" checked="checked" class="form-radio"', t('User account is currently disabled.'));

  // Testing account expiry by role.
  // Create a role.
  $role_name = 'Rainbows';
  $edit = array(
    'name' => $role_name,
  );
  $this
    ->drupalPost('admin/people/permissions/roles', $edit, t('Add role'));
  $this
    ->assertText(t('The role has been added.'), 'The role has been added.');
  $role = user_role_load_by_name($role_name);
  $this
    ->assertTrue(is_object($role), 'The role was successfully retrieved from the database.');

  // Grant that role to the basic user.
  $edit = array();
  $edit['status'] = 1;

  // And definitely unset the expiration.
  $edit['user_expiration'] = FALSE;
  $edit['roles[' . $role->rid . ']'] = $role->rid;
  $this
    ->drupalPost("user/{$this->basic_account->uid}/edit", $edit, t('Save'));
  $this
    ->assertRaw('type="radio" id="edit-status-1" name="status" value="1" checked="checked" class="form-radio"', t('User account is currently enabled.'));

  // Confirm there are no per-user expiration records.
  $this
    ->drupalGet('admin/reports/expiring-users');
  $this
    ->assertNoText('0 sec from now', t('Processed expiration does not show in Expiring users report'));

  // Fake that their access time is 90 days and 2 seconds.
  // Be sure to use REQUEST_TIME because the query to identify uses
  // REQUEST_TIME and that value gets pretty old in the context of simpletest.
  db_query('UPDATE {users} SET access = :time WHERE uid = :uid', array(
    ':time' => REQUEST_TIME - 7776002,
    ':uid' => $this->basic_account->uid,
  ));

  // Set it to expire after 90 days of inactivity.
  $edit = array(
    'user_expire_' . $role->rid => 7776000,
  );
  $this
    ->drupalPost("admin/config/people/user-expire", $edit, t('Save configuration'));

  // Process it.
  user_expire_expire_by_role();

  // Ensure they are disabled.
  $this
    ->drupalGet("user/{$this->basic_account->uid}/edit");
  $this
    ->assertRaw('type="radio" id="edit-status-0" name="status" value="0" checked="checked" class="form-radio"', t('User account is currently disabled.'));

  // Ensure a brand new user is not blocked (i.e. access = 0).
  // Instead of making new account in setUp method, we are creating this
  // account here to make sure that brand new user is created after above test
  // its satisfying the tests.
  $new_basic_account = $this
    ->drupalCreateUser();

  // Set auth users to expire after 90 days of inactivity.
  $edit = array(
    'user_expire_' . DRUPAL_AUTHENTICATED_RID => 7776000,
  );
  $this
    ->drupalPost("admin/config/people/user-expire", $edit, t('Save configuration'));

  // Process it.
  user_expire_expire_by_role();

  // Ensure they are still enabled.
  $this
    ->drupalGet("user/{$new_basic_account->uid}/edit");
  $this
    ->assertRaw('type="radio" id="edit-status-1" name="status" value="1" checked="checked" class="form-radio"', t('New user account stays active.'));

  // Age the new user's created by 90+ days.
  db_query('UPDATE {users} SET created = :time WHERE uid = :uid', array(
    ':time' => REQUEST_TIME - 7776002,
    ':uid' => $new_basic_account->uid,
  ));

  // Process it.
  user_expire_expire_by_role();

  // Ensure they are disabled
  $this
    ->drupalGet("user/{$new_basic_account->uid}/edit");
  $this
    ->assertRaw('type="radio" id="edit-status-0" name="status" value="0" checked="checked" class="form-radio"', t('User account is currently disabled.'));
}