]> BookStack Code Mirror - bookstack/commitdiff
Fixed incorrect API listing total when offset set
authorDan Brown <redacted>
Sat, 25 Apr 2020 15:38:11 +0000 (16:38 +0100)
committerDan Brown <redacted>
Sat, 25 Apr 2020 15:38:11 +0000 (16:38 +0100)
Fixes #2043

app/Api/ListingResponseBuilder.php
tests/Api/ApiListingTest.php

index 2fa5644c3d111c3cdbc792d8851fb504895b7072..942c5d27026a47fc9514a0317faa7bae0d0a2505 100644 (file)
@@ -36,8 +36,8 @@ class ListingResponseBuilder
      */
     public function toResponse()
     {
-        $data = $this->fetchData();
         $total = $this->query->count();
+        $data = $this->fetchData();
 
         return response()->json([
             'data' => $data,
index ac6d61aa142c85d1c4e005d85c2ecb1a7e3be454..bb4920cc3667d35d5c52ec3df904515a817cd602 100644 (file)
@@ -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