Skip to content

Commit

Permalink
Fix #28011 - remove page number from query on layered navigation swat…
Browse files Browse the repository at this point in the history
…ch filter
  • Loading branch information
Bartlomiejsz committed Apr 28, 2020
1 parent f442fa9 commit 92b29c6
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 84 deletions.
66 changes: 44 additions & 22 deletions app/code/Magento/Swatches/Block/LayeredNavigation/RenderLayered.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@
*/
namespace Magento\Swatches\Block\LayeredNavigation;

use Magento\Eav\Model\Entity\Attribute;
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Filter\Item as FilterItem;
use Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory;
use Magento\Framework\View\Element\Template;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Eav\Model\Entity\Attribute\Option;
use Magento\Catalog\Model\Layer\Filter\Item as FilterItem;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\View\Element\Template;
use Magento\Framework\View\Element\Template\Context;
use Magento\Swatches\Helper\Data;
use Magento\Swatches\Helper\Media;
use Magento\Theme\Block\Html\Pager;

/**
* Class RenderLayered Render Swatches at Layered Navigation
Expand Down Expand Up @@ -37,7 +43,7 @@ class RenderLayered extends Template
protected $eavAttribute;

/**
* @var \Magento\Catalog\Model\Layer\Filter\AbstractFilter
* @var AbstractFilter
*/
protected $filter;

Expand All @@ -47,35 +53,43 @@ class RenderLayered extends Template
protected $layerAttribute;

/**
* @var \Magento\Swatches\Helper\Data
* @var Data
*/
protected $swatchHelper;

/**
* @var \Magento\Swatches\Helper\Media
* @var Media
*/
protected $mediaHelper;

/**
* @param Template\Context $context
* @var Pager
*/
private $htmlPagerBlock;

/**
* @param Context $context
* @param Attribute $eavAttribute
* @param AttributeFactory $layerAttribute
* @param \Magento\Swatches\Helper\Data $swatchHelper
* @param \Magento\Swatches\Helper\Media $mediaHelper
* @param Data $swatchHelper
* @param Media $mediaHelper
* @param array $data
* @param Pager|null $htmlPagerBlock
*/
public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Context $context,
Attribute $eavAttribute,
AttributeFactory $layerAttribute,
\Magento\Swatches\Helper\Data $swatchHelper,
\Magento\Swatches\Helper\Media $mediaHelper,
array $data = []
Data $swatchHelper,
Media $mediaHelper,
array $data = [],
?Pager $htmlPagerBlock = null
) {
$this->eavAttribute = $eavAttribute;
$this->layerAttribute = $layerAttribute;
$this->swatchHelper = $swatchHelper;
$this->mediaHelper = $mediaHelper;
$this->htmlPagerBlock = $htmlPagerBlock ?? ObjectManager::getInstance()->get(Pager::class);

parent::__construct($context, $data);
}
Expand Down Expand Up @@ -114,15 +128,13 @@ public function getSwatchData()
$attributeOptionIds = array_keys($attributeOptions);
$swatches = $this->swatchHelper->getSwatchesByOptionsId($attributeOptionIds);

$data = [
return [
'attribute_id' => $this->eavAttribute->getId(),
'attribute_code' => $this->eavAttribute->getAttributeCode(),
'attribute_label' => $this->eavAttribute->getStoreLabel(),
'options' => $attributeOptions,
'swatches' => $swatches,
];

return $data;
}

/**
Expand All @@ -132,8 +144,20 @@ public function getSwatchData()
*/
public function buildUrl($attributeCode, $optionId)
{
$query = [$attributeCode => $optionId];
return $this->_urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $query]);
$query = [
$attributeCode => $optionId,
// exclude current page from urls
$this->htmlPagerBlock->getPageVarName() => null
];

return $this->_urlBuilder->getUrl(
'*/*/*',
[
'_current' => true,
'_use_rewrite' => true,
'_query' => $query
]
);
}

/**
Expand Down Expand Up @@ -192,7 +216,7 @@ protected function getOptionViewData(FilterItem $filterItem, Option $swatchOptio
*/
protected function isOptionVisible(FilterItem $filterItem)
{
return $this->isOptionDisabled($filterItem) && $this->isShowEmptyResults() ? false : true;
return !($this->isOptionDisabled($filterItem) && $this->isShowEmptyResults());
}

/**
Expand Down Expand Up @@ -234,8 +258,6 @@ protected function getFilterItemById(array $filterItems, $id)
*/
public function getSwatchPath($type, $filename)
{
$imagePath = $this->mediaHelper->getSwatchAttributeImage($type, $filename);

return $imagePath;
return $this->mediaHelper->getSwatchAttributeImage($type, $filename);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,112 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Swatches\Test\Unit\Block\LayeredNavigation;

use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Catalog\Model\Layer\Filter\Item;
use Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory;
use Magento\Eav\Model\Entity\Attribute;
use Magento\Eav\Model\Entity\Attribute\Option;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Url;
use Magento\Framework\View\Element\Template\Context;
use Magento\Swatches\Block\LayeredNavigation\RenderLayered;
use Magento\Swatches\Helper\Data;
use Magento\Swatches\Helper\Media;
use Magento\Theme\Block\Html\Pager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

/**
* Class RenderLayered Render Swatches at Layered Navigation
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class RenderLayeredTest extends \PHPUnit\Framework\TestCase
class RenderLayeredTest extends TestCase
{
/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $contextMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $requestMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $urlBuilder;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $eavAttributeMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $layerAttributeFactoryMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $layerAttributeMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $swatchHelperMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $mediaHelperMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $filterMock;

/** @var \PHPUnit_Framework_MockObject_MockObject */
protected $block;

protected function setUp()
/**
* @var RenderLayered|MockObject
*/
private $block;

/**
* @var Context|MockObject
*/
private $contextMock;

/**
* @var RequestInterface|MockObject
*/
private $requestMock;

/**
* @var Url|MockObject
*/
private $urlBuilder;

/**
* @var Attribute|MockObject
*/
private $eavAttributeMock;

/**
* @var AttributeFactory|MockObject
*/
private $layerAttributeFactoryMock;

/**
* @var \Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute|MockObject
*/
private $layerAttributeMock;

/**
* @var Data|MockObject
*/
private $swatchHelperMock;

/**
* @var Media|MockObject
*/
private $mediaHelperMock;

/**
* @var AbstractFilter|MockObject
*/
private $filterMock;

/**
* @var Pager|MockObject
*/
private $htmlBlockPagerMock;

protected function setUp(): void
{
$this->contextMock = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
$this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
$this->contextMock = $this->createMock(Context::class);
$this->requestMock = $this->createMock(RequestInterface::class);
$this->urlBuilder = $this->createPartialMock(
\Magento\Framework\Url::class,
Url::class,
['getCurrentUrl', 'getRedirectUrl', 'getUrl']
);
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
$this->contextMock->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
$this->eavAttributeMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute::class);
$this->contextMock->method('getRequest')->willReturn($this->requestMock);
$this->contextMock->method('getUrlBuilder')->willReturn($this->urlBuilder);
$this->eavAttributeMock = $this->createMock(Attribute::class);
$this->layerAttributeFactoryMock = $this->createPartialMock(
\Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory::class,
AttributeFactory::class,
['create']
);
$this->layerAttributeMock = $this->createPartialMock(
\Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute::class,
['getCount']
);
$this->swatchHelperMock = $this->createMock(\Magento\Swatches\Helper\Data::class);
$this->mediaHelperMock = $this->createMock(\Magento\Swatches\Helper\Media::class);
$this->filterMock = $this->createMock(\Magento\Catalog\Model\Layer\Filter\AbstractFilter::class);
$this->swatchHelperMock = $this->createMock(Data::class);
$this->mediaHelperMock = $this->createMock(Media::class);
$this->filterMock = $this->createMock(AbstractFilter::class);
$this->htmlBlockPagerMock = $this->createMock(Pager::class);

$this->block = $this->getMockBuilder(\Magento\Swatches\Block\LayeredNavigation\RenderLayered::class)
$this->block = $this->getMockBuilder(RenderLayered::class)
->setMethods(['filter', 'eavAttribute'])
->setConstructorArgs(
[
Expand All @@ -75,6 +118,7 @@ protected function setUp()
$this->swatchHelperMock,
$this->mediaHelperMock,
[],
$this->htmlBlockPagerMock
]
)
->getMock();
Expand All @@ -92,13 +136,13 @@ public function testSetSwatchFilter()

public function testGetSwatchData()
{
/** @var \PHPUnit_Framework_MockObject_MockObject $item */
$item1 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
$item2 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
$item3 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
$item4 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
/** @var MockObject $item */
$item1 = $this->createMock(Item::class);
$item2 = $this->createMock(Item::class);
$item3 = $this->createMock(Item::class);
$item4 = $this->createMock(Item::class);

$item1->expects($this->any())->method('__call')->withConsecutive(
$item1->method('__call')->withConsecutive(
['getValue'],
['getCount'],
['getValue'],
Expand All @@ -112,17 +156,17 @@ public function testGetSwatchData()
'Yellow'
);

$item2->expects($this->any())->method('__call')->with('getValue')->willReturn('blue');
$item2->method('__call')->with('getValue')->willReturn('blue');

$item3->expects($this->any())->method('__call')->withConsecutive(
$item3->method('__call')->withConsecutive(
['getValue'],
['getCount']
)->willReturnOnConsecutiveCalls(
'red',
0
);

$item4->expects($this->any())->method('__call')->withConsecutive(
$item4->method('__call')->withConsecutive(
['getValue'],
['getCount'],
['getValue'],
Expand All @@ -145,23 +189,23 @@ public function testGetSwatchData()

$this->block->method('filter')->willReturn($this->filterMock);

$option1 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
$option1->expects($this->any())->method('getValue')->willReturn('yellow');
$option1 = $this->createMock(Option::class);
$option1->method('getValue')->willReturn('yellow');

$option2 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
$option2->expects($this->any())->method('getValue')->willReturn(null);
$option2 = $this->createMock(Option::class);
$option2->method('getValue')->willReturn(null);

$option3 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
$option3->expects($this->any())->method('getValue')->willReturn('red');
$option3 = $this->createMock(Option::class);
$option3->method('getValue')->willReturn('red');

$option4 = $this->createMock(\Magento\Eav\Model\Entity\Attribute\Option::class);
$option4->expects($this->any())->method('getValue')->willReturn('green');
$option4 = $this->createMock(Option::class);
$option4->method('getValue')->willReturn('green');

$eavAttribute = $this->createMock(\Magento\Catalog\Model\ResourceModel\Eav\Attribute::class);
$eavAttribute->expects($this->once())
->method('getOptions')
->willReturn([$option1, $option2, $option3, $option4]);
$eavAttribute->expects($this->any())->method('getIsFilterable')->willReturn(0);
$eavAttribute->method('getIsFilterable')->willReturn(0);

$this->filterMock->expects($this->once())->method('getAttributeModel')->willReturn($eavAttribute);
$this->block->method('eavAttribute')->willReturn($eavAttribute);
Expand All @@ -184,7 +228,7 @@ public function testGetSwatchDataException()
{
$this->block->method('filter')->willReturn($this->filterMock);
$this->block->setSwatchFilter($this->filterMock);
$this->expectException('\RuntimeException');
$this->expectException(\RuntimeException::class);
$this->block->getSwatchData();
}

Expand Down

0 comments on commit 92b29c6

Please sign in to comment.