Files
timebank-cc-public/tests/Feature/DeleteAccountTest.php
Ronald Huynen 2547717edb Initial commit
2026-03-23 21:37:59 +01:00

79 lines
3.0 KiB
PHP

<?php
namespace Tests\Feature;
use App\Http\Livewire\Profile\DeleteUserForm;
use App\Models\User;
use Carbon\Carbon; // Import Carbon
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Auth;
use Livewire\Livewire;
use Tests\TestCase;
class DeleteAccountTest extends TestCase
{
use RefreshDatabase;
public function test_user_profile_can_be_deleted()
{
$this->actingAs($initialUser = User::factory()->create());
// Assuming 'password' is the default factory password or the one set for the user
$component = Livewire::test(DeleteUserForm::class)
->set('password', 'password')
->call('deleteUser');
// Fetch the user again from the database to get the updated attributes
$userAfterDeletionAttempt = User::find($initialUser->id);
// 1. Assert that the user record still exists (since it's a custom soft delete)
$this->assertNotNull($userAfterDeletionAttempt, 'User record should still exist in the database.');
// 2. Assert that the 'deleted_at' column is not null
$this->assertNotNull(
$userAfterDeletionAttempt->deleted_at,
"The 'deleted_at' column should be populated."
);
// 3. Optional: Assert that 'deleted_at' is a valid date instance (if cast)
// or a string in the expected format.
// If 'deleted_at' is cast to a Carbon instance in your User model:
if (isset($userAfterDeletionAttempt->getCasts()['deleted_at']) &&
in_array($userAfterDeletionAttempt->getCasts()['deleted_at'], ['date', 'datetime', 'immutable_date', 'immutable_datetime'])) {
$this->assertInstanceOf(
Carbon::class,
$userAfterDeletionAttempt->deleted_at,
"The 'deleted_at' column should be a Carbon instance."
);
} else {
// If it's a string, you might check its format (less robust)
$this->assertIsString($userAfterDeletionAttempt->deleted_at);
// Example: Basic check if it looks like a datetime string
$this->assertMatchesRegularExpression(
'/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/',
$userAfterDeletionAttempt->deleted_at,
"The 'deleted_at' column should be a valid datetime string."
);
}
// Assert that the user is no longer authenticated
$this->assertFalse(Auth::check(), 'User should be logged out after deletion.');
// Assert the redirect
$component->assertRedirect(route('goodbye-deleted-user'));
}
public function test_correct_password_must_be_provided_before_account_can_be_deleted()
{
$this->actingAs($user = User::factory()->create());
Livewire::test(DeleteUserForm::class)
->set('password', 'wrong-password')
->call('deleteUser')
->assertHasErrors(['password']);
$this->assertNotNull($user->fresh());
}
}