You are here

function UserpointsAPITestCase::testExpiration in User Points 7.2

Same name and namespace in other branches
  1. 7 tests/userpoints_api.test \UserpointsAPITestCase::testExpiration()

File

./userpoints.test, line 171
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.
  try {
    userpoints_grant_points('bad_time', $points, 'userpoints', $this->non_admin_user->uid)
      ->setExpiryDate($bad_time)
      ->save();
    $this
      ->fail(t('API succesfully blocked an entry with a string as the expiry date'));
  } catch (UserpointsInvalidArgumentException $e) {
    $this
      ->pass(t('API succesfully blocked an entry with a string as the expiry date'));
  }
  foreach ($times as $key => $time) {
    $transaction = userpoints_grant_points($key, $points, 'userpoints', $this->non_admin_user->uid)
      ->setExpiryDate($time['time']);
    $transaction
      ->save();
    $this
      ->assertTrue((bool) $transaction
      ->getTxnId(), 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."));
    $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);
  $transaction = userpoints_grant_points('must_expire', $expire_points, 'userpoints', $this->non_admin_user->uid)
    ->setExpiryDate(REQUEST_TIME - 100);
  $transaction
    ->save();
  $this
    ->assertTrue((bool) $transaction
    ->getTxnId(), t("API succesfully added points for expiration"));
  $transaction = userpoints_grant_points('must_not_expire', $keep_points, 'userpoints', $this->non_admin_user->uid)
    ->setExpiryDate(REQUEST_TIME + 10000);
  $transaction
    ->save();
  $this
    ->assertTrue((bool) $transaction
    ->getTxnId(), 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.'));
}