Skip to content

Commit 64fc283

Browse files
committed
Merge remote-tracking branch 'origin/MC-33168' into 2.4-develop-pr23
2 parents fd3945a + 759950a commit 64fc283

File tree

2 files changed

+100
-45
lines changed

2 files changed

+100
-45
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: 59 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,59 +6,79 @@
66
namespace Magento\Directory\Test\Unit\Helper;
77

88
use Magento\Directory\Helper\Data;
9+
use Magento\Directory\Model\AllowedCountries;
10+
use Magento\Directory\Model\CurrencyFactory;
11+
use Magento\Directory\Model\ResourceModel\Country\Collection as CountryCollection;
12+
use Magento\Directory\Model\ResourceModel\Region\Collection as RegionCollection;
13+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory;
14+
use Magento\Framework\App\Cache\Type\Config;
15+
use Magento\Framework\App\Config\ScopeConfigInterface;
16+
use Magento\Framework\App\Helper\Context;
17+
use Magento\Framework\App\RequestInterface;
18+
use Magento\Framework\DataObject;
19+
use Magento\Framework\Json\Helper\Data as JsonDataHelper;
20+
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
21+
use Magento\Store\Model\ScopeInterface;
22+
use Magento\Store\Model\Store;
23+
use Magento\Store\Model\StoreManagerInterface;
24+
use PHPUnit\Framework\Constraint\IsIdentical;
25+
use PHPUnit\Framework\MockObject\MockObject;
26+
use PHPUnit\Framework\TestCase;
927

1028
/**
1129
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1230
*/
13-
class DataTest extends \PHPUnit\Framework\TestCase
31+
class DataTest extends TestCase
1432
{
1533
/**
16-
* @var \Magento\Directory\Model\ResourceModel\Country\Collection|\PHPUnit_Framework_MockObject_MockObject
34+
* @var CountryCollection|MockObject
1735
*/
1836
protected $_countryCollection;
1937

2038
/**
21-
* @var \Magento\Directory\Model\ResourceModel\Region\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
39+
* @var CollectionFactory|MockObject
2240
*/
2341
protected $_regionCollection;
2442

2543
/**
26-
* @var \Magento\Framework\Json\Helper\Data|\PHPUnit_Framework_MockObject_MockObject
44+
* @var JsonDataHelper|MockObject
2745
*/
2846
protected $jsonHelperMock;
2947

3048
/**
31-
* @var \Magento\Store\Model\Store|\PHPUnit_Framework_MockObject_MockObject
49+
* @var Store|MockObject
3250
*/
3351
protected $_store;
3452

3553
/**
36-
* @var \Magento\Framework\App\Config\ScopeConfigInterface|\PHPUnit_Framework_MockObject_MockObject
54+
* @var ScopeConfigInterface|MockObject
3755
*/
3856
protected $scopeConfigMock;
3957

4058
/**
41-
* @var \Magento\Directory\Helper\Data
59+
* @var Data
4260
*/
4361
protected $_object;
4462

4563
protected function setUp()
4664
{
47-
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48-
$this->scopeConfigMock = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
65+
$objectManager = new ObjectManager($this);
66+
$this->scopeConfigMock = $this->createMock(ScopeConfigInterface::class);
4967
$this->scopeConfigMock->expects($this->any())->method('isSetFlag')->willReturn(false);
50-
$context = $this->createMock(\Magento\Framework\App\Helper\Context::class);
68+
$requestMock = $this->createMock(RequestInterface::class);
69+
$context = $this->createMock(Context::class);
70+
$context->method('getRequest')
71+
->willReturn($requestMock);
5172
$context->expects($this->any())
5273
->method('getScopeConfig')
5374
->willReturn($this->scopeConfigMock);
75+
$configCacheType = $this->createMock(Config::class);
5476

55-
$configCacheType = $this->createMock(\Magento\Framework\App\Cache\Type\Config::class);
77+
$this->_countryCollection = $this->createMock(CountryCollection::class);
5678

57-
$this->_countryCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Country\Collection::class);
58-
59-
$this->_regionCollection = $this->createMock(\Magento\Directory\Model\ResourceModel\Region\Collection::class);
79+
$this->_regionCollection = $this->createMock(RegionCollection::class);
6080
$regCollectionFactory = $this->createPartialMock(
61-
\Magento\Directory\Model\ResourceModel\Region\CollectionFactory::class,
81+
CollectionFactory::class,
6282
['create']
6383
);
6484
$regCollectionFactory->expects(
@@ -69,13 +89,13 @@ protected function setUp()
6989
$this->returnValue($this->_regionCollection)
7090
);
7191

72-
$this->jsonHelperMock = $this->createMock(\Magento\Framework\Json\Helper\Data::class);
92+
$this->jsonHelperMock = $this->createMock(JsonDataHelper::class);
7393

74-
$this->_store = $this->createMock(\Magento\Store\Model\Store::class);
75-
$storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
94+
$this->_store = $this->createMock(Store::class);
95+
$storeManager = $this->createMock(StoreManagerInterface::class);
7696
$storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->_store));
7797

78-
$currencyFactory = $this->createMock(\Magento\Directory\Model\CurrencyFactory::class);
98+
$currencyFactory = $this->createMock(CurrencyFactory::class);
7999

80100
$arguments = [
81101
'context' => $context,
@@ -86,32 +106,31 @@ protected function setUp()
86106
'storeManager' => $storeManager,
87107
'currencyFactory' => $currencyFactory,
88108
];
89-
$this->_object = $objectManager->getObject(\Magento\Directory\Helper\Data::class, $arguments);
109+
$this->_object = $objectManager->getObject(Data::class, $arguments);
90110
}
91111

92112
public function testGetRegionJson()
93113
{
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-
114+
$this->scopeConfigMock->method('getValue')
115+
->willReturnMap(
116+
[
117+
[
118+
AllowedCountries::ALLOWED_COUNTRIES_PATH,
119+
ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
120+
null,
121+
'Country1,Country2'
122+
],
123+
[Data::XML_PATH_STATES_REQUIRED, ScopeInterface::SCOPE_STORE, null, '']
124+
]
125+
);
107126
$regions = [
108-
new \Magento\Framework\DataObject(
127+
new DataObject(
109128
['country_id' => 'Country1', 'region_id' => 'r1', 'code' => 'r1-code', 'name' => 'r1-name']
110129
),
111-
new \Magento\Framework\DataObject(
130+
new DataObject(
112131
['country_id' => 'Country1', 'region_id' => 'r2', 'code' => 'r2-code', 'name' => 'r2-name']
113132
),
114-
new \Magento\Framework\DataObject(
133+
new DataObject(
115134
['country_id' => 'Country2', 'region_id' => 'r3', 'code' => 'r3-code', 'name' => 'r3-name']
116135
)
117136
];
@@ -148,7 +167,7 @@ public function testGetRegionJson()
148167
)->method(
149168
'jsonEncode'
150169
)->with(
151-
new \PHPUnit\Framework\Constraint\IsIdentical($expectedDataToEncode)
170+
new IsIdentical($expectedDataToEncode)
152171
)->will(
153172
$this->returnValue('encoded_json')
154173
);
@@ -220,7 +239,7 @@ public function testGetDefaultCountry()
220239
->method('getValue')
221240
->with(
222241
Data::XML_PATH_DEFAULT_COUNTRY,
223-
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
242+
ScopeInterface::SCOPE_STORE,
224243
$storeId
225244
)->will($this->returnValue($country));
226245

@@ -237,7 +256,7 @@ public function testGetCountryCollection()
237256
$this->returnValue(0)
238257
);
239258

240-
$store = $this->createMock(\Magento\Store\Model\Store::class);
259+
$store = $this->createMock(Store::class);
241260
$this->_countryCollection->expects(
242261
$this->once()
243262
)->method(
@@ -257,7 +276,7 @@ public function testGetCountryCollection()
257276
public function testGetTopCountryCodesReturnsParsedConfigurationValue($topCountriesValue, $expectedResult)
258277
{
259278
$this->scopeConfigMock->expects($this->once())
260-
->method('getValue')->with(\Magento\Directory\Helper\Data::XML_PATH_TOP_COUNTRIES)
279+
->method('getValue')->with(Data::XML_PATH_TOP_COUNTRIES)
261280
->willReturn($topCountriesValue);
262281

263282
$this->assertEquals($expectedResult, $this->_object->getTopCountryCodes());

0 commit comments

Comments
 (0)