Skip to content

Commit 92b29c6

Browse files
committed
Fix #28011 - remove page number from query on layered navigation swatch filter
1 parent f442fa9 commit 92b29c6

File tree

2 files changed

+150
-84
lines changed

2 files changed

+150
-84
lines changed

app/code/Magento/Swatches/Block/LayeredNavigation/RenderLayered.php

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@
55
*/
66
namespace Magento\Swatches\Block\LayeredNavigation;
77

8-
use Magento\Eav\Model\Entity\Attribute;
8+
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
9+
use Magento\Catalog\Model\Layer\Filter\Item as FilterItem;
910
use Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory;
10-
use Magento\Framework\View\Element\Template;
11+
use Magento\Eav\Model\Entity\Attribute;
1112
use Magento\Eav\Model\Entity\Attribute\Option;
12-
use Magento\Catalog\Model\Layer\Filter\Item as FilterItem;
13+
use Magento\Framework\App\ObjectManager;
14+
use Magento\Framework\View\Element\Template;
15+
use Magento\Framework\View\Element\Template\Context;
16+
use Magento\Swatches\Helper\Data;
17+
use Magento\Swatches\Helper\Media;
18+
use Magento\Theme\Block\Html\Pager;
1319

1420
/**
1521
* Class RenderLayered Render Swatches at Layered Navigation
@@ -37,7 +43,7 @@ class RenderLayered extends Template
3743
protected $eavAttribute;
3844

3945
/**
40-
* @var \Magento\Catalog\Model\Layer\Filter\AbstractFilter
46+
* @var AbstractFilter
4147
*/
4248
protected $filter;
4349

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

4955
/**
50-
* @var \Magento\Swatches\Helper\Data
56+
* @var Data
5157
*/
5258
protected $swatchHelper;
5359

5460
/**
55-
* @var \Magento\Swatches\Helper\Media
61+
* @var Media
5662
*/
5763
protected $mediaHelper;
5864

5965
/**
60-
* @param Template\Context $context
66+
* @var Pager
67+
*/
68+
private $htmlPagerBlock;
69+
70+
/**
71+
* @param Context $context
6172
* @param Attribute $eavAttribute
6273
* @param AttributeFactory $layerAttribute
63-
* @param \Magento\Swatches\Helper\Data $swatchHelper
64-
* @param \Magento\Swatches\Helper\Media $mediaHelper
74+
* @param Data $swatchHelper
75+
* @param Media $mediaHelper
6576
* @param array $data
77+
* @param Pager|null $htmlPagerBlock
6678
*/
6779
public function __construct(
68-
\Magento\Framework\View\Element\Template\Context $context,
80+
Context $context,
6981
Attribute $eavAttribute,
7082
AttributeFactory $layerAttribute,
71-
\Magento\Swatches\Helper\Data $swatchHelper,
72-
\Magento\Swatches\Helper\Media $mediaHelper,
73-
array $data = []
83+
Data $swatchHelper,
84+
Media $mediaHelper,
85+
array $data = [],
86+
?Pager $htmlPagerBlock = null
7487
) {
7588
$this->eavAttribute = $eavAttribute;
7689
$this->layerAttribute = $layerAttribute;
7790
$this->swatchHelper = $swatchHelper;
7891
$this->mediaHelper = $mediaHelper;
92+
$this->htmlPagerBlock = $htmlPagerBlock ?? ObjectManager::getInstance()->get(Pager::class);
7993

8094
parent::__construct($context, $data);
8195
}
@@ -114,15 +128,13 @@ public function getSwatchData()
114128
$attributeOptionIds = array_keys($attributeOptions);
115129
$swatches = $this->swatchHelper->getSwatchesByOptionsId($attributeOptionIds);
116130

117-
$data = [
131+
return [
118132
'attribute_id' => $this->eavAttribute->getId(),
119133
'attribute_code' => $this->eavAttribute->getAttributeCode(),
120134
'attribute_label' => $this->eavAttribute->getStoreLabel(),
121135
'options' => $attributeOptions,
122136
'swatches' => $swatches,
123137
];
124-
125-
return $data;
126138
}
127139

128140
/**
@@ -132,8 +144,20 @@ public function getSwatchData()
132144
*/
133145
public function buildUrl($attributeCode, $optionId)
134146
{
135-
$query = [$attributeCode => $optionId];
136-
return $this->_urlBuilder->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $query]);
147+
$query = [
148+
$attributeCode => $optionId,
149+
// exclude current page from urls
150+
$this->htmlPagerBlock->getPageVarName() => null
151+
];
152+
153+
return $this->_urlBuilder->getUrl(
154+
'*/*/*',
155+
[
156+
'_current' => true,
157+
'_use_rewrite' => true,
158+
'_query' => $query
159+
]
160+
);
137161
}
138162

139163
/**
@@ -192,7 +216,7 @@ protected function getOptionViewData(FilterItem $filterItem, Option $swatchOptio
192216
*/
193217
protected function isOptionVisible(FilterItem $filterItem)
194218
{
195-
return $this->isOptionDisabled($filterItem) && $this->isShowEmptyResults() ? false : true;
219+
return !($this->isOptionDisabled($filterItem) && $this->isShowEmptyResults());
196220
}
197221

198222
/**
@@ -234,8 +258,6 @@ protected function getFilterItemById(array $filterItems, $id)
234258
*/
235259
public function getSwatchPath($type, $filename)
236260
{
237-
$imagePath = $this->mediaHelper->getSwatchAttributeImage($type, $filename);
238-
239-
return $imagePath;
261+
return $this->mediaHelper->getSwatchAttributeImage($type, $filename);
240262
}
241263
}

app/code/Magento/Swatches/Test/Unit/Block/LayeredNavigation/RenderLayeredTest.php

Lines changed: 106 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3,69 +3,112 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
declare(strict_types=1);
7+
68
namespace Magento\Swatches\Test\Unit\Block\LayeredNavigation;
79

10+
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
11+
use Magento\Catalog\Model\Layer\Filter\Item;
12+
use Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory;
13+
use Magento\Eav\Model\Entity\Attribute;
14+
use Magento\Eav\Model\Entity\Attribute\Option;
15+
use Magento\Framework\App\RequestInterface;
16+
use Magento\Framework\Url;
17+
use Magento\Framework\View\Element\Template\Context;
18+
use Magento\Swatches\Block\LayeredNavigation\RenderLayered;
19+
use Magento\Swatches\Helper\Data;
20+
use Magento\Swatches\Helper\Media;
21+
use Magento\Theme\Block\Html\Pager;
22+
use PHPUnit\Framework\MockObject\MockObject;
23+
use PHPUnit\Framework\TestCase;
24+
825
/**
926
* Class RenderLayered Render Swatches at Layered Navigation
1027
*
1128
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1229
*/
13-
class RenderLayeredTest extends \PHPUnit\Framework\TestCase
30+
class RenderLayeredTest extends TestCase
1431
{
15-
/** @var \PHPUnit_Framework_MockObject_MockObject */
16-
protected $contextMock;
17-
18-
/** @var \PHPUnit_Framework_MockObject_MockObject */
19-
protected $requestMock;
20-
21-
/** @var \PHPUnit_Framework_MockObject_MockObject */
22-
protected $urlBuilder;
23-
24-
/** @var \PHPUnit_Framework_MockObject_MockObject */
25-
protected $eavAttributeMock;
26-
27-
/** @var \PHPUnit_Framework_MockObject_MockObject */
28-
protected $layerAttributeFactoryMock;
29-
30-
/** @var \PHPUnit_Framework_MockObject_MockObject */
31-
protected $layerAttributeMock;
32-
33-
/** @var \PHPUnit_Framework_MockObject_MockObject */
34-
protected $swatchHelperMock;
35-
36-
/** @var \PHPUnit_Framework_MockObject_MockObject */
37-
protected $mediaHelperMock;
38-
39-
/** @var \PHPUnit_Framework_MockObject_MockObject */
40-
protected $filterMock;
41-
42-
/** @var \PHPUnit_Framework_MockObject_MockObject */
43-
protected $block;
44-
45-
protected function setUp()
32+
/**
33+
* @var RenderLayered|MockObject
34+
*/
35+
private $block;
36+
37+
/**
38+
* @var Context|MockObject
39+
*/
40+
private $contextMock;
41+
42+
/**
43+
* @var RequestInterface|MockObject
44+
*/
45+
private $requestMock;
46+
47+
/**
48+
* @var Url|MockObject
49+
*/
50+
private $urlBuilder;
51+
52+
/**
53+
* @var Attribute|MockObject
54+
*/
55+
private $eavAttributeMock;
56+
57+
/**
58+
* @var AttributeFactory|MockObject
59+
*/
60+
private $layerAttributeFactoryMock;
61+
62+
/**
63+
* @var \Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute|MockObject
64+
*/
65+
private $layerAttributeMock;
66+
67+
/**
68+
* @var Data|MockObject
69+
*/
70+
private $swatchHelperMock;
71+
72+
/**
73+
* @var Media|MockObject
74+
*/
75+
private $mediaHelperMock;
76+
77+
/**
78+
* @var AbstractFilter|MockObject
79+
*/
80+
private $filterMock;
81+
82+
/**
83+
* @var Pager|MockObject
84+
*/
85+
private $htmlBlockPagerMock;
86+
87+
protected function setUp(): void
4688
{
47-
$this->contextMock = $this->createMock(\Magento\Framework\View\Element\Template\Context::class);
48-
$this->requestMock = $this->createMock(\Magento\Framework\App\RequestInterface::class);
89+
$this->contextMock = $this->createMock(Context::class);
90+
$this->requestMock = $this->createMock(RequestInterface::class);
4991
$this->urlBuilder = $this->createPartialMock(
50-
\Magento\Framework\Url::class,
92+
Url::class,
5193
['getCurrentUrl', 'getRedirectUrl', 'getUrl']
5294
);
53-
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
54-
$this->contextMock->expects($this->any())->method('getUrlBuilder')->willReturn($this->urlBuilder);
55-
$this->eavAttributeMock = $this->createMock(\Magento\Eav\Model\Entity\Attribute::class);
95+
$this->contextMock->method('getRequest')->willReturn($this->requestMock);
96+
$this->contextMock->method('getUrlBuilder')->willReturn($this->urlBuilder);
97+
$this->eavAttributeMock = $this->createMock(Attribute::class);
5698
$this->layerAttributeFactoryMock = $this->createPartialMock(
57-
\Magento\Catalog\Model\ResourceModel\Layer\Filter\AttributeFactory::class,
99+
AttributeFactory::class,
58100
['create']
59101
);
60102
$this->layerAttributeMock = $this->createPartialMock(
61103
\Magento\Catalog\Model\ResourceModel\Layer\Filter\Attribute::class,
62104
['getCount']
63105
);
64-
$this->swatchHelperMock = $this->createMock(\Magento\Swatches\Helper\Data::class);
65-
$this->mediaHelperMock = $this->createMock(\Magento\Swatches\Helper\Media::class);
66-
$this->filterMock = $this->createMock(\Magento\Catalog\Model\Layer\Filter\AbstractFilter::class);
106+
$this->swatchHelperMock = $this->createMock(Data::class);
107+
$this->mediaHelperMock = $this->createMock(Media::class);
108+
$this->filterMock = $this->createMock(AbstractFilter::class);
109+
$this->htmlBlockPagerMock = $this->createMock(Pager::class);
67110

68-
$this->block = $this->getMockBuilder(\Magento\Swatches\Block\LayeredNavigation\RenderLayered::class)
111+
$this->block = $this->getMockBuilder(RenderLayered::class)
69112
->setMethods(['filter', 'eavAttribute'])
70113
->setConstructorArgs(
71114
[
@@ -75,6 +118,7 @@ protected function setUp()
75118
$this->swatchHelperMock,
76119
$this->mediaHelperMock,
77120
[],
121+
$this->htmlBlockPagerMock
78122
]
79123
)
80124
->getMock();
@@ -92,13 +136,13 @@ public function testSetSwatchFilter()
92136

93137
public function testGetSwatchData()
94138
{
95-
/** @var \PHPUnit_Framework_MockObject_MockObject $item */
96-
$item1 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
97-
$item2 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
98-
$item3 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
99-
$item4 = $this->createMock(\Magento\Catalog\Model\Layer\Filter\Item::class);
139+
/** @var MockObject $item */
140+
$item1 = $this->createMock(Item::class);
141+
$item2 = $this->createMock(Item::class);
142+
$item3 = $this->createMock(Item::class);
143+
$item4 = $this->createMock(Item::class);
100144

101-
$item1->expects($this->any())->method('__call')->withConsecutive(
145+
$item1->method('__call')->withConsecutive(
102146
['getValue'],
103147
['getCount'],
104148
['getValue'],
@@ -112,17 +156,17 @@ public function testGetSwatchData()
112156
'Yellow'
113157
);
114158

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

117-
$item3->expects($this->any())->method('__call')->withConsecutive(
161+
$item3->method('__call')->withConsecutive(
118162
['getValue'],
119163
['getCount']
120164
)->willReturnOnConsecutiveCalls(
121165
'red',
122166
0
123167
);
124168

125-
$item4->expects($this->any())->method('__call')->withConsecutive(
169+
$item4->method('__call')->withConsecutive(
126170
['getValue'],
127171
['getCount'],
128172
['getValue'],
@@ -145,23 +189,23 @@ public function testGetSwatchData()
145189

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

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

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

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

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

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

166210
$this->filterMock->expects($this->once())->method('getAttributeModel')->willReturn($eavAttribute);
167211
$this->block->method('eavAttribute')->willReturn($eavAttribute);
@@ -184,7 +228,7 @@ public function testGetSwatchDataException()
184228
{
185229
$this->block->method('filter')->willReturn($this->filterMock);
186230
$this->block->setSwatchFilter($this->filterMock);
187-
$this->expectException('\RuntimeException');
231+
$this->expectException(\RuntimeException::class);
188232
$this->block->getSwatchData();
189233
}
190234

0 commit comments

Comments
 (0)