Skip to content

Commit

Permalink
Added Events PermissionAttached, PermissionDetached, `RoleAttache…
Browse files Browse the repository at this point in the history
…d` and `RoleDetached`
  • Loading branch information
sven-arztkonsultation committed Oct 22, 2024
1 parent 492d973 commit 7483792
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/HasPermissionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Spatie\Permission\Contracts\Permission;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Events\PermissionAttached;
use Spatie\Permission\Events\PermissionDetached;
use Spatie\Permission\Events\RoleAttached;
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
use Spatie\Permission\Exceptions\PermissionDoesNotExist;
Expand Down Expand Up @@ -780,4 +781,18 @@ public function it_fires_an_event_when_a_permission_is_added()
return $event->model instanceof User && $event->model->hasPermissionTo('edit-news');
});
}

/** @test */
public function it_fires_an_event_when_a_permission_is_removed()
{
Event::fake();

$this->testUser->givePermissionTo('edit-news');

$this->testUser->revokePermissionTo('edit-news');

Event::assertDispatched(PermissionDetached::class, function ($event) {
return $event->model instanceof User && !$event->model->hasPermissionTo('edit-news');
});
}
}
15 changes: 15 additions & 0 deletions tests/HasRolesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Support\Facades\Event;
use Spatie\Permission\Contracts\Role;
use Spatie\Permission\Events\RoleAttached;
use Spatie\Permission\Events\RoleDetached;
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
use Spatie\Permission\Exceptions\RoleDoesNotExist;
use Spatie\Permission\Tests\TestModels\Admin;
Expand Down Expand Up @@ -870,4 +871,18 @@ public function it_fires_an_event_when_a_role_is_added()
return $event->model instanceof User && $event->model->hasRole('testRole');
});
}

/** @test */
public function it_fires_an_event_when_a_role_is_removed()
{
Event::fake();

$this->testUser->assignRole('testRole');

$this->testUser->removeRole('testRole');

Event::assertDispatched(RoleDetached::class, function ($event) {
return $event->model instanceof User && !$event->model->hasRole('testRole');
});
}
}

0 comments on commit 7483792

Please sign in to comment.