Snapshot of main at 9f39a6e1e (2026-11-02)
This commit is contained in:
commit
6ccf1792d7
2580 files changed
+1040202
No files matched your search
@@ -0,0 +1,92 @@
|
||||
<?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());
|
||||
$this->setActiveProfile($initialUser);
|
||||
|
||||
// Assuming 'password' is the default factory password or the one set for the user
|
||||
$component = Livewire::test(DeleteUserForm::class)
|
||||
->set('password', 'password')
|
||||
->call('deleteUser', request(), app(\Laravel\Jetstream\Contracts\DeletesUsers::class), auth()->guard());
|
||||
|
||||
// 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());
|
||||
$this->setActiveProfile($user);
|
||||
|
||||
Livewire::test(DeleteUserForm::class)
|
||||
->set('password', 'wrong-password')
|
||||
->call('deleteUser', request(), app(\Laravel\Jetstream\Contracts\DeletesUsers::class), auth()->guard())
|
||||
->assertHasErrors(['password']);
|
||||
|
||||
$this->assertNotNull($user->fresh());
|
||||
}
|
||||
|
||||
/**
|
||||
* The form works on the active profile, which the login always sets
|
||||
*/
|
||||
private function setActiveProfile(User $user): void
|
||||
{
|
||||
session([
|
||||
'activeProfileType' => User::class,
|
||||
'activeProfileId' => $user->id,
|
||||
'active_guard' => 'web',
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user