function UserpointsTestCase::testBasicCall in User Points 6
File
- tests/
userpoints_api.test, line 38
Class
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
$sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d";
$db_points = (int) db_result(db_query($sql, $user->uid, $points));
$this
->assertTrue($db_points === $points, t('Successfully verified points in the txn table'));
//Check that the transaction table and the summary table match
$sql = "SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d";
$txn_points = (int) db_result(db_query($sql, $user->uid));
$sql = "SELECT SUM(points) from {userpoints} WHERE uid = %d";
$up_points = (int) db_result(db_query($sql, $user->uid));
$this
->assertTrue($txn_points === $up_points, 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
$sql = "SELECT points from {userpoints_txn} WHERE uid = %d AND points = %d";
$db_points = (int) db_result(db_query($sql, $user->uid, $points));
$this
->assertTrue($db_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;
}
if ($points > 0) {
$maxpoints = $maxpoints + $points;
}
$sumpoints = $sumpoints + $points;
userpoints_userpointsapi($points);
}
//Check the summary table to make sure everything is still kosher.
$sql = "SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d";
$txn_points = (int) db_result(db_query($sql, $user->uid));
$sql = "SELECT SUM(points) from {userpoints} WHERE uid = %d";
$up_points = (int) db_result(db_query($sql, $user->uid));
$this
->assertTrue($txn_points === $up_points, t('Sum of transactions matches the caching table'));
$this
->assertTrue($up_points === $sumpoints, t('Caching table matches testing code after !recs point records totaling !points points', array(
'!recs' => $i,
'!points' => $sumpoints,
)));
$this
->assertTrue($sumpoints == userpoints_get_current_points(), t("userpoints_get_current_points() returned correct point value"));
$this
->assertTrue($maxpoints == userpoints_get_max_points(), t("userpoints_get_max_points() returned correct point value"));
}