Skip to content

Commit 178fa2b

Browse files
🔃 [EngCom] Public Pull Requests - 2.2-develop
Accepted Public Pull Requests: - #14044: Make scope parameters of methods to save/delete config optional (by @avstudnitz) - #13024: resolved default country selection issue while creating new customer � (by @pradeep-wagento) Fixed GitHub Issues: - #3483: Default country selection issue while creating new customer from backend (reported by @keyurshah070) has been fixed in #13024 by @pradeep-wagento in 2.2-develop branch Related commits: 1. 2fb34ef 2. c0ef780 3. 8266e03
2 parents 9efa5e1 + 2d94c94 commit 178fa2b

File tree

6 files changed

+77
-7
lines changed

6 files changed

+77
-7
lines changed

app/code/Magento/Config/Model/ResourceModel/Config.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Config\Model\ResourceModel;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
810
/**
911
* Core Resource Resource Model
1012
*
@@ -34,7 +36,7 @@ protected function _construct()
3436
* @param int $scopeId
3537
* @return $this
3638
*/
37-
public function saveConfig($path, $value, $scope, $scopeId)
39+
public function saveConfig($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
3840
{
3941
$connection = $this->getConnection();
4042
$select = $connection->select()->from(
@@ -70,7 +72,7 @@ public function saveConfig($path, $value, $scope, $scopeId)
7072
* @param int $scopeId
7173
* @return $this
7274
*/
73-
public function deleteConfig($path, $scope, $scopeId)
75+
public function deleteConfig($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0)
7476
{
7577
$connection = $this->getConnection();
7678
$connection->delete(

app/code/Magento/Directory/Model/ResourceModel/Country/Collection.php

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
6868
* @since 100.1.0
6969
*/
7070
protected $countriesWithNotRequiredStates;
71+
/**
72+
* @var \Magento\Store\Model\StoreManagerInterface
73+
*/
74+
private $storeManager;
7175

7276
/**
7377
* Initialize dependencies.
@@ -85,6 +89,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab
8589
* @param array $countriesWithNotRequiredStates
8690
* @param mixed $connection
8791
* @param \Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource
92+
* @param \Magento\Store\Model\StoreManagerInterface|null $storeManager
8893
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
8994
*/
9095
public function __construct(
@@ -100,7 +105,8 @@ public function __construct(
100105
\Magento\Framework\App\Helper\AbstractHelper $helperData,
101106
array $countriesWithNotRequiredStates = [],
102107
\Magento\Framework\DB\Adapter\AdapterInterface $connection = null,
103-
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null
108+
\Magento\Framework\Model\ResourceModel\Db\AbstractDb $resource = null,
109+
\Magento\Store\Model\StoreManagerInterface $storeManager = null
104110
) {
105111
parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource);
106112
$this->_scopeConfig = $scopeConfig;
@@ -110,6 +116,9 @@ public function __construct(
110116
$this->_arrayUtils = $arrayUtils;
111117
$this->helperData = $helperData;
112118
$this->countriesWithNotRequiredStates = $countriesWithNotRequiredStates;
119+
$this->storeManager = $storeManager ?: ObjectManager::getInstance()->get(
120+
\Magento\Store\Model\StoreManagerInterface::class
121+
);
113122
}
114123

115124
/**
@@ -275,6 +284,7 @@ public function toOptionArray($emptyLabel = ' ')
275284
$sort = [$name => $foregroundCountry] + $sort;
276285
}
277286
$isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();
287+
278288
$options = [];
279289
foreach ($sort as $label => $value) {
280290
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
@@ -293,9 +303,36 @@ public function toOptionArray($emptyLabel = ' ')
293303
array_unshift($options, ['value' => '', 'label' => $emptyLabel]);
294304
}
295305

306+
$this->addDefaultCountryToOptions($options);
307+
296308
return $options;
297309
}
298310

311+
/**
312+
* Adds default country to options
313+
*
314+
* @param array $options
315+
* @return void
316+
*/
317+
private function addDefaultCountryToOptions(array &$options)
318+
{
319+
$defaultCountry = [];
320+
foreach ($this->storeManager->getWebsites() as $website) {
321+
$defaultCountryConfig = $this->_scopeConfig->getValue(
322+
\Magento\Directory\Helper\Data::XML_PATH_DEFAULT_COUNTRY,
323+
ScopeInterface::SCOPE_WEBSITES,
324+
$website
325+
);
326+
$defaultCountry[$defaultCountryConfig][] = $website->getId();
327+
}
328+
329+
foreach ($options as $key => $option) {
330+
if (isset($defaultCountry[$option['value']])) {
331+
$options[$key]['is_default'] = $defaultCountry[$option['value']];
332+
}
333+
}
334+
}
335+
299336
/**
300337
* Set foreground countries array
301338
*

app/code/Magento/Directory/Test/Unit/Model/ResourceModel/Country/CollectionTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
namespace Magento\Directory\Test\Unit\Model\ResourceModel\Country;
1010

11+
use Magento\Store\Api\Data\WebsiteInterface;
12+
1113
/**
1214
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1315
*/
@@ -23,6 +25,11 @@ class CollectionTest extends \PHPUnit\Framework\TestCase
2325
*/
2426
protected $scopeConfigMock;
2527

28+
/**
29+
* @var \Magento\Store\Model\StoreManagerInterface
30+
*/
31+
protected $storeManagerMock;
32+
2633
protected function setUp()
2734
{
2835
$connection = $this->createMock(\Magento\Framework\DB\Adapter\Pdo\Mysql::class);
@@ -53,6 +60,7 @@ protected function setUp()
5360
$logger = $this->createMock(\Psr\Log\LoggerInterface::class);
5461
$countryFactory = $this->createMock(\Magento\Directory\Model\ResourceModel\CountryFactory::class);
5562
$helperDataMock = $this->createMock(\Magento\Directory\Helper\Data::class);
63+
$this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
5664
$objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
5765
$arguments = [
5866
'logger' => $logger,
@@ -63,7 +71,8 @@ protected function setUp()
6371
'scopeConfig' => $this->scopeConfigMock,
6472
'countryFactory' => $countryFactory,
6573
'resource' => $resource,
66-
'helperData' => $helperDataMock
74+
'helperData' => $helperDataMock,
75+
'storeManager' => $this->storeManagerMock
6776
];
6877
$this->_model = $objectManager
6978
->getObject(\Magento\Directory\Model\ResourceModel\Country\Collection::class, $arguments);
@@ -78,6 +87,14 @@ protected function setUp()
7887
*/
7988
public function testToOptionArray($optionsArray, $emptyLabel, $foregroundCountries, $expectedResults)
8089
{
90+
$website1 = $this->createMock(WebsiteInterface::class);
91+
$website1->expects($this->atLeastOnce())
92+
->method('getId')
93+
->willReturn(1);
94+
$this->storeManagerMock->expects($this->once())
95+
->method('getWebsites')
96+
->willReturn([$website1]);
97+
8198
foreach ($optionsArray as $itemData) {
8299
$this->_model->addItem(new \Magento\Framework\DataObject($itemData));
83100
}

app/code/Magento/Ui/view/base/web/js/form/element/country.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ define([
2828
* @param {String} field
2929
*/
3030
filter: function (value, field) {
31-
var result;
31+
var result, defaultCountry, defaultValue;
3232

3333
if (!field) { //validate field, if we are on update
3434
field = this.filterBy.field;
@@ -46,6 +46,17 @@ define([
4646

4747
this.setOptions(result);
4848
this.reset();
49+
50+
if (!this.value()) {
51+
defaultCountry = _.filter(result, function (item) {
52+
return item['is_default'] && item['is_default'].includes(value);
53+
});
54+
55+
if (defaultCountry.length) {
56+
defaultValue = defaultCountry.shift();
57+
this.value(defaultValue.value);
58+
}
59+
}
4960
}
5061
});
5162
});

dev/tests/functional/tests/app/Magento/Customer/Test/TestCase/CreateCustomerBackendEntityTest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@
123123
<data name="customer/data/lastname" xsi:type="string">Doe%isolation%</data>
124124
<data name="customer/data/email" xsi:type="string">JohnDoe%isolation%@example.com</data>
125125
<data name="address/data/company" xsi:type="string">Magento</data>
126+
<data name="address/data/country_id" xsi:type="string"></data>
126127
<data name="expectedRequiredFields" xsi:type="array">
127128
<item name="2" xsi:type="string">Street Address</item>
128129
<item name="3" xsi:type="string">City</item>

lib/internal/Magento/Framework/App/Config/ConfigResource/ConfigInterface.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
*/
66
namespace Magento\Framework\App\Config\ConfigResource;
77

8+
use Magento\Framework\App\Config\ScopeConfigInterface;
9+
810
/**
911
* Resource for storing store configuration values
1012
*/
@@ -19,7 +21,7 @@ interface ConfigInterface
1921
* @param int $scopeId
2022
* @return $this
2123
*/
22-
public function saveConfig($path, $value, $scope, $scopeId);
24+
public function saveConfig($path, $value, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
2325

2426
/**
2527
* Delete config value from the storage resource
@@ -29,5 +31,5 @@ public function saveConfig($path, $value, $scope, $scopeId);
2931
* @param int $scopeId
3032
* @return $this
3133
*/
32-
public function deleteConfig($path, $scope, $scopeId);
34+
public function deleteConfig($path, $scope = ScopeConfigInterface::SCOPE_TYPE_DEFAULT, $scopeId = 0);
3335
}

0 commit comments

Comments
 (0)