Skip to content

Commit 5f26681

Browse files
author
larsroettig
committed
- Move Сarrier Сodes validation to service layer - Add New intigration Test
1 parent 3090cb4 commit 5f26681

File tree

4 files changed

+78
-23
lines changed

4 files changed

+78
-23
lines changed

app/code/Magento/Inventory/Controller/Adminhtml/Source/SourceCarrierDataProcessor.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ public function process(array $data)
4646
&& isset($data['carrier_codes'])
4747
&& is_array($data['carrier_codes'])
4848
) {
49-
$this->checkCarrierCodes($data['carrier_codes']);
5049
$data[SourceInterface::CARRIER_LINKS] = $this->getCarrierLinksData($data['carrier_codes']);
5150
} else {
5251
$data[SourceInterface::CARRIER_LINKS] = [];
@@ -55,21 +54,6 @@ public function process(array $data)
5554
return $data;
5655
}
5756

58-
/**
59-
* @param array $carrierCodes
60-
* @return void
61-
* @throws InputException
62-
*/
63-
private function checkCarrierCodes(array $carrierCodes)
64-
{
65-
$availableCarriers = $this->shippingConfig->getAllCarriers();
66-
67-
// TODO: move validation to service (management + tests)
68-
if (count(array_intersect_key(array_flip($carrierCodes), $availableCarriers)) !== count($carrierCodes)) {
69-
throw new InputException(__('Wrong carrier codes data'));
70-
}
71-
}
72-
7357
/**
7458
* @param array $carrierCodes
7559
* @return array

app/code/Magento/Inventory/Model/SourceCarrierLinkManagement.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,20 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\Inventory\Model;
78

89
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
910
use Magento\Framework\Api\SearchCriteriaBuilder;
1011
use Magento\Framework\App\ResourceConnection;
11-
use Magento\Inventory\Model\ResourceModel\SourceCarrierLink as SourceCarrierLinkResourceModel;
12+
use Magento\Framework\Validation\ValidationException;
1213
use Magento\Inventory\Model\ResourceModel\SourceCarrierLink;
14+
use Magento\Inventory\Model\ResourceModel\SourceCarrierLink as SourceCarrierLinkResourceModel;
1315
use Magento\Inventory\Model\ResourceModel\SourceCarrierLink\Collection;
1416
use Magento\Inventory\Model\ResourceModel\SourceCarrierLink\CollectionFactory;
1517
use Magento\InventoryApi\Api\Data\SourceCarrierLinkInterface;
1618
use Magento\InventoryApi\Api\Data\SourceInterface;
19+
use Magento\Shipping\Model\Config;
1720

1821
/**
1922
* @inheritdoc
@@ -45,25 +48,35 @@ class SourceCarrierLinkManagement implements SourceCarrierLinkManagementInterfac
4548
*/
4649
private $searchCriteriaBuilder;
4750

51+
/**
52+
* Shipping config
53+
*
54+
* @var Config
55+
*/
56+
private $shippingConfig;
57+
4858
/**
4959
* @param ResourceConnection $resourceConnection
5060
* @param SourceCarrierLinkResourceModel $sourceCarrierLinkResource
5161
* @param CollectionProcessorInterface $collectionProcessor
5262
* @param CollectionFactory $carrierLinkCollectionFactory
5363
* @param SearchCriteriaBuilder $searchCriteriaBuilder
64+
* @param Config $shippingConfig
5465
*/
5566
public function __construct(
5667
ResourceConnection $resourceConnection,
5768
SourceCarrierLinkResourceModel $sourceCarrierLinkResource,
5869
CollectionProcessorInterface $collectionProcessor,
5970
CollectionFactory $carrierLinkCollectionFactory,
60-
SearchCriteriaBuilder $searchCriteriaBuilder
71+
SearchCriteriaBuilder $searchCriteriaBuilder,
72+
Config $shippingConfig
6173
) {
6274
$this->resourceConnection = $resourceConnection;
6375
$this->sourceCarrierLinkResource = $sourceCarrierLinkResource;
6476
$this->collectionProcessor = $collectionProcessor;
6577
$this->carrierLinkCollectionFactory = $carrierLinkCollectionFactory;
6678
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
79+
$this->shippingConfig = $shippingConfig;
6780
}
6881

6982
/**
@@ -95,14 +108,24 @@ private function deleteCurrentCarrierLinks(SourceInterface $source)
95108
/**
96109
* @param SourceInterface $source
97110
* @return void
111+
* @throws ValidationException
98112
*/
99113
private function saveNewCarrierLinks(SourceInterface $source)
100114
{
101115
$carrierLinkData = [];
116+
117+
$availableCarriers = $this->shippingConfig->getAllCarriers();
118+
102119
foreach ($source->getCarrierLinks() as $carrierLink) {
120+
$carrierCode = $carrierLink->getCarrierCode();
121+
122+
if (array_key_exists($carrierCode, $availableCarriers) === false) {
123+
throw new ValidationException(__('CarrierCode %1 dos not exists', $carrierCode));
124+
}
125+
103126
$carrierLinkData[] = [
104127
'source_id' => $source->getSourceId(),
105-
SourceCarrierLinkInterface::CARRIER_CODE => $carrierLink->getCarrierCode(),
128+
SourceCarrierLinkInterface::CARRIER_CODE => $carrierCode,
106129
SourceCarrierLinkInterface::POSITION => $carrierLink->getPosition(),
107130
];
108131
}

app/code/Magento/InventoryApi/Test/Api/SourceRepository/CarrierLinkManagementTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,23 +66,23 @@ public function dataProviderCarrierLinks(): array
6666
SourceCarrierLinkInterface::POSITION => 200,
6767
],
6868
[
69-
SourceCarrierLinkInterface::CARRIER_CODE => 'new-link-1',
69+
SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
7070
SourceCarrierLinkInterface::POSITION => 300,
7171
],
7272
[
73-
SourceCarrierLinkInterface::CARRIER_CODE => 'new-link-2',
73+
SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
7474
SourceCarrierLinkInterface::POSITION => 400,
7575
],
7676
],
7777
],
7878
'replace_carrier_links' => [
7979
[
8080
[
81-
SourceCarrierLinkInterface::CARRIER_CODE => 'new-link-1',
81+
SourceCarrierLinkInterface::CARRIER_CODE => 'dhl',
8282
SourceCarrierLinkInterface::POSITION => 100,
8383
],
8484
[
85-
SourceCarrierLinkInterface::CARRIER_CODE => 'new-link-2',
85+
SourceCarrierLinkInterface::CARRIER_CODE => 'fedex',
8686
SourceCarrierLinkInterface::POSITION => 200,
8787
],
8888
],

app/code/Magento/InventoryApi/Test/Api/SourceRepository/CreateTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,54 @@ public function testCreate()
6868
AssertArrayContains::assert($expectedData, $this->getSourceDataById($sourceId));
6969
}
7070

71+
public function testCreateWithWrongCarrierLinks()
72+
{
73+
$expectedData = [
74+
SourceInterface::NAME => 'source-name-1',
75+
SourceInterface::CONTACT_NAME => 'source-contact-name',
76+
SourceInterface::EMAIL => 'source-email',
77+
SourceInterface::ENABLED => true,
78+
SourceInterface::DESCRIPTION => 'source-description',
79+
SourceInterface::LATITUDE => 11.123456,
80+
SourceInterface::LONGITUDE => 12.123456,
81+
SourceInterface::COUNTRY_ID => 'US',
82+
SourceInterface::REGION_ID => 10,
83+
SourceInterface::CITY => 'source-city',
84+
SourceInterface::STREET => 'source-street',
85+
SourceInterface::POSTCODE => 'source-postcode',
86+
SourceInterface::PHONE => 'source-phone',
87+
SourceInterface::FAX => 'source-fax',
88+
SourceInterface::PRIORITY => 200,
89+
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
90+
SourceInterface::CARRIER_LINKS => [
91+
[
92+
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_1',
93+
SourceCarrierLinkInterface::POSITION => 100,
94+
],
95+
[
96+
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_2',
97+
SourceCarrierLinkInterface::POSITION => 200,
98+
],
99+
],
100+
];
101+
$serviceInfo = [
102+
'rest' => [
103+
'resourcePath' => self::RESOURCE_PATH,
104+
'httpMethod' => Request::HTTP_METHOD_POST,
105+
],
106+
'soap' => [
107+
'service' => self::SERVICE_NAME,
108+
'operation' => self::SERVICE_NAME . 'Save',
109+
],
110+
];
111+
112+
try {
113+
$this->_webApiCall($serviceInfo, ['source' => $expectedData]);
114+
} catch (\Exception $e) {
115+
self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
116+
}
117+
}
118+
71119
protected function tearDown()
72120
{
73121
/** @var ResourceConnection $connection */

0 commit comments

Comments
 (0)