Skip to content

Commit 1ab103e

Browse files
committed
fix(API): keep limit(0) semantics and builder reset in paginate()
Scope the overflow guard to the offset calculation so that: - $perPage = 0 is still passed to the Query Builder, where limit(0) honours Feature::$limitZeroAsAll, instead of dividing by zero before the builder can apply it; - out-of-range pages still run get(), which resets the builder state as before the overflow guard was added; - a page past the end uses the total as the offset, which cannot overflow the int type for huge page numbers; - the total is cast to int so limit(?int, ?int) never receives a string from countAllResults() under strict types. Adds regression tests for perPage = 0 and for an out-of-range page when countAllResults() returns a numeric string.
1 parent 44e1d52 commit 1ab103e

2 files changed

Lines changed: 127 additions & 18 deletions

File tree

‎system/API/ResponseTrait.php‎

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,26 @@ protected function paginate(BaseBuilder|Model $resource, int $perPage = 20, ?str
416416
];
417417
} else {
418418
// Query Builder, we need to handle pagination manually
419-
$total = (clone $resource)->countAllResults();
420-
$totalPages = (int) ceil($total / $perPage);
419+
$total = (int) (clone $resource)->countAllResults();
421420

422-
if ($total === 0 || $page > $totalPages) {
423-
$data = [];
421+
if ($perPage > 0) {
422+
$totalPages = (int) ceil($total / $perPage);
423+
424+
// A page past the end returns no rows. Using the total as
425+
// the offset keeps the arithmetic inside the int type for
426+
// huge page numbers, and still runs the query so the
427+
// builder is reset by get() as before.
428+
$offset = $page > $totalPages ? $total : ($page - 1) * $perPage;
424429
} else {
425-
$offset = ($page - 1) * $perPage;
426-
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
430+
// $perPage = 0 is left to the Query Builder, where limit(0)
431+
// honours Feature::$limitZeroAsAll. There is no finite page
432+
// count in that case.
433+
$totalPages = 0;
434+
$offset = 0;
427435
}
428436

437+
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
438+
429439
$meta = [
430440
'page' => $page,
431441
'perPage' => $perPage,

‎tests/system/API/ResponseTraitTest.php‎

Lines changed: 111 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -809,12 +809,18 @@ public function testPaginateWithQueryBuilderOutOfRangePage(): void
809809
->onlyMethods(['countAllResults', 'limit', 'get'])
810810
->getMock();
811811

812+
$result = $this->createMock(BaseResult::class);
813+
812814
// Mock the query builder chain
813815
$builder->method('countAllResults')->willReturn(50);
814-
$builder->expects($this->never())
815-
->method('limit');
816-
$builder->expects($this->never())
817-
->method('get');
816+
$builder->expects($this->once())
817+
->method('limit')
818+
->with(20, 50)
819+
->willReturnSelf();
820+
$builder->expects($this->once())
821+
->method('get')
822+
->willReturn($result);
823+
$result->method('getResultArray')->willReturn([]);
818824

819825
$controller = $this->makeController('/api/items?page=4');
820826
$this->request->setGlobal('get', ['page' => '4']);
@@ -843,12 +849,18 @@ public function testPaginateWithQueryBuilderPageOverflow(): void
843849
->onlyMethods(['countAllResults', 'limit', 'get'])
844850
->getMock();
845851

852+
$result = $this->createMock(BaseResult::class);
853+
846854
// Mock the query builder chain
847855
$builder->method('countAllResults')->willReturn(50);
848-
$builder->expects($this->never())
849-
->method('limit');
850-
$builder->expects($this->never())
851-
->method('get');
856+
$builder->expects($this->once())
857+
->method('limit')
858+
->with(20, 50)
859+
->willReturnSelf();
860+
$builder->expects($this->once())
861+
->method('get')
862+
->willReturn($result);
863+
$result->method('getResultArray')->willReturn([]);
852864

853865
$controller = $this->makeController('/api/items?page=' . PHP_INT_MAX);
854866
$this->request->setGlobal('get', ['page' => (string) PHP_INT_MAX]);
@@ -875,12 +887,18 @@ public function testPaginateWithQueryBuilderZeroResultsPageOverflow(): void
875887
->onlyMethods(['countAllResults', 'limit', 'get'])
876888
->getMock();
877889

890+
$result = $this->createMock(BaseResult::class);
891+
878892
// Mock the query builder chain
879893
$builder->method('countAllResults')->willReturn(0);
880-
$builder->expects($this->never())
881-
->method('limit');
882-
$builder->expects($this->never())
883-
->method('get');
894+
$builder->expects($this->once())
895+
->method('limit')
896+
->with(20, 0)
897+
->willReturnSelf();
898+
$builder->expects($this->once())
899+
->method('get')
900+
->willReturn($result);
901+
$result->method('getResultArray')->willReturn([]);
884902

885903
$controller = $this->makeController('/api/items?page=' . PHP_INT_MAX);
886904
$this->request->setGlobal('get', ['page' => (string) PHP_INT_MAX]);
@@ -895,6 +913,87 @@ public function testPaginateWithQueryBuilderZeroResultsPageOverflow(): void
895913
$this->assertSame([], $responseBody['data']);
896914
}
897915

916+
public function testPaginateWithQueryBuilderZeroPerPage(): void
917+
{
918+
// Mock the database and builder
919+
$db = $this->createStub(BaseConnection::class);
920+
921+
$builder = $this->getMockBuilder(BaseBuilder::class)
922+
->setConstructorArgs(['test_table', $db])
923+
->onlyMethods(['countAllResults', 'limit', 'get'])
924+
->getMock();
925+
926+
$result = $this->createMock(BaseResult::class);
927+
928+
$data = [
929+
['id' => 1],
930+
['id' => 2],
931+
];
932+
933+
// Mock the query builder chain
934+
$builder->method('countAllResults')->willReturn(50);
935+
$builder->expects($this->once())
936+
->method('limit')
937+
->with(0, 0)
938+
->willReturnSelf();
939+
$builder->expects($this->once())
940+
->method('get')
941+
->willReturn($result);
942+
$result->method('getResultArray')->willReturn($data);
943+
944+
$controller = $this->makeController('/api/items');
945+
946+
$this->invoke($controller, 'paginate', [$builder, 0]);
947+
948+
$responseBody = json_decode($this->response->getBody(), true);
949+
950+
$this->assertSame(200, $this->response->getStatusCode());
951+
$this->assertSame(1, $responseBody['meta']['page']);
952+
$this->assertSame(0, $responseBody['meta']['perPage']);
953+
$this->assertSame(50, $responseBody['meta']['total']);
954+
$this->assertSame(0, $responseBody['meta']['totalPages']);
955+
$this->assertSame($data, $responseBody['data']);
956+
}
957+
958+
public function testPaginateWithQueryBuilderStringTotalFromCountAllResults(): void
959+
{
960+
// countAllResults() is documented as returning int|string, so the
961+
// total must be cast before it reaches limit(?int, ?int): with strict
962+
// types a string offset would throw a TypeError.
963+
$db = $this->createStub(BaseConnection::class);
964+
965+
$builder = $this->getMockBuilder(BaseBuilder::class)
966+
->setConstructorArgs(['test_table', $db])
967+
->onlyMethods(['countAllResults', 'limit', 'get'])
968+
->getMock();
969+
970+
$result = $this->createMock(BaseResult::class);
971+
972+
// Mock the query builder chain
973+
$builder->method('countAllResults')->willReturn('50');
974+
$builder->expects($this->once())
975+
->method('limit')
976+
->with(20, 50)
977+
->willReturnSelf();
978+
$builder->expects($this->once())
979+
->method('get')
980+
->willReturn($result);
981+
$result->method('getResultArray')->willReturn([]);
982+
983+
$controller = $this->makeController('/api/items?page=4');
984+
$this->request->setGlobal('get', ['page' => '4']);
985+
986+
$this->invoke($controller, 'paginate', [$builder, 20]);
987+
988+
$responseBody = json_decode($this->response->getBody(), true);
989+
990+
$this->assertSame(200, $this->response->getStatusCode());
991+
$this->assertSame([], $responseBody['data']);
992+
$this->assertSame(4, $responseBody['meta']['page']);
993+
$this->assertSame(50, $responseBody['meta']['total']);
994+
$this->assertSame(3, $responseBody['meta']['totalPages']);
995+
}
996+
898997
public function testPaginateWithCustomPerPage(): void
899998
{
900999
$data = [

0 commit comments

Comments
 (0)