Skip to content

Commit 151a776

Browse files
author
cspruiell
committed
Merge remote-tracking branch 'origin/MAGETWO-58652-Multiselect' into okapis-mainline-2.2-pr
# Conflicts: # dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php
2 parents 7afaae6 + 8783ada commit 151a776

File tree

2 files changed

+89
-8
lines changed

2 files changed

+89
-8
lines changed

app/code/Magento/Eav/Model/EavCustomAttributeTypeLocator/SimpleType.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,14 @@ class SimpleType
2828
*/
2929
public function getType($attribute)
3030
{
31-
if (in_array($attribute->getAttributeCode(), $this->anyTypeAttributes)) {
31+
$arrayFrontendInputs = ['multiselect'];
32+
$frontendInput = $attribute->getFrontendInput();
33+
if (in_array($attribute->getAttributeCode(), $this->anyTypeAttributes)
34+
|| in_array($frontendInput, $arrayFrontendInputs)
35+
) {
3236
return TypeProcessor::NORMALIZED_ANY_TYPE;
3337
}
34-
$frontendInput = $attribute->getFrontendInput();
38+
3539
$backendType = $attribute->getBackendType();
3640
$backendTypeMap = [
3741
'static' => TypeProcessor::NORMALIZED_ANY_TYPE,
@@ -41,11 +45,6 @@ public function getType($attribute)
4145
'datetime' => TypeProcessor::NORMALIZED_STRING_TYPE,
4246
'decimal' => TypeProcessor::NORMALIZED_DOUBLE_TYPE,
4347
];
44-
$arrayFrontendInputs = ['multiselect'];
45-
$type = $backendTypeMap[$backendType];
46-
if (in_array($frontendInput, $arrayFrontendInputs)) {
47-
$type .= '[]';
48-
}
49-
return $type;
48+
return $backendTypeMap[$backendType];
5049
}
5150
}

dev/tests/api-functional/testsuite/Magento/Catalog/Api/ProductRepositoryInterfaceTest.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,4 +1296,86 @@ public function testUpdateStatus()
12961296
// Price should be updated
12971297
$this->assertEquals(200, $response['price']);
12981298
}
1299+
1300+
/**
1301+
* Test saving product with custom attribute of multiselect type
1302+
*
1303+
* 1. Create multi-select attribute
1304+
* 2. Create product and set 2 options out of 3 to multi-select attribute
1305+
* 3. Verify that 2 options are selected
1306+
* 4. Unselect all options
1307+
* 5. Verify that non options are selected
1308+
*
1309+
* @magentoApiDataFixture Magento/Catalog/_files/multiselect_attribute.php
1310+
*/
1311+
public function testUpdateMultiselectAttributes()
1312+
{
1313+
$multiselectAttributeCode = 'multiselect_attribute';
1314+
$multiselectOptions = $this->getAttributeOptions($multiselectAttributeCode);
1315+
$option1 = $multiselectOptions[1]['value'];
1316+
$option2 = $multiselectOptions[2]['value'];
1317+
1318+
$productData = $this->getSimpleProductData();
1319+
$productData['custom_attributes'] = [
1320+
['attribute_code' => $multiselectAttributeCode, 'value' => "{$option1},{$option2}"]
1321+
];
1322+
$this->saveProduct($productData, 'all');
1323+
1324+
$this->assertMultiselectValue(
1325+
$productData[ProductInterface::SKU],
1326+
$multiselectAttributeCode,
1327+
"{$option1},{$option2}"
1328+
);
1329+
1330+
$productData['custom_attributes'] = [
1331+
['attribute_code' => $multiselectAttributeCode, 'value' => ""]
1332+
];
1333+
$this->saveProduct($productData, 'all');
1334+
$this->assertMultiselectValue(
1335+
$productData[ProductInterface::SKU],
1336+
$multiselectAttributeCode,
1337+
""
1338+
);
1339+
}
1340+
1341+
/**
1342+
* @param string $attributeCode
1343+
* @return array|bool|float|int|string
1344+
*/
1345+
private function getAttributeOptions($attributeCode)
1346+
{
1347+
$serviceInfo = [
1348+
'rest' => [
1349+
'resourcePath' => '/V1/products/attributes/' . $attributeCode . '/options',
1350+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET,
1351+
],
1352+
'soap' => [
1353+
'service' => 'catalogProductAttributeOptionManagementV1',
1354+
'serviceVersion' => 'V1',
1355+
'operation' => 'catalogProductAttributeOptionManagementV1getItems',
1356+
],
1357+
];
1358+
1359+
return $this->_webApiCall($serviceInfo, ['attributeCode' => $attributeCode]);
1360+
}
1361+
1362+
/**
1363+
* @param string $productSku
1364+
* @param string $multiselectAttributeCode
1365+
* @param string $expectedMultiselectValue
1366+
*/
1367+
private function assertMultiselectValue($productSku, $multiselectAttributeCode, $expectedMultiselectValue)
1368+
{
1369+
$response = $this->getProduct($productSku, 'all');
1370+
$customAttributes = $response['custom_attributes'];
1371+
$this->assertNotEmpty($customAttributes);
1372+
$multiselectValue = null;
1373+
foreach ($customAttributes as $customAttribute) {
1374+
if ($customAttribute['attribute_code'] == $multiselectAttributeCode) {
1375+
$multiselectValue = $customAttribute['value'];
1376+
break;
1377+
}
1378+
}
1379+
$this->assertEquals($expectedMultiselectValue, $multiselectValue);
1380+
}
12991381
}

0 commit comments

Comments
 (0)