|
3 | 3 | namespace Spatie\Permission\Tests;
|
4 | 4 |
|
5 | 5 | use DB;
|
| 6 | +use Illuminate\Support\Facades\Event; |
6 | 7 | use Illuminate\Database\Eloquent\Model;
|
7 | 8 | use PHPUnit\Framework\Attributes\RequiresPhp;
|
8 | 9 | use PHPUnit\Framework\Attributes\Test;
|
9 | 10 | use Spatie\Permission\Contracts\Permission;
|
10 | 11 | use Spatie\Permission\Contracts\Role;
|
| 12 | +use Spatie\Permission\Events\PermissionAttached; |
| 13 | +use Spatie\Permission\Events\PermissionDetached; |
| 14 | +use Spatie\Permission\Events\RoleAttached; |
11 | 15 | use Spatie\Permission\Exceptions\GuardDoesNotMatch;
|
12 | 16 | use Spatie\Permission\Exceptions\PermissionDoesNotExist;
|
13 | 17 | use Spatie\Permission\Tests\TestModels\SoftDeletingUser;
|
@@ -824,6 +828,64 @@ public function it_can_reject_permission_based_on_logged_in_user_guard()
|
824 | 828 | ]);
|
825 | 829 | }
|
826 | 830 |
|
| 831 | + /** @test */ |
| 832 | + #[Test] |
| 833 | + public function it_fires_an_event_when_a_permission_is_added() |
| 834 | + { |
| 835 | + Event::fake(); |
| 836 | + app('config')->set('permission.events_enabled', true); |
| 837 | + |
| 838 | + $this->testUser->givePermissionTo(['edit-articles', 'edit-news']); |
| 839 | + |
| 840 | + $ids = app(Permission::class)::whereIn('name', ['edit-articles', 'edit-news']) |
| 841 | + ->pluck($this->testUserPermission->getKeyName()) |
| 842 | + ->toArray(); |
| 843 | + |
| 844 | + Event::assertDispatched(PermissionAttached::class, function ($event) use ($ids) { |
| 845 | + return $event->model instanceof User |
| 846 | + && $event->model->hasPermissionTo('edit-news') |
| 847 | + && $event->model->hasPermissionTo('edit-articles') |
| 848 | + && $ids === $event->permissionsOrIds; |
| 849 | + }); |
| 850 | + } |
| 851 | + |
| 852 | + /** @test */ |
| 853 | + #[Test] |
| 854 | + public function it_does_not_fire_an_event_when_events_are_not_enabled() |
| 855 | + { |
| 856 | + Event::fake(); |
| 857 | + app('config')->set('permission.events_enabled', false); |
| 858 | + |
| 859 | + $this->testUser->givePermissionTo(['edit-articles', 'edit-news']); |
| 860 | + |
| 861 | + $ids = app(Permission::class)::whereIn('name', ['edit-articles', 'edit-news']) |
| 862 | + ->pluck($this->testUserPermission->getKeyName()) |
| 863 | + ->toArray(); |
| 864 | + |
| 865 | + Event::assertNotDispatched(PermissionAttached::class); |
| 866 | + } |
| 867 | + |
| 868 | + /** @test */ |
| 869 | + #[Test] |
| 870 | + public function it_fires_an_event_when_a_permission_is_removed() |
| 871 | + { |
| 872 | + Event::fake(); |
| 873 | + app('config')->set('permission.events_enabled', true); |
| 874 | + |
| 875 | + $permissions = app(Permission::class)::whereIn('name', ['edit-articles', 'edit-news'])->get(); |
| 876 | + |
| 877 | + $this->testUser->givePermissionTo($permissions); |
| 878 | + |
| 879 | + $this->testUser->revokePermissionTo($permissions); |
| 880 | + |
| 881 | + Event::assertDispatched(PermissionDetached::class, function ($event) use ($permissions) { |
| 882 | + return $event->model instanceof User |
| 883 | + && !$event->model->hasPermissionTo('edit-news') |
| 884 | + && !$event->model->hasPermissionTo('edit-articles') |
| 885 | + && $event->permissionsOrIds === $permissions; |
| 886 | + }); |
| 887 | + } |
| 888 | + |
827 | 889 | /** @test */
|
828 | 890 | #[Test]
|
829 | 891 | public function it_can_be_given_a_permission_on_role_when_lazy_loading_is_restricted()
|
|
0 commit comments