function UserpointsTestCase::testBasicCall in User Points 6
File
- tests/userpoints_api.test, line 38
Class
- UserpointsTestCase
Code
function testBasicCall() {
global $user;
$points = (int) rand(1, 500);
$sumpoints = $points;
$maxpoints = $points;
$return = userpoints_userpointsapi($points);
$this
->assertTrue($return['status'] == TRUE, t('API responded with successful grant of points'));
$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'));
$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'));
$points = -rand(1, 500);
$sumpoints = $sumpoints + $points;
userpoints_userpointsapi($points);
$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'));
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);
}
$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"));
}