You are here

function UserpointsTestCase::testUserpermissions in User Points 6

Test user permissions

File

tests/userpoints_api.test, line 395

Class

UserpointsTestCase

Code

function testUserpermissions() {
  $username = 'test';
  $points = 10;

  /** check permission with admin user **/
  $admin_user = $this
    ->drupalCreateUser(array(
    'administer userpoints',
  ));
  $this
    ->drupalLogin($admin_user);

  /*check access page*/
  $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"));

  /* check modify points */
  $edit = array(
    'txn_user' => $admin_user->name,
    'points' => $points,
  );
  $this
    ->drupalPost('admin/user/userpoints/add', $edit, 'Save');

  /* check database */
  $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."));

  /* logout and change user */
  $this
    ->drupalLogout();

  /* check permission with view user */
  $view_user = $this
    ->drupalCreateUser(array(
    'view userpoints',
  ));
  $this
    ->drupalLogin($view_user);

  /*check access page*/
  $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."));

  /* check modify points */

  //This part of the test should be enabled and it ensures that a POST

  //won't add points (regardless of what the HTML says). If it is uncommented

  //it will throw errors because the POST fields don't exist (which is good).

  /*
      $edit = array(
        'txn_user' => $view_user->name,
        'points' => $points,
      );
      $this->drupalPost('admin/user/userpoints/add', $edit, 'Save');
  */

  /* check database */
  $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();
}