12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- <?php
- namespace Tests\Unit;
- use App\Models\AdminPermission;
- use App\Models\AdminRole;
- use App\Models\AdminUser;
- use Illuminate\Foundation\Testing\WithFaker;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- use Tests\TestCase;
- class HasPermissionsTest extends TestCase
- {
- use RefreshDatabase;
- public function testMethods()
- {
- /** @var AdminUser $user */
- $user = factory(AdminUser::class)->create();
- $role1 = factory(AdminRole::class)->create(['slug' => 'role_1']);
- $perm1 = factory(AdminPermission::class)->create(['slug' => 'perm_1']);
- $role1->permissions()->attach($perm1->id);
- $user->roles()->attach($role1->id);
- $role2 = factory(AdminRole::class)->create(['slug' => 'role_2']);
- $perm2 = factory(AdminPermission::class)->create(['slug' => 'perm_2']);
- $user->permissions()->attach($perm2->id);
- $perm3 = factory(AdminPermission::class)->create(['slug' => 'perm_3']);
- // allPermissions
- $allPerms = $user->allPermissions()->pluck('slug')->all();
- $this->assertTrue(count(array_intersect($allPerms, ['perm_1', 'perm_2'])) == 2);
- // can
- $this->assertTrue($user->can('perm_1'));
- $this->assertTrue($user->can('perm_2'));
- $this->assertFalse($user->can('perm_3'));
- // isRole
- $this->assertTrue($user->isRole('role_1'));
- $this->assertFalse($user->isRole('role_2'));
- // inRoles
- $this->assertTrue($user->inRoles(['role_1', 'role_2']));
- $this->assertFalse($user->inRoles(['role_2']));
- // visible
- $this->assertTrue($user->visible([$role1, $role2]));
- $this->assertFalse($user->visible([$role2]));
- // administrator
- $admin = factory(AdminRole::class)->create(['slug' => 'administrator']);
- $user->roles()->attach($admin->id);
- $user->setRelation('roles', $user->roles()->get());
- $this->assertTrue($user->can('perm_3'));
- $this->assertTrue($user->visible(['role_2']));
- }
- }
|