Skip to content

Commit 3f7e98f

Browse files
Merge branch 'develop' into ACQE-5379
2 parents e6cc61f + 062205f commit 3f7e98f

File tree

31 files changed

+1556
-496
lines changed

31 files changed

+1556
-496
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
],

InventoryBundleProduct/Test/Api/BundleProductChildSourceUpdateTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,11 @@ public function testAddSourceToChildShipmentTypeSeparately(): void
158158
self::assertNotNull($result);
159159
$this->addSourceItems($this->sourceItems);
160160
$actualData = $this->getSourceItems('SKU-4');
161-
self::assertEquals(2, $actualData['total_count']);
162-
AssertArrayContains::assert($this->sourceItems, $actualData['items']);
161+
self::assertEquals(count($this->sourceItems), $actualData['total_count']);
162+
self::assertCount(count($this->sourceItems), $actualData['items']);
163+
foreach ($this->sourceItems as $sourceItem) {
164+
self::assertContainsEquals($sourceItem, $actualData['items']);
165+
}
163166
}
164167

165168
#[
Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,66 @@
55
*/
66
declare(strict_types=1);
77

8-
namespace Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
8+
namespace Magento\InventoryBundleProductIndexer\Indexer;
99

1010
use Magento\Framework\Exception\StateException;
1111
use Magento\InventoryBundleProductIndexer\Indexer\SourceItem\SourceItemIndexer as BundleProductsSourceItemIndexer;
12-
use Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync;
12+
use Magento\InventoryIndexer\Indexer\SourceItem\CompositeProductProcessorInterface;
1313

1414
/**
1515
* Reindex bundle product source items.
1616
*/
17-
class SourceItemIndexerPlugin
17+
class SourceItemIndexerProcessor implements CompositeProductProcessorInterface
1818
{
1919
/**
2020
* @var BundleProductsSourceItemIndexer
2121
*/
2222
private $bundleProductsSourceItemIndexer;
2323

24+
/**
25+
* Processor sort order
26+
*
27+
* @var int
28+
*/
29+
private $sortOrder;
30+
2431
/**
2532
* @param BundleProductsSourceItemIndexer $configurableProductsSourceItemIndexer
33+
* @param array $sortOrder
2634
*/
2735
public function __construct(
28-
BundleProductsSourceItemIndexer $configurableProductsSourceItemIndexer
36+
BundleProductsSourceItemIndexer $configurableProductsSourceItemIndexer,
37+
int $sortOrder = 5
2938
) {
3039
$this->bundleProductsSourceItemIndexer = $configurableProductsSourceItemIndexer;
40+
$this->sortOrder = $sortOrder;
3141
}
3242

3343
/**
3444
* Reindex source items list for bundle products.
3545
*
36-
* @param Sync $subject
37-
* @param callable $proceed
3846
* @param array $sourceItemIds
47+
* @param array $saleableStatusesBeforeSync
48+
* @param array $saleableStatusesAfterSync
49+
* @return void
3950
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
4051
* @throws StateException
4152
*/
42-
public function aroundExecuteList(
43-
Sync $subject,
44-
callable $proceed,
45-
array $sourceItemIds
46-
) {
47-
$proceed($sourceItemIds);
53+
public function process(
54+
array $sourceItemIds,
55+
array $saleableStatusesBeforeSync,
56+
array $saleableStatusesAfterSync
57+
): void {
4858
$this->bundleProductsSourceItemIndexer->executeList($sourceItemIds);
4959
}
60+
61+
/**
62+
* @inheritdoc
63+
*
64+
* @return int
65+
*/
66+
public function getSortOrder(): int
67+
{
68+
return $this->sortOrder;
69+
}
5070
}

InventoryBundleProductIndexer/etc/di.xml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
<plugin name="bundle_product_index_full" type="Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\Stock\Strategy\Sync\ReindexFullPlugin"/>
1111
<plugin name="bundle_product_index_list" type="Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\Stock\Strategy\Sync\ReindexListPlugin"/>
1212
</type>
13-
<type name="Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync">
14-
<plugin name="bundle_product_index" type="Magento\InventoryBundleProductIndexer\Plugin\InventoryIndexer\Indexer\SourceItem\Strategy\Sync\SourceItemIndexerPlugin" sortOrder="20"/>
15-
</type>
1613
<type name="Magento\InventoryBundleProductIndexer\Indexer\Stock\StockIndexer">
1714
<arguments>
1815
<argument name="indexHandler" xsi:type="object">Magento\InventoryIndexer\Indexer\IndexHandler</argument>
@@ -33,4 +30,16 @@
3330
<type name="Magento\Bundle\Api\ProductLinkManagementAddChildrenInterface">
3431
<plugin name="reindex_source_items_after_bulk_add_bundle_selection" type="Magento\InventoryBundleProductIndexer\Plugin\Bundle\Model\LinkManagement\ReindexSourceItemsAfterBulkAddBundleSelectionPlugin"/>
3532
</type>
33+
<type name="Magento\InventoryBundleProductIndexer\Indexer\SourceItemIndexerProcessor">
34+
<arguments>
35+
<argument name="sortOrder" xsi:type="number">5</argument>
36+
</arguments>
37+
</type>
38+
<type name="Magento\InventoryIndexer\Indexer\SourceItem\Strategy\Sync">
39+
<arguments>
40+
<argument name="saleabilityChangesProcessorsPool" xsi:type="array">
41+
<item name="sourceItemIndexerProcessor" xsi:type="object">Magento\InventoryBundleProductIndexer\Indexer\SourceItemIndexerProcessor</item>
42+
</argument>
43+
</arguments>
44+
</type>
3645
</config>

0 commit comments

Comments
 (0)