You are here

function UserpointsTestCase::testExpiration in User Points 6

File

tests/userpoints_api.test, line 212

Class

UserpointsTestCase

Code

function testExpiration() {
  global $user;

  //clear the table before we begin
  db_query("TRUNCATE TABLE {userpoints}");
  db_query("TRUNCATE TABLE {userpoints_txn}");
  $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'] = 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' => $user->uid,
    'points' => $points,
    'expirydate' => $bad_time,
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertTrue($return['status'] == FALSE, t(print_r($return, TRUE) . "API succesfully blocked an entry with a string as the expiry date"));
  foreach ($times as $time) {
    $params = array(
      'uid' => $user->uid,
      'points' => $points,
      'expirydate' => $time['time'],
    );
    $return = userpoints_userpointsapi($params);
    $this
      ->assertTrue($return['status'] == TRUE, 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 = %d AND points = %d AND expirydate = %d";
    $db_points = (int) db_result(db_query($sql, $user->uid, $points, $time['time']));
    $this
      ->assertTrue($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 */
    $sql1 = "SELECT points FROM {userpoints} WHERE uid=%d";
    $db_points = (int) db_result(db_query($sql1, $user->uid));
    $this
      ->assertTrue($db_points == $sum_points, t($time['string'] . "Successfully verified that the summary table was updated"));
  }

  // Clear the state again.
  db_query("TRUNCATE TABLE {userpoints}");
  db_query("TRUNCATE TABLE {userpoints_txn}");
  userpoints_get_current_points(NULL, NULL, TRUE);

  // Set a default expire time.
  variable_set(USERPOINTS_EXPIREON_DATE, 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' => $user->uid,
    'points' => $expire_points,
    'expirydate' => time() - 1000,
    'operation' => 'must_expire',
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertTrue($return['status'] == TRUE, t("API succesfully added points for expiration"));

  // Load timestamp of the first transaction.
  $time_stamp = db_result(db_query("SELECT time_stamp FROM {userpoints_txn} WHERE operation = 'must_expire'"));
  $params = array(
    'uid' => $user->uid,
    'points' => $keep_points,
    'expirydate' => time() + 10000,
    'operation' => 'must_not_expire',
  );
  userpoints_userpointsapi($params);
  $this
    ->assertTrue($return['status'] == TRUE, t("API succesfully added points for expiration"));

  /* Call cron to check expiration */
  userpoints_cron();

  /* Check the user points removed or not if the point was expiration */
  $sql = "SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d";
  $db_points = (int) db_result(db_query($sql, $user->uid));
  $this
    ->assertTrue($db_points == $keep_points, t("Successfully removed expired points from the txn table."));
  $sql = "SELECT points FROM {userpoints} WHERE uid=%d";
  $db_points = (int) db_result(db_query($sql, $user->uid));
  $this
    ->assertTrue($db_points == $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_result(db_query("SELECT expirydate FROM {userpoints_txn} WHERE operation = 'expiry'"));
  $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_result(db_query('SELECT time_stamp FROM {userpoints_txn} WHERE expired = 1'));
  $this
    ->assertEqual($time_stamp, $updated_time_stamp, t('Time stamp of expired transaction was not changed.'));
}