You are here

function UserpointsAPITestCase::testBasicCall in User Points 7

Call userpoints_userpointsapi() with just points.

File

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

Class

UserpointsAPITestCase
API Tests.

Code

function testBasicCall() {
  global $user;
  $points = (int) rand(1, 500);
  $sumpoints = $points;

  // NOTE: increment max points with all positive point changes, tests userpoints_get_max_points.
  $maxpoints = $points;

  // Test the a basic API call.
  $return = userpoints_userpointsapi($points);
  $this
    ->assertTrue($return['status'] == TRUE, t('API responded with successful grant of points'));

  // Check the database to ensure the point were properly saved.
  $this
    ->assertTrue($this
    ->getTxnPoints($user->uid, $points) === $points, t('Successfully verified points in the txn table'));

  // Check that the transaction table and the summary table match.
  $this
    ->assertTrue($this
    ->getTxnPoints($user->uid, NULL, TRUE) === $this
    ->getPoints($user->uid, NULL, TRUE), t('Sum of transactions match total points for user'));

  // Add negative points to the initial value and check the values.
  $points = -rand(1, 500);
  $sumpoints = $sumpoints + $points;
  userpoints_userpointsapi($points);

  // Check the database to ensure the negative point value was properly saved.
  $this
    ->assertTrue($this
    ->getTxnPoints($user->uid, $points) === $points, t('Successfully verified negative points in the txn table'));

  // Now test to make sure the transaction and and caching table stay in sync.
  // Also test userpoints_get_max_points and userpoints_get_current_points.
  for ($i = 0; $i <= rand(1, 50); $i++) {
    $points = rand(1, 500);
    if (rand() & 1) {
      $points = -$points;
    }
    $sumpoints = $sumpoints + $points;
    if ($sumpoints > $maxpoints) {
      $maxpoints = $sumpoints;
    }
    userpoints_userpointsapi($points);
  }

  // Check the summary table to make sure everything is still kosher.
  $this
    ->assertEqual($this
    ->getTxnPoints($user->uid, NULL, TRUE), $this
    ->getPoints($user->uid, NULL, TRUE));
  $this
    ->assertEqual($this
    ->getPoints($user->uid, NULL, TRUE), $sumpoints);
  $this
    ->assertEqual($sumpoints, userpoints_get_current_points());
  $this
    ->assertEqual($maxpoints, userpoints_get_max_points());
}