Skip to content

Commit 97fd1a3

Browse files
committed
ACP2E-3660: REST endpoint Product Import Json not validate the mandatory fields
1 parent 09471ba commit 97fd1a3

File tree

2 files changed

+40
-7
lines changed
  • app/code/Magento/CatalogImportExport

2 files changed

+40
-7
lines changed

app/code/Magento/CatalogImportExport/Model/Import/Product/Validator/Name.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@
88
namespace Magento\CatalogImportExport\Model\Import\Product\Validator;
99

1010
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
11+
use Magento\CatalogImportExport\Model\Import\Product\SkuStorage;
12+
use Magento\CatalogImportExport\Model\Import\Product;
1113

1214
class Name extends AbstractImportValidator implements RowValidatorInterface
1315
{
16+
/**
17+
* @var SkuStorage
18+
*/
19+
private SkuStorage $skuStorage;
20+
21+
/**
22+
* @param SkuStorage $skuStorage
23+
*/
24+
public function __construct(SkuStorage $skuStorage)
25+
{
26+
$this->skuStorage = $skuStorage;
27+
}
28+
1429
/**
1530
* @inheritDoc
1631
*/
@@ -19,7 +34,8 @@ public function isValid($value)
1934
$this->_clearMessages();
2035
if (!$this->hasNameValue($value) &&
2136
!$this->hasCustomOptions($value) &&
22-
(!isset($value['store_view_code']) || $value['store_view_code'] != 'default')
37+
(!isset($value['store_view_code']) || $value['store_view_code'] != 'default') &&
38+
!$this->skuStorage->has($value[Product::COL_SKU])
2339
) {
2440
$this->_addMessages(
2541
[
@@ -43,9 +59,9 @@ public function isValid($value)
4359
*/
4460
private function hasNameValue(array $rowData): bool
4561
{
46-
return array_key_exists('name', $rowData) &&
47-
!empty($rowData['name']) &&
48-
$rowData['name'] !== $this->context->getEmptyAttributeValueConstant();
62+
return array_key_exists(Product::COL_NAME, $rowData) &&
63+
!empty($rowData[Product::COL_NAME]) &&
64+
$rowData[Product::COL_NAME] !== $this->context->getEmptyAttributeValueConstant();
4965
}
5066

5167
/**

app/code/Magento/CatalogImportExport/Test/Unit/Model/Import/Product/Validator/NameTest.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,28 @@
88
namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Validator;
99

1010
use Magento\CatalogImportExport\Model\Import\Product;
11+
use Magento\CatalogImportExport\Model\Import\Product\SkuStorage;
1112
use Magento\CatalogImportExport\Model\Import\Product\Validator\Name;
1213
use PHPUnit\Framework\MockObject\Exception;
14+
use PHPUnit\Framework\MockObject\MockObject;
1315
use PHPUnit\Framework\TestCase;
1416

1517
class NameTest extends TestCase
1618
{
19+
/**
20+
* @var SkuStorage|MockObject
21+
*/
22+
private SkuStorage $skuStorage;
23+
24+
/**
25+
* @return void
26+
*/
27+
protected function setUp(): void
28+
{
29+
parent::setUp();
30+
$this->skuStorage = $this->createMock(SkuStorage::class);
31+
}
32+
1733
/**
1834
* @param $expected
1935
* @param $value
@@ -23,7 +39,8 @@ class NameTest extends TestCase
2339
*/
2440
public function testIsValid($expected, $value): void
2541
{
26-
$nameValidator = new Name();
42+
$this->skuStorage->expects($this->any())->method('has')->willReturn(false);
43+
$nameValidator = new Name($this->skuStorage);
2744
$context = $this->createMock(Product::class);
2845
$context->expects($this->any())->method('getEmptyAttributeValueConstant')->willReturn('|');
2946
$context->expects($this->any())->method('retrieveMessageTemplate')->willReturn('%s error %s');
@@ -39,11 +56,11 @@ public static function getRowData(): array
3956
return [
4057
[
4158
false,
42-
['name' => null, 'store_view_code' => 'en']
59+
['name' => null, 'store_view_code' => 'en', 'sku' => 'sku']
4360
],
4461
[
4562
true,
46-
['name' => 'anything goes here', 'store_view_code' => 'en']
63+
['name' => 'anything goes here', 'store_view_code' => 'en', 'sku' => 'sku']
4764
]
4865
];
4966
}

0 commit comments

Comments
 (0)