From 980d184ed4b03d5a16dc369490c64727e31a7ff6 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 1 Aug 2022 17:34:53 +0100 Subject: [PATCH 1/3] exclude author solutions from counting --- app/Models/User.php | 5 ++++- resources/views/forum/overview.blade.php | 2 +- tests/Integration/Models/UserTest.php | 19 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/Models/User.php b/app/Models/User.php index 9b24ec338..7ad344e45 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -259,7 +259,10 @@ public function scopeMostSolutions(Builder $query, int $inLastDays = null) { return $query->withCount(['replyAble as solutions_count' => function ($query) use ($inLastDays) { $query->where('replyable_type', 'threads') - ->join('threads', 'threads.solution_reply_id', '=', 'replies.id'); + ->join('threads', function($join) { + $join->on('threads.solution_reply_id', '=', 'replies.id') + ->on('threads.author_id', '!=', 'replies.author_id'); + }); if ($inLastDays) { $query->where('replies.created_at', '>', now()->subDays($inLastDays)); diff --git a/resources/views/forum/overview.blade.php b/resources/views/forum/overview.blade.php index ab4262aa6..130142265 100644 --- a/resources/views/forum/overview.blade.php +++ b/resources/views/forum/overview.blade.php @@ -155,7 +155,7 @@

- Solutions given in the past year. + Solutions given in the past year. Excluding solutions from thread authors.

diff --git a/tests/Integration/Models/UserTest.php b/tests/Integration/Models/UserTest.php index b787d7c24..1124b9355 100644 --- a/tests/Integration/Models/UserTest.php +++ b/tests/Integration/Models/UserTest.php @@ -1,5 +1,6 @@ countArticles())->toBe(1); }); +it('exclude author solutions from mostSolutions count', function () { + $user = $this->login(); + $thread = Thread::factory()->create([ + 'author_id' => $user->id(), + ]); + $reply = Reply::factory()->create([ + 'author_id' => $user->id(), + ]); + + $this->dispatch(new MarkThreadSolution($thread, $reply, $user)); + expect($user->MostSolutions()->find($user->id())->solutions_count)->toBe(0); + + $otherThread = Thread::factory()->create(); + + $this->dispatch(new MarkThreadSolution($otherThread, $reply, $user)); + expect($user->MostSolutions()->find($user->id())->solutions_count)->toBe(1); +}); + // Helpers function createTwoSolutionReplies(User $user) { From 854befea3009c4b80bf949e6700cec1ceecb1340 Mon Sep 17 00:00:00 2001 From: faissaloux Date: Mon, 1 Aug 2022 17:37:57 +0100 Subject: [PATCH 2/3] apply StyleCI fixes --- app/Models/User.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Models/User.php b/app/Models/User.php index 7ad344e45..6a6f8d882 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -259,7 +259,7 @@ public function scopeMostSolutions(Builder $query, int $inLastDays = null) { return $query->withCount(['replyAble as solutions_count' => function ($query) use ($inLastDays) { $query->where('replyable_type', 'threads') - ->join('threads', function($join) { + ->join('threads', function ($join) { $join->on('threads.solution_reply_id', '=', 'replies.id') ->on('threads.author_id', '!=', 'replies.author_id'); }); From f5e5094393232ef569acce0632710afd6b669db2 Mon Sep 17 00:00:00 2001 From: Dries Vints Date: Mon, 1 Aug 2022 21:52:32 +0200 Subject: [PATCH 3/3] Update UserTest.php --- tests/Integration/Models/UserTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/Integration/Models/UserTest.php b/tests/Integration/Models/UserTest.php index 1124b9355..fb39dd545 100644 --- a/tests/Integration/Models/UserTest.php +++ b/tests/Integration/Models/UserTest.php @@ -57,7 +57,7 @@ expect($user->countArticles())->toBe(1); }); -it('exclude author solutions from mostSolutions count', function () { +it('excludes author solutions from mostSolutions count', function () { $user = $this->login(); $thread = Thread::factory()->create([ 'author_id' => $user->id(), @@ -67,12 +67,12 @@ ]); $this->dispatch(new MarkThreadSolution($thread, $reply, $user)); - expect($user->MostSolutions()->find($user->id())->solutions_count)->toBe(0); + expect($user->mostSolutions()->find($user->id())->solutions_count)->toBe(0); $otherThread = Thread::factory()->create(); $this->dispatch(new MarkThreadSolution($otherThread, $reply, $user)); - expect($user->MostSolutions()->find($user->id())->solutions_count)->toBe(1); + expect($user->mostSolutions()->find($user->id())->solutions_count)->toBe(1); }); // Helpers