Skip to content

Commit

Permalink
Merge pull request magento#1019 from magento-engcom/889-remove-sku-ve…
Browse files Browse the repository at this point in the history
…rification

magento#889 - Remove all cross checks between Inventory and Catalog regarding SKU Verification
  • Loading branch information
Valeriy Nayda authored May 17, 2018
2 parents d7513cf + 006abd2 commit f9f56b3
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\InventoryCatalog\Plugin\InventoryApi;

use Magento\Framework\Exception\InputException;
use Magento\InventoryApi\Api\Data\SourceItemInterface;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
use Magento\InventoryCatalog\Model\SourceItemsSaveSynchronization\SetDataToLegacyCatalogInventory;
Expand Down Expand Up @@ -73,7 +74,13 @@ public function afterExecute(SourceItemsSaveInterface $subject, $result, array $
}

$sku = $sourceItem->getSku();
$typeId = $this->getProductTypeBySku->execute([$sku])[$sku];

try {
$typeId = $this->getProductTypeBySku->execute([$sku])[$sku];
} catch (InputException $e) {
// Save source item data for not existed product
continue;
}

if (false === $this->isSourceItemsAllowedForProductType->execute($typeId)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

namespace Magento\InventoryImportExport\Model\Import\Validator;

use Magento\Catalog\Api\Data\ProductInterface;
use Magento\Catalog\Model\ResourceModel\Product\CollectionFactory;
use Magento\Framework\Validation\ValidationResultFactory;
use Magento\InventoryImportExport\Model\Import\Sources;

Expand All @@ -17,25 +15,17 @@
*/
class SkuValidator implements ValidatorInterface
{
/**
* @var CollectionFactory
*/
private $collectionFactory;

/**
* @var ValidationResultFactory
*/
private $validationResultFactory;

/**
* @param CollectionFactory $collectionFactory
* @param ValidationResultFactory $validationResultFactory
*/
public function __construct(
CollectionFactory $collectionFactory,
ValidationResultFactory $validationResultFactory
) {
$this->collectionFactory = $collectionFactory;
$this->validationResultFactory = $validationResultFactory;
}

Expand All @@ -48,24 +38,8 @@ public function validate(array $rowData, int $rowNumber)

if (!isset($rowData[Sources::COL_SKU])) {
$errors[] = __('Missing required column "%column"', ['column' => Sources::COL_SKU]);
} elseif (!$this->isValidSku($rowData[Sources::COL_SKU])) {
$errors[] = __('Product with SKU "%sku" does not exist', ['sku' => $rowData[Sources::COL_SKU]]);
}

return $this->validationResultFactory->create(['errors' => $errors]);
}

/**
* Attempt to get Product collection filtered using SKU check size and return bool
*
* @param string $sku
* @return bool
*/
private function isValidSku(string $sku): bool
{
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
$collection = $this->collectionFactory->create();
$collection->addAttributeToFilter(ProductInterface::SKU, $sku);
return $collection->getSize() > 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public function setUp()
}

/**
* Tests that with an invalid SKU the validation does not pass as expected
* Tests that with a SKU that deosn't exist in the catalog still passes as valid
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/products.php
* @magentoDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/sources.php
*/
public function testInvalidSkuDoesNotPassValidation()
public function testNonExistentSkuDoesPassValidation()
{
// SKU-50000 is an invalid SKU and should therefore fail validation
// SKU-50000 is a non-existent SKU but should still pass Sku Validation
$rowData = $this->buildRowDataArray(
'default',
'SKU-50000',
10,
1
);
$result = $this->importer->validateRow($rowData, 1);
$this->assertFalse($result);
$this->assertTrue($result);
}

/**
Expand Down
1 change: 0 additions & 1 deletion app/code/Magento/InventoryImportExport/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"require": {
"php": "~7.1.3||~7.2.0",
"magento/framework": "*",
"magento/module-catalog": "*",
"magento/module-eav": "*",
"magento/module-import-export": "*",
"magento/module-inventory-api": "*",
Expand Down

0 comments on commit f9f56b3

Please sign in to comment.