Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [Pager] can't get correct current page from segment #5803

Merged
merged 7 commits into from
Mar 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions system/Pager/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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()
);
}

/**
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down
48 changes: 37 additions & 11 deletions tests/system/Pager/PagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
}
Expand Down Expand Up @@ -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)
);
}
Expand Down Expand Up @@ -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());
Expand Down