Skip to content

Commit 6548d3c

Browse files
fix issue
1 parent ac3aa70 commit 6548d3c

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

app/code/Magento/Theme/Block/Html/Pager.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Theme\Block\Html;
78

89
/**
@@ -466,7 +467,30 @@ public function getPageUrl($page)
466467
*/
467468
public function getLimitUrl($limit)
468469
{
469-
return $this->getPagerUrl([$this->getLimitVarName() => $limit]);
470+
return $this->getPagerUrl($this->getPageLimitParams($limit));
471+
}
472+
473+
/**
474+
* Return page limit params
475+
*
476+
* @param int $limit
477+
* @return array
478+
*/
479+
private function getPageLimitParams(int $limit): array
480+
{
481+
$data = [$this->getLimitVarName() => $limit];
482+
483+
$currentPage = $this->getCurrentPage();
484+
if ($currentPage === 1) {
485+
return $data;
486+
}
487+
488+
$availableCount = (int) ceil($this->getTotalNum() / $limit);
489+
if ($availableCount < $currentPage) {
490+
$data = array_merge($data, [$this->getPageVarName() => $availableCount === 1 ? null : $availableCount]);
491+
}
492+
493+
return $data;
470494
}
471495

472496
/**

app/code/Magento/Theme/Test/Unit/Block/Html/PagerTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,60 @@ public function testGetPages(): void
8989
$this->assertEquals($expectedPages, $this->pager->getPages());
9090
}
9191

92+
/**
93+
* Test get limit url.
94+
*
95+
* @dataProvider limitUrlDataProvider
96+
*
97+
* @param int $page
98+
* @param int $size
99+
* @param int $limit
100+
* @param array $expectedParams
101+
* @return void
102+
*/
103+
public function testGetLimitUrl(int $page, int $size, int $limit, array $expectedParams): void
104+
{
105+
$expectedArray = [
106+
'_current' => true,
107+
'_escape' => true,
108+
'_use_rewrite' => true,
109+
'_fragment' => null,
110+
'_query' => $expectedParams,
111+
];
112+
113+
$collectionMock = $this->createMock(Collection::class);
114+
$collectionMock->expects($this->once())
115+
->method('getCurPage')
116+
->willReturn($page);
117+
$collectionMock->expects($this->once())
118+
->method('getSize')
119+
->willReturn($size);
120+
$this->setCollectionProperty($collectionMock);
121+
122+
$this->urlBuilderMock->expects($this->once())
123+
->method('getUrl')
124+
->with('*/*/*', $expectedArray);
125+
126+
$this->pager->getLimitUrl($limit);
127+
}
128+
129+
/**
130+
* DataProvider for testGetLimitUrl
131+
*
132+
* @return array
133+
*/
134+
public function limitUrlDataProvider(): array
135+
{
136+
return [
137+
[2, 21, 10, ['limit' => 10]],
138+
[3, 21, 10, ['limit' => 10]],
139+
[2, 21, 20, ['limit' => 20]],
140+
[3, 21, 50, ['limit' => 50, 'p' => null]],
141+
[2, 11, 20, ['limit' => 20, 'p' => null]],
142+
[4, 40, 20, ['limit' => 20, 'p' => 2]],
143+
];
144+
}
145+
92146
/**
93147
* Set Collection
94148
*

0 commit comments

Comments
 (0)