diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index f3d6d4bec39d..32843ba9e98d 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -163,6 +163,10 @@ public function setSegment(int $number, string $group = 'default') { $this->segment[$group] = $number; + // Recalculate current page + $this->ensureGroup($group); + $this->calculateCurrentPage($group); + return $this; } @@ -279,7 +283,15 @@ public function getPageURI(?int $page = null, string $group = 'default', bool $r $uri->setQueryArray($query); } - return $returnObject === true ? $uri : URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $uri->getQuery(), $uri->getFragment()); + return ($returnObject === true) + ? $uri + : URI::createURIString( + $uri->getScheme(), + $uri->getAuthority(), + $uri->getPath(), + $uri->getQuery(), + $uri->getFragment() + ); } /** @@ -383,6 +395,7 @@ protected function ensureGroup(string $group, ?int $perPage = null) } $this->groups[$group] = [ + 'currentUri' => clone current_url(true), 'uri' => clone current_url(true), 'hasMore' => false, 'total' => null, @@ -405,7 +418,8 @@ protected function calculateCurrentPage(string $group) { if (array_key_exists($group, $this->segment)) { try { - $this->groups[$group]['currentPage'] = (int) $this->groups[$group]['uri']->setSilent(false)->getSegment($this->segment[$group]); + $this->groups[$group]['currentPage'] = (int) $this->groups[$group]['currentUri'] + ->setSilent(false)->getSegment($this->segment[$group]); } catch (HTTPException $e) { $this->groups[$group]['currentPage'] = 1; } diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 745a8464111b..6c6b14fdca7b 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -11,7 +11,10 @@ namespace CodeIgniter\Pager; +use CodeIgniter\Config\Factories; +use CodeIgniter\HTTP\IncomingRequest; use CodeIgniter\HTTP\URI; +use CodeIgniter\HTTP\UserAgent; use CodeIgniter\Pager\Exceptions\PagerException; use CodeIgniter\Test\CIUnitTestCase; use Config\App; @@ -36,18 +39,31 @@ protected function setUp(): void { parent::setUp(); - $_SERVER['HTTP_HOST'] = 'example.com'; - $_SERVER['REQUEST_URI'] = '/'; + $this->createPager('/'); + } + + private function createPager(string $requestUri): void + { + $_SERVER['REQUEST_URI'] = $requestUri; + $_SERVER['SCRIPT_NAME'] = '/index.php'; $_GET = []; - $config = new App(); - $config->baseURL = 'http://example.com/'; - $request = Services::request($config); - $request->uri = new URI('http://example.com'); + $config = new App(); + $config->baseURL = 'http://example.com/'; + $config->indexPage = ''; + Factories::injectMock('config', 'App', $config); + + $request = new IncomingRequest( + $config, + new URI($config->baseURL . ltrim($requestUri, '/')), + 'php://input', + new UserAgent() + ); + $request = $request->withMethod('GET'); + Services::injectMock('request', $request); Services::injectMock('request', $request); - $_GET = []; $this->config = new Pager(); $this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer()); } @@ -145,11 +161,11 @@ public function testStoreWithQueries() $this->pager->store('default', 3, 25, 100); - $this->assertSame('http://example.com/index.php?page=2&foo=bar', $this->pager->getPreviousPageURI()); - $this->assertSame('http://example.com/index.php?page=4&foo=bar', $this->pager->getNextPageURI()); - $this->assertSame('http://example.com/index.php?page=5&foo=bar', $this->pager->getPageURI(5)); + $this->assertSame('http://example.com/?page=2&foo=bar', $this->pager->getPreviousPageURI()); + $this->assertSame('http://example.com/?page=4&foo=bar', $this->pager->getNextPageURI()); + $this->assertSame('http://example.com/?page=5&foo=bar', $this->pager->getPageURI(5)); $this->assertSame( - 'http://example.com/index.php?foo=bar&page=5', + 'http://example.com/?foo=bar&page=5', $this->pager->only(['foo'])->getPageURI(5) ); } @@ -213,6 +229,16 @@ public function testGetCurrentPageDetectsGroupedURI() $this->assertSame(2, $this->pager->getCurrentPage('foo')); } + public function testGetCurrentPageFromSegment() + { + $this->createPager('/page/2'); + + $this->pager->setPath('foo'); + $this->pager->setSegment(2); + + $this->assertSame(2, $this->pager->getCurrentPage()); + } + public function testGetTotalPagesDefaultsToOne() { $this->assertSame(1, $this->pager->getPageCount());