diff --git a/app/Models/User.php b/app/Models/User.php index 10d41fb9a..d1d6f5dc5 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -319,7 +319,9 @@ public function scopeMostSolutions(Builder $query, ?int $inLastDays = null) } return $query; - }])->orderBy('solutions_count', 'desc'); + }]) + ->having('solutions_count', '>', 0) + ->orderBy('solutions_count', 'desc'); } public function scopeMostSubmissions(Builder $query, ?int $inLastDays = null) diff --git a/tests/Integration/Models/UserTest.php b/tests/Integration/Models/UserTest.php index fb39dd545..cbb7c8e8f 100644 --- a/tests/Integration/Models/UserTest.php +++ b/tests/Integration/Models/UserTest.php @@ -67,13 +67,43 @@ ]); $this->dispatch(new MarkThreadSolution($thread, $reply, $user)); - expect($user->mostSolutions()->find($user->id())->solutions_count)->toBe(0); + + expect($user->mostSolutions()->find($user->id()))->toBeNull(); $otherThread = Thread::factory()->create(); $this->dispatch(new MarkThreadSolution($otherThread, $reply, $user)); + expect($user->mostSolutions()->find($user->id())->solutions_count)->toBe(1); -}); +})->group('emeka'); + +it('only shows users with solutions in the widget', function () { + $userWithSolution = User::factory()->create(); + $userWithoutSolution = User::factory()->create(); + $anotherUserWithSolution = User::factory()->create(); + + $thread1 = Thread::factory()->create(); + $thread2 = Thread::factory()->create(); + + $reply1 = Reply::factory()->create([ + 'author_id' => $userWithSolution->id, + ]); + + $reply2 = Reply::factory()->create([ + 'author_id' => $anotherUserWithSolution->id, + ]); + + $this->dispatch(new MarkThreadSolution($thread1, $reply1, $userWithSolution)); + $this->dispatch(new MarkThreadSolution($thread2, $reply2, $anotherUserWithSolution)); + + $topMembers = User::mostSolutions(365)->take(5)->get(); + + expect($topMembers)->toHaveCount(2) + ->and($topMembers->pluck('id'))->toContain($userWithSolution->id) + ->and($topMembers->pluck('id'))->toContain($anotherUserWithSolution->id) + ->and($topMembers->pluck('id'))->not->toContain($userWithoutSolution->id); +})->group('widget'); + // Helpers function createTwoSolutionReplies(User $user)