Skip to content

Commit 0eea7a1

Browse files
committed
Merge branch 'develop' of https://github.com/magento-commerce/inventory into PR2-L3-09132023
2 parents ea759db + d6d53b2 commit 0eea7a1

File tree

7 files changed

+473
-7
lines changed

7 files changed

+473
-7
lines changed

Inventory/Model/SourceItem/Validator/SkuValidator.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
use Magento\Framework\Validation\ValidationResult;
1111
use Magento\Framework\Validation\ValidationResultFactory;
12+
use Magento\Inventory\Model\Validators\NoSpaceBeforeAndAfterString;
1213
use Magento\Inventory\Model\Validators\NotAnEmptyString;
1314
use Magento\InventoryApi\Api\Data\SourceItemInterface;
1415
use Magento\InventoryApi\Model\SourceItemValidatorInterface;
@@ -28,16 +29,24 @@ class SkuValidator implements SourceItemValidatorInterface
2829
*/
2930
private $notAnEmptyString;
3031

32+
/**
33+
* @var NoSpaceBeforeAndAfterString
34+
*/
35+
private $noSpaceBeforeAndAfterString;
36+
3137
/**
3238
* @param ValidationResultFactory $validationResultFactory
3339
* @param NotAnEmptyString $notAnEmptyString
40+
* @param NoSpaceBeforeAndAfterString $noSpaceBeforeAndAfterString
3441
*/
3542
public function __construct(
3643
ValidationResultFactory $validationResultFactory,
37-
NotAnEmptyString $notAnEmptyString
44+
NotAnEmptyString $notAnEmptyString,
45+
NoSpaceBeforeAndAfterString $noSpaceBeforeAndAfterString
3846
) {
3947
$this->validationResultFactory = $validationResultFactory;
4048
$this->notAnEmptyString = $notAnEmptyString;
49+
$this->noSpaceBeforeAndAfterString = $noSpaceBeforeAndAfterString;
4150
}
4251

4352
/**
@@ -47,10 +56,10 @@ public function validate(SourceItemInterface $source): ValidationResult
4756
{
4857
$value = $source->getSku();
4958
$errors = [
50-
$this->notAnEmptyString->execute(SourceItemInterface::SKU, (string)$value)
59+
$this->notAnEmptyString->execute(SourceItemInterface::SKU, (string)$value),
60+
$this->noSpaceBeforeAndAfterString->execute(SourceItemInterface::SKU, (string)$value)
5161
];
52-
$errors = !empty($errors) ? array_merge(...$errors) : $errors;
53-
62+
$errors = array_merge(...$errors);
5463
return $this->validationResultFactory->create(['errors' => $errors]);
5564
}
5665
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Inventory\Model\Validators;
9+
10+
/**
11+
* Checks whether given value contains spaces before and after
12+
*/
13+
class NoSpaceBeforeAndAfterString
14+
{
15+
/**
16+
* Checks whether given value contains spaces before and after
17+
*
18+
* @param string $fieldName
19+
* @param string $value
20+
* @return array
21+
*/
22+
public function execute(string $fieldName, string $value): array
23+
{
24+
$errors = [];
25+
26+
$trimValue = trim($value);
27+
if ($trimValue !== $value) {
28+
$errors[] = __('"%field" can not contain leading or trailing spaces.', ['field' => $fieldName]);
29+
}
30+
31+
return $errors;
32+
}
33+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Inventory\Test\Unit\Model\SourceItem\Validator;
9+
10+
use Magento\Framework\Phrase;
11+
use Magento\Framework\Validation\ValidationResult;
12+
use Magento\Framework\Validation\ValidationResultFactory;
13+
use Magento\Inventory\Model\SourceItem;
14+
use Magento\Inventory\Model\SourceItem\Validator\SkuValidator;
15+
use Magento\Inventory\Model\Validators\NoSpaceBeforeAndAfterString;
16+
use Magento\Inventory\Model\Validators\NotAnEmptyString;
17+
use PHPUnit\Framework\MockObject\MockObject;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class SkuValidatorTest extends TestCase
21+
{
22+
/**
23+
* @var ValidationResultFactory|MockObject
24+
*/
25+
private $validationResultFactory;
26+
27+
/**
28+
* @var NotAnEmptyString
29+
*/
30+
private $notAnEmptyString;
31+
32+
/**
33+
* @var NoSpaceBeforeAndAfterString
34+
*/
35+
private $noSpaceBeforeAndAfterString;
36+
37+
/**
38+
* @var SourceItem|MockObject
39+
*/
40+
private $sourceItemMock;
41+
42+
/**
43+
* @var SkuValidator
44+
*/
45+
private $skuValidator;
46+
47+
protected function setUp(): void
48+
{
49+
$this->validationResultFactory = $this->createMock(ValidationResultFactory::class);
50+
$this->notAnEmptyString = $this->createMock(NotAnEmptyString::class);
51+
$this->noSpaceBeforeAndAfterString = $this->createMock(NoSpaceBeforeAndAfterString::class);
52+
$this->sourceItemMock = $this->getMockBuilder(SourceItem::class)->disableOriginalConstructor()
53+
->onlyMethods(['getSku', 'getSourceCode', 'getQuantity', 'getStatus', 'getData', 'setData'])->getMock();
54+
$this->skuValidator = new SkuValidator(
55+
$this->validationResultFactory,
56+
$this->notAnEmptyString,
57+
$this->noSpaceBeforeAndAfterString
58+
);
59+
}
60+
61+
/**
62+
* @return array
63+
*/
64+
public function sourceDataProvider(): array
65+
{
66+
return [
67+
[
68+
[
69+
"sku" => "4444454",
70+
"quantity" => 30,
71+
"status" => 1,
72+
"execute" => [],
73+
"is_string_whitespace" => 0
74+
]
75+
],
76+
[
77+
[
78+
"sku" => "4444454 ",
79+
"quantity" => 30,
80+
"status" => 1,
81+
"execute" => [new Phrase('"%field" can not contain leading or trailing spaces.', ['sku'])],
82+
"is_string_whitespace" => 1
83+
]
84+
]
85+
];
86+
}
87+
88+
/**
89+
* @dataProvider sourceDataProvider
90+
* @param array $source
91+
* @return void
92+
*/
93+
public function testValidate(array $source): void
94+
{
95+
$this->sourceItemMock->expects($this->atLeastOnce())->method('getSku')
96+
->willReturn($source['sku']);
97+
$errors = [$source['execute']];
98+
$errors = array_merge(...$errors);
99+
$this->noSpaceBeforeAndAfterString->method('execute')->willReturn($source['execute']);
100+
$this->validationResultFactory->method('create')->with(
101+
['errors' => $errors]
102+
)->willReturn(new ValidationResult($errors));
103+
$result = $this->skuValidator->validate($this->sourceItemMock);
104+
if ($source['is_string_whitespace']) {
105+
foreach ($result->getErrors() as $error) {
106+
$this->assertEquals('"%field" can not contain leading or trailing spaces.', $error->getText());
107+
}
108+
} else {
109+
$this->assertEmpty($result->getErrors());
110+
}
111+
}
112+
}

Inventory/i18n/en_US.csv

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,4 @@
1818
"Could not save StockSourceLinks","Could not save StockSourceLinks"
1919
"""%field"" should be greater then 0.","""%field"" should be greater then 0."
2020
"Some error","Some error"
21+
"""%field"" can not contain leading or trailing spaces.","""%field"" can not contain leading or trailing spaces."

InventoryApi/Test/Api/SourceItemsSave/ValidationTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class ValidationTest extends WebapiAbstract
1717
/**#@+
1818
* Service constants
1919
*/
20-
const RESOURCE_PATH = '/V1/inventory/source-items';
21-
const SERVICE_NAME = 'inventoryApiSourceItemsSaveV1';
20+
public const RESOURCE_PATH = '/V1/inventory/source-items';
21+
public const SERVICE_NAME = 'inventoryApiSourceItemsSaveV1';
2222
/**#@-*/
2323

2424
/**
@@ -206,6 +206,12 @@ public function failedValidationDataProvider(): array
206206
'field' => SourceItemInterface::SKU,
207207
],
208208
],
209+
[
210+
'message' => '"%field" can not contain leading or trailing spaces.',
211+
'parameters' => [
212+
'field' => SourceItemInterface::SKU,
213+
],
214+
],
209215
],
210216
],
211217
],

InventoryInStorePickupSales/Model/Order/IsFulfillable.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,12 @@ public function execute(OrderInterface $order): bool
7474
if ($item->getHasChildren()) {
7575
continue;
7676
}
77-
if (!$this->isItemFulfillable($item->getSku(), $sourceCode, (float)$item->getQtyOrdered())) {
77+
78+
$stockItem = $item->getProduct()->getExtensionAttributes()->getStockItem();
79+
if (!$stockItem->getManageStock()) {
80+
return $stockItem->getIsInStock() === (bool)SourceItemInterface::STATUS_IN_STOCK &&
81+
$this->sourceRepository->get($sourceCode)->isEnabled();
82+
} elseif (!$this->isItemFulfillable($item->getSku(), $sourceCode, (float)$item->getQtyOrdered())) {
7883
return false;
7984
}
8085
}

0 commit comments

Comments
 (0)