Skip to content

Commit d0c0707

Browse files
authored
Merge pull request #5612 from magento-tsg-csl3/2.3-develop-pr44
[TSG-CSL3] For 2.3 (pr44)
2 parents dc928a7 + b53081e commit d0c0707

File tree

11 files changed

+320
-84
lines changed

11 files changed

+320
-84
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']

app/code/Magento/Sales/Block/Adminhtml/Order/Create/Form/Account.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
88

9+
use Magento\Customer\Api\GroupManagementInterface;
910
use Magento\Framework\Api\ExtensibleDataObjectConverter;
11+
use Magento\Framework\App\ObjectManager;
1012
use Magento\Framework\Data\Form\Element\AbstractElement;
1113
use Magento\Framework\Pricing\PriceCurrencyInterface;
1214

@@ -39,6 +41,13 @@ class Account extends AbstractForm
3941
*/
4042
protected $_extensibleDataObjectConverter;
4143

44+
/**
45+
* Group Management
46+
*
47+
* @var GroupManagementInterface
48+
*/
49+
private $groupManagement;
50+
4251
/**
4352
* @param \Magento\Backend\Block\Template\Context $context
4453
* @param \Magento\Backend\Model\Session\Quote $sessionQuote
@@ -50,6 +59,7 @@ class Account extends AbstractForm
5059
* @param \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
5160
* @param ExtensibleDataObjectConverter $extensibleDataObjectConverter
5261
* @param array $data
62+
* @param GroupManagementInterface|null $groupManagement
5363
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
5464
*/
5565
public function __construct(
@@ -62,11 +72,13 @@ public function __construct(
6272
\Magento\Customer\Model\Metadata\FormFactory $metadataFormFactory,
6373
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
6474
\Magento\Framework\Api\ExtensibleDataObjectConverter $extensibleDataObjectConverter,
65-
array $data = []
75+
array $data = [],
76+
?GroupManagementInterface $groupManagement = null
6677
) {
6778
$this->_metadataFormFactory = $metadataFormFactory;
6879
$this->customerRepository = $customerRepository;
6980
$this->_extensibleDataObjectConverter = $extensibleDataObjectConverter;
81+
$this->groupManagement = $groupManagement ?: ObjectManager::getInstance()->get(GroupManagementInterface::class);
7082
parent::__construct(
7183
$context,
7284
$sessionQuote,
@@ -163,6 +175,7 @@ public function getFormValues()
163175
{
164176
try {
165177
$customer = $this->customerRepository->getById($this->getCustomerId());
178+
// phpcs:ignore Magento2.CodeAnalysis.EmptyBlock.DetectedCatch
166179
} catch (\Exception $e) {
167180
/** If customer does not exist do nothing. */
168181
}
@@ -179,6 +192,10 @@ public function getFormValues()
179192
}
180193
}
181194

195+
if (array_key_exists('group_id', $data) && empty($data['group_id'])) {
196+
$data['group_id'] = $this->groupManagement->getDefaultGroup($this->getQuote()->getStoreId())->getId();
197+
}
198+
182199
if ($this->getQuote()->getCustomerEmail()) {
183200
$data['email'] = $this->getQuote()->getCustomerEmail();
184201
}

app/code/Magento/Sales/Model/AdminOrder/Create.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1368,7 +1368,7 @@ protected function _setQuoteAddress(\Magento\Quote\Model\Quote\Address $address,
13681368
$data = isset($data['region']) && is_array($data['region']) ? array_merge($data, $data['region']) : $data;
13691369

13701370
$addressForm = $this->_metadataFormFactory->create(
1371-
1371+
13721372
AddressMetadataInterface::ENTITY_TYPE_ADDRESS,
13731373
'adminhtml_customer_address',
13741374
$data,
@@ -1644,6 +1644,7 @@ public function setAccountData($accountData)
16441644
$data,
16451645
\Magento\Customer\Api\Data\CustomerInterface::class
16461646
);
1647+
$customer->setStoreId($this->getQuote()->getStoreId());
16471648
$this->getQuote()->updateCustomerData($customer);
16481649
$data = [];
16491650

app/code/Magento/Sales/Test/Unit/Model/AdminOrder/CreateTest.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030
use Magento\Sales\Model\Order\Item as OrderItem;
3131
use Magento\Sales\Model\ResourceModel\Order\Item\Collection as ItemCollection;
3232
use Magento\Store\Api\Data\StoreInterface;
33-
use PHPUnit_Framework_MockObject_MockObject as MockObject;
33+
use PHPUnit\Framework\TestCase;
34+
use PHPUnit\Framework\MockObject\MockObject;
3435

3536
/**
3637
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
3738
* @SuppressWarnings(PHPMD.TooManyFields)
3839
*/
39-
class CreateTest extends \PHPUnit\Framework\TestCase
40+
class CreateTest extends TestCase
4041
{
4142
const CUSTOMER_ID = 1;
4243

@@ -46,12 +47,12 @@ class CreateTest extends \PHPUnit\Framework\TestCase
4647
private $adminOrderCreate;
4748

4849
/**
49-
* @var CartRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
50+
* @var CartRepositoryInterface|MockObject
5051
*/
5152
private $quoteRepository;
5253

5354
/**
54-
* @var QuoteFactory|\PHPUnit_Framework_MockObject_MockObject
55+
* @var QuoteFactory|MockObject
5556
*/
5657
private $quoteFactory;
5758

@@ -111,7 +112,7 @@ protected function setUp()
111112
->setMethods(['getForCustomer'])
112113
->getMockForAbstractClass();
113114

114-
$this->sessionQuote = $this->getMockBuilder(\Magento\Backend\Model\Session\Quote::class)
115+
$this->sessionQuote = $this->getMockBuilder(SessionQuote::class)
115116
->disableOriginalConstructor()
116117
->setMethods(
117118
[
@@ -227,6 +228,7 @@ public function testSetAccountData()
227228
'customer_tax_class_id' => $taxClassId
228229
]
229230
);
231+
$quote->method('getStoreId')->willReturn(1);
230232
$this->dataObjectHelper->method('populateWithArray')
231233
->with(
232234
$customer,
@@ -245,6 +247,10 @@ public function testSetAccountData()
245247
$this->groupRepository->method('getById')
246248
->willReturn($customerGroup);
247249

250+
$customer->expects($this->once())
251+
->method('setStoreId')
252+
->with(1);
253+
248254
$this->adminOrderCreate->setAccountData(['group_id' => 1]);
249255
}
250256

dev/tests/integration/testsuite/Magento/Customer/_files/customer_group_rollback.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
use Magento\Framework\Api\SearchCriteriaBuilder;
99
use Magento\Customer\Api\GroupRepositoryInterface;
10+
use Magento\Framework\Registry;
1011
use Magento\TestFramework\Helper\Bootstrap;
1112
use Magento\Framework\ObjectManagerInterface;
1213
use Magento\Framework\Exception\NoSuchEntityException;
@@ -21,10 +22,17 @@
2122
->create();
2223
$groups = $groupRepository->getList($searchCriteria)
2324
->getItems();
25+
26+
/** @var Registry $registry */
27+
$registry = $objectManager->get(Registry::class);
28+
$registry->unregister('isSecureArea');
29+
$registry->register('isSecureArea', true);
2430
foreach ($groups as $group) {
2531
try {
2632
$groupRepository->delete($group);
2733
} catch (NoSuchEntityException $exception) {
2834
//Group already removed
2935
}
3036
}
37+
$registry->unregister('isSecureArea');
38+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)