Skip to content

Commit 65e0ea4

Browse files
committed
#37796: Fixed issue with adding new country regions
1 parent 2932016 commit 65e0ea4

File tree

2 files changed

+149
-20
lines changed

2 files changed

+149
-20
lines changed

app/code/Magento/Directory/Setup/DataInstaller.php

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,46 +6,53 @@
66

77
namespace Magento\Directory\Setup;
88

9+
use Magento\Directory\Helper\Data;
910
use Magento\Framework\App\ResourceConnection;
1011
use Magento\Framework\DB\Adapter\AdapterInterface;
1112

1213
/**
13-
* Class DataInstaller
14-
* @package Magento\Directory\Setup
14+
* Add Required Regions for Country
1515
*/
1616
class DataInstaller
1717
{
18-
/**
19-
* @var \Magento\Directory\Helper\Data
20-
*/
21-
private $data;
22-
2318
/**
2419
* @var ResourceConnection
2520
*/
2621
private $resourceConnection;
2722

2823
/**
2924
* DatInstaller constructor.
30-
* @param \Magento\Directory\Helper\Data $data
25+
*
3126
* @param ResourceConnection $resourceConnection
3227
*/
3328
public function __construct(
34-
\Magento\Directory\Helper\Data $data,
3529
ResourceConnection $resourceConnection
3630
) {
37-
$this->data = $data;
3831
$this->resourceConnection = $resourceConnection;
3932
}
4033

4134
/**
4235
* Add country-region data.
4336
*
44-
* @param AdapterInterface $adapter
45-
* @param array $data
37+
* @param AdapterInterface $adapter
38+
* @param array $data
39+
* @return void
4640
*/
47-
public function addCountryRegions(AdapterInterface $adapter, array $data)
41+
public function addCountryRegions(AdapterInterface $adapter, array $data): void
4842
{
43+
$where = [
44+
$adapter->quoteInto('path = ?', Data::XML_PATH_STATES_REQUIRED),
45+
$adapter->quoteInto('scope = ?', 'default'),
46+
$adapter->quoteInto('scope_id = ?', 0),
47+
];
48+
49+
$select = $adapter->select()
50+
->from($this->resourceConnection->getTableName('core_config_data'), 'value')
51+
->where(implode(' AND ', $where));
52+
53+
$currRequiredStates = $adapter->fetchOne($select);
54+
$currRequiredStates = (!empty($currRequiredStates)) ? explode(',', $currRequiredStates) : [];
55+
4956
/**
5057
* Fill table directory/country_region
5158
* Fill table directory/country_region_name for en_US locale
@@ -56,21 +63,21 @@ public function addCountryRegions(AdapterInterface $adapter, array $data)
5663
$regionId = $adapter->lastInsertId($this->resourceConnection->getTableName('directory_country_region'));
5764
$bind = ['locale' => 'en_US', 'region_id' => $regionId, 'name' => $row[2]];
5865
$adapter->insert($this->resourceConnection->getTableName('directory_country_region_name'), $bind);
66+
67+
if (!in_array($row[0], $currRequiredStates)) {
68+
$currRequiredStates[] = $row[0];
69+
}
5970
}
71+
6072
/**
6173
* Upgrade core_config_data general/region/state_required field.
6274
*/
63-
$countries = $this->data->getCountryCollection()->getCountriesWithRequiredStates();
6475
$adapter->update(
6576
$this->resourceConnection->getTableName('core_config_data'),
6677
[
67-
'value' => implode(',', array_keys($countries))
78+
'value' => implode(',', $currRequiredStates)
6879
],
69-
[
70-
'scope="default"',
71-
'scope_id=0',
72-
'path=?' => \Magento\Directory\Helper\Data::XML_PATH_STATES_REQUIRED
73-
]
80+
$where
7481
);
7582
}
7683
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
3+
namespace integration\testsuite\Magento\Directory\Setup;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Magento\TestFramework\Helper\Bootstrap;
7+
use Magento\Directory\Setup\DataInstaller;
8+
use Magento\Framework\App\ResourceConnection;
9+
10+
/**
11+
* Provide test for DataInstaller
12+
*/
13+
class DataInstallerTest extends TestCase
14+
{
15+
/**
16+
* @var DataInstaller
17+
*/
18+
private $dataInstaller;
19+
20+
/**
21+
* @var ResourceConnection
22+
*/
23+
private $resourceConnection;
24+
25+
/**
26+
* @return void
27+
*/
28+
protected function setUp(): void
29+
{
30+
$objectManager = Bootstrap::getObjectManager();
31+
$this->dataInstaller = $objectManager->create(DataInstaller::class);
32+
$this->resourceConnection = $objectManager->create(ResourceConnection::class);
33+
}
34+
35+
/**
36+
* @return void
37+
*/
38+
public function testAddCountryRegions(): void
39+
{
40+
$adapter = $this->resourceConnection->getConnection();
41+
$expectedCountries = $this->getCountries(true);
42+
43+
$regionsBefore = $this->getTableRowsCount('directory_country_region');
44+
$regionsNamesBefore = $this->getTableRowsCount('directory_country_region_name');
45+
46+
$this->dataInstaller->addCountryRegions(
47+
$adapter,
48+
$this->getDataForRegions()
49+
);
50+
51+
$regionsAfter = $this->getTableRowsCount('directory_country_region');
52+
$regionsNamesAfter = $this->getTableRowsCount('directory_country_region_name');
53+
54+
$this->assertEquals(4, ($regionsAfter - $regionsBefore));
55+
$this->assertEquals(4, ($regionsNamesAfter - $regionsNamesBefore));
56+
$this->assertEquals($expectedCountries, $this->getCountries());
57+
}
58+
59+
/**
60+
* Count table rows
61+
*
62+
* @param string $tableName
63+
* @return int
64+
*/
65+
private function getTableRowsCount(string $tableName): int
66+
{
67+
$connection = $this->resourceConnection->getConnection();
68+
$select = $connection->select()->from(
69+
$this->resourceConnection->getTableName($tableName),
70+
['count(*)']
71+
);
72+
73+
return (int)$connection->fetchOne($select);
74+
}
75+
76+
/**
77+
* Return required countries with regions
78+
*
79+
* @param bool $isConfig
80+
* @return string
81+
*/
82+
private function getCountries(bool $isConfig = false): string
83+
{
84+
$connection = $this->resourceConnection->getConnection();
85+
$select = $connection->select()
86+
->from($connection->getTableName('core_config_data'), 'value')
87+
->where('path = ?', 'general/region/state_required')
88+
->where('scope = ?', 'default')
89+
->where('scope_id = ?', 0);
90+
91+
$countries = $connection->fetchOne($select);
92+
$countries = (!empty($countries)) ? explode(',', $countries) : [];
93+
94+
if (!$isConfig) {
95+
return implode(',', $countries);
96+
}
97+
98+
$countryCodes = ['JP', 'UA'];
99+
foreach ($countryCodes as $country) {
100+
if (!in_array($country, $countries)) {
101+
$countries[] = $country;
102+
}
103+
}
104+
105+
return implode(',', $countries);
106+
}
107+
108+
/**
109+
* Return test data for new regions
110+
*
111+
* @return array[]
112+
*/
113+
private function getDataForRegions(): array
114+
{
115+
return [
116+
['JP', 'JP-01', 'State1'],
117+
['JP', 'JP-02', 'State2'],
118+
['JP', 'JP-03', 'State3'],
119+
['UA', 'UA-410', 'State4'],
120+
];
121+
}
122+
}

0 commit comments

Comments
 (0)