Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rugavy's avatar
Level 10

Testing with user roles

Hey guys!

I have some actions in my controller which can only be accessed if the logged in user hase one of these roles: admin, planning, production.

Would you make tests for every role or just for one of these roles?

For example: "an_admin_can_add_something", "a_plannning_user_can_add_something" & "a_production_user_can_add_something' OR just "authorized_users_can_add_something"?

I'm curious how guys would think this through.

Thanks!

0 likes
3 replies
martinbean's avatar
Level 80

@rugavy Yeah, I’d approach it in the similar way. In fact, I have done in one of my own applications.

It’s a video on demand site so there’s lots around what users can and can’t do based on various conditions. Is the user a guest? Has the user rented the title? Is the user subscribed to the channel the title belongs to? These have their own tests named as such, i.e.

  • test_guest_cannot_see_video
  • test_user_cannot_see_video
  • test_user_can_see_video_if_has_active_rental
  • test_user_cannot_see_video_if_rental_has_expired
  • test_user_can_see_video_if_subscribed_to_channel
  • test_user_cannot_see_video_if_subscription_has_expired

Under the hood, these use Laravel policies, but I prefer testing the logic in situ rather than writing unit tests for a policy class.

Please or to participate in this conversation.