How to test permissions for the Delete action?

I've got tests which check my policies are setup correct - they basically create a user with a role, and check the HTTP response from accessing that page (view/edit etc) through filament. I'm not sure how to test the Delete permissions, because it's only an action - can anyone offer any suggestions? Here's an example of my Edit User test: (any feedback/suggestions on the tests also welcome :))
#[TestWith(['admin', 200], 'admin')]
#[TestWith(['api', 403], 'api')]
public function test_permission_to_edit_user(string $role, int $expectedStatus)
{
$this->actingAs($this->createUserWithRole($role));

$user = User::factory()->admin()->create();

$response = $this->get(Filament::getResourceUrl(
User::class,
'edit',
['record' => $user->id]
));
$response->assertStatus($expectedStatus);
}
#[TestWith(['admin', 200], 'admin')]
#[TestWith(['api', 403], 'api')]
public function test_permission_to_edit_user(string $role, int $expectedStatus)
{
$this->actingAs($this->createUserWithRole($role));

$user = User::factory()->admin()->create();

$response = $this->get(Filament::getResourceUrl(
User::class,
'edit',
['record' => $user->id]
));
$response->assertStatus($expectedStatus);
}
3 Replies
Povilas Korop
Povilas Korop3w ago
Hmm, I prefer using Livewire tests instead of $this->get like Laravel tests. https://filamentphp.com/docs/4.x/testing/testing-actions So, livewire()->callAction() would be probably appropriate here. Example from the docs:
it('can send invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->callAction('send');

expect($invoice->refresh())
->isSent()->toBeTrue();
});
it('can send invoices', function () {
$invoice = Invoice::factory()->create();

livewire(EditInvoice::class, [
'invoice' => $invoice,
])
->callAction('send');

expect($invoice->refresh())
->isSent()->toBeTrue();
});
Patabugen
PatabugenOP2w ago
Thanks @Povilas Korop - I'm also using Livewire tests in other places and I'll see if I can use it to call the action - have you got any examples of calling actions via it perchance?
Povilas Korop
Povilas Korop2w ago
I don't think I have a ready-made example but its code would probably be the same as in the docs, what different example do you need?

Did you find this page helpful?