Skip to content

Commit 191ee93

Browse files
ENGCOM-6455: #14971 Improper Handling of Pagination SEO #25337
- Merge Pull Request #25337 from chickenland/magento2:2.3-develop - Merged commits: 1. c57d98c 2. 3f4642e 3. 2860283 4. 94e8685 5. 9c4991a
2 parents 87b0a4b + 9c4991a commit 191ee93

File tree

2 files changed

+109
-1
lines changed

2 files changed

+109
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,11 @@ public function getLastPageUrl()
450450
*/
451451
public function getPageUrl($page)
452452
{
453-
return $this->getPagerUrl([$this->getPageVarName() => $page]);
453+
return $this->getPagerUrl(
454+
[
455+
$this->getPageVarName() => $page > 1 ? $page : null,
456+
]
457+
);
454458
}
455459

456460
/**
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Theme\Test\Unit\Block\Html;
7+
8+
use Magento\Framework\App\Config;
9+
use Magento\Framework\Data\Collection;
10+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
11+
use Magento\Framework\UrlInterface;
12+
use Magento\Framework\View\Element\Template\Context;
13+
use Magento\Theme\Block\Html\Pager;
14+
use PHPUnit\Framework\TestCase;
15+
16+
/**
17+
* Test For Page class
18+
*/
19+
class PagerTest extends TestCase
20+
{
21+
22+
/**
23+
* @var Pager $pager
24+
*/
25+
private $pager;
26+
27+
/**
28+
* @var Context $context
29+
*/
30+
private $context;
31+
32+
/**
33+
* @var Config $scopeConfig
34+
*/
35+
private $scopeConfig;
36+
37+
/**
38+
* @var UrlInterface $urlBuilderMock
39+
*/
40+
private $urlBuilderMock;
41+
42+
/**
43+
* @inheritdoc
44+
*/
45+
protected function setUp()
46+
{
47+
$this->context = $this->createMock(Context::class);
48+
$this->urlBuilderMock = $this->createMock(UrlInterface::class);
49+
$this->context->expects($this->any())
50+
->method('getUrlBuilder')
51+
->willReturn($this->urlBuilderMock);
52+
$this->scopeConfig = $this->createMock(Config::class);
53+
$this->pager = (new ObjectManager($this))->getObject(
54+
Pager::class,
55+
['context' => $this->context]
56+
);
57+
}
58+
59+
/**
60+
* Verify current page Url
61+
*
62+
* @return void
63+
*/
64+
public function testGetPageUrl(): void
65+
{
66+
$expectedPageUrl = 'page-url';
67+
$this->urlBuilderMock->expects($this->once())
68+
->method('getUrl')
69+
->willReturn($expectedPageUrl);
70+
$this->assertEquals($expectedPageUrl, $this->pager->getPageUrl(0));
71+
}
72+
73+
/**
74+
* Verify get pages method.
75+
*
76+
* @return void
77+
*/
78+
public function testGetPages(): void
79+
{
80+
$expectedPages = range(1, 5);
81+
$collectionMock = $this->createMock(Collection::class);
82+
$collectionMock->expects($this->exactly(2))
83+
->method('getCurPage')
84+
->willReturn(2);
85+
$collectionMock->expects($this->any())
86+
->method('getLastPageNumber')
87+
->willReturn(10);
88+
$this->setCollectionProperty($collectionMock);
89+
$this->assertEquals($expectedPages, $this->pager->getPages());
90+
}
91+
92+
/**
93+
* Set Collection
94+
*
95+
* @return void
96+
*/
97+
private function setCollectionProperty($collection): void
98+
{
99+
$reflection = new \ReflectionClass($this->pager);
100+
$reflection_property = $reflection->getProperty('_collection');
101+
$reflection_property->setAccessible(true);
102+
$reflection_property->setValue($this->pager, $collection);
103+
}
104+
}

0 commit comments

Comments
 (0)