diff --git a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php index 1f529818872dd..aed1e25593e48 100644 --- a/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php +++ b/app/code/Magento/CatalogInventory/Model/ResourceModel/Stock.php @@ -125,13 +125,23 @@ public function lockProductsStock($productIds, $websiteId) return []; } $itemTable = $this->getTable('cataloginventory_stock_item'); - $productTable = $this->getTable('catalog_product_entity'); $select = $this->getConnection()->select()->from(['si' => $itemTable]) - ->join(['p' => $productTable], 'p.entity_id=si.product_id', ['type_id']) ->where('website_id=?', $websiteId) ->where('product_id IN(?)', $productIds) ->forUpdate(true); - return $this->getConnection()->fetchAll($select); + + $productTable = $this->getTable('catalog_product_entity'); + $selectProducts = $this->getConnection()->select()->from(['p' => $productTable], []) + ->where('entity_id IN (?)', $productIds) + ->columns( + [ + 'product_id' => 'entity_id', + 'type_id' => 'type_id' + ] + ); + $this->getConnection()->query($select); + + return $this->getConnection()->fetchAll($selectProducts); } /** diff --git a/app/code/Magento/CatalogInventory/Test/Unit/Model/ResourceModel/StockTest.php b/app/code/Magento/CatalogInventory/Test/Unit/Model/ResourceModel/StockTest.php new file mode 100644 index 0000000000000..b9894eb39197f --- /dev/null +++ b/app/code/Magento/CatalogInventory/Test/Unit/Model/ResourceModel/StockTest.php @@ -0,0 +1,99 @@ +contextMock = $this->getMockBuilder(Context::class)->disableOriginalConstructor()->getMock(); + $this->resourceMock = $this->getMockBuilder(ResourceConnection::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->contextMock->expects($this->once())->method('getResources')->willReturn($this->resourceMock); + + $this->stockModel = (new ObjectManager($this))->getObject( + Stock::class, + [ + 'context' => $this->contextMock + ] + ); + } + + public function testLockProductsStock() + { + $productIds = [1, 2]; + $websiteId = 1; + $itemTable = 'item_table'; + $productTable = 'product_table'; + + $connectionMock = $this->getMockBuilder(\Magento\Framework\DB\Adapter\AdapterInterface::class)->getMock(); + $this->resourceMock->method('getConnection')->will($this->returnValue($connectionMock)); + + $this->resourceMock->method('getTableName') + ->withConsecutive(['cataloginventory_stock_item'], ['catalog_product_entity']) + ->willReturnOnConsecutiveCalls($itemTable, $productTable); + + $selectMock = $this->getMockBuilder(\Magento\Framework\DB\Select::class) + ->disableOriginalConstructor() + ->getMock(); + + $selectProductsMock = clone $selectMock; + + $selectMock->expects($this->once())->method('forUpdate')->with(true)->willReturnSelf(); + $selectMock->expects($this->once())->method('from')->with(['si' => $itemTable])->willReturnSelf(); + $selectMock->expects($this->exactly(2)) + ->method('where') + ->withConsecutive(['website_id=?', $websiteId], ['product_id IN(?)', $productIds]) + ->willReturnSelf(); + $connectionMock->expects($this->exactly(2)) + ->method('select') + ->willReturnOnConsecutiveCalls($selectMock, $selectProductsMock); + + $selectProductsMock->expects($this->once())->method('from')->with(['p' => $productTable], [])->willReturnSelf(); + $selectProductsMock->expects($this->once())->method('where') + ->with('entity_id IN (?)', $productIds) + ->willReturnSelf(); + $selectProductsMock->expects($this->once())->method('columns')->willReturnSelf(); + + $connectionMock->expects($this->once())->method('query')->with($selectMock); + $connectionMock->expects($this->once())->method('fetchAll')->with($selectProductsMock)->willReturn([]); + + $this->assertEquals([], $this->stockModel->lockProductsStock($productIds, $websiteId)); + } + + public function testLockNoProductsStock() + { + $productIds = []; + $websiteId = 1; + + $this->assertEquals([], $this->stockModel->lockProductsStock($productIds, $websiteId)); + } +}