story 6.8 and fixed the redundunt options issue

This commit is contained in:
Naser Mansour
2025-12-28 22:28:55 +02:00
parent 31520c0398
commit 102f8553f4
7 changed files with 237 additions and 559 deletions
-269
View File
@@ -1,269 +0,0 @@
<?php
use App\Mail\TestEmail;
use App\Models\User;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
use Livewire\Volt\Volt;
beforeEach(function () {
$this->admin = User::factory()->admin()->create([
'password' => Hash::make('old-password'),
]);
});
// ===========================================
// Access Tests
// ===========================================
test('admin can view settings page', function () {
$this->actingAs($this->admin)
->get(route('admin.settings'))
->assertOk()
->assertSeeLivewire('admin.settings');
});
test('non-admin cannot access settings page', function () {
$client = User::factory()->individual()->create();
$this->actingAs($client)
->get(route('admin.settings'))
->assertForbidden();
});
test('unauthenticated user cannot access settings page', function () {
$this->get(route('admin.settings'))
->assertRedirect(route('login'));
});
// ===========================================
// Profile Update Tests
// ===========================================
test('admin can update profile information', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('full_name', 'Updated Name')
->set('email', 'updated@example.com')
->set('preferred_language', 'en')
->call('updateProfile')
->assertHasNoErrors();
expect($this->admin->fresh())
->full_name->toBe('Updated Name')
->email->toBe('updated@example.com')
->preferred_language->toBe('en');
});
test('profile update validates required fields', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('full_name', '')
->set('email', '')
->call('updateProfile')
->assertHasErrors(['full_name', 'email']);
});
test('profile update prevents duplicate email', function () {
$existingUser = User::factory()->create(['email' => 'taken@example.com']);
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('email', 'taken@example.com')
->call('updateProfile')
->assertHasErrors(['email']);
});
test('profile update allows keeping own email', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('email', $this->admin->email)
->call('updateProfile')
->assertHasNoErrors();
});
test('profile update validates email format', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('email', 'invalid-email')
->call('updateProfile')
->assertHasErrors(['email']);
});
test('profile update validates language is ar or en', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('preferred_language', 'fr')
->call('updateProfile')
->assertHasErrors(['preferred_language']);
});
// ===========================================
// Password Update Tests
// ===========================================
test('admin can update password with correct current password', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('current_password', 'old-password')
->set('password', 'new-password123')
->set('password_confirmation', 'new-password123')
->call('updatePassword')
->assertHasNoErrors();
expect(Hash::check('new-password123', $this->admin->fresh()->password))->toBeTrue();
});
test('password update fails with wrong current password', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('current_password', 'wrong-password')
->set('password', 'new-password123')
->set('password_confirmation', 'new-password123')
->call('updatePassword')
->assertHasErrors(['current_password']);
});
test('password update requires confirmation match', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('current_password', 'old-password')
->set('password', 'new-password123')
->set('password_confirmation', 'different-password')
->call('updatePassword')
->assertHasErrors(['password']);
});
test('password fields are cleared after successful update', function () {
$this->actingAs($this->admin);
$component = Volt::test('admin.settings')
->set('current_password', 'old-password')
->set('password', 'new-password123')
->set('password_confirmation', 'new-password123')
->call('updatePassword')
->assertHasNoErrors();
expect($component->get('current_password'))->toBe('');
expect($component->get('password'))->toBe('');
expect($component->get('password_confirmation'))->toBe('');
});
test('password update requires minimum length', function () {
$this->actingAs($this->admin);
Volt::test('admin.settings')
->set('current_password', 'old-password')
->set('password', 'short')
->set('password_confirmation', 'short')
->call('updatePassword')
->assertHasErrors(['password']);
});
// ===========================================
// Test Email Tests
// ===========================================
test('admin can send test email', function () {
Mail::fake();
$this->actingAs($this->admin);
Volt::test('admin.settings')
->call('sendTestEmail')
->assertHasNoErrors();
Mail::assertSent(TestEmail::class, fn ($mail) => $mail->hasTo($this->admin->email)
);
});
test('test email uses admin preferred language', function () {
Mail::fake();
$this->admin->update(['preferred_language' => 'ar']);
$this->actingAs($this->admin);
Volt::test('admin.settings')
->call('sendTestEmail');
Mail::assertSent(TestEmail::class, fn ($mail) => $mail->locale === 'ar'
);
});
// ===========================================
// Component Initialization Tests
// ===========================================
test('component initializes with current admin data', function () {
$this->admin->update([
'full_name' => 'Test Admin',
'email' => 'admin@test.com',
'preferred_language' => 'en',
]);
$this->actingAs($this->admin);
$component = Volt::test('admin.settings');
expect($component->get('full_name'))->toBe('Test Admin');
expect($component->get('email'))->toBe('admin@test.com');
expect($component->get('preferred_language'))->toBe('en');
});
test('component loads preferred language from user', function () {
// Create admin with specific language preference
$admin = User::factory()->admin()->create(['preferred_language' => 'ar']);
$this->actingAs($admin);
$component = Volt::test('admin.settings');
expect($component->get('preferred_language'))->toBe('ar');
});
// ===========================================
// UI Display Tests
// ===========================================
test('settings page displays mail configuration info', function () {
$this->actingAs($this->admin);
$this->get(route('admin.settings'))
->assertOk()
->assertSee(config('mail.from.name'))
->assertSee(config('mail.from.address'));
});
test('settings page displays profile settings section', function () {
$this->actingAs($this->admin);
$this->get(route('admin.settings'))
->assertOk()
->assertSee(__('admin.profile_settings'));
});
test('settings page displays password settings section', function () {
$this->actingAs($this->admin);
$this->get(route('admin.settings'))
->assertOk()
->assertSee(__('admin.password_settings'));
});
test('settings page displays email settings section', function () {
$this->actingAs($this->admin);
$this->get(route('admin.settings'))
->assertOk()
->assertSee(__('admin.email_settings'));
});
@@ -1,6 +1,8 @@
<?php
use App\Mail\TestEmail;
use App\Models\User;
use Illuminate\Support\Facades\Mail;
use Livewire\Volt\Volt;
test('profile page is displayed', function () {
@@ -17,6 +19,7 @@ test('profile information can be updated', function () {
$response = Volt::test('settings.profile')
->set('full_name', 'Test User')
->set('email', 'test@example.com')
->set('preferred_language', 'en')
->call('updateProfileInformation');
$response->assertHasNoErrors();
@@ -25,6 +28,7 @@ test('profile information can be updated', function () {
expect($user->full_name)->toEqual('Test User');
expect($user->email)->toEqual('test@example.com');
expect($user->preferred_language)->toEqual('en');
expect($user->email_verified_at)->toBeNull();
});
@@ -43,6 +47,98 @@ test('email verification status is unchanged when email address is unchanged', f
expect($user->refresh()->email_verified_at)->not->toBeNull();
});
// ===========================================
// Preferred Language Tests
// ===========================================
test('user can update preferred language', function () {
$user = User::factory()->create(['preferred_language' => 'ar']);
$this->actingAs($user);
Volt::test('settings.profile')
->set('preferred_language', 'en')
->call('updateProfileInformation')
->assertHasNoErrors();
expect($user->fresh()->preferred_language)->toBe('en');
});
test('preferred language validates allowed values', function () {
$user = User::factory()->create();
$this->actingAs($user);
Volt::test('settings.profile')
->set('preferred_language', 'fr')
->call('updateProfileInformation')
->assertHasErrors(['preferred_language']);
});
test('component initializes with user preferred language', function () {
$user = User::factory()->create(['preferred_language' => 'en']);
$this->actingAs($user);
$component = Volt::test('settings.profile');
expect($component->get('preferred_language'))->toBe('en');
});
// ===========================================
// Test Email Tests (Admin Only)
// ===========================================
test('admin can send test email', function () {
Mail::fake();
$admin = User::factory()->admin()->create();
$this->actingAs($admin);
Volt::test('settings.profile')
->call('sendTestEmail')
->assertHasNoErrors();
Mail::assertSent(TestEmail::class, fn ($mail) => $mail->hasTo($admin->email));
});
test('test email uses admin preferred language', function () {
Mail::fake();
$admin = User::factory()->admin()->create(['preferred_language' => 'ar']);
$this->actingAs($admin);
Volt::test('settings.profile')
->call('sendTestEmail');
Mail::assertSent(TestEmail::class, fn ($mail) => $mail->locale === 'ar');
});
test('non-admin cannot send test email', function () {
Mail::fake();
$client = User::factory()->individual()->create();
$this->actingAs($client);
Volt::test('settings.profile')
->call('sendTestEmail');
Mail::assertNothingSent();
});
test('admin sees email configuration section', function () {
$admin = User::factory()->admin()->create();
$this->actingAs($admin)
->get(route('profile.edit'))
->assertOk()
->assertSee(config('mail.from.name'))
->assertSee(config('mail.from.address'));
});
test('user can delete their account', function () {
$user = User::factory()->create();