Skip to content

Commit 6c65b4b

Browse files
committed
MAGETWO-58395: [Backport] An error occurs while saving configurable product - 2.1
1 parent 87046fa commit 6c65b4b

File tree

3 files changed

+86
-34
lines changed

3 files changed

+86
-34
lines changed

app/code/Magento/ConfigurableProduct/Model/Product/VariationHandler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,14 @@ protected function fillSimpleProductData(
152152
\Magento\Catalog\Model\Product $parentProduct,
153153
$postData
154154
) {
155+
$typeId = isset($postData['weight']) && !empty($postData['weight'])
156+
? ProductType::TYPE_SIMPLE
157+
: ProductType::TYPE_VIRTUAL;
158+
155159
$product->setStoreId(
156160
\Magento\Store\Model\Store::DEFAULT_STORE_ID
157161
)->setTypeId(
158-
$postData['weight'] ? ProductType::TYPE_SIMPLE : ProductType::TYPE_VIRTUAL
162+
$typeId
159163
)->setAttributeSetId(
160164
$parentProduct->getNewVariationsAttributeSetId()
161165
);

app/code/Magento/ConfigurableProduct/Test/Unit/Model/Product/VariationHandlerTest.php

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
namespace Magento\ConfigurableProduct\Test\Unit\Model\Product;
1010

11+
use Magento\Catalog\Model\Product\Type;
1112
use Magento\ConfigurableProduct\Model\Product\VariationHandler;
1213

1314
/**
@@ -62,45 +63,45 @@ protected function setUp()
6263
{
6364
$this->objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
6465
$this->productFactoryMock = $this->getMock(
65-
'Magento\Catalog\Model\ProductFactory',
66+
\Magento\Catalog\Model\ProductFactory::class,
6667
['create'],
6768
[],
6869
'',
6970
false
7071
);
7172
$this->entityFactoryMock = $this->getMock(
72-
'Magento\Eav\Model\EntityFactory',
73+
\Magento\Eav\Model\EntityFactory::class,
7374
['create'],
7475
[],
7576
'',
7677
false
7778
);
7879
$this->attributeSetFactory = $this->getMock(
79-
'Magento\Eav\Model\Entity\Attribute\SetFactory',
80+
\Magento\Eav\Model\Entity\Attribute\SetFactory::class,
8081
['create'],
8182
[],
8283
'',
8384
false
8485
);
8586
$this->stockConfiguration = $this->getMock(
86-
'Magento\CatalogInventory\Api\StockConfigurationInterface',
87+
\Magento\CatalogInventory\Api\StockConfigurationInterface::class,
8788
[],
8889
[],
8990
'',
9091
false
9192
);
9293
$this->configurableProduct = $this->getMock(
93-
'Magento\ConfigurableProduct\Model\Product\Type\Configurable',
94+
\Magento\ConfigurableProduct\Model\Product\Type\Configurable::class,
9495
[],
9596
[],
9697
'',
9798
false
9899
);
99100

100-
$this->product = $this->getMock('Magento\Catalog\Model\Product', ['getMediaGallery'], [], '', false);
101+
$this->product = $this->getMock(\Magento\Catalog\Model\Product::class, ['getMediaGallery'], [], '', false);
101102

102103
$this->model = $this->objectHelper->getObject(
103-
'Magento\ConfigurableProduct\Model\Product\VariationHandler',
104+
\Magento\ConfigurableProduct\Model\Product\VariationHandler::class,
104105
[
105106
'productFactory' => $this->productFactoryMock,
106107
'entityFactory' => $this->entityFactoryMock,
@@ -114,19 +115,19 @@ protected function setUp()
114115
public function testPrepareAttributeSet()
115116
{
116117

117-
$productMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
118+
$productMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
118119
->setMethods(['getNewVariationsAttributeSetId'])
119120
->disableOriginalConstructor()
120121
->getMock();
121-
$attributeMock = $this->getMockBuilder('\Magento\Eav\Model\Entity\Attribute')
122+
$attributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
122123
->setMethods(['isInSet', 'setAttributeSetId', 'setAttributeGroupId', 'save'])
123124
->disableOriginalConstructor()
124125
->getMock();
125-
$attributeSetMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Set')
126+
$attributeSetMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Set::class)
126127
->setMethods(['load', 'addSetInfo', 'getDefaultGroupId'])
127128
->disableOriginalConstructor()
128129
->getMock();
129-
$eavEntityMock = $this->getMockBuilder('\Magento\Eav\Model\Entity')
130+
$eavEntityMock = $this->getMockBuilder(\Magento\Eav\Model\Entity::class)
130131
->setMethods(['setType', 'getTypeId'])
131132
->disableOriginalConstructor()
132133
->getMock();
@@ -161,23 +162,30 @@ public function testPrepareAttributeSet()
161162

162163
/**
163164
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
165+
* @dataProvider dataProviderTestGenerateSimpleProducts
166+
* @param int|string|null $weight
167+
* @param string $typeId
164168
*/
165-
public function testGenerateSimpleProducts()
169+
public function testGenerateSimpleProducts($weight, $typeId)
166170
{
167171
$productsData = [
168-
6 =>
169-
[
170-
'image' => 'image.jpg',
171-
'name' => 'config-red',
172-
'configurable_attribute' => '{"new_attr":"6"}',
173-
'sku' => 'config-red',
174-
'quantity_and_stock_status' =>
175-
[
176-
'qty' => '',
177-
],
178-
'weight' => '333',
179-
]
172+
[
173+
'image' => 'image.jpg',
174+
'name' => 'config-red',
175+
'configurable_attribute' => '{"new_attr":"6"}',
176+
'sku' => 'config-red',
177+
'quantity_and_stock_status' =>
178+
[
179+
'qty' => '',
180+
],
181+
]
180182
];
183+
184+
// Do not add 'weight' attribute if it's value is null!
185+
if ($weight !== null) {
186+
$productsData[0]['weight'] = $weight;
187+
}
188+
181189
$stockData = [
182190
'manage_stock' => '0',
183191
'use_config_enable_qty_increments' => '1',
@@ -186,7 +194,7 @@ public function testGenerateSimpleProducts()
186194
'is_decimal_divided' => 0
187195
];
188196

189-
$parentProductMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
197+
$parentProductMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
190198
->setMethods(
191199
[
192200
'__wakeup',
@@ -198,7 +206,7 @@ public function testGenerateSimpleProducts()
198206
)
199207
->disableOriginalConstructor()
200208
->getMock();
201-
$newSimpleProductMock = $this->getMockBuilder('\Magento\Catalog\Model\Product')
209+
$newSimpleProductMock = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
202210
->setMethods(
203211
[
204212
'__wakeup',
@@ -217,15 +225,15 @@ public function testGenerateSimpleProducts()
217225
)
218226
->disableOriginalConstructor()
219227
->getMock();
220-
$productTypeMock = $this->getMockBuilder('Magento\Catalog\Model\Product\Type')
228+
$productTypeMock = $this->getMockBuilder(Type::class)
221229
->setMethods(['getSetAttributes'])
222230
->disableOriginalConstructor()
223231
->getMock();
224-
$editableAttributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute')
232+
$editableAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute::class)
225233
->setMethods(['getIsUnique', 'getAttributeCode', 'getFrontend', 'getIsVisible'])
226234
->disableOriginalConstructor()
227235
->getMock();
228-
$frontendAttributeMock = $this->getMockBuilder('Magento\Eav\Model\Entity\Attribute\Frontend')
236+
$frontendAttributeMock = $this->getMockBuilder(\Magento\Eav\Model\Entity\Attribute\Frontend::class)
229237
->setMethods(['getInputType'])
230238
->disableOriginalConstructor()
231239
->getMock();
@@ -235,7 +243,7 @@ public function testGenerateSimpleProducts()
235243
->willReturn('new_attr_set_id');
236244
$this->productFactoryMock->expects($this->once())->method('create')->willReturn($newSimpleProductMock);
237245
$newSimpleProductMock->expects($this->once())->method('setStoreId')->with(0)->willReturnSelf();
238-
$newSimpleProductMock->expects($this->once())->method('setTypeId')->with('simple')->willReturnSelf();
246+
$newSimpleProductMock->expects($this->once())->method('setTypeId')->with($typeId)->willReturnSelf();
239247
$newSimpleProductMock->expects($this->once())
240248
->method('setAttributeSetId')
241249
->with('new_attr_set_id')
@@ -264,6 +272,27 @@ public function testGenerateSimpleProducts()
264272
$this->assertEquals(['product_id'], $this->model->generateSimpleProducts($parentProductMock, $productsData));
265273
}
266274

275+
/**
276+
* @return array
277+
*/
278+
public function dataProviderTestGenerateSimpleProducts()
279+
{
280+
return [
281+
[
282+
'weight' => 333,
283+
'type_id' => Type::TYPE_SIMPLE,
284+
],
285+
[
286+
'weight' => '',
287+
'type_id' => Type::TYPE_VIRTUAL,
288+
],
289+
[
290+
'weight' => null,
291+
'type_id' => Type::TYPE_VIRTUAL,
292+
],
293+
];
294+
}
295+
267296
public function testProcessMediaGalleryWithImagesAndGallery()
268297
{
269298
$this->product->expects($this->atLeastOnce())->method('getMediaGallery')->with('images')->willReturn([]);

dev/tests/integration/testsuite/Magento/ConfigurableProduct/Model/Product/VariationHandlerTest.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,29 @@ public function testGenerateSimpleProducts($productsData)
4848
$this->_product->setNewVariationsAttributeSetId(4);
4949
// Default attribute set id
5050
$generatedProducts = $this->_model->generateSimpleProducts($this->_product, $productsData);
51-
$this->assertEquals(3, count($generatedProducts));
51+
$this->assertEquals(4, count($generatedProducts));
5252
foreach ($generatedProducts as $productId) {
5353
$stockItem = $this->stockRegistry->getStockItem($productId);
5454
/** @var $product \Magento\Catalog\Model\Product */
5555
$product = Bootstrap::getObjectManager()->create(
56-
'Magento\Catalog\Model\Product'
56+
\Magento\Catalog\Model\Product::class
5757
);
5858
$product->load($productId);
59+
60+
$productDataWeight = null;
61+
foreach ($productsData as $productItemData) {
62+
if ($productItemData['name'] == $product->getName()
63+
&& isset($productItemData['weight'])
64+
) {
65+
$productDataWeight = (int)$productItemData['weight'];
66+
break;
67+
}
68+
}
69+
5970
$this->assertNotNull($product->getName());
6071
$this->assertNotNull($product->getSku());
6172
$this->assertNotNull($product->getPrice());
62-
$this->assertNotNull($product->getWeight());
73+
$this->assertEquals($productDataWeight, (int)$product->getWeight());
6374
$this->assertEquals('1', $stockItem->getIsInStock());
6475
}
6576
}
@@ -113,6 +124,14 @@ public static function generateSimpleProductsDataProvider()
113124
'quantity_and_stock_status' => ['qty' => '5'],
114125
'weight' => '6'
115126
],
127+
[
128+
'name' => '1-ddd',
129+
'configurable_attribute' => '{"configurable_attribute":"22"}',
130+
'price' => '3',
131+
'sku' => '1-ddd',
132+
'quantity_and_stock_status' => ['qty' => '5'],
133+
'weight' => ''
134+
],
116135
],
117136
]
118137
];

0 commit comments

Comments
 (0)