Skip to content

Commit 07d60c2

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-51611' into PR-4
2 parents 317ffc2 + 3fd3c55 commit 07d60c2

File tree

2 files changed

+117
-6
lines changed

2 files changed

+117
-6
lines changed

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ protected function _getItemsData()
8484
->getProductCollection();
8585
$optionsFacetedData = $productCollection->getFacetedData($attribute->getAttributeCode());
8686

87+
if (count($optionsFacetedData) === 0
88+
&& $this->getAttributeIsFilterable($attribute) !== static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
89+
) {
90+
return $this->itemDataBuilder->build();
91+
}
92+
8793
$productSize = $productCollection->getSize();
8894

8995
$options = $attribute->getFrontend()
@@ -100,9 +106,8 @@ protected function _getItemsData()
100106
: 0;
101107
// Check filter type
102108
if (
103-
$count === 0
104-
&& $this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
105-
&& !$this->isOptionReducesResults($count, $productSize)
109+
$this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
110+
&& (!$this->isOptionReducesResults($count, $productSize) || $count === 0)
106111
) {
107112
continue;
108113
}

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

Lines changed: 109 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
namespace Magento\CatalogSearch\Test\Unit\Model\Layer\Filter;
88

9+
use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
910
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
1011
use PHPUnit_Framework_MockObject_MockObject as MockObject;
1112

@@ -104,9 +105,6 @@ protected function setUp()
104105
->disableOriginalConstructor()
105106
->setMethods(['getAttributeCode', 'getFrontend', 'getIsFilterable'])
106107
->getMock();
107-
$this->attribute->expects($this->atLeastOnce())
108-
->method('getFrontend')
109-
->will($this->returnValue($this->frontend));
110108

111109
$this->request = $this->getMockBuilder('\Magento\Framework\App\RequestInterface')
112110
->setMethods(['getParam'])
@@ -143,6 +141,9 @@ public function testApplyFilter()
143141
$this->attribute->expects($this->exactly(2))
144142
->method('getAttributeCode')
145143
->will($this->returnValue($attributeCode));
144+
$this->attribute->expects($this->atLeastOnce())
145+
->method('getFrontend')
146+
->will($this->returnValue($this->frontend));
146147

147148
$this->target->setAttributeModel($this->attribute);
148149

@@ -202,6 +203,9 @@ public function testGetItemsWithApply()
202203
$this->attribute->expects($this->exactly(2))
203204
->method('getAttributeCode')
204205
->will($this->returnValue($attributeCode));
206+
$this->attribute->expects($this->atLeastOnce())
207+
->method('getFrontend')
208+
->will($this->returnValue($this->frontend));
205209

206210
$this->target->setAttributeModel($this->attribute);
207211

@@ -283,6 +287,9 @@ public function testGetItemsWithoutApply()
283287
$this->attribute->expects($this->exactly(2))
284288
->method('getAttributeCode')
285289
->will($this->returnValue($attributeCode));
290+
$this->attribute->expects($this->atLeastOnce())
291+
->method('getFrontend')
292+
->will($this->returnValue($this->frontend));
286293

287294
$this->target->setAttributeModel($this->attribute);
288295

@@ -329,6 +336,105 @@ public function testGetItemsWithoutApply()
329336
$this->assertEquals($expectedFilterItems, $result);
330337
}
331338

339+
/**
340+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
341+
*/
342+
public function testGetItemsOnlyWithResults()
343+
{
344+
$attributeCode = 'attributeCode';
345+
$selectedOptions = [
346+
[
347+
'label' => 'selectedOptionLabel1',
348+
'value' => 'selectedOptionValue1',
349+
],
350+
[
351+
'label' => 'selectedOptionLabel2',
352+
'value' => 'selectedOptionValue2',
353+
],
354+
];
355+
$facetedData = [
356+
'selectedOptionValue1' => ['count' => 10],
357+
'selectedOptionValue2' => ['count' => 0],
358+
];
359+
$builtData = [
360+
[
361+
'label' => $selectedOptions[0]['label'],
362+
'value' => $selectedOptions[0]['value'],
363+
'count' => $facetedData[$selectedOptions[0]['value']]['count'],
364+
],
365+
];
366+
367+
$this->attribute->expects($this->atLeastOnce())
368+
->method('getAttributeCode')
369+
->willReturn($attributeCode);
370+
$this->attribute->expects($this->atLeastOnce())
371+
->method('getIsFilterable')
372+
->willReturn(AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS);
373+
$this->attribute->expects($this->atLeastOnce())
374+
->method('getFrontend')
375+
->will($this->returnValue($this->frontend));
376+
377+
$this->target->setAttributeModel($this->attribute);
378+
379+
$this->frontend->expects($this->once())
380+
->method('getSelectOptions')
381+
->willReturn($selectedOptions);
382+
383+
$this->fulltextCollection->expects($this->once())
384+
->method('getFacetedData')
385+
->willReturn($facetedData);
386+
$this->fulltextCollection->expects($this->once())
387+
->method('getSize')
388+
->will($this->returnValue(50));
389+
390+
$this->itemDataBuilder->expects($this->once())
391+
->method('addItemData')
392+
->with(
393+
$selectedOptions[0]['label'],
394+
$selectedOptions[0]['value'],
395+
$facetedData[$selectedOptions[0]['value']]['count']
396+
)
397+
->will($this->returnSelf());
398+
399+
$this->itemDataBuilder->expects($this->once())
400+
->method('build')
401+
->willReturn($builtData);
402+
403+
$expectedFilterItems = [
404+
$this->createFilterItem(0, $builtData[0]['label'], $builtData[0]['value'], $builtData[0]['count']),
405+
];
406+
$result = $this->target->getItems();
407+
408+
$this->assertEquals($expectedFilterItems, $result);
409+
}
410+
411+
/**
412+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
413+
*/
414+
public function testGetItemsIfFacetedDataIsEmpty()
415+
{
416+
$attributeCode = 'attributeCode';
417+
418+
$this->attribute->expects($this->atLeastOnce())
419+
->method('getAttributeCode')
420+
->willReturn($attributeCode);
421+
$this->attribute->expects($this->atLeastOnce())
422+
->method('getIsFilterable')
423+
->willReturn(0);
424+
425+
$this->target->setAttributeModel($this->attribute);
426+
427+
$this->fulltextCollection->expects($this->once())
428+
->method('getFacetedData')
429+
->willReturn([]);
430+
431+
$this->itemDataBuilder->expects($this->once())
432+
->method('build')
433+
->willReturn([]);
434+
435+
$this->assertEquals([], $this->target->getItems());
436+
}
437+
332438
/**
333439
* @param int $index
334440
* @param string $label

0 commit comments

Comments
 (0)