From 79a949836b93c89b5efd9d9cf94e0f2b93dda48f Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Sat, 25 Apr 2020 16:38:11 +0100 Subject: [PATCH] Fixed incorrect API listing total when offset set Fixes #2043 --- app/Api/ListingResponseBuilder.php | 2 +- tests/Api/ApiListingTest.php | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/app/Api/ListingResponseBuilder.php b/app/Api/ListingResponseBuilder.php index 2fa5644c3..942c5d270 100644 --- a/app/Api/ListingResponseBuilder.php +++ b/app/Api/ListingResponseBuilder.php @@ -36,8 +36,8 @@ class ListingResponseBuilder */ public function toResponse() { - $data = $this->fetchData(); $total = $this->query->count(); + $data = $this->fetchData(); return response()->json([ 'data' => $data, diff --git a/tests/Api/ApiListingTest.php b/tests/Api/ApiListingTest.php index ac6d61aa1..bb4920cc3 100644 --- a/tests/Api/ApiListingTest.php +++ b/tests/Api/ApiListingTest.php @@ -81,4 +81,20 @@ class ApiListingTest extends TestCase } } + public function test_total_on_results_shows_correctly() + { + $this->actingAsApiEditor(); + $bookCount = Book::query()->count(); + $resp = $this->get($this->endpoint . '?count=1'); + $resp->assertJson(['total' => $bookCount ]); + } + + public function test_total_on_results_shows_correctly_when_offset_provided() + { + $this->actingAsApiEditor(); + $bookCount = Book::query()->count(); + $resp = $this->get($this->endpoint . '?count=1&offset=1'); + $resp->assertJson(['total' => $bookCount ]); + } + } \ No newline at end of file