Skip to content

Commit ceb9e5a

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 24c8007 commit ceb9e5a

2 files changed

Lines changed: 117 additions & 18 deletions

File tree

‎system/API/ResponseTrait.php‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,16 +416,19 @@ 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+
$offset = $page > $totalPages ? $total : ($page - 1) * $perPage;
424425
} else {
425-
$offset = ($page - 1) * $perPage;
426-
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
426+
$totalPages = 0;
427+
$offset = 0;
427428
}
428429

430+
$data = $resource->limit($perPage, $offset)->get()->getResultArray();
431+
429432
$meta = [
430433
'page' => $page,
431434
'perPage' => $perPage,

‎tests/system/API/ResponseTraitTest.php‎

Lines changed: 108 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,84 @@ 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+
$db = $this->createStub(BaseConnection::class);
961+
962+
$builder = $this->getMockBuilder(BaseBuilder::class)
963+
->setConstructorArgs(['test_table', $db])
964+
->onlyMethods(['countAllResults', 'limit', 'get'])
965+
->getMock();
966+
967+
$result = $this->createMock(BaseResult::class);
968+
969+
// Mock the query builder chain
970+
$builder->method('countAllResults')->willReturn('50');
971+
$builder->expects($this->once())
972+
->method('limit')
973+
->with(20, 50)
974+
->willReturnSelf();
975+
$builder->expects($this->once())
976+
->method('get')
977+
->willReturn($result);
978+
$result->method('getResultArray')->willReturn([]);
979+
980+
$controller = $this->makeController('/api/items?page=4');
981+
$this->request->setGlobal('get', ['page' => '4']);
982+
983+
$this->invoke($controller, 'paginate', [$builder, 20]);
984+
985+
$responseBody = json_decode($this->response->getBody(), true);
986+
987+
$this->assertSame(200, $this->response->getStatusCode());
988+
$this->assertSame([], $responseBody['data']);
989+
$this->assertSame(4, $responseBody['meta']['page']);
990+
$this->assertSame(50, $responseBody['meta']['total']);
991+
$this->assertSame(3, $responseBody['meta']['totalPages']);
992+
}
993+
898994
public function testPaginateWithCustomPerPage(): void
899995
{
900996
$data = [

0 commit comments

Comments
 (0)