Otherwise, it can be very hard to test users with the lack of certain permissions.

db_query('DELETE FROM {role_permission}');

in the setUp method should work fine. Sorry I cannot create patches at the moment.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

boombatower’s picture

Status: Active » Postponed (maintainer needs more info)

If anything we should provide a convenience method to clear the permissions, but since the entire framework is setup to test from the "default" state that would be a large deviation.

Something like:

$this->clearPermissions();

?

cwgordon7’s picture

Status: Postponed (maintainer needs more info) » Active

That works too.


  /**
   * Clears the default permissions for the specified role.
   *
   * @param $rid
   *   The user role to clear the permissions of. Defaults to the authenticated user.
   */
  function clearPermissions($rid = USER_AUTHENTICATED_RID) {
    db_query('DELETE FROM {role_permission} WHERE rid = %d', $rid);
  }

should work.

boombatower’s picture

Status: Active » Needs review
FileSize
1009 bytes
1.1 KB

Which is preferred:
using RID
or name?

tweaked code & comment a tad.

cwgordon7’s picture

Category: bug » task
Status: Needs review » Reviewed & tested by the community

RID is better, IMO - it's easier to access, for one thing, seeing as $user->roles contains it. The RID patch is rtbc. :) It would be great if we could get this in before the testing party.

boombatower’s picture

Still applies....

clarification "simpletest_clear_permissions_rid.patch".

webchick’s picture

Status: Reviewed & tested by the community » Needs work

Hm. I'm not so sure about this.

I think a better way to go about this would be to add general functions to user.module that allow adding or deleting a permission from a given role, and have the tests that need that call those instead. This would be useful in far more than just SimpleTest... install profiles, for example.

Moshe thinks it's not fair to ask for that in this patch since it's about something very specific. But it'd be way more awesome if it were generalized. :D

Also, Moshe pointed out:

one caveat about clearing permissions is that you have to do a new request to have that take effect
user_access() has a static cache
so, this function should call user_access(NULL, NULL, TRUE)
depends on what the tst is doing
It might not want to clear for the current logged in user

So probably need a $reset param.

boombatower’s picture

Title: DrupalWebTestCase should deny all permissions from authenticated users » Provide function to add/delete permisions from a given role
Component: simpletest.module » user.module
Assigned: cwgordon7 » Unassigned
Category: task » feature

Once this feature is decided upon and patch created, lets create tests as well.

Dave Reid’s picture

I'd invite anyone from this issue to take a look at #300993: User roles and permissions API where I've done a lot of work trying to improve the role and permissions API, as well as implemented a few improvements from this issue. Maybe we should mark one of these as a duplicate?

boombatower’s picture

Status: Needs work » Closed (duplicate)

Seems valid to mark this one...since you have already been working in other.