Skip to content

Commit a056502

Browse files
MC-19031: Sorting the product grid by custom product attribute sorts by value ID instead of Alphabetically
1 parent f35c6eb commit a056502

File tree

6 files changed

+133
-7
lines changed

6 files changed

+133
-7
lines changed

app/code/Magento/Catalog/Test/Mftf/Data/ProductAttributeOptionData.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@
8686
<data key="label" unique="suffix">White</data>
8787
<data key="value" unique="suffix">white</data>
8888
</entity>
89+
<entity name="ProductAttributeOption9" type="ProductAttributeOption">
90+
<var key="attribute_code" entityKey="attribute_code" entityType="ProductAttribute"/>
91+
<data key="label" unique="suffix">Blue</data>
92+
<data key="is_default">false</data>
93+
<data key="sort_order">3</data>
94+
<requiredEntity type="StoreLabel">Option11Store0</requiredEntity>
95+
<requiredEntity type="StoreLabel">Option11Store1</requiredEntity>
96+
</entity>
8997
<!-- Product attribute options from file "export_import_configurable_product.csv" -->
9098
<entity name="ProductAttributeOptionOneForExportImport" extends="productAttributeOption1" type="ProductAttributeOption">
9199
<data key="label">option1</data>

app/code/Magento/Catalog/Test/Mftf/Data/StoreLabelData.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,12 @@
7272
<data key="store_id">1</data>
7373
<data key="label">Red</data>
7474
</entity>
75+
<entity name="Option11Store0" type="StoreLabel">
76+
<data key="store_id">0</data>
77+
<data key="label">Blue</data>
78+
</entity>
79+
<entity name="Option11Store1" type="StoreLabel">
80+
<data key="store_id">1</data>
81+
<data key="label">Blue</data>
82+
</entity>
7583
</entities>
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
/**
4+
* Copyright © Magento, Inc. All rights reserved.
5+
* See COPYING.txt for license details.
6+
*/
7+
-->
8+
9+
<tests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
10+
xsi:noNamespaceSchemaLocation="urn:magento:mftf:Test/etc/testSchema.xsd">
11+
<test name="AdminProductGridFilteringByCustomAttributeTest">
12+
<annotations>
13+
<features value="Catalog"/>
14+
<stories value="Product grid"/>
15+
<title value="Sorting the product grid by custom product attribute"/>
16+
<description value="Sorting the product grid by custom product attribute should sort Alphabetically instead of value id"/>
17+
<severity value="MAJOR"/>
18+
<useCaseId value="MC-19031"/>
19+
<testCaseId value="MC-20329"/>
20+
<group value="catalog"/>
21+
</annotations>
22+
<before>
23+
<!--Login as admin and delete all products -->
24+
<actionGroup ref="LoginAsAdmin" stepKey="login"/>
25+
<actionGroup ref="deleteAllProductsUsingProductGrid" stepKey="deleteAllProducts"/>
26+
<!--Create dropdown product attribute-->
27+
<createData entity="productDropDownAttribute" stepKey="createDropdownAttribute"/>
28+
<!--Create attribute options-->
29+
<createData entity="ProductAttributeOption7" stepKey="createFirstProductAttributeOption">
30+
<requiredEntity createDataKey="createDropdownAttribute"/>
31+
</createData>
32+
<createData entity="ProductAttributeOption8" stepKey="createSecondProductAttributeOption">
33+
<requiredEntity createDataKey="createDropdownAttribute"/>
34+
</createData>
35+
<createData entity="ProductAttributeOption9" stepKey="createThirdProductAttributeOption">
36+
<requiredEntity createDataKey="createDropdownAttribute"/>
37+
</createData>
38+
<!--Add attribute to default attribute set-->
39+
<createData entity="AddToDefaultSet" stepKey="addAttributeToDefaultSet">
40+
<requiredEntity createDataKey="createDropdownAttribute"/>
41+
</createData>
42+
<!--Create category-->
43+
<createData entity="_defaultCategory" stepKey="createCategory"/>
44+
<!--Create 3 products-->
45+
<createData entity="ApiSimpleProduct" stepKey="createFirstProduct">
46+
<requiredEntity createDataKey="createCategory"/>
47+
</createData>
48+
<createData entity="ApiSimpleProduct" stepKey="createSecondProduct">
49+
<requiredEntity createDataKey="createCategory"/>
50+
</createData>
51+
<createData entity="ApiSimpleProduct" stepKey="createThirdProduct">
52+
<requiredEntity createDataKey="createCategory"/>
53+
</createData>
54+
<!--Update first product-->
55+
<actionGroup ref="SearchForProductOnBackendActionGroup" stepKey="searchForFirstProduct">
56+
<argument name="product" value="$$createFirstProduct$$"/>
57+
</actionGroup>
58+
<actionGroup ref="OpenEditProductOnBackendActionGroup" stepKey="editFirstProduct">
59+
<argument name="product" value="$$createFirstProduct$$"/>
60+
</actionGroup>
61+
<selectOption selector="{{AdminProductFormSection.customSelectField($$createDropdownAttribute.attribute[attribute_code]$$)}}" userInput="$$createFirstProductAttributeOption.option[store_labels][0][label]$$" stepKey="setFirstAttributeValue"/>
62+
<actionGroup ref="saveProductForm" stepKey="saveFirstProduct"/>
63+
<!--Update second product-->
64+
<actionGroup ref="SearchForProductOnBackendActionGroup" stepKey="searchForSecondProduct">
65+
<argument name="product" value="$$createSecondProduct$$"/>
66+
</actionGroup>
67+
<actionGroup ref="OpenEditProductOnBackendActionGroup" stepKey="editSecondProduct">
68+
<argument name="product" value="$$createSecondProduct$$"/>
69+
</actionGroup>
70+
<selectOption selector="{{AdminProductFormSection.customSelectField($$createDropdownAttribute.attribute[attribute_code]$$)}}" userInput="$$createSecondProductAttributeOption.option[store_labels][0][label]$$" stepKey="setSecondAttributeValue"/>
71+
<actionGroup ref="saveProductForm" stepKey="saveSecondProduct"/>
72+
<!--Update third product-->
73+
<actionGroup ref="SearchForProductOnBackendActionGroup" stepKey="searchForThirdProduct">
74+
<argument name="product" value="$$createThirdProduct$$"/>
75+
</actionGroup>
76+
<actionGroup ref="OpenEditProductOnBackendActionGroup" stepKey="editThirdProduct">
77+
<argument name="product" value="$$createThirdProduct$$"/>
78+
</actionGroup>
79+
<selectOption selector="{{AdminProductFormSection.customSelectField($$createDropdownAttribute.attribute[attribute_code]$$)}}" userInput="$$createThirdProductAttributeOption.option[store_labels][0][label]$$" stepKey="setThirdAttributeValue"/>
80+
<actionGroup ref="saveProductForm" stepKey="saveThirdProduct"/>
81+
</before>
82+
<after>
83+
<!--Delete products-->
84+
<deleteData createDataKey="createFirstProduct" stepKey="deleteFirstProduct"/>
85+
<deleteData createDataKey="createSecondProduct" stepKey="deleteSecondProduct"/>
86+
<deleteData createDataKey="createThirdProduct" stepKey="deleteThirdProduct"/>
87+
<!--Delete attribute-->
88+
<deleteData createDataKey="createDropdownAttribute" stepKey="deleteDropdownAttribute"/>
89+
<!--Delete category-->
90+
<deleteData createDataKey="createCategory" stepKey="deleteCategory"/>
91+
<actionGroup ref="NavigateToAndResetProductGridToDefaultView" stepKey="NavigateToAndResetProductGridToDefaultViewAfterTest"/>
92+
<actionGroup ref="logout" stepKey="logout"/>
93+
</after>
94+
<amOnPage url="{{AdminProductIndexPage.url}}" stepKey="navigateToProductIndex"/>
95+
<actionGroup ref="clearFiltersAdminDataGrid" stepKey="clearProductGridFilters"/>
96+
<!--Sort by custom attribute DESC using grabbed value-->
97+
<conditionalClick selector="{{AdminProductGridSection.columnHeader($$createDropdownAttribute.attribute[frontend_labels][0][label]$$)}}" dependentSelector="{{AdminProductGridSection.columnHeader($$createDropdownAttribute.attribute[frontend_labels][0][label]$$)}}" visible="true" stepKey="ascendSortByCustomAttribute"/>
98+
<waitForPageLoad stepKey="waitForProductGridLoad"/>
99+
<!--Check products sorting. Expected result => Blue-Green-Red -->
100+
<see selector="{{AdminProductGridSection.productGridNameProduct($$createSecondProduct.name$$)}}" userInput="$$createSecondProduct.name$$" stepKey="seeSecondProductName"/>
101+
<see selector="{{AdminProductGridSection.productGridNameProduct($$createFirstProduct.name$$)}}" userInput="$$createFirstProduct.name$$" stepKey="seeFirstProductName"/>
102+
<see selector="{{AdminProductGridSection.productGridNameProduct($$createThirdProduct.name$$)}}" userInput="$$createThirdProduct.name$$" stepKey="seeThirdProductName"/>
103+
</test>
104+
</tests>

app/code/Magento/Eav/Model/Entity/Attribute/Source/Table.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public function addValueSortToCollection($collection, $dir = \Magento\Framework\
213213
$valueExpr
214214
);
215215

216-
$collection->getSelect()->order("{$attribute->getAttributeCode()} {$dir}");
216+
$collection->getSelect()->order("{$attribute->getAttributeCode()}_value {$dir}");
217217

218218
return $this;
219219
}

app/code/Magento/Eav/Model/ResourceModel/Entity/Attribute/Option.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,13 @@ public function addOptionValueToCollection($collection, $attribute, $valueExpr)
4242
"{$optionTable2}.option_id={$valueExpr} AND {$optionTable2}.store_id=?",
4343
$collection->getStoreId()
4444
);
45-
$valueExpr = $connection->getCheckSql(
46-
"{$optionTable2}.value_id IS NULL",
47-
"{$optionTable1}.option_id",
48-
"{$optionTable2}.option_id"
45+
$valueIdExpr = $connection->getIfNullSql(
46+
"{$optionTable2}.option_id",
47+
"{$optionTable1}.option_id"
48+
);
49+
$valueExpr = $connection->getIfNullSql(
50+
"{$optionTable2}.value",
51+
"{$optionTable1}.value"
4952
);
5053

5154
$collection->getSelect()->joinLeft(
@@ -55,7 +58,10 @@ public function addOptionValueToCollection($collection, $attribute, $valueExpr)
5558
)->joinLeft(
5659
[$optionTable2 => $this->getTable('eav_attribute_option_value')],
5760
$tableJoinCond2,
58-
[$attributeCode => $valueExpr]
61+
[
62+
$attributeCode => $valueIdExpr,
63+
$attributeCode . '_value' => $valueExpr,
64+
]
5965
);
6066

6167
return $this;

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Source/TableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ public function testAddValueSortToCollection()
314314
$attrOption->expects($this->once())->method('addOptionValueToCollection')
315315
->with($collection, $this->abstractAttributeMock, $expr)
316316
->willReturnSelf();
317-
$select->expects($this->once())->method('order')->with("{$attributeCode} {$dir}");
317+
$select->expects($this->once())->method('order')->with("{$attributeCode}_value {$dir}");
318318

319319
$this->assertEquals($this->model, $this->model->addValueSortToCollection($collection, $dir));
320320
}

0 commit comments

Comments
 (0)