AmpersandHQ / magento2-disable-stock-reservation

This module disables the inventory reservation logic introduced as part of MSI in Magento 2.3.3
GNU Lesser General Public License v3.0
215 stars 61 forks source link

Fix `is_in_stock` not being handled properly with `back_to_stock` on refunds #120

Closed convenient closed 1 year ago

convenient commented 1 year ago

In some cases if a product has moved to is_in_stock=0 and a refund is issued and returns the stock, the is_in_stock=1 flag is not being set so the product remains unsaleable.

This is detailed in https://github.com/AmpersandHQ/magento2-disable-stock-reservation/pull/92

The only change from #92 that was necessary for me to get working tests was b1a1c7b3a21d782b8cae4ed4fbf7c8d917ecb6d5 so that's what has been committed into this branch.

Before code fix

2.4.3 fails like

There was 1 failure:
1) Ampersand\DisableStockReservation\Test\Integration\Refund\ProductGoesBackInStockWhenRefundedTest::testProductBackInStockHandling with data set "back_to_stock=true returns items" ('1', array(true, 5))
is_in_stock does not match expected
Failed asserting that false matches expected true.

2.4.5 and higher fails like

1) CheckoutCest: Product goes back in stock when order is refunded
 Test  tests/acceptance/CheckoutCest.php:productGoesBackInStockWhenOrderIsRefunded
 Step  Assert equals 1,0,"Product did not go to is_in_stock=1 after a refund"
 Fail  Product did not go to is_in_stock=1 after a refund
Failed asserting that 0 matches expected 1.

Rest API testing

See the code for the details, but we could reproduce this issue using the rest api and it went away after cherry picking in the fix.

I cover two flows

Adminhtml Controller testing

See the code for the details, but we could reproduce this issue using the adminhtml controller and it went away after cherry picking in the fix.

A bit of work was needed to identify which caches need cleared during the execution, this is not needed for the rest api test because it actually spins up a new request with no object cache storage to cause problems.

I cover two flows

Manual Testing

I have also tested this on the frontend manually

  1. On vanilla install of magento 2.4.3
  2. require this module at this branch
  3. Create a simple product
  4. Place order for simple product taking its full stock using checkmo
  5. Product goes is_in_stock=0
  6. Invoice the order
  7. Ship the order

Before this fix

  1. Create a credit memo for the order, return items to stock, see that is_in_stock=0 goes back. ⚠️

After this fix

  1. Create a credit memo for the order, return items to stock, see that is_in_stock=1 goes back. ✅

Checklist