From f83160b2be6393938a46bf04e378c05b21ed7e2f Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 16 Mar 2022 11:34:22 +0900 Subject: [PATCH 1/7] test: refactor setUp() $_SERVER['HTTP_HOST'] is not used in Pager or Request. --- tests/system/Pager/PagerTest.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 745a8464111b..1259e0065b50 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -36,18 +36,16 @@ protected function setUp(): void { parent::setUp(); - $_SERVER['HTTP_HOST'] = 'example.com'; $_SERVER['REQUEST_URI'] = '/'; $_GET = []; $config = new App(); $config->baseURL = 'http://example.com/'; $request = Services::request($config); - $request->uri = new URI('http://example.com'); + $request->uri = new URI($config->baseURL); Services::injectMock('request', $request); - $_GET = []; $this->config = new Pager(); $this->pager = new \CodeIgniter\Pager\Pager($this->config, Services::renderer()); } From d071fc4bb97e7d83b9724e9f98c1874e5cb37140 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 16 Mar 2022 11:37:07 +0900 Subject: [PATCH 2/7] test: extract createPager() method --- tests/system/Pager/PagerTest.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 1259e0065b50..4a317fcdef42 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -36,13 +36,18 @@ protected function setUp(): void { parent::setUp(); - $_SERVER['REQUEST_URI'] = '/'; + $this->createPager('/'); + } + + private function createPager(string $path): void + { + $_SERVER['REQUEST_URI'] = $path; $_GET = []; $config = new App(); $config->baseURL = 'http://example.com/'; $request = Services::request($config); - $request->uri = new URI($config->baseURL); + $request->uri = new URI($config->baseURL . ltrim($path, '/')); Services::injectMock('request', $request); From 82a5a98e6ff887323c008a01f73e5fecfe7d0c1a Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 16 Mar 2022 11:45:00 +0900 Subject: [PATCH 3/7] test: fix Request and Config state Setting $request->uri later does not make correct state of Request. At least Request::$path is incorrect. --- tests/system/Pager/PagerTest.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 4a317fcdef42..c4b180a00f05 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; @@ -46,8 +49,16 @@ private function createPager(string $path): void $config = new App(); $config->baseURL = 'http://example.com/'; - $request = Services::request($config); - $request->uri = new URI($config->baseURL . ltrim($path, '/')); + Factories::injectMock('config', 'App', $config); + + $request = new IncomingRequest( + $config, + new URI($config->baseURL . ltrim($path, '/')), + 'php://input', + new UserAgent() + ); + $request = $request->withMethod('GET'); + Services::injectMock('request', $request); Services::injectMock('request', $request); @@ -148,11 +159,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/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/index.php?foo=bar&page=5', + 'http://example.com/index.php/?foo=bar&page=5', $this->pager->only(['foo'])->getPageURI(5) ); } From 1094c371bc32e253bf4abea9c211fafcf86a69aa Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 16 Mar 2022 12:55:16 +0900 Subject: [PATCH 4/7] test: remove `index.php` from URI --- tests/system/Pager/PagerTest.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index c4b180a00f05..e3f61694d76b 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -45,10 +45,12 @@ protected function setUp(): void private function createPager(string $path): void { $_SERVER['REQUEST_URI'] = $path; + $_SERVER['SCRIPT_NAME'] = '/index.php'; $_GET = []; - $config = new App(); - $config->baseURL = 'http://example.com/'; + $config = new App(); + $config->baseURL = 'http://example.com/'; + $config->indexPage = ''; Factories::injectMock('config', 'App', $config); $request = new IncomingRequest( @@ -159,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) ); } From 41845d4ebe878caa519bdf8a355cb64765951afc Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 16 Mar 2022 13:05:49 +0900 Subject: [PATCH 5/7] fix: can't get current page from segment --- system/Pager/Pager.php | 7 ++++++- tests/system/Pager/PagerTest.php | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index f3d6d4bec39d..76eed216f5d5 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; } @@ -383,6 +387,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 +410,7 @@ 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 e3f61694d76b..085c569864fa 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -229,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()); From e07611de529ca9c580b18a38788186c0812111a7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 16 Mar 2022 13:28:39 +0900 Subject: [PATCH 6/7] test: rename variable name --- tests/system/Pager/PagerTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/system/Pager/PagerTest.php b/tests/system/Pager/PagerTest.php index 085c569864fa..6c6b14fdca7b 100644 --- a/tests/system/Pager/PagerTest.php +++ b/tests/system/Pager/PagerTest.php @@ -42,9 +42,9 @@ protected function setUp(): void $this->createPager('/'); } - private function createPager(string $path): void + private function createPager(string $requestUri): void { - $_SERVER['REQUEST_URI'] = $path; + $_SERVER['REQUEST_URI'] = $requestUri; $_SERVER['SCRIPT_NAME'] = '/index.php'; $_GET = []; @@ -55,7 +55,7 @@ private function createPager(string $path): void $request = new IncomingRequest( $config, - new URI($config->baseURL . ltrim($path, '/')), + new URI($config->baseURL . ltrim($requestUri, '/')), 'php://input', new UserAgent() ); From 65f5f6f6032f64a9ffd3349f7784118d7001d772 Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 16 Mar 2022 13:39:25 +0900 Subject: [PATCH 7/7] refactor: break long lines --- system/Pager/Pager.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/system/Pager/Pager.php b/system/Pager/Pager.php index 76eed216f5d5..32843ba9e98d 100644 --- a/system/Pager/Pager.php +++ b/system/Pager/Pager.php @@ -283,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() + ); } /** @@ -410,7 +418,8 @@ protected function calculateCurrentPage(string $group) { if (array_key_exists($group, $this->segment)) { try { - $this->groups[$group]['currentPage'] = (int) $this->groups[$group]['currentUri']->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; }