-
Notifications
You must be signed in to change notification settings - Fork 335
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subsequent protected routes test fails, but pass independently #306
Comments
For anyone else who might be having the same trouble, https://github.com/JosephSilber/bouncer/blob/v1.0.0-rc.1/src/Database/Queries/Abilities.php#L21-L24 is returning different results on subsequent calls regardless of my tests running in transactions and disabling Bouncer's cache. |
I attempted to revert with no luck as only |
It's not making this easier that I'm having to piecemeal my production migrations together to get what the database state should look like since this project is still modifying a single migration... |
Here's an example of the test:
I'm not sure what else I can do without some feedback from Joseph. |
Joseph are you able to speak to this issue? |
Which version of Bouncer are you upgrading from? |
|
The only meaningful schema changes since beta 4 is the addition of the alter table `abilities` add `scope` int null after `only_owned`
alter table `roles` add `scope` int null after `level`
alter table `assigned_roles` add `scope` int null after `entity_type`
alter table `permissions` add `scope` int null after `forbidden` |
I don't think this is a database problem. I have added those columns via the below database migration and have looked over them several times.
|
Could you maybe create a simple repo that demonstrates your issue with the least amount of code possible?
👍 |
I understand the need to go hands on with it. I'll see what I can do. It's a sizable project and these failures aren't with unit tests. |
I have a similar issue with a fresh 5.6 project. At first I had one single feature test which logs in some different users with different roles and does some tests against GET routes. This works flawless. As soon as I add another test file or class which comes before the said test, it stops working. Some debugging shows that the user has roles, but the roles do not have any abilities and therefore failing the "can" checks. I tried turning off cache and refreshing but this does not help either. |
Ah I found something: if you edit the EDIT: It seems it does not work in every constellation. Multiple tests in one class work, but two classes still fail. EDIT2: Doing multiple (more than once) EDIT3: New finding: if I remove EDIT4 (I am sorry ;)): Just found a "fix": I use |
Finally I found the base problem: As soon as you use custom models, you have to use |
What's the problem with the actual class name being stored in the DB?
I personally don't like it, so I always call Relation::morphMap for all my model classes.
But that's a personal choice. Why would it introduce this bug?
|
The problem is that when running phpunit for my test with a custom role model (defined in the boot method of my app service provider), each test except the first one will fail because the morphmap returns |
Why? This doesn't make any sense to me.
|
I made an example laravel project containing this issue, please take a look: |
@pr4xx Thanks for the sample project ❤️ Now that I got to look at it, I actually figured out what's going on:
The solution to this is for you to register the morph map for your custom model on your own in your service provider: use Bouncer;
use App\Role;
use Illuminate\Database\Eloquent\Relations\Relation;
public function boot()
{
Bouncer::useRoleModel(Role::class);
Relation::morphMap([Role::class]);
} I'll now have to figure out where to add it to the docs. Thanks for all the help! |
BTW, if any of you are not clear about what What's missing from the documentation there is that instead of passing an associative array to |
Thanks for this super detailed answer! This fixes this issue for me. Maybe @bkuhl can try to resolve his issue with this information? |
I'm not sure when this will be a priority for me again. The project dependent on this is on the back burner for a few weeks. I'll close it for now until I can confirm none of what has been discussed resolves it for me. Thanks! |
@pr4xx setting out to document this, I realized there isn't really anywhere I could point people that fully explains what the morph map is, or why you'd want to use that. So I created a detailed article about it: How to rid your database of PHP class names in Eloquent's Polymorphic tables Enjoy! |
I picked up this project again. Resuming where I left off (I think), adding the morphMap does not resolve the issue for me. Running
I understand the role |
The good news is that this isn't the issue I'm having, the issue I'm having is because I was using |
I'm upgrading to Laravel 5.6 and
v1.0.0-rc.1
from Laravel 5.5. and beta4. I'm noticing all my tests are failing that check permissions except for the first one. If a test file has 5 tests on routes that are protected bycan:
middleware, the first will pass and every other will fail at the$this->visit('my-url')
step. If I run all the tests separately they will pass.My version:
"silber/bouncer": "v1.0.0-rc.1",
My routes:
I've tried adding
Bouncer::dontCache();
in mysetUp()
with no luck. Any ideas on things that might be an issue causing this or something I can look at?The text was updated successfully, but these errors were encountered: