Skip to content

Commit 417042e

Browse files
committed
Merge remote-tracking branch '38076/fix-for-issue-37796' into bt_comm_pr_247beta3
2 parents 5905097 + 0cba48d commit 417042e

File tree

2 files changed

+154
-20
lines changed

2 files changed

+154
-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: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\Directory\Setup;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use Magento\TestFramework\Helper\Bootstrap;
12+
use Magento\Directory\Setup\DataInstaller;
13+
use Magento\Framework\App\ResourceConnection;
14+
15+
/**
16+
* Provide test for DataInstaller
17+
*/
18+
class DataInstallerTest extends TestCase
19+
{
20+
/**
21+
* @var DataInstaller
22+
*/
23+
private $dataInstaller;
24+
25+
/**
26+
* @var ResourceConnection
27+
*/
28+
private $resourceConnection;
29+
30+
/**
31+
* @inheritDoc
32+
*/
33+
protected function setUp(): void
34+
{
35+
$objectManager = Bootstrap::getObjectManager();
36+
$this->dataInstaller = $objectManager->create(DataInstaller::class);
37+
$this->resourceConnection = $objectManager->create(ResourceConnection::class);
38+
}
39+
40+
/**
41+
* @return void
42+
*/
43+
public function testAddCountryRegions(): void
44+
{
45+
$adapter = $this->resourceConnection->getConnection();
46+
$expectedCountries = $this->getCountries(true);
47+
48+
$regionsBefore = $this->getTableRowsCount('directory_country_region');
49+
$regionsNamesBefore = $this->getTableRowsCount('directory_country_region_name');
50+
51+
$this->dataInstaller->addCountryRegions(
52+
$adapter,
53+
$this->getDataForRegions()
54+
);
55+
56+
$regionsAfter = $this->getTableRowsCount('directory_country_region');
57+
$regionsNamesAfter = $this->getTableRowsCount('directory_country_region_name');
58+
59+
$this->assertEquals(4, ($regionsAfter - $regionsBefore));
60+
$this->assertEquals(4, ($regionsNamesAfter - $regionsNamesBefore));
61+
$this->assertEquals($expectedCountries, $this->getCountries());
62+
}
63+
64+
/**
65+
* Count table rows
66+
*
67+
* @param string $tableName
68+
* @return int
69+
*/
70+
private function getTableRowsCount(string $tableName): int
71+
{
72+
$connection = $this->resourceConnection->getConnection();
73+
$select = $connection->select()->from(
74+
$this->resourceConnection->getTableName($tableName),
75+
['count(*)']
76+
);
77+
78+
return (int)$connection->fetchOne($select);
79+
}
80+
81+
/**
82+
* Return required countries with regions
83+
*
84+
* @param bool $isConfig
85+
* @return string
86+
*/
87+
private function getCountries(bool $isConfig = false): string
88+
{
89+
$connection = $this->resourceConnection->getConnection();
90+
$select = $connection->select()
91+
->from($connection->getTableName('core_config_data'), 'value')
92+
->where('path = ?', 'general/region/state_required')
93+
->where('scope = ?', 'default')
94+
->where('scope_id = ?', 0);
95+
96+
$countries = $connection->fetchOne($select);
97+
$countries = (!empty($countries)) ? explode(',', $countries) : [];
98+
99+
if (!$isConfig) {
100+
return implode(',', $countries);
101+
}
102+
103+
$countryCodes = ['JP', 'UA'];
104+
foreach ($countryCodes as $country) {
105+
if (!in_array($country, $countries)) {
106+
$countries[] = $country;
107+
}
108+
}
109+
110+
return implode(',', $countries);
111+
}
112+
113+
/**
114+
* Return test data for new regions
115+
*
116+
* @return array[]
117+
*/
118+
private function getDataForRegions(): array
119+
{
120+
return [
121+
['JP', 'JP-01', 'State1'],
122+
['JP', 'JP-02', 'State2'],
123+
['JP', 'JP-03', 'State3'],
124+
['UA', 'UA-410', 'State4'],
125+
];
126+
}
127+
}

0 commit comments

Comments
 (0)