Spun off from #691938: Block caching per-role, per-user, per-page is broken, I had a problem where I wanted to create two test users with the same role. drupalCreateUser() internally calls drupalCreateRole(), and you get a new role every time, even if you pass the same permissions to drupalCreateUser(). I had to do some workarounds to copy the roles from one user to another.

This patch caches the roles according to (permission list + optional role name), allowing you to do:

$this->normal_user = $this->drupalCreateUser();
$this->normal_user_alt = $this->drupalCreateUser();

and they share a role since they both have the same (default) permissions. I think this a nice unobtrusive way to approach it, rather than modifying drupalCreateUser() to accept a role id/array/object. I'm too lazy for that :)

CommentFileSizeAuthor
simpletest-roles.patch1.42 KBcarlos8f

Comments

dries’s picture

I'm not sure. I'd actually change drupalCreateUser() to accept a roles array instead of a permissions array. That would be a bigger change, of course, but it would make sense.

Chaulky’s picture

I agree that drupalCreateUser() should accept a roles array. There are many use cases where testing with a specific role rather than a specific set of permissions makes sense. The current behavior should absolutely be preserved as it is also useful in many situations.

Status: Needs review » Closed (outdated)

Automatically closed because Drupal 7 security and bugfix support has ended as of 5 January 2025. If the issue verifiably applies to later versions, please reopen with details and update the version.