View source
<?php
class UserpointsTestCase extends DrupalWebTestCase {
function getInfo() {
return array(
'name' => t('Userpoints API '),
'description' => t('Tests the core API for proper inserts & updates to the database tables,
moderation, expiration, as well as permission checks'),
'group' => t('Userpoints'),
);
}
function setUp() {
parent::setUp('userpoints');
global $admin_user;
$admin_user = $this
->drupalCreateUser(array(
'administer userpoints',
));
$non_admin_user = $this
->drupalCreateUser();
$this
->drupalLogin($non_admin_user);
}
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"));
}
function testParamsArrayCall() {
global $user;
global $admin_user;
$points = rand();
$params = array(
'points' => $points,
);
$this
->assertTrue(userpoints_userpointsapi($params) == TRUE, t('API call using a params array responded with successful grant of points'));
$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'));
$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'));
$params = array(
'points' => $points,
'uid' => $admin_user->uid,
);
$this
->assertTrue(userpoints_userpointsapi($params) == TRUE, t('Successfully granted points to admin user'));
$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'));
$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'));
$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'));
$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'));
}
function testExpiration() {
global $user;
db_query("TRUNCATE TABLE {userpoints}");
db_query("TRUNCATE TABLE {userpoints_txn}");
$points = rand(1, 100);
$sum_points = 0;
$times['valid']['time'] = rand(1, 10000);
$times['valid']['string'] = '(expirydate = random number)';
$times['expire']['time'] = time();
$times['expire']['string'] = '(expirydate = today)';
$times['null']['time'] = NULL;
$times['null']['string'] = '(expirydate = NULL)';
$bad_time = 'test string';
$params = array(
'uid' => $user->uid,
'points' => $points,
'expirydate' => $bad_time,
);
$return = userpoints_userpointsapi($params);
$this
->assertTrue($return['status'] == FALSE, t(print_r($return, TRUE) . "API succesfully blocked an entry with a string as the expiry date"));
foreach ($times as $time) {
$params = array(
'uid' => $user->uid,
'points' => $points,
'expirydate' => $time['time'],
);
$return = userpoints_userpointsapi($params);
$this
->assertTrue($return['status'] == TRUE, t($time['string'] . " API responded with a successful grant of points"));
$sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %d AND expirydate = %d";
$db_points = (int) db_result(db_query($sql, $user->uid, $points, $time['time']));
$this
->assertTrue($db_points == $points, t($time['string'] . "Successfully verified points in the txn table."));
if ($db_points == $points) {
$sum_points = $sum_points + $points;
}
$sql1 = "SELECT points FROM {userpoints} WHERE uid=%d";
$db_points = (int) db_result(db_query($sql1, $user->uid));
$this
->assertTrue($db_points == $sum_points, t($time['string'] . "Successfully verified that the summary table was updated"));
}
db_query("TRUNCATE TABLE {userpoints}");
db_query("TRUNCATE TABLE {userpoints_txn}");
userpoints_get_current_points(NULL, NULL, TRUE);
variable_set(USERPOINTS_EXPIREON_DATE, time() + 5000);
$keep_points = rand(1, 100);
$expire_points = rand(1, 100);
$params = array(
'uid' => $user->uid,
'points' => $expire_points,
'expirydate' => time() - 1000,
'operation' => 'must_expire',
);
$return = userpoints_userpointsapi($params);
$this
->assertTrue($return['status'] == TRUE, t("API succesfully added points for expiration"));
$time_stamp = db_result(db_query("SELECT time_stamp FROM {userpoints_txn} WHERE operation = 'must_expire'"));
$params = array(
'uid' => $user->uid,
'points' => $keep_points,
'expirydate' => time() + 10000,
'operation' => 'must_not_expire',
);
userpoints_userpointsapi($params);
$this
->assertTrue($return['status'] == TRUE, t("API succesfully added points for expiration"));
userpoints_cron();
$sql = "SELECT SUM(points) FROM {userpoints_txn} WHERE uid = %d";
$db_points = (int) db_result(db_query($sql, $user->uid));
$this
->assertTrue($db_points == $keep_points, t("Successfully removed expired points from the txn table."));
$sql = "SELECT points FROM {userpoints} WHERE uid=%d";
$db_points = (int) db_result(db_query($sql, $user->uid));
$this
->assertTrue($db_points == $keep_points, t("Successfully removed expired points from the summary table."));
$expired_time = db_result(db_query("SELECT expirydate FROM {userpoints_txn} WHERE operation = 'expiry'"));
$this
->assertEqual($expired_time, 0, t('Expiry userpoints transaction does not expire.'));
$updated_time_stamp = db_result(db_query('SELECT time_stamp FROM {userpoints_txn} WHERE expired = 1'));
$this
->assertEqual($time_stamp, $updated_time_stamp, t('Time stamp of expired transaction was not changed.'));
}
function testDefaultExpireDate() {
global $admin_user;
$this
->drupalLogin($admin_user);
$date = time() + 100000;
$date_array = array(
'month' => date('n', $date),
'day' => date('d', $date),
'year' => date('Y', $date),
);
$edit = array(
'userpoints_expireon_date[month]' => $date_array['month'],
'userpoints_expireon_date[day]' => $date_array['day'],
'userpoints_expireon_date[year]' => $date_array['year'],
);
$this
->drupalPost('admin/settings/userpoints', $edit, 'Save configuration');
$database_date = variable_get('userpoints_expireon_date', FALSE);
$this
->assertEqual($database_date['day'], $date_array['day']);
$this
->assertEqual($database_date['month'], $date_array['month']);
$this
->assertEqual($database_date['year'], $date_array['year']);
$expiry_date = userpoints_get_default_expiry_date();
$this
->assertEqual($expiry_date, userpoints_date_to_timestamp($date_array));
}
function testGetDefaultTid() {
$vid = userpoints_get_vid();
$term_name = $this
->randomName(10);
$desc = $this
->randomName(10);
$term = array(
'name' => $term_name,
'description' => $desc,
'vid' => $vid,
);
taxonomy_save_term($term);
$admin_user = $this
->drupalCreateUser(array(
'administer userpoints',
));
$this
->drupalLogin($admin_user);
$edit = array(
'userpoints_category_default_tid' => $term['tid'],
);
$this
->drupalPost('admin/settings/userpoints', $edit, 'Save configuration');
$tid = userpoints_get_default_tid();
$this
->assertTrue($tid == $term['tid'], t("Successfully retrieved default tid = %d.", array(
'%d' => $tid,
)));
$api_vid = userpoints_get_vid();
$this
->assertEqual($api_vid, $vid, t("Successfully retrieved default vid %d.", array(
'%d' => $vid,
)));
}
function testUserpermissions() {
$username = 'test';
$points = 10;
$admin_user = $this
->drupalCreateUser(array(
'administer userpoints',
));
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/user/userpoints');
$content = $this
->drupalGetContent();
$content = strstr($content, 'Access denied');
$this
->assertTrue($content == FALSE, t("Successful navigated to the page modify points"));
$edit = array(
'txn_user' => $admin_user->name,
'points' => $points,
);
$this
->drupalPost('admin/user/userpoints/add', $edit, 'Save');
$sql = "SELECT points FROM {userpoints_txn} WHERE uid=%d AND points=%d";
$db_points = (int) db_result(db_query($sql, $admin_user->uid, $points));
$this
->assertTrue($db_points == $points, t("Successful verified that points were added into database."));
$this
->drupalLogout();
$view_user = $this
->drupalCreateUser(array(
'view userpoints',
));
$this
->drupalLogin($view_user);
$this
->drupalGet('admin/user/userpoints');
$content = $this
->drupalGetContent();
$content = strstr($content, 'Access denied');
$this
->assertTrue(is_string($content), t("Successful verified that a user without admin userpoints permissions can not modify points."));
$sql = "SELECT points FROM {userpoints_txn} WHERE uid=%d AND points=%d";
$db_points = (int) db_result(db_query($sql, $view_user->uid, $points));
$this
->assertTrue($db_points != $points, t("Successful verified that points do not add into database."));
$this
->drupalLogout();
}
function testModeration() {
global $user;
$points = rand(1, 100);
$params = array(
'uid' => $user->uid,
'points' => $points,
'moderate' => TRUE,
);
$return = userpoints_userpointsapi($params);
$this
->assertTrue($return['status'] == TRUE, t("1. (moderate=TRUE) API responded with successful grant of points"));
$sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %d AND status = 1";
$db_points = (int) db_result(db_query($sql, $user->uid, $points));
$this
->assertTrue($db_points == $points, t("1. (moderate=TRUE) Successfully verified points in the txn table and waiting moderation."));
$sql1 = "SELECT points FROM {userpoints} WHERE uid=%d";
$db_points = (int) db_result(db_query($sql1, $user->uid));
$this
->assertTrue($db_points == 0, t("1. (moderate=TRUE) Successfully verified that points were added and the summary table was not updated."));
$params = array(
'uid' => $user->uid,
'points' => $points,
'moderate' => FALSE,
);
$return = userpoints_userpointsapi($params);
$this
->assertTrue($return['status'] == TRUE, t("7. (moderate=FALSE) API responded with successful grant of points"));
$sql = "SELECT points FROM {userpoints_txn} WHERE uid = %d AND points = %d AND status = 0";
$db_points = (int) db_result(db_query($sql, $user->uid, $points));
$this
->assertTrue($db_points == $points, t("7. (moderate=FALSE) Successfully verified points in the txn table and NOT waiting moderation."));
}
function testGetCategories() {
$cats = userpoints_get_categories();
$this
->assertTrue(is_array($cats), 'Successfully verified userpoints_get_categories() returned an array');
}
}