Skip to content

Commit 0846826

Browse files
committed
Merge remote-tracking branch 'origin/ACP2E-4116' into PR_2025_09_08_muntianu
2 parents b48b526 + e7ff85e commit 0846826

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogImportExport\Model\Import\Product\Validator;
9+
10+
use Magento\CatalogImportExport\Model\Import\Product\RowValidatorInterface;
11+
use Magento\CatalogInventory\Model\Source\Backorders as BackordersSource;
12+
13+
class Backorders extends AbstractImportValidator implements RowValidatorInterface
14+
{
15+
/**
16+
* @param BackordersSource $backordersSource
17+
*/
18+
public function __construct(
19+
private readonly BackordersSource $backordersSource
20+
) {
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
public function isValid($value)
27+
{
28+
$this->_clearMessages();
29+
if (!isset($value['allow_backorders'])) {
30+
return true;
31+
}
32+
33+
$backorders = $value['allow_backorders'];
34+
if (in_array($backorders, [$this->context->getEmptyAttributeValueConstant(), ''], true)) {
35+
return true;
36+
}
37+
38+
if (!is_numeric($backorders)) {
39+
$messageTemplate = $this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_TYPE);
40+
$message = sprintf($messageTemplate, 'allow_backorders');
41+
$this->_addMessages([$message]);
42+
return false;
43+
}
44+
45+
$allowedValues = array_column($this->backordersSource->toOptionArray(), 'label', 'value');
46+
if (!isset($allowedValues[$backorders])) {
47+
$messageTemplate = $this->context->retrieveMessageTemplate(self::ERROR_INVALID_ATTRIBUTE_OPTION);
48+
$message = sprintf($messageTemplate, 'allow_backorders');
49+
$this->_addMessages([$message]);
50+
return false;
51+
}
52+
53+
return true;
54+
}
55+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright 2025 Adobe
4+
* All Rights Reserved.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\CatalogImportExport\Test\Unit\Model\Import\Product\Validator;
9+
10+
use Magento\CatalogImportExport\Model\Import\Product;
11+
use Magento\CatalogImportExport\Model\Import\Product\Validator\Backorders;
12+
use Magento\CatalogInventory\Model\Source\Backorders as BackordersSource;
13+
use Magento\ImportExport\Model\Import;
14+
use PHPUnit\Framework\Attributes\TestWith;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
18+
class BackordersTest extends TestCase
19+
{
20+
/**
21+
* @var BackordersSource|MockObject
22+
*/
23+
private $backordersSourceMock;
24+
25+
/**
26+
* @var Backorders
27+
*/
28+
private $backorders;
29+
30+
protected function setUp(): void
31+
{
32+
$this->backordersSourceMock = $this->createMock(BackordersSource::class);
33+
$this->backorders = new Backorders($this->backordersSourceMock);
34+
35+
$this->backordersSourceMock->method('toOptionArray')
36+
->willReturn([
37+
['value' => 0, 'label' => 'No Backorders'],
38+
['value' => 1, 'label' => 'Allow Qty Below 0'],
39+
['value' => 2, 'label' => 'Allow Qty Below 0 and Notify Customer'],
40+
]);
41+
42+
$contextStub = $this->createMock(Product::class);
43+
$contextStub->method('getEmptyAttributeValueConstant')
44+
->willReturn(Import::DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT);
45+
$contextStub->method('retrieveMessageTemplate')
46+
->willReturnMap([
47+
[Backorders::ERROR_INVALID_ATTRIBUTE_TYPE, 'Value for \'%s\' attribute contains incorrect value'],
48+
[Backorders::ERROR_INVALID_ATTRIBUTE_OPTION, 'Value for \'%s\' attribute contains unacceptable value'],
49+
]);
50+
$this->backorders->init($contextStub);
51+
}
52+
53+
#[
54+
TestWith([['allow_backorders' => 0], true]),
55+
TestWith([['allow_backorders' => 1], true]),
56+
TestWith([['allow_backorders' => 2], true]),
57+
TestWith([['allow_backorders' => Import::DEFAULT_EMPTY_ATTRIBUTE_VALUE_CONSTANT], true]),
58+
TestWith([['some_field' => 'value'], true]),
59+
TestWith([['allow_backorders' => 3], false]),
60+
TestWith([['allow_backorders' => 'value'], false]),
61+
]
62+
public function testIsValid(array $value, bool $expected): void
63+
{
64+
$result = $this->backorders->isValid($value);
65+
$this->assertEquals($expected, $result);
66+
}
67+
}

app/code/Magento/CatalogImportExport/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
<item name="layout_update" xsi:type="object">Magento\CatalogImportExport\Model\Import\Product\Validator\LayoutUpdate</item>
3434
<item name="layout_update_permissions" xsi:type="object">Magento\CatalogImportExport\Model\Import\Product\Validator\LayoutUpdatePermissions</item>
3535
<item name="name" xsi:type="object">Magento\CatalogImportExport\Model\Import\Product\Validator\Name</item>
36+
<item name="backorders" xsi:type="object">Magento\CatalogImportExport\Model\Import\Product\Validator\Backorders</item>
3637
</argument>
3738
</arguments>
3839
</type>

0 commit comments

Comments
 (0)