Skip to content

Commit 64de082

Browse files
MC-22738: Layered Navigation with different product attributes on Category page
1 parent ae51778 commit 64de082

File tree

8 files changed

+72
-97
lines changed

8 files changed

+72
-97
lines changed

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/Category/AbstractFiltersTest.php

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,34 @@ protected function setUp()
4343
$this->productRepository = $this->objectManager->create(ProductRepositoryInterface::class);
4444
}
4545

46+
/**
47+
* Tests getFilters method from navigation block.
48+
*
49+
* @param array $products
50+
* @param int $filterable
51+
* @param array $expectation
52+
* @param string $attributeCode
53+
* @return void
54+
*/
55+
protected function getFiltersAndAssert(
56+
array $products,
57+
int $filterable,
58+
array $expectation,
59+
string $attributeCode
60+
): void {
61+
$this->updateAttribute($attributeCode, $filterable);
62+
$this->updateProducts($products, $attributeCode);
63+
$this->prepareNavigationBlock('Category 999');
64+
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), $attributeCode);
65+
66+
if ($filterable) {
67+
$this->assertNotNull($filter);
68+
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
69+
} else {
70+
$this->assertNull($filter);
71+
}
72+
}
73+
4674
/**
4775
* @inheritdoc
4876
*/
@@ -58,7 +86,7 @@ protected function prepareNavigationBlock(string $categoryName, int $storeId = S
5886
/**
5987
* Returns filter with specified attribute.
6088
*
61-
* @param array $filters
89+
* @param AbstractFilter[] $filters
6290
* @param string $code
6391
* @return AbstractFilter|null
6492
*/
@@ -68,37 +96,27 @@ protected function getFilterByCode(array $filters, string $code): ?AbstractFilte
6896
$filters,
6997
function (AbstractFilter $filter) use ($code) {
7098
return $filter->getData('attribute_model')
71-
&& $filter->getData('attribute_model')->getAttributeCode() == $code;
99+
&& $filter->getData('attribute_model')->getAttributeCode() === $code;
72100
}
73101
);
74102

75103
return array_shift($filter);
76104
}
77105

78106
/**
79-
* Updates attribute and products data.
107+
* Updates attribute data.
80108
*
81109
* @param string $attributeCode
82110
* @param int $filterable
83-
* @param array $products
84111
* @return void
85112
*/
86-
protected function updateAttributeAndProducts(
113+
protected function updateAttribute(
87114
string $attributeCode,
88-
int $filterable,
89-
array $products
115+
int $filterable
90116
): void {
91117
$attribute = $this->attributeRepository->get($attributeCode);
92118
$attribute->setData('is_filterable', $filterable);
93119
$this->attributeRepository->save($attribute);
94-
95-
foreach ($products as $productSku => $stringValue) {
96-
$product = $this->productRepository->get($productSku, false, Store::DEFAULT_STORE_ID, true);
97-
$product->addData(
98-
[$attribute->getAttributeCode() => $attribute->getSource()->getOptionId($stringValue)]
99-
);
100-
$this->productRepository->save($product);
101-
}
102120
}
103121

104122
/**
@@ -112,13 +130,33 @@ protected function prepareFilterItems(AbstractFilter $filter): array
112130
$items = [];
113131
/** @var Item $item */
114132
foreach ($filter->getItems() as $item) {
115-
$item = [
133+
$itemArray = [
116134
'label' => $item->getData('label'),
117135
'count' => $item->getData('count'),
118136
];
119-
$items[] = $item;
137+
$items[] = $itemArray;
120138
}
121139

122140
return $items;
123141
}
142+
143+
/**
144+
* Update products data by attribute.
145+
*
146+
* @param array $products
147+
* @param string $attributeCode
148+
* @return void
149+
*/
150+
protected function updateProducts(array $products, string $attributeCode): void
151+
{
152+
$attribute = $this->attributeRepository->get($attributeCode);
153+
154+
foreach ($products as $productSku => $stringValue) {
155+
$product = $this->productRepository->get($productSku, false, Store::DEFAULT_STORE_ID, true);
156+
$product->addData(
157+
[$attribute->getAttributeCode() => $attribute->getSource()->getOptionId($stringValue)]
158+
);
159+
$this->productRepository->save($product);
160+
}
161+
}
124162
}

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/Category/BooleanFilterTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@ class BooleanFilterTest extends AbstractFiltersTest
2929
*/
3030
public function testGetFiltersWithCustomAttribute(array $products, int $filterable, array $expectation): void
3131
{
32-
$this->updateAttributeAndProducts('boolean_attribute', $filterable, $products);
33-
$this->prepareNavigationBlock('Category 999');
34-
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), 'boolean_attribute');
35-
36-
if ($filterable) {
37-
$this->assertNotNull($filter);
38-
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
39-
} else {
40-
$this->assertNull($filter);
41-
}
32+
$this->getFiltersAndAssert($products, $filterable, $expectation, 'boolean_attribute');
4233
}
4334

4435
/**

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/Category/DecimalFilterTest.php

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,11 @@ class DecimalFilterTest extends AbstractFiltersTest
3131
*/
3232
public function testGetFiltersWithCustomAttribute(array $products, int $filterable, array $expectation): void
3333
{
34-
$this->updateAttributeAndProducts('decimal_attribute', $filterable, $products);
35-
$this->prepareNavigationBlock('Category 999');
36-
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), 'decimal_attribute');
37-
38-
if ($filterable) {
39-
$this->assertNotNull($filter);
40-
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
41-
} else {
42-
$this->assertNull($filter);
43-
}
34+
$this->getFiltersAndAssert($products, $filterable, $expectation, 'decimal_attribute');
4435
}
4536

4637
/**
47-
* Returns filter items as array.
48-
*
49-
* @param AbstractFilter $filter
50-
* @return array
38+
* @inheritdoc
5139
*/
5240
protected function prepareFilterItems(AbstractFilter $filter): array
5341
{
@@ -68,14 +56,9 @@ protected function prepareFilterItems(AbstractFilter $filter): array
6856
/**
6957
* @inheritdoc
7058
*/
71-
protected function updateAttributeAndProducts(
72-
string $attributeCode,
73-
int $filterable,
74-
array $products
75-
): void {
59+
protected function updateProducts(array $products, string $attributeCode): void
60+
{
7661
$attribute = $this->attributeRepository->get($attributeCode);
77-
$attribute->setData('is_filterable', $filterable);
78-
$this->attributeRepository->save($attribute);
7962

8063
foreach ($products as $productSku => $value) {
8164
$product = $this->productRepository->get($productSku, false, Store::DEFAULT_STORE_ID, true);

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/Category/MultiselectFilterTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@ class MultiselectFilterTest extends AbstractFiltersTest
2929
*/
3030
public function testGetFiltersWithCustomAttribute(array $products, int $filterable, array $expectation): void
3131
{
32-
$this->updateAttributeAndProducts('multiselect_attribute', $filterable, $products);
33-
$this->prepareNavigationBlock('Category 999');
34-
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), 'multiselect_attribute');
35-
36-
if ($filterable) {
37-
$this->assertNotNull($filter);
38-
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
39-
} else {
40-
$this->assertNull($filter);
41-
}
32+
$this->getFiltersAndAssert($products, $filterable, $expectation, 'multiselect_attribute');
4233
}
4334

4435
/**

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/Category/SelectFilterTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,7 @@ class SelectFilterTest extends AbstractFiltersTest
2929
*/
3030
public function testGetFiltersWithCustomAttribute(array $products, int $filterable, array $expectation): void
3131
{
32-
$this->updateAttributeAndProducts('dropdown_attribute', $filterable, $products);
33-
$this->prepareNavigationBlock('Category 999');
34-
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), 'dropdown_attribute');
35-
36-
if ($filterable) {
37-
$this->assertNotNull($filter);
38-
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
39-
} else {
40-
$this->assertNull($filter);
41-
}
32+
$this->getFiltersAndAssert($products, $filterable, $expectation, 'dropdown_attribute');
4233
}
4334

4435
/**

dev/tests/integration/testsuite/Magento/LayeredNavigation/Block/Navigation/CategoryTest.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testCanShowBlockWithoutFilterOptions(): void
6666
*/
6767
public function testCanShowBlockWithDisplayMode(string $displayMode, bool $canShow): void
6868
{
69-
$this->updateTestCategory($displayMode, 'Category 999');
69+
$this->updateCategoryDisplayMode('Category 999', $displayMode);
7070
$this->prepareNavigationBlock('Category 999');
7171
$this->assertEquals($canShow, $this->navigationBlock->canShowBlock());
7272
}
@@ -98,8 +98,8 @@ public function testCanShowBlockWithDisplayModeOnStoreView(
9898
bool $canShow
9999
): void {
100100
$secondStoreId = (int)$this->storeManager->getStore('fixture_second_store')->getId();
101-
$this->updateTestCategory($defaultMode, 'Category 999');
102-
$this->updateTestCategory($storeMode, 'Category 999', $secondStoreId);
101+
$this->updateCategoryDisplayMode('Category 999', $defaultMode);
102+
$this->updateCategoryDisplayMode('Category 999', $storeMode, $secondStoreId);
103103
$this->prepareNavigationBlock('Category 999', $secondStoreId);
104104
$this->assertEquals($canShow, $this->navigationBlock->canShowBlock());
105105
}
@@ -131,21 +131,20 @@ public function canShowBlockWithDisplayModeDataProviderOnStoreView(): array
131131
/**
132132
* Updates category display mode.
133133
*
134-
* @param string $displayMode
135134
* @param string $categoryName
135+
* @param string $displayMode
136136
* @param int $storeId
137137
* @return void
138138
*/
139-
private function updateTestCategory(
140-
string $displayMode,
139+
private function updateCategoryDisplayMode(
141140
string $categoryName,
141+
string $displayMode,
142142
int $storeId = Store::DEFAULT_STORE_ID
143143
): void {
144144
$category = $this->loadCategory($categoryName, $storeId);
145-
$currentMode = $category->getData('display_mode');
145+
$category->setData('display_mode', $displayMode);
146146

147-
if ($currentMode !== $displayMode) {
148-
$category->setData('display_mode', $displayMode);
147+
if ($category->dataHasChangedFor('display_mode')) {
149148
$this->categoryResource->save($category);
150149
}
151150
}

dev/tests/integration/testsuite/Magento/SwatchesLayeredNavigation/Block/Navigation/Category/SwatchTextFilterTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,7 @@ class SwatchTextFilterTest extends AbstractFiltersTest
3030
*/
3131
public function testGetFiltersWithCustomAttribute(array $products, int $filterable, array $expectation): void
3232
{
33-
$this->updateAttributeAndProducts('text_swatch_attribute', $filterable, $products);
34-
$this->prepareNavigationBlock('Category 999');
35-
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), 'text_swatch_attribute');
36-
37-
if ($filterable) {
38-
$this->assertNotNull($filter);
39-
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
40-
} else {
41-
$this->assertNull($filter);
42-
}
33+
$this->getFiltersAndAssert($products, $filterable, $expectation, 'text_swatch_attribute');
4334
}
4435

4536
/**

dev/tests/integration/testsuite/Magento/SwatchesLayeredNavigation/Block/Navigation/Category/SwatchVisualFilterTest.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,7 @@ class SwatchVisualFilterTest extends AbstractFiltersTest
3030
*/
3131
public function testGetFiltersWithCustomAttribute(array $products, int $filterable, array $expectation): void
3232
{
33-
$this->updateAttributeAndProducts('visual_swatch_attribute', $filterable, $products);
34-
$this->prepareNavigationBlock('Category 999');
35-
$filter = $this->getFilterByCode($this->navigationBlock->getFilters(), 'visual_swatch_attribute');
36-
37-
if ($filterable) {
38-
$this->assertNotNull($filter);
39-
$this->assertEquals($expectation, $this->prepareFilterItems($filter));
40-
} else {
41-
$this->assertNull($filter);
42-
}
33+
$this->getFiltersAndAssert($products, $filterable, $expectation, 'visual_swatch_attribute');
4334
}
4435

4536
/**

0 commit comments

Comments
 (0)