You are here

function UserpointsAPITestCase::testExpiration in User Points 7

Same name and namespace in other branches
  1. 7.2 userpoints.test \UserpointsAPITestCase::testExpiration()

File

tests/userpoints_api.test, line 311
Contains test classes for userpoints module.

Class

UserpointsAPITestCase
API Tests.

Code

function testExpiration() {
  $points = rand(1, 100);
  $sum_points = 0;

  // Create various time tests.
  $times['valid']['time'] = rand(1, 10000);
  $times['valid']['string'] = '(expirydate = random number)';
  $times['expire']['time'] = REQUEST_TIME;
  $times['expire']['string'] = '(expirydate = today)';
  $times['null']['time'] = NULL;
  $times['null']['string'] = '(expirydate = NULL)';
  $bad_time = 'test string';

  // First lets check to make sure it is blocking bad times.
  $params = array(
    'uid' => $this->non_admin_user->uid,
    'points' => $points,
    'expirydate' => $bad_time,
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertFalse($return['status'], t(print_r($return, TRUE) . "API succesfully blocked an entry with a string as the expiry date"));
  foreach ($times as $time) {
    $params = array(
      'uid' => $this->non_admin_user->uid,
      'points' => $points,
      'expirydate' => $time['time'],
    );
    $return = userpoints_userpointsapi($params);
    $this
      ->assertTrue($return['status'], t($time['string'] . " API responded with a successful grant of points"));

    // Check the database to ensure the points were properly saved.
    $sql = "SELECT points FROM {userpoints_txn} WHERE uid = :uid AND points = :points AND expirydate = :date";
    $db_points = (int) db_query($sql, array(
      ':uid' => $this->non_admin_user->uid,
      ':points' => $points,
      ':date' => (int) $time['time'],
    ))
      ->fetchField();
    $this
      ->assertEqual($db_points, $points, t($time['string'] . "Successfully verified points in the txn table."));
    if ($db_points == $points) {
      $sum_points = $sum_points + $points;
    }

    // Check update point to 'userpoints' table.
    $this
      ->assertEqual($this
      ->getPoints($this->non_admin_user->uid), $sum_points, t($time['string'] . "Successfully verified that the summary table was updated"));
  }

  // Clear the slate again.
  db_truncate('userpoints')
    ->execute();
  db_truncate('userpoints_txn')
    ->execute();

  // Set a default expire time.
  variable_set(USERPOINTS_EXPIREON_DATE, REQUEST_TIME + 5000);

  // Add two different points in, one to post immediately another to expire in the future.
  $keep_points = rand(1, 100);
  $expire_points = rand(1, 100);
  $params = array(
    'uid' => $this->non_admin_user->uid,
    'points' => $expire_points,
    'expirydate' => REQUEST_TIME - 1000,
    'operation' => 'must_expire',
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertTrue($return['status'], t("API succesfully added points for expiration"));
  $params = array(
    'uid' => $this->non_admin_user->uid,
    'points' => $keep_points,
    'expirydate' => REQUEST_TIME + 10000,
    'operation' => 'must_not_expire',
  );
  userpoints_userpointsapi($params);
  $this
    ->assertTrue($return['status'], t("API succesfully added points for expiration"));

  // Load timestamp of the first transaction.
  $time_stamp = db_query('SELECT time_stamp FROM {userpoints_txn} WHERE operation = :op', array(
    ':op' => 'must_expire',
  ))
    ->fetchField();

  // Call cron to check expiration.
  drupal_cron_run();

  // Check the user points removed or not if the point was expiration.
  $this
    ->assertEqual($this
    ->getTxnPoints($this->non_admin_user->uid, NULL, TRUE), $keep_points, t("Successfully removed expired points from the txn table."));
  $this
    ->assertEqual($this
    ->getPoints($this->non_admin_user->uid), $keep_points, t("Successfully removed expired points from the summary table."));

  // Load the expiry transaction from the database and verify that it does
  // not expire.
  $expired_time = db_query('SELECT expirydate FROM {userpoints_txn} WHERE operation = :expiry', array(
    ':expiry' => 'expiry',
  ))
    ->fetchField();
  $this
    ->assertEqual($expired_time, 0, t('Expiry userpoints transaction does not expire.'));

  // Load expired transaction and verify that time_stamp was not updated.
  $updated_time_stamp = db_query('SELECT time_stamp FROM {userpoints_txn} WHERE expired = 1')
    ->fetchField();
  $this
    ->assertEqual($time_stamp, $updated_time_stamp, t('Time stamp of expired transaction was not changed.'));
}