Skip to content

Commit 1fab7e3

Browse files
author
Valeriy Nayda
authored
Merge pull request #142 from magento-engcom/move-carrier-codes-validation
Move Сarrier Сodes validation to service layer #131
2 parents af4a7d3 + b6ba017 commit 1fab7e3

File tree

6 files changed

+199
-131
lines changed

6 files changed

+199
-131
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/Source/Validator/CarrierLinks.php

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Inventory\Model\Source\Validator;
8+
9+
use Magento\Framework\Validation\ValidationResult;
10+
use Magento\Framework\Validation\ValidationResultFactory;
11+
use Magento\InventoryApi\Api\Data\SourceInterface;
12+
use Magento\Shipping\Model\Config;
13+
14+
/**
15+
* Check that carrier links is valid
16+
*/
17+
class CarrierLinksValidator implements SourceValidatorInterface
18+
{
19+
/**
20+
* @var ValidationResultFactory
21+
*/
22+
private $validationResultFactory;
23+
24+
/**
25+
* Shipping config
26+
*
27+
* @var Config
28+
*/
29+
private $shippingConfig;
30+
31+
/**
32+
* @param ValidationResultFactory $validationResultFactory
33+
* @param Config $shippingConfig
34+
*/
35+
public function __construct(ValidationResultFactory $validationResultFactory, Config $shippingConfig)
36+
{
37+
$this->validationResultFactory = $validationResultFactory;
38+
$this->shippingConfig = $shippingConfig;
39+
}
40+
41+
/**
42+
* @inheritdoc
43+
*/
44+
public function validate(SourceInterface $source): ValidationResult
45+
{
46+
$carrierLinks = $source->getCarrierLinks();
47+
$errors = [];
48+
49+
if (null === $carrierLinks) {
50+
return $this->buildValidationResult($errors);
51+
}
52+
53+
if (!is_array($carrierLinks)) {
54+
$errors[] = __('"%field" must be list of SourceCarrierLinkInterface.', [
55+
'field' => SourceInterface::CARRIER_LINKS
56+
]);
57+
return $this->buildValidationResult($errors);
58+
}
59+
60+
if (count($carrierLinks) && $source->isUseDefaultCarrierConfig()) {
61+
$errors[] = __('You can\'t configure "%field" because you have chosen Global Shipping configuration.', [
62+
'field' => SourceInterface::CARRIER_LINKS
63+
]);
64+
return $this->buildValidationResult($errors);
65+
}
66+
67+
$availableCarriers = $this->shippingConfig->getAllCarriers();
68+
foreach ($carrierLinks as $carrierLink) {
69+
$carrierCode = $carrierLink->getCarrierCode();
70+
if (array_key_exists($carrierCode, $availableCarriers) === false) {
71+
$errors[] = __('Carrier with code: "%carrier" don\'t exists.', [
72+
'carrier' => $carrierCode
73+
]);
74+
}
75+
}
76+
77+
return $this->buildValidationResult($errors);
78+
}
79+
80+
/**
81+
* Build the ValidationResult by given errors.
82+
*
83+
* @param array $errors
84+
* @return ValidationResult
85+
*/
86+
private function buildValidationResult(array $errors): ValidationResult
87+
{
88+
return $this->validationResultFactory->create(['errors' => $errors]);
89+
}
90+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<item name="name" xsi:type="object">Magento\Inventory\Model\Source\Validator\NameValidator</item>
2222
<item name="postcode" xsi:type="object">Magento\Inventory\Model\Source\Validator\PostcodeValidator</item>
2323
<item name="country" xsi:type="object">Magento\Inventory\Model\Source\Validator\CountryValidator</item>
24-
<item name="carrier_links" xsi:type="object">Magento\Inventory\Model\Source\Validator\CarrierLinks</item>
24+
<item name="carrier_links" xsi:type="object">Magento\Inventory\Model\Source\Validator\CarrierLinksValidator</item>
2525
</argument>
2626
</arguments>
2727
</type>

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

Lines changed: 106 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* Copyright © Magento, Inc. All rights reserved.
44
* See COPYING.txt for license details.
55
*/
6+
67
namespace Magento\InventoryApi\Test\Api\SourceRepository;
78

8-
use Magento\Framework\Webapi\Exception;
99
use Magento\Framework\Webapi\Rest\Request;
1010
use Magento\InventoryApi\Api\Data\SourceCarrierLinkInterface;
1111
use Magento\InventoryApi\Api\Data\SourceInterface;
@@ -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
],
@@ -93,62 +93,6 @@ public function dataProviderCarrierLinks(): array
9393
];
9494
}
9595

96-
/**
97-
* @magentoApiDataFixture ../../../../app/code/Magento/InventoryApi/Test/_files/source.php
98-
*/
99-
public function testAssignCarrierLinksIfUseGlobalConfigurationChosen()
100-
{
101-
$sourceId = 10;
102-
$expectedData = [
103-
SourceInterface::NAME => 'source-name-1',
104-
SourceInterface::POSTCODE => 'source-postcode',
105-
SourceInterface::COUNTRY_ID => 'US',
106-
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 1,
107-
SourceInterface::CARRIER_LINKS => [
108-
[
109-
SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
110-
SourceCarrierLinkInterface::POSITION => 100,
111-
],
112-
[
113-
SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
114-
SourceCarrierLinkInterface::POSITION => 200,
115-
],
116-
],
117-
];
118-
119-
$expectedErrorData = [
120-
'message' => 'Validation Failed',
121-
'errors' => [
122-
[
123-
'message' => 'You can\'t configure "%field" because you have chosen Global Shipping configuration.',
124-
'parameters' => [
125-
'field' => SourceInterface::CARRIER_LINKS,
126-
],
127-
],
128-
],
129-
];
130-
131-
try {
132-
$this->saveSource($sourceId, $expectedData);
133-
$this->fail('Expected throwing exception');
134-
} catch (\Exception $e) {
135-
if (TESTS_WEB_API_ADAPTER == self::ADAPTER_REST) {
136-
self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
137-
self::assertEquals(Exception::HTTP_BAD_REQUEST, $e->getCode());
138-
} elseif (TESTS_WEB_API_ADAPTER == self::ADAPTER_SOAP) {
139-
$this->assertInstanceOf('SoapFault', $e);
140-
// @see \Magento\TestFramework\TestCase\WebapiAbstract::getActualWrappedErrors()
141-
$expectedWrappedErrors = $expectedErrorData['errors'];
142-
$expectedWrappedErrors[0]['params'] = $expectedWrappedErrors[0]['parameters'];
143-
unset($expectedWrappedErrors[0]['parameters']);
144-
145-
$this->checkSoapFault($e, $expectedErrorData['message'], 'env:Sender', [], $expectedWrappedErrors);
146-
} else {
147-
throw $e;
148-
}
149-
}
150-
}
151-
15296
/**
15397
* @param int $sourceId
15498
* @param array $data
@@ -197,4 +141,105 @@ private function getSourceDataById(int $sourceId): array
197141
self::assertArrayHasKey(SourceInterface::SOURCE_ID, $response);
198142
return $response;
199143
}
144+
145+
/**
146+
* @param array $carrierData
147+
* @param array $expectedErrorData
148+
* @dataProvider failedValidationDataProvider
149+
*/
150+
public function testCarrierLinksValidation(array $carrierData, array $expectedErrorData)
151+
{
152+
$serviceInfo = [
153+
'rest' => [
154+
'resourcePath' => self::RESOURCE_PATH,
155+
'httpMethod' => Request::HTTP_METHOD_POST,
156+
],
157+
'soap' => [
158+
'service' => self::SERVICE_NAME,
159+
'operation' => self::SERVICE_NAME . 'Save',
160+
],
161+
];
162+
163+
try {
164+
$this->_webApiCall($serviceInfo, ['source' => $carrierData]);
165+
$this->fail('Expected throwing exception');
166+
} catch (\Exception $e) {
167+
self::assertEquals($expectedErrorData, $this->processRestExceptionResult($e));
168+
self::assertEquals(\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST, $e->getCode());
169+
}
170+
}
171+
172+
/**
173+
* @return array
174+
*/
175+
public function failedValidationDataProvider(): array
176+
{
177+
return [
178+
'use_global_configuration_chosen' => [
179+
[
180+
SourceInterface::NAME => 'source-name-1',
181+
SourceInterface::POSTCODE => 'source-postcode',
182+
SourceInterface::COUNTRY_ID => 'US',
183+
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 1,
184+
SourceInterface::CARRIER_LINKS => [
185+
[
186+
SourceCarrierLinkInterface::CARRIER_CODE => 'ups',
187+
SourceCarrierLinkInterface::POSITION => 100,
188+
],
189+
[
190+
SourceCarrierLinkInterface::CARRIER_CODE => 'usps',
191+
SourceCarrierLinkInterface::POSITION => 200,
192+
],
193+
],
194+
],
195+
[
196+
'message' => 'Validation Failed',
197+
'errors' => [
198+
[
199+
'message' =>
200+
'You can\'t configure "%field" because you have chosen Global Shipping configuration.',
201+
'parameters' => [
202+
'field' => SourceInterface::CARRIER_LINKS,
203+
],
204+
],
205+
],
206+
],
207+
],
208+
'carrier_codes_not_exits' => [
209+
[
210+
SourceInterface::NAME => 'source-name-1',
211+
SourceInterface::POSTCODE => 'source-postcode',
212+
SourceInterface::COUNTRY_ID => 'US',
213+
SourceInterface::USE_DEFAULT_CARRIER_CONFIG => 0,
214+
SourceInterface::CARRIER_LINKS => [
215+
[
216+
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_1',
217+
SourceCarrierLinkInterface::POSITION => 100,
218+
],
219+
[
220+
SourceCarrierLinkInterface::CARRIER_CODE => 'no_exists_2',
221+
SourceCarrierLinkInterface::POSITION => 200,
222+
],
223+
],
224+
],
225+
[
226+
'message' => 'Validation Failed',
227+
'errors' => [
228+
[
229+
'message' => 'Carrier with code: "%carrier" don\'t exists.',
230+
'parameters' => [
231+
'carrier' => 'no_exists_1'
232+
],
233+
],
234+
[
235+
'message' => 'Carrier with code: "%carrier" don\'t exists.',
236+
'parameters' => [
237+
'carrier' => 'no_exists_2'
238+
],
239+
],
240+
],
241+
],
242+
],
243+
];
244+
}
200245
}

0 commit comments

Comments
 (0)