Does your Client model use Soft Deletes? If so, then it could be because of that - because the database record still exists, but the deleted_at column will now be set, rather than null.
Dec 31, 2024
9
Level 7
PHPUnit test fails when it should pass
Hi all. I am running this test, where in fact I am testing my polices, that a user cannot do what he is not supposed to:
<?php
declare(strict_types=1);
namespace Tests\Feature\Client;
use App\Models\Client;
use App\Models\User;
use PHPUnit\Framework\Attributes\Test;
use Tests\TestCase;
class OthersRecordsTest extends TestCase
{
#[Test]
public function list_have_only_own_records()
{
$response = $this->get(route('clients.list'));
$response->assertDontSee($this->foreignClient->name);
$response->assertSee($this->ownClient->name);
}
#[Test]
public function cannot_view_foreign_record()
{
$response = $this->get(route('client.show', ['id' => $this->foreignClient->id]));
$response->assertStatus(403);
}
#[Test]
public function cannot_view_edit_form_for_foreign_record()
{
$response = $this->get(route('client.edit', ['id' => $this->foreignClient->id]));
$response->assertStatus(403);
}
#[Test]
public function cannot_edit_foreign_record()
{
$this->foreignClient->save(['name' => 'Some test Name']);
$this->assertDatabaseMissing('clients', ['name' => 'Some test Name']);
}
#[Test]
public function cannot_delete_foreign_record()
{
$this->foreignClient->delete();
$this->assertDatabaseHas('clients', ['name' => $this->foreignClient->name]);
}
#[Test]
protected function setUp(): void
{
parent::setUp();
$user = User::factory()->create();
$user->assignRole('ProjectOwner');
$this->actingAs($user);
$this->ownClient = Client::factory()->create(['user_id' => $user->id]);
$users = User::all()->where('id', '!=', $user->id);
$this->foreignClient = Client::factory()->create([
'user_id' => $users->random(1)->first()->id,
]);
}
}
For some reason, the test cannot_delete_foreign_record fails i.e. the client is deleted, when it should not, because in my Client model policy I have this:
/**
* Determine whether the user can delete the client.
*/
public function delete(User $user, Client $client): bool
{
return $user->can('delete_client') || $client->user_id === $user->id;
}
The test result:
FAILED Tests\Feature\Client\OthersRecordsTest > cannot delete foreign record
Failed asserting that a row in the table [clients] matches the attributes {
"name": "Runolfsson-Durgan"
}.
Found: [
{
"name": "Aufderhar, Schiller and Marvin"
},
{
"name": "Barrows, Glover and Murray"
},
{
"name": "Bergnaum, Dibbert and Ferry"
}
] and 23 others.
at tests\Feature\Client\OthersRecordsTest.php:52
48▕ public function cannot_delete_foreign_record()
49▕ {
50▕ $this->foreignClient->delete();
51▕
➜ 52▕ $this->assertDatabaseHas('clients', ['name' => $this->foreignClient->name]);
53▕ }
54▕
55▕ #[Test]
56▕ protected function setUp(): void
Tests: 1 failed, 4 passed (6 assertions)
Duration: 7.59s
The assigned role 'ProjectOwner' doesn't have permission to delete_client.
What am I doing wrong on my test? Because all the other tests pass.
Please or to participate in this conversation.