Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public function save(StockItemInterface $stockItem)
}
} else {
$stockItem->setQty(0);
if ($stockItem->isObjectNew() && !$stockItem->hasStockStatusChangedAuto()) {
// A composite product's stock status is derived from its children. An implicitly
// created stock item records no merchant decision, so it must start under automatic
// control, otherwise ChangeParentStockStatus can never move the parent back in stock.
$stockItem->setStockStatusChangedAuto(1);
}
}

$stockItem->setWebsiteId($stockItem->getWebsiteId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ protected function setUp(): void
'setIsInStock',
'getIsInStock',
'getStockStatusChangedAuto',
'hasStockStatusChangedAuto',
'isObjectNew',
'getManageStock',
'setLowStockDate',
'setStockStatusChangedAuto',
Expand Down Expand Up @@ -469,6 +471,63 @@ public static function saveDataProvider(): array
];
}

/**
* A composite product's stock item is created implicitly and records no merchant decision, so it must
* start under automatic control.
*
* @param bool $isObjectNew
* @param bool $hasFlag
* @param bool $expectMarked
* @return void
* @throws CouldNotSaveException
*/
#[DataProvider('compositeStockItemDataProvider')]
public function testSaveMarksNewCompositeStockItemAsAutomaticallyMaintained(
bool $isObjectNew,
bool $hasFlag,
bool $expectMarked
): void {
$productId = 1;

$this->productMock->expects($this->once())->method('getId')->willReturn($productId);
$this->productMock->expects($this->once())->method('getTypeId')->willReturn('configurable');
$this->stockConfigurationMock->expects($this->once())
->method('isQty')
->with('configurable')
->willReturn(false);

$this->stockItemMock->expects($this->any())->method('getProductId')->willReturn($productId);
$this->stockItemMock->expects($this->once())->method('setQty')->with(0)->willReturnSelf();
$this->stockItemMock->expects($this->any())->method('isObjectNew')->willReturn($isObjectNew);
$this->stockItemMock->expects($this->any())->method('hasStockStatusChangedAuto')->willReturn($hasFlag);
$this->stockItemMock->expects($expectMarked ? $this->once() : $this->never())
->method('setStockStatusChangedAuto')
->with(1);
$this->stockItemMock->expects($this->any())->method('getWebsiteId')->willReturn(1);
$this->stockItemMock->expects($this->any())->method('getStockId')->willReturn(1);
$this->stockItemMock->expects($this->once())->method('setWebsiteId')->with(1)->willReturnSelf();
$this->stockItemMock->expects($this->once())->method('setStockId')->with(1)->willReturnSelf();

$this->stockItemResourceMock->expects($this->once())
->method('save')
->with($this->stockItemMock)
->willReturnSelf();

$this->assertEquals($this->stockItemMock, $this->model->save($this->stockItemMock));
}

/**
* @return array
*/
public static function compositeStockItemDataProvider(): array
{
return [
'new item under no merchant decision is marked' => [true, false, true],
'new item with an explicit decision is left alone' => [true, true, false],
'existing item is left alone' => [false, false, false],
];
}

/**
* @param MockObject $mockObject
* @param array $configs
Expand Down
Loading