Skip to content

Commit 59d4251

Browse files
author
Yaroslav Onischenko
committed
MAGETWO-59315: Non consistent save Product Stock Item via Web API and repository directly
1 parent 36b07c7 commit 59d4251

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

app/code/Magento/Catalog/Model/Product/Attribute/Backend/Stock.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function beforeSave($object)
5959
if (isset($stockData['qty']) && $stockData['qty'] === '') {
6060
$stockData['qty'] = null;
6161
}
62-
if ($object->getStockData() !== null || $stockData !== null) {
62+
if ($object->getStockData() !== null && $stockData !== null) {
6363
$object->setStockData(array_replace((array)$object->getStockData(), (array)$stockData));
6464
}
6565
$object->unsetData($this->getAttribute()->getAttributeCode());

app/code/Magento/Catalog/Test/Unit/Model/Product/Attribute/Backend/StockTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,17 @@ public function testBeforeSaveQtyIsZero()
125125
$stockData = $object->getStockData();
126126
$this->assertEquals(0, $stockData['qty']);
127127
}
128+
129+
public function testBeforeSaveNoStockData()
130+
{
131+
$object = new \Magento\Framework\DataObject(
132+
[
133+
self::ATTRIBUTE_NAME => ['is_in_stock' => 1, 'qty' => 0]
134+
]
135+
);
136+
137+
$this->model->beforeSave($object);
138+
$this->assertNull($object->getStockData());
139+
$this->assertNull($object->getData(self::ATTRIBUTE_NAME));
140+
}
128141
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\CatalogInventory\Api;
7+
8+
use Magento\Catalog\Api\Data\ProductExtensionInterface;
9+
use Magento\Catalog\Api\Data\ProductInterface;
10+
use Magento\Catalog\Api\ProductRepositoryInterface;
11+
12+
class StockItemSaveTest extends \PHPUnit_Framework_TestCase
13+
{
14+
/**
15+
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
16+
*/
17+
public function testSave()
18+
{
19+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
20+
/** @var ProductRepositoryInterface $productRepository */
21+
$productRepository = $objectManager->get(ProductRepositoryInterface::class);
22+
/** @var ProductInterface $product */
23+
$product = $productRepository->get('simple');
24+
25+
/** @var ProductExtensionInterface $ea */
26+
$ea = $product->getExtensionAttributes();
27+
$ea->getStockItem()->setQty(555);
28+
$productRepository->save($product);
29+
30+
$product = $productRepository->get('simple');
31+
$this->assertEquals(555, $product->getExtensionAttributes()->getStockItem()->getQty());
32+
33+
$stockItem = $product->getExtensionAttributes()->getStockItem();
34+
$stockItem->setQty(200);
35+
/** @var StockItemRepositoryInterface $stockItemRepository */
36+
$stockItemRepository = $objectManager->get(StockItemRepositoryInterface::class);
37+
$stockItemRepository->save($stockItem);
38+
$this->assertEquals(200, $product->getExtensionAttributes()->getStockItem()->getQty());
39+
40+
$product = $productRepository->get('simple');
41+
$this->assertEquals(200, $product->getExtensionAttributes()->getStockItem()->getQty());
42+
}
43+
}

0 commit comments

Comments
 (0)