From af0d333687271526db15ee60d87158ab9a8515bd Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 17 Apr 2020 00:37:14 +0700 Subject: [PATCH] Pagination: open page > pageCount get last page --- system/Pager/Pager.php | 4 +++- tests/system/Pager/PagerTest.php | 6 ++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index ab936eef730a..5eb258cf9640 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -208,10 +208,12 @@ public function store(string $group, int $page, int $perPage = null, int $total, $this->ensureGroup($group, $perPage); $perPage = $perPage ?? $this->config->perPage; + $pageCount = (int)ceil($total / $perPage); + $page = $page > $pageCount ? $pageCount : $page; $this->groups[$group]['currentPage'] = $page; $this->groups[$group]['perPage'] = $perPage; $this->groups[$group]['total'] = $total; - $this->groups[$group]['pageCount'] = (int)ceil($total / $perPage); + $this->groups[$group]['pageCount'] = $pageCount; return $this; } diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index ee62573aefa8..5b976caea711 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -419,4 +419,10 @@ public function testBasedURI() $this->assertEquals((string)$expected, $this->pager->getPreviousPageURI('foo')); } + public function testAccessPageMoreThanPageCountGetLastPage() + { + $this->pager->store('default', 11, 1, 10); + $this->assertEquals(10, $this->pager->getCurrentPage()); + } + }