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

#14971 Improper Handling of Pagination SEO #25337

Merged
merged 5 commits into from
Jan 9, 2020
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
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);
}
}