Skip to content

Commit 3e9c3c7

Browse files
committed
ACP2E-87: Product price range filter exception on category page
1 parent 58ede28 commit 3e9c3c7

File tree

2 files changed

+59
-26
lines changed

2 files changed

+59
-26
lines changed

app/code/Magento/CatalogSearch/Model/Layer/Filter/Price.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ protected function _renderRangeLabel($fromPrice, $toPrice, $isLast = false)
185185
$toPrice = empty($toPrice) ? $toPrice : $toPrice * $this->getCurrencyRate();
186186

187187
$formattedFromPrice = $this->priceCurrency->format($fromPrice);
188-
if ($isLast) {
188+
if ($isLast || $toPrice === '') {
189189
return __('%1 and above', $formattedFromPrice);
190190
} elseif ($fromPrice == $toPrice && $this->dataProvider->getOnePriceIntervalValue()) {
191191
return $formattedFromPrice;

app/code/Magento/CatalogSearch/Test/Unit/Model/Layer/Filter/PriceTest.php

Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ protected function setUp(): void
102102
->onlyMethods(['getState', 'getProductCollection'])
103103
->getMock();
104104

105-
$this->state = $this->getMockBuilder(State::class)
106-
->disableOriginalConstructor()
107-
->onlyMethods(['addFilter'])
108-
->getMock();
105+
$this->state = new State();
109106
$this->layer->expects($this->any())
110107
->method('getState')
111108
->willReturn($this->state);
@@ -129,15 +126,24 @@ protected function setUp(): void
129126
->onlyMethods(['create'])
130127
->getMock();
131128

132-
$filterItem = $this->getMockBuilder(Item::class)
133-
->disableOriginalConstructor()
134-
->addMethods(['setFilter', 'setLabel', 'setValue', 'setCount'])
135-
->getMock();
136-
$filterItem->expects($this->any())
137-
->method($this->anything())->willReturnSelf();
138129
$this->filterItemFactory->expects($this->any())
139130
->method('create')
140-
->willReturn($filterItem);
131+
->willReturnCallback(
132+
function (array $data) {
133+
return new Item(
134+
$this->createMock(\Magento\Framework\UrlInterface::class),
135+
$this->createMock(\Magento\Theme\Block\Html\Pager::class),
136+
$data
137+
);
138+
}
139+
);
140+
$priceFormatter = $this->createMock(\Magento\Framework\Pricing\PriceCurrencyInterface::class);
141+
$priceFormatter->method('format')
142+
->willReturnCallback(
143+
function ($number) {
144+
return sprintf('$%01.2f', $number);
145+
}
146+
);
141147

142148
$escaper = $this->getMockBuilder(Escaper::class)
143149
->disableOriginalConstructor()
@@ -160,7 +166,8 @@ protected function setUp(): void
160166
'layer' => $this->layer,
161167
'itemDataBuilder' => $this->itemDataBuilder,
162168
'filterItemFactory' => $this->filterItemFactory,
163-
'escaper' => $escaper
169+
'escaper' => $escaper,
170+
'priceCurrency' => $priceFormatter,
164171
]
165172
);
166173
}
@@ -209,29 +216,55 @@ public function applyWithEmptyRequestDataProvider(): array
209216
}
210217

211218
/**
212-
* @return void
213-
*/
214-
public function testApply(): void
219+
* @dataProvider applyDataProvider
220+
*/
221+
public function testApply(string $filter, array $expected): void
215222
{
216-
$priceId = '15-50';
217-
$requestVar = 'test_request_var';
218-
219-
$this->target->setRequestVar($requestVar);
223+
$requestVar = 'price';
220224
$this->request->expects($this->exactly(1))
221225
->method('getParam')
222-
->willReturnCallback(
223-
function ($field) use ($requestVar, $priceId) {
224-
$this->assertContains($field, [$requestVar, 'id']);
225-
return $priceId;
226-
}
227-
);
226+
->with($requestVar)
227+
->willReturn($filter);
228228

229229
$this->fulltextCollection->expects($this->once())
230230
->method('addFieldToFilter')
231231
->with('price')->willReturnSelf();
232232

233233
$this->target->setCurrencyRate(1);
234234
$this->target->apply($this->request);
235+
$actual = [];
236+
foreach ($this->state->getFilters() as $item) {
237+
$actual[] = ['label' => $item->getLabel(), 'value' => $item->getValue(), 'count' => $item->getCount()];
238+
}
239+
240+
$this->assertEquals($expected, $actual);
241+
}
242+
243+
/**
244+
* @return array
245+
*/
246+
public function applyDataProvider(): array
247+
{
248+
return [
249+
[
250+
'10-50',
251+
[
252+
['label' => '$10.00 - $49.99', 'value' => ['10', '50'], 'count' => '0'],
253+
]
254+
],
255+
[
256+
'-50',
257+
[
258+
['label' => '$0.00 - $49.99', 'value' => ['', '50'], 'count' => '0'],
259+
]
260+
],
261+
[
262+
'10-',
263+
[
264+
['label' => '$10.00 and above', 'value' => ['10', ''], 'count' => '0'],
265+
]
266+
]
267+
];
235268
}
236269

237270
/**

0 commit comments

Comments
 (0)