Skip to content

Commit

Permalink
8624: Stock status not coming back after qty update
Browse files Browse the repository at this point in the history
  • Loading branch information
RomaKis committed Dec 11, 2017
1 parent 321278b commit 15928fb
Show file tree
Hide file tree
Showing 15 changed files with 144 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function __construct(
/**
* @inheritdoc
*/
public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stockItem)
public function save(StockItemInterface $stockItem)
{
try {
/** @var \Magento\Catalog\Model\Product $product */
Expand All @@ -161,10 +161,7 @@ public function save(\Magento\CatalogInventory\Api\Data\StockItemInterface $stoc
$typeId = $product->getTypeId() ?: $product->getTypeInstance()->getTypeId();
$isQty = $this->stockConfiguration->isQty($typeId);
if ($isQty) {
$isInStock = $this->stockStateProvider->verifyStock($stockItem);
if ($stockItem->getManageStock() && !$isInStock) {
$stockItem->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true);
}
$this->changeIsInStockIfNecessary($stockItem);
// if qty is below notify qty, update the low stock date to today date otherwise set null
$stockItem->setLowStockDate(null);
if ($this->stockStateProvider->verifyNotification($stockItem)) {
Expand Down Expand Up @@ -260,4 +257,27 @@ private function getStockRegistryStorage()
}
return $this->stockRegistryStorage;
}

/**
* Change is_in_stock value if necessary.
*
* @param StockItemInterface $stockItem
*
* @return void
*/
private function changeIsInStockIfNecessary(StockItemInterface $stockItem)
{
$isInStock = $this->stockStateProvider->verifyStock($stockItem);
if ($stockItem->getManageStock() && !$isInStock) {
$stockItem->setIsInStock(false)->setStockStatusChangedAutomaticallyFlag(true);
}

if ($stockItem->getManageStock()
&& $isInStock
&& !$stockItem->getIsInStock()
&& $stockItem->getOrigData(\Magento\CatalogInventory\Api\Data\StockItemInterface::QTY) == 0
) {
$stockItem->setIsInStock(true)->setStockStatusChangedAutomaticallyFlag(true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function testSave()
->method('verifyStock')
->with($this->stockItemMock)
->willReturn(false);
$this->stockItemMock->expects($this->once())->method('getManageStock')->willReturn(true);
$this->stockItemMock->expects($this->exactly(2))->method('getManageStock')->willReturn(true);
$this->stockItemMock->expects($this->once())->method('setIsInStock')->with(false)->willReturnSelf();
$this->stockItemMock->expects($this->once())
->method('setStockStatusChangedAutomaticallyFlag')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,36 @@ public function testGetOptions()
}
}
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple_out_of_stock.php
*/
public function testSaveWithDifferentQty()
{
//if save (out of stock product with qty 0) with new qty > 0 it should become in stock.
//if set out of stock for product with qty > 0 it should become out of stock
$product = $this->productRepository->get('simple-out-of-stock');
$stockItem = $product->getExtensionAttributes()->getStockItem();
$this->assertEquals(false, $stockItem->getIsInStock());
$stockData = [
'qty' => 5,
'is_in_stock' => 0,
];
$product->setStockData($stockData);
$product->save();

$product = $this->productRepository->get('simple-out-of-stock');
$stockItem = $product->getExtensionAttributes()->getStockItem();
$this->assertEquals(true, $stockItem->getIsInStock());
$stockData = [
'qty' => 3,
'is_in_stock' => 0,
];
$product->setStockData($stockData);
$product->save();

$product = $this->productRepository->get('simple-out-of-stock');
$stockItem = $product->getExtensionAttributes()->getStockItem();
$this->assertEquals(false, $stockItem->getIsInStock());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();

/** @var \Magento\TestFramework\ObjectManager $objectManager */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();

/** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */
$categoryLinkManagement = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class);

/** @var $product \Magento\Catalog\Model\Product */
$product = $objectManager->create(\Magento\Catalog\Model\Product::class);
$product->isObjectNew(true);
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
->setId(1)
->setAttributeSetId(4)
->setWebsiteIds([1])
->setName('Simple Product')
->setSku('simple-out-of-stock')
->setPrice(10)
->setWeight(1)
->setShortDescription("Short description")
->setTaxClassId(0)
->setDescription('Description with <b>html tag</b>')
->setMetaTitle('meta title')
->setMetaKeyword('meta keyword')
->setMetaDescription('meta description')
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
->setStockData(
[
'use_config_manage_stock' => 1,
'qty' => 0,
'is_qty_decimal' => 0,
'is_in_stock' => 0,
]
)->setCanSaveCustomOptions(true)
->setHasOptions(true);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepositoryFactory */
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$productRepository->save($product);

$categoryLinkManagement->assignProductToCategories(
$product->getSku(),
[2]
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
use Magento\Framework\Exception\NoSuchEntityException;

\Magento\TestFramework\Helper\Bootstrap::getInstance()->getInstance()->reinitialize();

/** @var \Magento\Framework\Registry $registry */
$registry = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\Registry::class);

$registry->unregister('isSecureArea');
$registry->register('isSecureArea', true);

/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
$productRepository = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Catalog\Api\ProductRepositoryInterface::class);
try {
$product = $productRepository->get('simple-out-of-stock', false, null, true);
$productRepository->delete($product);
} catch (NoSuchEntityException $e) {
}
$registry->unregister('isSecureArea');
$registry->register('isSecureArea', false);
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class ByStockItemRepositoryTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ByQuantityAndStockStatusTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ByStockDataTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ByStockItemTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ByQuantityAndStockStatusTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ByStockDataTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class ByStockItemTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ByStockItemTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ByStockItemTest extends \PHPUnit\Framework\TestCase
private $stockItemData = [
StockItemInterface::QTY => 555,
StockItemInterface::MANAGE_STOCK => true,
StockItemInterface::IS_IN_STOCK => false,
StockItemInterface::IS_IN_STOCK => true,
];

public function setUp()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ protected function load()
if (is_array($data)) {
foreach ($data as $row) {
$item = $this->createDataObject(['data' => $row]);
$item->setOrigData();
$this->addItem($item);
}
}
Expand Down

0 comments on commit 15928fb

Please sign in to comment.