Skip to content

Commit d4569ee

Browse files
committed
ACP2E-885: GraphqI call sets wrong value for region field
1 parent 0117201 commit d4569ee

File tree

4 files changed

+60
-39
lines changed

4 files changed

+60
-39
lines changed

app/code/Magento/Customer/Model/Address/AbstractAddress.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,11 @@ class AbstractAddress extends AbstractExtensibleModel implements AddressModelInt
120120
/** @var CompositeValidator */
121121
private $compositeValidator;
122122

123+
/**
124+
* @var array
125+
*/
126+
private array $regionIdCountry = [];
127+
123128
/**
124129
* @param \Magento\Framework\Model\Context $context
125130
* @param \Magento\Framework\Registry $registry
@@ -398,7 +403,13 @@ public function getRegionCode()
398403
$region = $this->getData('region');
399404

400405
if (!$regionId && is_numeric($region)) {
401-
if ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
406+
$regionId = $this->getRegionIdByCode(
407+
(string)$region,
408+
(string)$this->getCountryId()
409+
);
410+
if ($regionId) {
411+
$this->setData('region_code', $region);
412+
} elseif ($this->getRegionModel($region)->getCountryId() == $this->getCountryId()) {
402413
$this->setData('region_code', $this->getRegionModel($region)->getCode());
403414
}
404415
} elseif ($regionId) {
@@ -419,20 +430,53 @@ public function getRegionCode()
419430
public function getRegionId()
420431
{
421432
$regionId = $this->getData('region_id');
433+
if ($regionId) {
434+
return $regionId;
435+
}
436+
422437
$region = $this->getData('region');
423-
if (!$regionId) {
424-
if (is_numeric($region)) {
425-
$this->setData('region_id', $region);
438+
if (is_numeric($region)) {
439+
$regionId = $this->getRegionIdByCode(
440+
(string)$region,
441+
(string)$this->getCountryId()
442+
);
443+
if ($regionId) {
444+
$this->setData('region_id', $regionId);
426445
$this->unsRegion();
427446
} else {
428-
$regionModel = $this->_createRegionInstance()->loadByCode(
429-
$this->getRegionCode(),
430-
$this->getCountryId()
431-
);
432-
$this->setData('region_id', $regionModel->getId());
447+
$this->setData('region_id', $region);
433448
}
449+
} else {
450+
$regionId = $this->getRegionIdByCode(
451+
(string)$this->getRegionCode(),
452+
(string)$this->getCountryId()
453+
);
454+
$this->setData('region_id', $regionId);
434455
}
435-
return $this->getData('region_id');
456+
457+
return $regionId;
458+
}
459+
460+
/**
461+
* Returns region id.
462+
*
463+
* @param string $regionCode
464+
* @param string $countryId
465+
* @return int|null
466+
*/
467+
private function getRegionIdByCode(string $regionCode, string $countryId): ?int
468+
{
469+
$key = $countryId . '_' . $regionCode;
470+
if (!array_key_exists($key, $this->regionIdCountry)) {
471+
$regionModel = $this->_createRegionInstance()->loadByCode(
472+
$regionCode,
473+
$countryId
474+
);
475+
476+
$this->regionIdCountry[$key] = $regionModel->getId() ? (int)$regionModel->getId() : null;
477+
}
478+
479+
return $this->regionIdCountry[$key];
436480
}
437481

438482
/**

app/code/Magento/Customer/Test/Unit/Model/Address/AbstractAddressTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,17 +217,18 @@ protected function prepareGetRegionCode($countryId, $regionCode = 'UK')
217217
{
218218
$region = $this->getMockBuilder(Region::class)
219219
->addMethods(['getCountryId', 'getCode'])
220-
->onlyMethods(['__wakeup', 'load'])
220+
->onlyMethods(['__wakeup', 'load', 'loadByCode'])
221221
->disableOriginalConstructor()
222222
->getMock();
223+
$region->method('loadByCode')
224+
->willReturnSelf();
223225
$region->expects($this->once())
224226
->method('getCode')
225227
->willReturn($regionCode);
226228
$region->expects($this->once())
227229
->method('getCountryId')
228230
->willReturn($countryId);
229-
$this->regionFactoryMock->expects($this->once())
230-
->method('create')
231+
$this->regionFactoryMock->method('create')
231232
->willReturn($region);
232233
}
233234

app/code/Magento/QuoteGraphQl/Model/Cart/QuoteAddressFactory.php

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ public function createBasedOnInputData(array $addressInput): QuoteAddress
100100
throw new GraphQlInputException(__('Country is not available'));
101101
}
102102

103-
if (!empty($addressInput['region'])) {
104-
$this->normalizeRegion($addressInput);
105-
}
106-
107103
$this->validateRegion($addressInput);
108104

109105
$maxAllowedLineCount = $this->addressHelper->getStreetLines();
@@ -148,26 +144,6 @@ private function validateRegion(array $addressInput): void
148144
}
149145
}
150146

151-
/**
152-
* Normalize region code to region id while requesting to setShippingAddress
153-
*
154-
* @param array $addressInput
155-
*/
156-
private function normalizeRegion(array &$addressInput)
157-
{
158-
$shippingAddressRegion = $addressInput['region'];
159-
if (is_numeric($shippingAddressRegion)) {
160-
$regionCollection = $this->regionCollectionFactory
161-
->create()
162-
->addCountryFilter($addressInput['country_code']);
163-
$allRegions = $regionCollection->toOptionArray();
164-
$regionId = (int) $addressInput['region'];
165-
if (array_key_exists($regionId, $allRegions)) {
166-
$addressInput['region'] = $allRegions[$regionId]['value'];
167-
}
168-
}
169-
}
170-
171147
/**
172148
* Validate the address region when region is required for the country
173149
*

dev/tests/api-functional/testsuite/Magento/GraphQl/Quote/Customer/SetShippingAddressOnCartTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function testSetShippingAddressOnCartWithRegionIdForSimpleProduct()
162162
company: "Magento"
163163
street: ["Via Solferino", "45"]
164164
city: "Ceriano Laghetto"
165-
region: "1"
165+
region: "58"
166166
postcode: "20816"
167167
country_code: "FR"
168168
telephone: "3273581975",
@@ -1898,7 +1898,7 @@ private function assertShippingAddressWithRegionIdFields(array $shippingAddressR
18981898
['response_field' => 'city', 'expected_value' => 'Ceriano Laghetto'],
18991899
['response_field' => 'postcode', 'expected_value' => '20816'],
19001900
['response_field' => 'telephone', 'expected_value' => '3273581975'],
1901-
['response_field' => 'region', 'expected_value' => ['code' => '1', 'label' => 'Ain']],
1901+
['response_field' => 'region', 'expected_value' => ['code' => '58', 'label' => 'Nièvre']],
19021902
['response_field' => 'country', 'expected_value' => ['code' => 'FR', 'label' => 'FR']]
19031903
];
19041904

0 commit comments

Comments
 (0)