Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#38488): check source_code integrity during source_item mass save #3411

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
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
39 changes: 39 additions & 0 deletions Inventory/Model/ResourceModel/SourceItem/SaveMultiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\DB\Adapter\AdapterInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Inventory\Model\ResourceModel\Source as SourceResourceModel;
use Magento\Inventory\Model\ResourceModel\SourceItem as SourceItemResourceModel;
use Magento\InventoryApi\Api\Data\SourceItemInterface;

Expand Down Expand Up @@ -47,6 +49,8 @@ public function execute(array $sourceItems)
$connection = $this->resourceConnection->getConnection();
$tableName = $this->resourceConnection->getTableName(SourceItemResourceModel::TABLE_NAME_SOURCE_ITEM);

$this->assertSourceCodesIntegrity($sourceItems);

[$newItems, $existingItems] = $this->separateExistingAndNewItems($sourceItems);
if (count($newItems)) {
$this->insertNewItems($newItems, $connection, $tableName);
Expand All @@ -56,6 +60,41 @@ public function execute(array $sourceItems)
}
}

/**
* Ensure stocks for incoming items are exists
*
* @param SourceItemInterface[] $sourceItems
* @return void
*
* @throws LocalizedException
*/
private function assertSourceCodesIntegrity($sourceItems)
{
$sourceCodes = array_unique(array_map(function($sourceItem) {
return $sourceItem->getSourceCode();
}, $sourceItems));

$sourcesCheckSql = sprintf(
'SELECT `%s` FROM `%s` WHERE `%s` in (%s)',
SourceItemInterface::SOURCE_CODE,
$this->resourceConnection->getTableName(SourceResourceModel::TABLE_NAME_SOURCE),
SourceItemInterface::SOURCE_CODE,
implode(', ', array_map(function ($code) {
return '"' . $code . '"';
}, $sourceCodes)),
);

$existingSources = $this->resourceConnection
->getConnection()
->query($sourcesCheckSql)
->fetchAll(\Zend_Db::FETCH_COLUMN);

$missing = array_diff($sourceCodes, $existingSources);
if (count($missing) > 0) {
throw new LocalizedException(__("Requested source(s) was not found by the source_code: %1", implode(', ', $missing)));
}
}

/**
* Build column sql part
*
Expand Down
68 changes: 45 additions & 23 deletions InventoryApi/Test/Api/SourceItemsSave/SaveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,9 @@ class SaveTest extends WebapiAbstract
const SERVICE_NAME_DELETE = 'inventoryApiSourceItemsDeleteV1';
/**#@-*/

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
*
* @see https://app.hiptest.com/projects/69435/test-plan/folders/529092/scenarios/1824126
* @see https://app.hiptest.com/projects/69435/test-plan/folders/530616/scenarios/1824143
*/
public function testExecute()
private function createSourceItemsStub()
{
$sourceItems = [
return [
[
SourceItemInterface::SOURCE_CODE => 'eu-1',
SourceItemInterface::SKU => 'SKU-1',
Expand All @@ -46,6 +39,18 @@ public function testExecute()
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
],
];
}

/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
*/
public function testSourceIntegrityFails()
{
$sourceItems = $this->createSourceItemsStub();
foreach ($sourceItems as $item) {
$item[SourceItemInterface::SOURCE_CODE] = strtoupper($item[SourceItemInterface::SOURCE_CODE]);
}

$serviceInfo = [
'rest' => [
Expand All @@ -61,26 +66,42 @@ public function testExecute()

$actualData = $this->getSourceItems();

self::assertEquals(2, $actualData['total_count']);
AssertArrayContains::assert($sourceItems, $actualData['items']);
self::assertEquals(0, $actualData['total_count']);
}

protected function tearDown(): void
/**
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
*
* @see https://app.hiptest.com/projects/69435/test-plan/folders/529092/scenarios/1824126
* @see https://app.hiptest.com/projects/69435/test-plan/folders/530616/scenarios/1824143
*/
public function testExecute()
{
$sourceItems = [
[
SourceItemInterface::SOURCE_CODE => 'eu-1',
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 5.5,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
$sourceItems = $this->createSourceItemsStub();

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH,
'httpMethod' => Request::HTTP_METHOD_POST,
],
[
SourceItemInterface::SOURCE_CODE => 'eu-2',
SourceItemInterface::SKU => 'SKU-1',
SourceItemInterface::QUANTITY => 3,
SourceItemInterface::STATUS => SourceItemInterface::STATUS_IN_STOCK,
'soap' => [
'service' => self::SERVICE_NAME_SAVE,
'operation' => self::SERVICE_NAME_SAVE . 'Execute',
],
];
$this->_webApiCall($serviceInfo, ['sourceItems' => $sourceItems]);

$actualData = $this->getSourceItems();

self::assertEquals(2, $actualData['total_count']);
AssertArrayContains::assert($sourceItems, $actualData['items']);
}

protected function tearDown(): void
{
$sourceItems = $sourceItems = $this->createSourceItemsStub();

$serviceInfo = [
'rest' => [
'resourcePath' => self::RESOURCE_PATH . '?'
Expand All @@ -92,6 +113,7 @@ protected function tearDown(): void
'operation' => self::SERVICE_NAME_DELETE . 'Execute',
],
];

(TESTS_WEB_API_ADAPTER === self::ADAPTER_REST)
? $this->_webApiCall($serviceInfo)
: $this->_webApiCall($serviceInfo, ['sourceItems' => $sourceItems]);
Expand Down