@@ -1296,4 +1296,86 @@ public function testUpdateStatus()
1296
1296
// Price should be updated
1297
1297
$ this ->assertEquals (200 , $ response ['price ' ]);
1298
1298
}
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
+ }
1299
1381
}
0 commit comments