Files
BauIN/tests/Feature/Settings/SecurityTest.php
T
ChristophandClaude Opus 5.5 db2d5d6228 Mehrsprachigkeit der Oberfläche (Deutsch Standard, Englisch wählbar)
- Sprache je Benutzer (users.locale), Middleware SetLocale, Auswahl unter
  Einstellungen → Darstellung; angebotene Sprachen in config/app.php
- Übersetzungen über Laravel Lang (lang/de.json, lang/de/*.php), Seitentitel
  aus #[Title] werden übersetzt
- Eigene Texte und Korrekturen in lang/project/de.json, übernommen mit
  `artisan lang:apply-project` (überstehen `lang:update`)
- Tests: jede im Code verwendete Übersetzung ist vorhanden, eigene
  Übersetzungen sind übernommen, Sprachwahl und Standardsprache;
  SecurityTest prüft Texte sprachunabhängig über __()
- lang/ von Pint ausgenommen (erzeugte Dateien)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 09:52:20 +02:00

133 lines
3.9 KiB
PHP

<?php
namespace Tests\Feature\Settings;
use App\Livewire\Settings\Security;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Hash;
use Laravel\Fortify\Features;
use Livewire\Livewire;
use Tests\TestCase;
class SecurityTest extends TestCase
{
use RefreshDatabase;
protected function setUp(): void
{
parent::setUp();
$this->skipUnlessFortifyHas(Features::twoFactorAuthentication());
Features::twoFactorAuthentication([
'confirm' => true,
'confirmPassword' => true,
]);
Features::passkeys([
'confirmPassword' => true,
]);
}
public function test_security_settings_page_can_be_rendered(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->withSession(['auth.password_confirmed_at' => time()])
->get(route('security.edit'));
$response->assertOk();
$response->assertSee(__('Passkeys'));
$response->assertSee(__('No passkeys yet'));
$response->assertSee(__('Two-factor authentication'));
$response->assertSee(__('Enable 2FA'));
}
public function test_security_settings_page_requires_password_confirmation_when_enabled(): void
{
$user = User::factory()->create();
$response = $this->actingAs($user)
->get(route('security.edit'));
$response->assertRedirect(route('password.confirm'));
}
public function test_security_settings_page_renders_without_two_factor_when_feature_is_disabled(): void
{
config(['fortify.features' => []]);
$user = User::factory()->create();
$this->actingAs($user)
->withSession(['auth.password_confirmed_at' => time()])
->get(route('security.edit'))
->assertOk()
->assertSee(__('Update password'))
->assertDontSee(__('Manage your passkeys for passwordless sign-in'))
->assertDontSee(__('Add a passkey to sign in without a password'))
->assertDontSee(__('Two-factor authentication'));
}
public function test_two_factor_authentication_disabled_when_confirmation_abandoned_between_requests(): void
{
$user = User::factory()->create();
$user->forceFill([
'two_factor_secret' => encrypt('test-secret'),
'two_factor_recovery_codes' => encrypt(json_encode(['code1', 'code2'])),
'two_factor_confirmed_at' => null,
])->save();
$this->actingAs($user);
$component = Livewire::test(Security::class);
$component->assertSet('twoFactorEnabled', false);
$this->assertDatabaseHas('users', [
'id' => $user->id,
'two_factor_secret' => null,
'two_factor_recovery_codes' => null,
]);
}
public function test_password_can_be_updated(): void
{
$user = User::factory()->create([
'password' => Hash::make('password'),
]);
$this->actingAs($user);
$response = Livewire::test(Security::class)
->set('current_password', 'password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$response->assertHasNoErrors();
$this->assertTrue(Hash::check('new-password', $user->refresh()->password));
}
public function test_correct_password_must_be_provided_to_update_password(): void
{
$user = User::factory()->create([
'password' => Hash::make('password'),
]);
$this->actingAs($user);
$response = Livewire::test(Security::class)
->set('current_password', 'wrong-password')
->set('password', 'new-password')
->set('password_confirmation', 'new-password')
->call('updatePassword');
$response->assertHasErrors(['current_password']);
}
}