Skip to content

Commit 2b4cfab

Browse files
MC-33168: Default State dropdown is not filled with correct data
1 parent d4a9098 commit 2b4cfab

File tree

2 files changed

+60
-19
lines changed

2 files changed

+60
-19
lines changed

app/code/Magento/Directory/Helper/Data.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66

77
namespace Magento\Directory\Helper;
88

9+
use Magento\Directory\Model\AllowedCountries;
910
use Magento\Directory\Model\Currency;
1011
use Magento\Directory\Model\CurrencyFactory;
1112
use Magento\Directory\Model\ResourceModel\Country\Collection;
1213
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory;
1314
use Magento\Framework\App\Cache\Type\Config;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
1416
use Magento\Framework\App\Helper\Context;
1517
use Magento\Framework\Json\Helper\Data as JsonData;
1618
use Magento\Store\Model\ScopeInterface;
@@ -21,6 +23,7 @@
2123
*
2224
* @api
2325
* @since 100.0.2
26+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
2427
*/
2528
class Data extends \Magento\Framework\App\Helper\AbstractHelper
2629
{
@@ -156,6 +159,7 @@ public function getRegionCollection()
156159
{
157160
if (!$this->_regionCollection) {
158161
$this->_regionCollection = $this->_regCollectionFactory->create();
162+
// phpstan:ignore
159163
$this->_regionCollection->addCountryFilter($this->getAddress()->getCountryId())->load();
160164
}
161165
return $this->_regionCollection;
@@ -185,7 +189,9 @@ public function getRegionJson()
185189
{
186190
\Magento\Framework\Profiler::start('TEST: ' . __METHOD__, ['group' => 'TEST', 'method' => __METHOD__]);
187191
if (!$this->_regionJson) {
188-
$cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $this->_storeManager->getStore()->getId();
192+
$scope = $this->getCurrentScope();
193+
$scopeKey = $scope['value'] ? '_' . implode('_', $scope) : null;
194+
$cacheKey = 'DIRECTORY_REGIONS_JSON_STORE' . $scopeKey;
189195
$json = $this->_configCacheType->load($cacheKey);
190196
if (empty($json)) {
191197
$regions = $this->getRegionData();
@@ -344,10 +350,13 @@ public function getDefaultCountry($store = null)
344350
*/
345351
public function getRegionData()
346352
{
347-
$countryIds = [];
348-
foreach ($this->getCountryCollection() as $country) {
349-
$countryIds[] = $country->getCountryId();
350-
}
353+
$scope = $this->getCurrentScope();
354+
$allowedCountries = $this->scopeConfig->getValue(
355+
AllowedCountries::ALLOWED_COUNTRIES_PATH,
356+
$scope['type'],
357+
$scope['value']
358+
);
359+
$countryIds = explode(',', $allowedCountries);
351360
$collection = $this->_regCollectionFactory->create();
352361
$collection->addCountryFilter($countryIds)->load();
353362
$regions = [
@@ -392,4 +401,31 @@ public function getWeightUnit()
392401
{
393402
return $this->scopeConfig->getValue(self::XML_PATH_WEIGHT_UNIT, ScopeInterface::SCOPE_STORE);
394403
}
404+
405+
/**
406+
* Get current scope from request
407+
*
408+
* @return array
409+
*/
410+
private function getCurrentScope(): array
411+
{
412+
$scope = [
413+
'type' => ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
414+
'value' => null,
415+
];
416+
$request = $this->_getRequest();
417+
if ($request->getParam(ScopeInterface::SCOPE_WEBSITE)) {
418+
$scope = [
419+
'type' => ScopeInterface::SCOPE_WEBSITE,
420+
'value' => $request->getParam(ScopeInterface::SCOPE_WEBSITE),
421+
];
422+
} elseif ($request->getParam(ScopeInterface::SCOPE_STORE)) {
423+
$scope = [
424+
'type' => ScopeInterface::SCOPE_STORE,
425+
'value' => $request->getParam(ScopeInterface::SCOPE_STORE),
426+
];
427+
}
428+
429+
return $scope;
430+
}
395431
}

app/code/Magento/Directory/Test/Unit/Helper/DataTest.php

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
namespace Magento\Directory\Test\Unit\Helper;
77

88
use Magento\Directory\Helper\Data;
9+
use Magento\Directory\Model\AllowedCountries;
10+
use Magento\Framework\App\Config\ScopeConfigInterface;
11+
use Magento\Framework\App\RequestInterface;
12+
use Magento\Store\Model\ScopeInterface;
913

1014
/**
1115
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
@@ -47,11 +51,13 @@ protected function setUp()
4751
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
4852
$this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
4953
$this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
54+
$requestMock = $this->createMock(RequestInterface::class);
5055
$context = $this->createMock(\Magento\Framework\App\Helper\Context::class);
56+
$context->method('getRequest')
57+
->willReturn($requestMock);
5158
$context->expects($this->any())
5259
->method('getScopeConfig')
5360
->willReturn($this->scopeConfigMock);
54-
5561
$configCacheType = $this->createMock(\Magento\Framework\App\Cache\Type\Config::class);
5662

5763
$this->_countryCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Country\Collection::class);
@@ -91,19 +97,18 @@ protected function setUp()
9197

9298
public function testGetRegionJson()
9399
{
94-
$countries = [
95-
new \Magento\Framework\DataObject(['country_id' => 'Country1']),
96-
new \Magento\Framework\DataObject(['country_id' => 'Country2'])
97-
];
98-
$countryIterator = new \ArrayIterator($countries);
99-
$this->_countryCollection->expects(
100-
$this->atLeastOnce()
101-
)->method(
102-
'getIterator'
103-
)->will(
104-
$this->returnValue($countryIterator)
105-
);
106-
100+
$this->scopeConfigMock->method('getValue')
101+
->willReturnMap(
102+
[
103+
[
104+
AllowedCountries::ALLOWED_COUNTRIES_PATH,
105+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
106+
null,
107+
'Country1,Country2'
108+
],
109+
[Data::XML_PATH_STATES_REQUIRED, ScopeInterface::SCOPE_STORE, null, '']
110+
]
111+
);
107112
$regions = [
108113
new \Magento\Framework\DataObject(
109114
['country_id' => 'Country1', 'region_id' => 'r1', 'code' => 'r1-code', 'name' => 'r1-name']

0 commit comments

Comments
 (0)