Create tests for app/actions/fortify/UpdateUserProfileInformation

This commit is contained in:
Matt Young 2025-07-01 23:08:45 -05:00
parent 618ae79cd0
commit b04bdc960b
2 changed files with 87 additions and 10 deletions

View File

@ -44,17 +44,18 @@ class UpdateUserProfileInformation implements UpdatesUserProfileInformation
'cell_phone' => $input['cell_phone'], 'cell_phone' => $input['cell_phone'],
'email' => $input['email'], 'email' => $input['email'],
])->save(); ])->save();
$message = 'Updated user #'.$user->id.' - '.$user->email
.'<br>Name: '.$user->full_name()
.'<br>Judging Pref: '.$user->judging_preference
.'<br>Cell Phone: '.$user->cell_phone;
AuditLogEntry::create([
'user' => auth()->user()->email,
'ip_address' => request()->ip(),
'message' => $message,
'affected' => ['users' => [$user->id]],
]);
} }
$message = 'Updated user #'.$user->id.' - '.$user->email
.'<br>Name: '.$user->full_name()
.'<br>Judging Pref: '.$user->judging_preference
.'<br>Cell Phone: '.$user->cell_phone;
AuditLogEntry::create([
'user' => auth()->user()->email,
'ip_address' => request()->ip(),
'message' => $message,
'affected' => ['users' => [$user->id]],
]);
} }
/** /**

View File

@ -0,0 +1,76 @@
<?php
use App\Actions\Fortify\CreateNewUser;
use App\Actions\Fortify\UpdateUserProfileInformation;
use App\Models\AuditLogEntry;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
uses(RefreshDatabase::class);
beforeEach(function () {
$creator = app(CreateNewUser::class);
$this->user = $creator->create([
'registration_code' => auditionSetting('registrationCode'),
'first_name' => 'Old',
'last_name' => 'Name',
'judging_preference' => 'oldPreference',
'cell_phone' => '0123456789',
'email' => 'old@email.com',
'password' => 'password',
'password_confirmation' => 'password',
]);
$this->changer = app(UpdateUserProfileInformation::class);
$this->user->email_verified_at = Carbon::now();
$this->user->save();
$this->user->refresh();
$this->actingAs($this->user);
});
it('updates a user profile, maintains verification when retaining email', function () {
$newdata = [
'first_name' => 'New',
'last_name' => 'Named',
'judging_preference' => 'newPreference',
'cell_phone' => '0987654321',
'email' => 'old@email.com',
];
$this->changer->update($this->user, $newdata);
expect($this->user->first_name)->toEqual('New')
->and($this->user->last_name)->toEqual('Named')
->and($this->user->judging_preference)->toEqual('newPreference')
->and($this->user->cell_phone)->toEqual('0987654321')
->and($this->user->email)->toEqual('old@email.com')
->and($this->user->email_verified_at)->not()->toBeNull();
});
it('updates a user profile, clears verification when changing email', function () {
$newdata = [
'first_name' => 'New',
'last_name' => 'Named',
'judging_preference' => 'newPreference',
'cell_phone' => '0987654321',
'email' => 'new@email.com',
];
$this->changer->update($this->user, $newdata);
$this->user->refresh();
expect($this->user->first_name)->toEqual('New')
->and($this->user->last_name)->toEqual('Named')
->and($this->user->judging_preference)->toEqual('newPreference')
->and($this->user->cell_phone)->toEqual('0987654321')
->and($this->user->email)->toEqual('new@email.com')
->and($this->user->email_verified_at)->toBeNull();
});
it('logs changes in use profile', function () {
$newdata = [
'first_name' => 'New',
'last_name' => 'Named',
'judging_preference' => 'newPreference',
'cell_phone' => '0987654321',
'email' => 'new@email.com',
];
$this->changer->update($this->user, $newdata);
$this->user->refresh();
expect(AuditLogEntry::where('message', 'like', 'Updated user %')->count())->toEqual(1);
});