Skip to content

Commit

Permalink
ENGCOM-6455: #14971 Improper Handling of Pagination SEO #25337
Browse files Browse the repository at this point in the history
  • Loading branch information
VladimirZaets authored Jan 8, 2020
2 parents d1e27c4 + 191ee93 commit a2800e8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 1 deletion.
6 changes: 5 additions & 1 deletion app/code/Magento/Theme/Block/Html/Pager.php
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,11 @@ public function getLastPageUrl()
*/
public function getPageUrl($page)
{
return $this->getPagerUrl([$this->getPageVarName() => $page]);
return $this->getPagerUrl(
[
$this->getPageVarName() => $page > 1 ? $page : null,
]
);
}

/**
Expand Down
104 changes: 104 additions & 0 deletions app/code/Magento/Theme/Test/Unit/Block/Html/PagerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Theme\Test\Unit\Block\Html;

use Magento\Framework\App\Config;
use Magento\Framework\Data\Collection;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Element\Template\Context;
use Magento\Theme\Block\Html\Pager;
use PHPUnit\Framework\TestCase;

/**
* Test For Page class
*/
class PagerTest extends TestCase
{

/**
* @var Pager $pager
*/
private $pager;

/**
* @var Context $context
*/
private $context;

/**
* @var Config $scopeConfig
*/
private $scopeConfig;

/**
* @var UrlInterface $urlBuilderMock
*/
private $urlBuilderMock;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->context = $this->createMock(Context::class);
$this->urlBuilderMock = $this->createMock(UrlInterface::class);
$this->context->expects($this->any())
->method('getUrlBuilder')
->willReturn($this->urlBuilderMock);
$this->scopeConfig = $this->createMock(Config::class);
$this->pager = (new ObjectManager($this))->getObject(
Pager::class,
['context' => $this->context]
);
}

/**
* Verify current page Url
*
* @return void
*/
public function testGetPageUrl(): void
{
$expectedPageUrl = 'page-url';
$this->urlBuilderMock->expects($this->once())
->method('getUrl')
->willReturn($expectedPageUrl);
$this->assertEquals($expectedPageUrl, $this->pager->getPageUrl(0));
}

/**
* Verify get pages method.
*
* @return void
*/
public function testGetPages(): void
{
$expectedPages = range(1, 5);
$collectionMock = $this->createMock(Collection::class);
$collectionMock->expects($this->exactly(2))
->method('getCurPage')
->willReturn(2);
$collectionMock->expects($this->any())
->method('getLastPageNumber')
->willReturn(10);
$this->setCollectionProperty($collectionMock);
$this->assertEquals($expectedPages, $this->pager->getPages());
}

/**
* Set Collection
*
* @return void
*/
private function setCollectionProperty($collection): void
{
$reflection = new \ReflectionClass($this->pager);
$reflection_property = $reflection->getProperty('_collection');
$reflection_property->setAccessible(true);
$reflection_property->setValue($this->pager, $collection);
}
}

0 comments on commit a2800e8

Please sign in to comment.