You are here

function UserpointsAPITestCase::testParamsArrayCall in User Points 7

Call the api functions with an array.

File

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

Class

UserpointsAPITestCase
API Tests.

Code

function testParamsArrayCall() {
  $points = rand();

  // Assert that the use of a params array with simply points in it works.
  $params = array(
    'points' => $points,
    'uid' => $this->non_admin_user->uid,
  );
  $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.
  $this
    ->assertTrue($this
    ->getPoints($this->non_admin_user->uid, NULL, TRUE) == $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' => '',
    'uid' => $this->non_admin_user->uid,
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertFalse($return['status'], t('API successfully prevented null points from being added'));
  $params = array(
    'points' => 'abcd',
    'uid' => $this->non_admin_user->uid,
  );
  $return = userpoints_userpointsapi($params);
  $this
    ->assertFalse($return['status'], 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' => $this->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.
  $this
    ->assertTrue($this
    ->getPoints($this->non_admin_user->uid, NULL, TRUE) == $points, t('Successfully verified points in the txn table'));

  // Attempt to award points to a non-existent user.
  $sql = "SELECT MAX(uid) FROM {users}";
  $nonuid = db_query($sql)
    ->fetchField() + 1;
  $params = array(
    'points' => $points,
    'uid' => $nonuid,
  );
  $ret = userpoints_userpointsapi($params);
  $this
    ->assertFalse($ret['status'], t('Successfully blocked points given to a non-existent user'));

  /**
   * @todo: This is not actually implemented, just passed the tests because it
   *        has set the 'uid' key instead of 'tid'.
  // Attempt to award points to a non-existent term.
  $maxtid = "SELECT MAX(tid) from {taxonomy_term_data}";
  $nontid = db_query($sql)->fetchField() + 1;
  $params = array(
    'points' => $points,
    'tid' => $nontid,
  );

  $ret = userpoints_userpointsapi($params);
  $this->assertFalse($ret['status'], 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 = :description AND operation = :operation";
  $db_point_rec = db_query($sql, array(
    ':description' => $description,
    ':operation' => $operation,
  ))
    ->fetchAssoc();
  $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'));
}