You are here

function UserpointsTestCase::testParamsArrayCall in User Points 6

File

tests/userpoints_api.test, line 114

Class

UserpointsTestCase

Code

function testParamsArrayCall() {
  global $user;
  global $admin_user;
  $points = rand();

  //Assert that the use of a params array with simply points in it works.
  $params = array(
    'points' => $points,
  );
  $this
    ->assertTrue(userpoints_userpointsapi($params) == TRUE, t('API call using a params array responded with successful grant of points'));

  //Check the Database to make sure the points made it there
  $sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d";
  $db_points = db_result(db_query($sql, $user->uid, $points));
  $this
    ->assertTrue($db_points == $points, t('Successfully verified points in the txn table'));

  //Test to ensure that calling it with no points returns FALSE indicating an error
  $params = array(
    'points' => '',
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertTrue($return['status'] == FALSE, t('API successfully prevented null points from being added'));
  $params = array(
    'points' => 'abcd',
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertTrue($return['status'] == FALSE, t('API successfully prevented non-numeric points from being added'));

  //Award points to admin user and test to ensure they were awarded to the correct user
  $params = array(
    'points' => $points,
    'uid' => $admin_user->uid,
  );
  $this
    ->assertTrue(userpoints_userpointsapi($params) == TRUE, t('Successfully granted points to admin user'));

  //Check the Database to make sure the points made it there
  $sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d";
  $db_points = db_result(db_query($sql, $admin_user->uid, $points));
  $this
    ->assertTrue($db_points == $points, t('Successfully verified points in the txn table'));

  //Attempt to award points to a non-existent user
  $sql = "SELECT uid FROM {users} ORDER BY uid DESC";
  $nonuid = db_result(db_query_range($sql, 0, 1)) + 1;
  $params = array(
    'points' => $points,
    'uid' => $nonuid,
  );
  $ret = userpoints_userpointsapi($params);
  $this
    ->assertTrue($ret['status'] == FALSE, t('Successfully blocked points given to a non-existent user'));

  //attempt to award points to a non-existent term
  $maxtid = "SELECT tid from {term_data} ORDER BY tid DESC";
  $nontid = db_result(db_query_range($sql, 0, 1)) + 1;
  $params = array(
    'points' => $points,
    'uid' => $nontid,
  );
  $ret = userpoints_userpointsapi($params);
  $this
    ->assertTrue($ret['status'] == FALSE, t('Successfully blocked points given to a non-existent tid'));

  //Test various aspects of the API to ensure the DB is being updated successfully
  $points = rand(1, 500);
  $description = $this
    ->randomName();
  $operation = $this
    ->randomName();
  $params = array(
    'points' => $points,
    'description' => $description,
    'operation' => $operation,
  );
  $ret = userpoints_userpointsapi($params);
  $sql = "SELECT description, operation, reference FROM {userpoints_txn} WHERE description = '%s' AND operation = '%s'";
  $db_point_rec = db_fetch_array(db_query($sql, $description, $operation));
  $this
    ->assertTrue($db_point_rec['description'] == $description, t('Point description successfully verified in DB'));
  $this
    ->assertTrue($db_point_rec['operation'] == $operation, t('Point event successfully verified in DB'));
}