Skip to content

Commit

Permalink
MAGETWO-51611: Layered navigation include list of all product attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Valeriy Nayda committed Apr 28, 2016
1 parent 3fa0347 commit 3fd3c55
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 6 deletions.
11 changes: 8 additions & 3 deletions app/code/Magento/CatalogSearch/Model/Layer/Filter/Attribute.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ protected function _getItemsData()
->getProductCollection();
$optionsFacetedData = $productCollection->getFacetedData($attribute->getAttributeCode());

if (count($optionsFacetedData) === 0
&& $this->getAttributeIsFilterable($attribute) !== static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
) {
return $this->itemDataBuilder->build();
}

$productSize = $productCollection->getSize();

$options = $attribute->getFrontend()
Expand All @@ -100,9 +106,8 @@ protected function _getItemsData()
: 0;
// Check filter type
if (
$count === 0
&& $this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
&& !$this->isOptionReducesResults($count, $productSize)
$this->getAttributeIsFilterable($attribute) === static::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS
&& (!$this->isOptionReducesResults($count, $productSize) || $count === 0)
) {
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

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

use Magento\Catalog\Model\Layer\Filter\AbstractFilter;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper;
use PHPUnit_Framework_MockObject_MockObject as MockObject;

Expand Down Expand Up @@ -104,9 +105,6 @@ protected function setUp()
->disableOriginalConstructor()
->setMethods(['getAttributeCode', 'getFrontend', 'getIsFilterable'])
->getMock();
$this->attribute->expects($this->atLeastOnce())
->method('getFrontend')
->will($this->returnValue($this->frontend));

$this->request = $this->getMockBuilder('\Magento\Framework\App\RequestInterface')
->setMethods(['getParam'])
Expand Down Expand Up @@ -143,6 +141,9 @@ public function testApplyFilter()
$this->attribute->expects($this->exactly(2))
->method('getAttributeCode')
->will($this->returnValue($attributeCode));
$this->attribute->expects($this->atLeastOnce())
->method('getFrontend')
->will($this->returnValue($this->frontend));

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

Expand Down Expand Up @@ -202,6 +203,9 @@ public function testGetItemsWithApply()
$this->attribute->expects($this->exactly(2))
->method('getAttributeCode')
->will($this->returnValue($attributeCode));
$this->attribute->expects($this->atLeastOnce())
->method('getFrontend')
->will($this->returnValue($this->frontend));

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

Expand Down Expand Up @@ -283,6 +287,9 @@ public function testGetItemsWithoutApply()
$this->attribute->expects($this->exactly(2))
->method('getAttributeCode')
->will($this->returnValue($attributeCode));
$this->attribute->expects($this->atLeastOnce())
->method('getFrontend')
->will($this->returnValue($this->frontend));

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

Expand Down Expand Up @@ -329,6 +336,105 @@ public function testGetItemsWithoutApply()
$this->assertEquals($expectedFilterItems, $result);
}

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testGetItemsOnlyWithResults()
{
$attributeCode = 'attributeCode';
$selectedOptions = [
[
'label' => 'selectedOptionLabel1',
'value' => 'selectedOptionValue1',
],
[
'label' => 'selectedOptionLabel2',
'value' => 'selectedOptionValue2',
],
];
$facetedData = [
'selectedOptionValue1' => ['count' => 10],
'selectedOptionValue2' => ['count' => 0],
];
$builtData = [
[
'label' => $selectedOptions[0]['label'],
'value' => $selectedOptions[0]['value'],
'count' => $facetedData[$selectedOptions[0]['value']]['count'],
],
];

$this->attribute->expects($this->atLeastOnce())
->method('getAttributeCode')
->willReturn($attributeCode);
$this->attribute->expects($this->atLeastOnce())
->method('getIsFilterable')
->willReturn(AbstractFilter::ATTRIBUTE_OPTIONS_ONLY_WITH_RESULTS);
$this->attribute->expects($this->atLeastOnce())
->method('getFrontend')
->will($this->returnValue($this->frontend));

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

$this->frontend->expects($this->once())
->method('getSelectOptions')
->willReturn($selectedOptions);

$this->fulltextCollection->expects($this->once())
->method('getFacetedData')
->willReturn($facetedData);
$this->fulltextCollection->expects($this->once())
->method('getSize')
->will($this->returnValue(50));

$this->itemDataBuilder->expects($this->once())
->method('addItemData')
->with(
$selectedOptions[0]['label'],
$selectedOptions[0]['value'],
$facetedData[$selectedOptions[0]['value']]['count']
)
->will($this->returnSelf());

$this->itemDataBuilder->expects($this->once())
->method('build')
->willReturn($builtData);

$expectedFilterItems = [
$this->createFilterItem(0, $builtData[0]['label'], $builtData[0]['value'], $builtData[0]['count']),
];
$result = $this->target->getItems();

$this->assertEquals($expectedFilterItems, $result);
}

/**
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function testGetItemsIfFacetedDataIsEmpty()
{
$attributeCode = 'attributeCode';

$this->attribute->expects($this->atLeastOnce())
->method('getAttributeCode')
->willReturn($attributeCode);
$this->attribute->expects($this->atLeastOnce())
->method('getIsFilterable')
->willReturn(0);

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

$this->fulltextCollection->expects($this->once())
->method('getFacetedData')
->willReturn([]);

$this->itemDataBuilder->expects($this->once())
->method('build')
->willReturn([]);

$this->assertEquals([], $this->target->getItems());
}

/**
* @param int $index
* @param string $label
Expand Down

0 comments on commit 3fd3c55

Please sign in to comment.