Skip to content

Commit

Permalink
Merge pull request #249 from pinkary-project/fix/avatar-job-dispatch
Browse files Browse the repository at this point in the history
Fix: Avatar job dispatch
  • Loading branch information
nunomaduro authored Apr 24, 2024
2 parents 7787096 + 5b372f9 commit b283595
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
12 changes: 8 additions & 4 deletions app/Http/Controllers/ProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ public function update(ProfileUpdateRequest $request): RedirectResponse

$user->fill($request->validated());

if ($user->isDirty('email')) {
$user->email_verified_at = null;
}
$emailChanged = $user->isDirty('email');

$user->save();

if (! $user->is_uploaded_avatar) {
if ($emailChanged) {
$user->email_verified_at = null;
$user->save();
$user->sendEmailVerificationNotification();
}

if (! $user->is_uploaded_avatar && $emailChanged) {
UpdateUserAvatar::dispatch($user);
}

Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/UpdateUserAvatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function handle(): void
Storage::disk('public')->put($avatar, $contents, 'public');

$this->resizer()->read($disk->path($avatar))
->resize(200, 200)
->coverDown(200, 200)
->save();

$this->user->update([
Expand Down
5 changes: 0 additions & 5 deletions app/Livewire/Links/Create.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace App\Livewire\Links;

use App\Jobs\UpdateUserAvatar;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -53,10 +52,6 @@ public function store(Request $request): void

$user->links()->create($validated);

if (! $user->is_uploaded_avatar) {
dispatch(new UpdateUserAvatar($user));
}

$this->description = '';
$this->url = '';

Expand Down
36 changes: 36 additions & 0 deletions tests/Http/Profile/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

declare(strict_types=1);

use App\Jobs\UpdateUserAvatar;
use App\Models\User;
use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

Expand Down Expand Up @@ -123,6 +125,40 @@
$this->assertNotNull($user->refresh()->email_verified_at);
});

test('email verification job sent & status reset when the email address is changed', function () {
Notification::fake();
$user = User::factory()->create();

$this->actingAs($user)
->patch('/profile', [
'name' => $user->name,
'username' => 'valid_username',
'email' => 'new@email.address',
'prefers_anonymous_questions' => false,
])
->assertSessionHasNoErrors();

expect($user->email_verified_at)->toBeNull();

Notification::assertSentTo($user, VerifyEmail::class);
});

test('only updates avatar if email changes & avatar not been uploaded', function () {
Queue::fake();
$user = User::factory()->create();

$this->actingAs($user)
->patch('/profile', [
'name' => $user->name,
'username' => 'valid_username',
'email' => $user->email,
'prefers_anonymous_questions' => false,
])
->assertSessionHasNoErrors();

Queue::assertNotPushed(UpdateUserAvatar::class);
});

test('password can be updated', function () {
$user = User::factory()->create();

Expand Down

0 comments on commit b283595

Please sign in to comment.