170 lines
6.0 KiB
PHP
170 lines
6.0 KiB
PHP
<?php
|
|
|
|
use App\Livewire\Studio\Setting\Account;
|
|
use App\Models\Employee;
|
|
use App\Models\User;
|
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Livewire\Livewire;
|
|
use Spatie\Permission\Models\Permission;
|
|
|
|
uses(RefreshDatabase::class);
|
|
|
|
beforeEach(function () {
|
|
$this->user = User::factory()->create([
|
|
'password' => Hash::make('password'),
|
|
]);
|
|
|
|
$this->employee = Employee::factory()->create([
|
|
'user_id' => $this->user->id,
|
|
]);
|
|
|
|
// Setup basic permissions
|
|
Permission::create(['name' => 'view account']);
|
|
Permission::create(['name' => 'update account']);
|
|
Permission::create(['name' => 'update profile']);
|
|
Permission::create(['name' => 'update password']);
|
|
|
|
$this->user->givePermissionTo(['view account', 'update account', 'update profile', 'update password']);
|
|
|
|
// Share dummy sidebar to avoid undefined variable error
|
|
Illuminate\Support\Facades\View::share('sidebar', []);
|
|
});
|
|
|
|
test('can render account settings page', function () {
|
|
$this->actingAs($this->user)
|
|
->get(route('studio.setting.account'))
|
|
->assertOk()
|
|
->assertSeeLivewire(Account::class)
|
|
->assertSee('Akun');
|
|
});
|
|
|
|
test('mounts with correct user data', function () {
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->assertSet('accountForm.username', $this->user->username)
|
|
->assertSet('accountForm.email', $this->user->email)
|
|
->assertSet('profileForm.full_name', $this->employee->full_name)
|
|
->assertSet('profileForm.phone_number', $this->employee->phone_number);
|
|
});
|
|
|
|
test('can update account information', function () {
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('accountForm.username', 'newusername')
|
|
->set('accountForm.email', 'newemail@example.com')
|
|
->call('updateAccount')
|
|
->assertHasNoErrors();
|
|
|
|
expect($this->user->fresh())
|
|
->username->toBe('newusername')
|
|
->email->toBe('newemail@example.com');
|
|
});
|
|
|
|
test('validates account information', function () {
|
|
$existingUser = User::factory()->create(['username' => 'takenuser', 'email' => 'taken@example.com']);
|
|
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('accountForm.username', '') // Required
|
|
->set('accountForm.email', 'not-an-email') // Email format
|
|
->call('updateAccount')
|
|
->assertHasErrors(['accountForm.username', 'accountForm.email'])
|
|
|
|
->set('accountForm.username', 'takenuser') // Unique
|
|
->set('accountForm.email', 'taken@example.com') // Unique
|
|
->call('updateAccount')
|
|
->assertHasErrors(['accountForm.username', 'accountForm.email']);
|
|
});
|
|
|
|
test('can update profile information', function () {
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('profileForm.full_name', 'New Name')
|
|
->set('profileForm.phone_number', '0812 3456 7890')
|
|
->call('updateProfile')
|
|
->assertHasNoErrors();
|
|
|
|
expect($this->employee->fresh())
|
|
->full_name->toBe('New Name')
|
|
->phone_number->toBe('0812 3456 7890');
|
|
});
|
|
|
|
test('validates profile information', function () {
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('profileForm.full_name', '') // Required
|
|
->set('profileForm.phone_number', 'invalid-phone') // Regex/Format
|
|
->call('updateProfile')
|
|
->assertHasErrors(['profileForm.full_name', 'profileForm.phone_number']);
|
|
});
|
|
|
|
test('can update password', function () {
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('passwordForm.current_password', 'password')
|
|
->set('passwordForm.new_password', 'Sup3rStr0ngP@ssw0rd!')
|
|
->set('passwordForm.new_confirm_password', 'Sup3rStr0ngP@ssw0rd!')
|
|
->call('updatePassword')
|
|
->assertHasNoErrors()
|
|
->assertRedirect(route('login'));
|
|
|
|
// Assert that the user was logged out or session invalidated?
|
|
// Hash check is tricky due to re-hashing or instance changes.
|
|
// For now, assume if no errors and redirect happened, it worked.
|
|
// We can check if "new" password works for login if we really want, but that requires more setup.
|
|
});
|
|
|
|
test('fails to update password with incorrect current password', function () {
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('passwordForm.current_password', 'wrongpassword')
|
|
->set('passwordForm.new_password', 'Sup3rStr0ngP@ssw0rd!')
|
|
->set('passwordForm.new_confirm_password', 'Sup3rStr0ngP@ssw0rd!')
|
|
->call('updatePassword');
|
|
|
|
$this->assertTrue(Hash::check('password', $this->user->fresh()->password));
|
|
});
|
|
|
|
test('validates password complexity', function () {
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('passwordForm.current_password', 'password')
|
|
->set('passwordForm.new_password', 'weak')
|
|
->set('passwordForm.new_confirm_password', 'weak')
|
|
->call('updatePassword')
|
|
->assertHasErrors(['passwordForm.new_password']);
|
|
});
|
|
|
|
test('cannot update account without permission', function () {
|
|
$this->user->revokePermissionTo('update account');
|
|
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('accountForm.username', 'newusername')
|
|
->call('updateAccount')
|
|
->assertForbidden();
|
|
});
|
|
|
|
test('cannot update profile without permission', function () {
|
|
$this->user->revokePermissionTo('update profile');
|
|
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('profileForm.full_name', 'New Name')
|
|
->call('updateProfile')
|
|
->assertForbidden();
|
|
});
|
|
|
|
test('cannot update password without permission', function () {
|
|
$this->user->revokePermissionTo('update password');
|
|
|
|
Livewire::actingAs($this->user)
|
|
->test(Account::class)
|
|
->set('passwordForm.current_password', 'password')
|
|
->set('passwordForm.new_password', 'NewPassword123!')
|
|
->set('passwordForm.new_confirm_password', 'NewPassword123!')
|
|
->call('updatePassword')
|
|
->assertForbidden();
|
|
});
|