You are here

function ProfileModuleTestTextarea::_rolesApi in SimpleTest 6

Same name and namespace in other branches
  1. 5 tests/profile_module.test \ProfileModuleTestTextarea::_rolesApi()
1 call to ProfileModuleTestTextarea::_rolesApi()
ProfileModuleTestTextarea::testProfileSingle in tests/profile_module.test

File

tests/profile_module.test, line 126

Class

ProfileModuleTestTextarea

Code

function _rolesApi($op, $edit) {
  if ($op == 'delete') {
    $id = $edit['rid'];
    db_query('DELETE FROM {role} WHERE rid = %d', $id);
    db_query('DELETE FROM {permission} WHERE rid = %d', $id);

    // Update the users who have this role set:
    $result = db_query('SELECT DISTINCT(ur1.uid) FROM {users_roles} ur1 LEFT JOIN {users_roles} ur2 ON ur2.uid = ur1.uid WHERE ur1.rid = %d AND ur2.rid != ur1.rid', $id);
    $uid = array();
    while ($u = db_fetch_object($result)) {
      $uid[] = $u->uid;
    }
    if ($uid) {
      db_query('DELETE FROM {users_roles} WHERE rid = %d AND uid IN (%s)', $id, implode(', ', $uid));
    }

    // Users with only the deleted role are put back in the authenticated users pool.
    db_query('UPDATE {users_roles} SET rid = %d WHERE rid = %d', DRUPAL_AUTHENTICATED_RID, $id);
  }
  else {
    if ($op == 'add') {
      if (isset($edit['name'])) {
        db_query("INSERT INTO {role} (name) VALUES ('%s')", $edit['name']);
        $result = db_query("SELECT rid FROM {role} WHERE name = '%s'", $edit['name']);
        $rid = db_result($result);
        db_query("INSERT INTO {permission} (rid, perm) VALUES (%d, '%s')", $rid, $edit['perm']);
        return $rid;
      }
      else {
        return 0;
      }
    }
  }
}