From 4868a979873b35a1f64ec790675303b5cf9cf757 Mon Sep 17 00:00:00 2001 From: MaximLogic Date: Fri, 9 Jan 2026 16:14:32 +0200 Subject: [PATCH 1/2] magento/magento2#38154: Getting success message after sending invalid SKU for the inventory/source-items API - added validation for existing SKUs on request processing --- .../SourceItem/Validator/SkuValidator.php | 39 +++++++++++- .../SourceItem/Validator/SkuValidatorTest.php | 59 +++++++++++++++++-- 2 files changed, 91 insertions(+), 7 deletions(-) diff --git a/Inventory/Model/SourceItem/Validator/SkuValidator.php b/Inventory/Model/SourceItem/Validator/SkuValidator.php index 20d5b3572a02..8afa6ada5d8c 100644 --- a/Inventory/Model/SourceItem/Validator/SkuValidator.php +++ b/Inventory/Model/SourceItem/Validator/SkuValidator.php @@ -1,12 +1,14 @@ validationResultFactory = $validationResultFactory; $this->notAnEmptyString = $notAnEmptyString; $this->noSpaceBeforeAndAfterString = $noSpaceBeforeAndAfterString; + $this->productRepository = $productRepository; } /** @@ -57,9 +67,32 @@ public function validate(SourceItemInterface $source): ValidationResult $value = $source->getSku(); $errors = [ $this->notAnEmptyString->execute(SourceItemInterface::SKU, (string)$value), - $this->noSpaceBeforeAndAfterString->execute(SourceItemInterface::SKU, (string)$value) + $this->noSpaceBeforeAndAfterString->execute(SourceItemInterface::SKU, (string)$value), + $this->validateSkuExists((string)$value) ]; $errors = array_merge(...$errors); return $this->validationResultFactory->create(['errors' => $errors]); } + + /** + * Validate that product with given SKU exists + * + * @param string $sku + * @return array + */ + private function validateSkuExists(string $sku): array + { + $errors = []; + if (empty($sku)) { + return $errors; + } + + try { + $this->productRepository->get($sku); + } catch (NoSuchEntityException $e) { + $errors[] = __('Product with SKU "%1" does not exist.', $sku); + } + + return $errors; + } } diff --git a/Inventory/Test/Unit/Model/SourceItem/Validator/SkuValidatorTest.php b/Inventory/Test/Unit/Model/SourceItem/Validator/SkuValidatorTest.php index 2a152a1ae4ab..e55a415da91b 100644 --- a/Inventory/Test/Unit/Model/SourceItem/Validator/SkuValidatorTest.php +++ b/Inventory/Test/Unit/Model/SourceItem/Validator/SkuValidatorTest.php @@ -7,6 +7,9 @@ namespace Magento\Inventory\Test\Unit\Model\SourceItem\Validator; +use Magento\Catalog\Api\Data\ProductInterface; +use Magento\Catalog\Api\ProductRepositoryInterface; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\Phrase; use Magento\Framework\Validation\ValidationResult; use Magento\Framework\Validation\ValidationResultFactory; @@ -35,6 +38,11 @@ class SkuValidatorTest extends TestCase */ private $noSpaceBeforeAndAfterString; + /** + * @var ProductRepositoryInterface|MockObject + */ + private $productRepository; + /** * @var SourceItem|MockObject */ @@ -50,12 +58,14 @@ protected function setUp(): void $this->validationResultFactory = $this->createMock(ValidationResultFactory::class); $this->notAnEmptyString = $this->createMock(NotAnEmptyString::class); $this->noSpaceBeforeAndAfterString = $this->createMock(NoSpaceBeforeAndAfterString::class); + $this->productRepository = $this->createMock(ProductRepositoryInterface::class); $this->sourceItemMock = $this->getMockBuilder(SourceItem::class)->disableOriginalConstructor() ->onlyMethods(['getSku', 'getSourceCode', 'getQuantity', 'getStatus', 'getData', 'setData'])->getMock(); $this->skuValidator = new SkuValidator( $this->validationResultFactory, $this->notAnEmptyString, - $this->noSpaceBeforeAndAfterString + $this->noSpaceBeforeAndAfterString, + $this->productRepository ); } @@ -98,9 +108,14 @@ public function testValidate(array $source): void $errors = [$source['execute']]; $errors = array_merge(...$errors); $this->noSpaceBeforeAndAfterString->method('execute')->willReturn($source['execute']); - $this->validationResultFactory->method('create')->with( - ['errors' => $errors] - )->willReturn(new ValidationResult($errors)); + + // Mock product repository to return existing product + $product = $this->createMock(ProductInterface::class); + $this->productRepository->method('get')->willReturn($product); + + $this->validationResultFactory->method('create')->with( + ['errors' => $errors] + )->willReturn(new ValidationResult($errors)); $result = $this->skuValidator->validate($this->sourceItemMock); if ($source['is_string_whitespace']) { foreach ($result->getErrors() as $error) { @@ -110,4 +125,40 @@ public function testValidate(array $source): void $this->assertEmpty($result->getErrors()); } } + + /** + * Test validation when SKU does not exist + * + * @return void + */ + public function testValidateNonExistentSku(): void + { + $sku = 'non_existent_sku'; + $this->sourceItemMock->expects($this->atLeastOnce())->method('getSku') + ->willReturn($sku); + + $this->notAnEmptyString->method('execute')->willReturn([]); + $this->noSpaceBeforeAndAfterString->method('execute')->willReturn([]); + + // Mock product repository to throw NoSuchEntityException + $this->productRepository->expects($this->once()) + ->method('get') + ->with($sku) + ->willThrowException(new NoSuchEntityException(__('Product not found'))); + + $expectedError = __('Product with SKU "%1" does not exist.', $sku); + $errors = [$expectedError]; + + $this->validationResultFactory->method('create')->with( + ['errors' => $errors] + )->willReturn(new ValidationResult($errors)); + + $result = $this->skuValidator->validate($this->sourceItemMock); + + $this->assertCount(1, $result->getErrors()); + $errorMessage = $result->getErrors()[0]; + $this->assertInstanceOf(Phrase::class, $errorMessage); + $this->assertEquals('Product with SKU "%1" does not exist.', $errorMessage->getText()); + $this->assertEquals(['non_existent_sku'], $errorMessage->getArguments()); + } } From e8ce440eb1886e2cfe63f460405f905ba067eba3 Mon Sep 17 00:00:00 2001 From: MaximLogic Date: Thu, 22 Jan 2026 12:37:37 +0200 Subject: [PATCH 2/2] magento/magento2#38154: Getting success message after sending invalid SKU for the inventory/source-items API - reverted copyright change - added $productRepository initialisation via Object Manager if null --- Inventory/Model/SourceItem/Validator/SkuValidator.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Inventory/Model/SourceItem/Validator/SkuValidator.php b/Inventory/Model/SourceItem/Validator/SkuValidator.php index 8afa6ada5d8c..31be62b4099f 100644 --- a/Inventory/Model/SourceItem/Validator/SkuValidator.php +++ b/Inventory/Model/SourceItem/Validator/SkuValidator.php @@ -1,6 +1,6 @@ validationResultFactory = $validationResultFactory; $this->notAnEmptyString = $notAnEmptyString; $this->noSpaceBeforeAndAfterString = $noSpaceBeforeAndAfterString; - $this->productRepository = $productRepository; + $this->productRepository = $productRepository ?: + \Magento\Framework\App\ObjectManager::getInstance()->get(ProductRepositoryInterface::class); } /**