How to setup and use fixtures for rbac\DbManager so that authorization to given user can be tested ?
How to setup and use fixtures for rbac\DbManager so that authorization to given user can be tested ?
In your text fixtures, all you have to do is call \Yii::$app->user->can(‘permissionname’) to test permissions. Depending on how much you want to test them, you will have to create lots of different types of code - if for example you have many permissions.
One of the problems though is that permissions are not static. You would have to create a test user with a known set of permissions and test these but in production, you might not be able to guarantee that somebody has permissions modified and then is able to do something that the test said wasn’t allowed.
That’s up to you though!
I am in the process of unit testing.
So far, I have found a workaround by creating classes for the tables used by RBAC and defining the fixtures as if they were standard ActiveRecord Models.
This way the fixtures are loaded with the benefit that before each test, the records corresponding the permissions are reloaded, as in a normal fixture. Thus enabling authorization to be tested.
Please have a look at my repo, https://github.com/aleoy/yii2app/blob/master/tests/codeception/backend/unit/RbacTest.php
Now the question has changed to … Is there a better way to do it ?
just use db dump for rbac. it will remove like 90% of your current test case. hehe
anyway,
you must move testCreate() into _bootstrap.php or in setUP() function, as this method does not do any testing, but globally alters db.
Also, you must run Yii::$app->auth instead of just making new object.
or i just cannot understand what is the point of this test case. my bad