Skip to content

Commit 0731944

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-99424' into 2.3-develop-pr22
2 parents 51643b4 + b4ed154 commit 0731944

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

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

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,6 @@ public function __construct(
126126
*/
127127
protected $_foregroundCountries = [];
128128

129-
/**
130-
* Add top destinition countries to head of option array
131-
*
132-
* @param string $emptyLabel
133-
* @param array $options
134-
* @return array
135-
*/
136-
private function addForegroundCountriesToOptionArray($emptyLabel, $options)
137-
{
138-
if ($emptyLabel !== false && count($this->_foregroundCountries) !== 0 &&
139-
count($options) === count($this->_foregroundCountries)
140-
) {
141-
$options[] = ['value' => '', 'label' => $emptyLabel];
142-
return $options;
143-
}
144-
return $options;
145-
}
146-
147129
/**
148130
* Define main table
149131
*
@@ -269,24 +251,20 @@ public function addCountryIdFilter($countryId)
269251
public function toOptionArray($emptyLabel = ' ')
270252
{
271253
$options = $this->_toOptionArray('country_id', 'name', ['title' => 'iso2_code']);
272-
$sort = [];
273-
foreach ($options as $data) {
274-
$name = (string)$this->_localeLists->getCountryTranslation($data['value']);
275-
if (!empty($name)) {
276-
$sort[$name] = $data['value'];
277-
}
278-
}
254+
$sort = $this->getSort($options);
255+
279256
$this->_arrayUtils->ksortMultibyte($sort, $this->_localeResolver->getLocale());
280257
foreach (array_reverse($this->_foregroundCountries) as $foregroundCountry) {
281258
$name = array_search($foregroundCountry, $sort);
282-
unset($sort[$name]);
283-
$sort = [$name => $foregroundCountry] + $sort;
259+
if ($name) {
260+
unset($sort[$name]);
261+
$sort = [$name => $foregroundCountry] + $sort;
262+
}
284263
}
285264
$isRegionVisible = (bool)$this->helperData->isShowNonRequiredState();
286265

287266
$options = [];
288267
foreach ($sort as $label => $value) {
289-
$options = $this->addForegroundCountriesToOptionArray($emptyLabel, $options);
290268
$option = ['value' => $value, 'label' => $label];
291269
if ($this->helperData->isRegionRequired($value)) {
292270
$option['is_region_required'] = true;
@@ -366,4 +344,23 @@ public function getCountriesWithRequiredStates()
366344
}
367345
return $countries;
368346
}
347+
348+
/**
349+
* Get sort
350+
*
351+
* @param array $options
352+
* @return array
353+
*/
354+
private function getSort(array $options): array
355+
{
356+
$sort = [];
357+
foreach ($options as $data) {
358+
$name = (string)$this->_localeLists->getCountryTranslation($data['value']);
359+
if (!empty($name)) {
360+
$sort[$name] = $data['value'];
361+
}
362+
}
363+
364+
return $sort;
365+
}
369366
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ public function testToOptionArray($optionsArray, $emptyLabel, $foregroundCountri
9999

100100
$this->_model->setForegroundCountries($foregroundCountries);
101101
$result = $this->_model->toOptionArray($emptyLabel);
102-
$this->assertCount(count($optionsArray) + (int)(!empty($emptyLabel) && !empty($foregroundCountries)) +
103-
(int)(!empty($emptyLabel)), $result);
102+
$this->assertCount(count($optionsArray) + (int)(!empty($emptyLabel)), $result);
104103
foreach ($expectedResults as $index => $expectedResult) {
105104
$this->assertEquals($expectedResult, $result[$index]['label']);
106105
}
@@ -121,8 +120,10 @@ public function toOptionArrayDataProvider()
121120
[$optionsArray, false, [], ['AD', 'US', 'ES', 'BZ']],
122121
[$optionsArray, false, 'US', ['US', 'AD', 'ES', 'BZ']],
123122
[$optionsArray, false, ['US', 'BZ'], ['US', 'BZ', 'AD', 'ES']],
124-
[$optionsArray, ' ', 'US', [' ', 'US', ' ', 'AD', 'ES', 'BZ']],
125-
[$optionsArray, ' ', [], [' ', 'AD', 'US', 'ES', 'BZ']]
123+
[$optionsArray, ' ', 'US', [' ', 'US', 'AD', 'ES', 'BZ']],
124+
[$optionsArray, ' ', [], [' ', 'AD', 'US', 'ES', 'BZ']],
125+
[$optionsArray, ' ', 'UA', [' ', 'AD', 'US', 'ES', 'BZ']],
126+
[$optionsArray, ' ', ['AF', 'UA'], [' ', 'AD', 'US', 'ES', 'BZ']],
126127
];
127128
}
128129
}

0 commit comments

Comments
 (0)