Skip to content

Commit 6cee3c7

Browse files
author
cspruiell
committed
MAGETWO-58401: Cannot create configurable product with child by REST API - for 2.1.x
- Fix and increase api-functional tests
1 parent 7d150f6 commit 6cee3c7

File tree

3 files changed

+167
-3
lines changed

3 files changed

+167
-3
lines changed

app/code/Magento/ConfigurableProduct/Model/LinkManagement.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public function addChild($sku, $childSku)
127127
}
128128
$configurableOptionData = $this->getConfigurableAttributesData($attributeIds);
129129

130-
/** @var Factory $optionFactory */
130+
/** @var \Magento\ConfigurableProduct\Helper\Product\Options\Factory $optionFactory */
131131
$optionFactory = $this->getOptionsFactory();
132132
$options = $optionFactory->create($configurableOptionData);
133133
$childrenIds[] = $child->getId();
@@ -206,6 +206,7 @@ private function getAttributeFactory()
206206
*/
207207
private function getConfigurableAttributesData($attributeIds)
208208
{
209+
$configurableAttributesData = [];
209210
$attributeValues = [];
210211
$attributes = $this->getAttributeFactory()->create()
211212
->getCollection()

dev/tests/api-functional/testsuite/Magento/ConfigurableProduct/Api/LinkManagementTest.php

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,34 @@
66
*/
77
namespace Magento\ConfigurableProduct\Api;
88

9+
use Magento\Catalog\Model\ResourceModel\Eav\Attribute;
10+
use Magento\Eav\Model\AttributeRepository;
11+
912
class LinkManagementTest extends \Magento\TestFramework\TestCase\WebapiAbstract
1013
{
1114
const SERVICE_NAME = 'configurableProductLinkManagementV1';
1215
const SERVICE_VERSION = 'V1';
1316
const RESOURCE_PATH = '/V1/configurable-products';
1417

18+
/**
19+
* @var \Magento\Framework\ObjectManagerInterface
20+
*/
21+
protected $objectManager;
22+
23+
/**
24+
* @var AttributeRepository
25+
*/
26+
protected $attributeRepository;
27+
28+
/**
29+
* Execute per test initialization
30+
*/
31+
public function setUp()
32+
{
33+
$this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
34+
$this->attributeRepository = $this->objectManager->get('Magento\Eav\Model\AttributeRepository');
35+
}
36+
1537
/**
1638
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
1739
*/
@@ -40,8 +62,8 @@ public function testGetChildren()
4062
}
4163

4264
/**
43-
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_simple_77.php
4465
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
66+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_simple_77.php
4567
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/delete_association.php
4668
*/
4769
public function testAddChild()
@@ -63,6 +85,146 @@ public function testAddChild()
6385
$this->assertTrue($res);
6486
}
6587

88+
/**
89+
* Test case to cover bug MAGETWO-58401
90+
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/configurable_attribute.php
91+
*/
92+
public function testAddChildFullRestCreation()
93+
{
94+
$this->createConfigurableProduct();
95+
$attribute = $this->attributeRepository->get('catalog_product', 'test_configurable');
96+
$attributeValues = $attribute->getOptions();
97+
$this->addOptionToConfigurableProduct($attribute->getAttributeId(), $attributeValues[1]->getValue());
98+
$this->createSimpleProduct($attributeValues[1]->getValue());
99+
100+
$requestData = [
101+
'sku' => 'configurable-product-sku',
102+
'childSku' => 'simple-product-sku'
103+
];
104+
$serviceInfo = [
105+
'rest' => [
106+
'resourcePath' => self::RESOURCE_PATH . '/configurable-product-sku/child',
107+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
108+
],
109+
'soap' => [
110+
'service' => self::SERVICE_NAME,
111+
'serviceVersion' => self::SERVICE_VERSION,
112+
'operation' => self::SERVICE_NAME . 'AddChild'
113+
]
114+
];
115+
$res = $this->_webApiCall($serviceInfo, $requestData);
116+
$this->assertTrue($res);
117+
118+
// clean up products
119+
$serviceInfo = [
120+
'rest' => [
121+
'resourcePath' => '/V1/products/configurable-product-sku',
122+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
123+
],
124+
'soap' => [
125+
'service' => 'catalogProductRepositoryV1',
126+
'serviceVersion' => self::SERVICE_VERSION,
127+
'operation' => 'catalogProductRepositoryV1DeleteById',
128+
],
129+
];
130+
$this->_webApiCall($serviceInfo, ['sku' => 'configurable-product-sku']);
131+
$serviceInfo = [
132+
'rest' => [
133+
'resourcePath' => '/V1/products/simple-product-sku',
134+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE
135+
],
136+
'soap' => [
137+
'service' => 'catalogProductRepositoryV1',
138+
'serviceVersion' => self::SERVICE_VERSION,
139+
'operation' => 'catalogProductRepositoryV1DeleteById',
140+
],
141+
];
142+
$this->_webApiCall($serviceInfo, ['sku' => 'simple-product-sku']);
143+
}
144+
145+
protected function createConfigurableProduct()
146+
{
147+
$requestData = [
148+
'product' => [
149+
'sku' => 'configurable-product-sku',
150+
'name' => 'configurable-product',
151+
'type_id' => 'configurable',
152+
'price' => 50,
153+
'attribute_set_id' => 4,
154+
]
155+
];
156+
$serviceInfo = [
157+
'rest' => [
158+
'resourcePath' => '/V1/products',
159+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST
160+
],
161+
'soap' => [
162+
'service' => 'catalogProductRepositoryV1',
163+
'serviceVersion' => self::SERVICE_VERSION,
164+
'operation' => 'catalogProductRepositoryV1Save',
165+
],
166+
];
167+
return $this->_webApiCall($serviceInfo, $requestData);
168+
}
169+
170+
protected function addOptionToConfigurableProduct($attributeId, $attributeValue)
171+
{
172+
$requestData = [
173+
'sku' => 'configurable-product-sku',
174+
'option' => [
175+
'attribute_id' => $attributeId,
176+
'label' => 'color',
177+
'position' => 0,
178+
'is_use_default' => true,
179+
'values' => [
180+
['value_index' => $attributeValue],
181+
]
182+
]
183+
];
184+
$serviceInfo = [
185+
'rest' => [
186+
'resourcePath' => '/V1/configurable-products/configurable-product-sku/options',
187+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
188+
],
189+
'soap' => [
190+
'service' => 'configurableProductOptionRepositoryV1',
191+
'serviceVersion' => self::SERVICE_VERSION,
192+
'operation' => 'configurableProductOptionRepositoryV1Save',
193+
],
194+
];
195+
return $this->_webApiCall($serviceInfo, $requestData);
196+
}
197+
198+
protected function createSimpleProduct($attributeValue)
199+
{
200+
$requestData = [
201+
'product' => [
202+
'sku' => 'simple-product-sku',
203+
'name' => 'simple-product',
204+
'type_id' => 'simple',
205+
'attribute_set_id' => 4,
206+
'price' => 3.62,
207+
'status' => 1,
208+
'visibility' => 4,
209+
'custom_attributes' => [
210+
['attribute_code' => 'test_configurable', 'value' => $attributeValue],
211+
]
212+
]
213+
];
214+
$serviceInfo = [
215+
'rest' => [
216+
'resourcePath' => '/V1/products',
217+
'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST,
218+
],
219+
'soap' => [
220+
'service' => 'catalogProductRepositoryV1',
221+
'serviceVersion' => self::SERVICE_VERSION,
222+
'operation' => 'catalogProductRepositoryV1Save',
223+
],
224+
];
225+
return $this->_webApiCall($serviceInfo, $requestData);
226+
}
227+
66228
/**
67229
* @magentoApiDataFixture Magento/ConfigurableProduct/_files/product_configurable.php
68230
*/

dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/product_simple_77.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
'is_in_stock' => 1,
7373
]
7474
)->setCanSaveCustomOptions(true)
75-
->setHasOptions(true);
75+
->setHasOptions(true)
76+
->setCustomAttribute('test_configurable', 42);
7677

7778
$oldOptions = [
7879
[

0 commit comments

Comments
 (0)