Skip to content

Commit d9653bd

Browse files
committed
Updated Switzerland regions
1 parent 9728fe3 commit d9653bd

File tree

2 files changed

+190
-4
lines changed

2 files changed

+190
-4
lines changed
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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\Patch\Data;
9+
10+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
11+
use Magento\Framework\AppInterface;
12+
use Magento\Framework\DataObject;
13+
use Magento\Framework\Setup\ModuleDataSetupInterface;
14+
use Magento\Framework\Setup\Patch\DataPatchInterface;
15+
16+
/**
17+
* Update region names for Switzerland.
18+
*/
19+
class UpdateRegionNamesForSwitzerland implements DataPatchInterface
20+
{
21+
public const SWITZERLAND_COUNTRY_CODE = 'CH';
22+
/**
23+
* Array structure:
24+
* - key - region code (like the 'code' field in the 'directory_country_region' table)
25+
* - value - new default name for the region (like the 'default_name' field in the 'directory_country_region' table)
26+
* @var array
27+
*/
28+
public const SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE = [
29+
'FR' => 'Friburg',
30+
'GE' => 'Geneva',
31+
'LU' => 'Lucerne',
32+
'NE' => 'Neuchâtel',
33+
'TI' => 'Ticino',
34+
'VD' => 'Vaud',
35+
];
36+
37+
private const REGION_KEY_REGION_ID = 'region_id';
38+
private const REGION_KEY_COUNTRY_ID = 'country_id';
39+
private const REGION_KEY_CODE = 'code';
40+
private const REGION_KEY_DEFAULT_NAME = 'default_name';
41+
42+
private const REGION_NAME_REGION_ID = 'region_id';
43+
private const REGION_NAME_KEY_LOCALE = 'locale';
44+
private const REGION_NAME_KEY_NAME = 'name';
45+
46+
/**
47+
* @var ModuleDataSetupInterface
48+
*/
49+
private $moduleDataSetup;
50+
51+
/**
52+
* @var RegionCollectionFactory
53+
*/
54+
private $regionCollectionFactory;
55+
56+
/**
57+
* @param ModuleDataSetupInterface $moduleDataSetup
58+
* @param RegionCollectionFactory $regionCollectionFactory
59+
*/
60+
public function __construct(
61+
ModuleDataSetupInterface $moduleDataSetup,
62+
RegionCollectionFactory $regionCollectionFactory
63+
) {
64+
$this->moduleDataSetup = $moduleDataSetup;
65+
$this->regionCollectionFactory = $regionCollectionFactory;
66+
}
67+
68+
/**
69+
* @inheritdoc
70+
*/
71+
public function apply(): DataPatchInterface
72+
{
73+
$regionItems = $this->getRegionItemsToUpdate();
74+
if (!count($regionItems)) {
75+
return $this;
76+
}
77+
78+
$countryRegionDataToUpdate = [];
79+
$countryRegionNameDataToUpdate = [];
80+
$connection = $this->moduleDataSetup->getConnection();
81+
foreach ($regionItems as $regionItem) {
82+
$code = $regionItem->getData(self::REGION_KEY_CODE);
83+
$newRegionName = self::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE[$code] ?? null;
84+
if ($newRegionName === null) {
85+
continue;
86+
}
87+
// Collect data to insert into the 'directory_country_region' table
88+
$countryRegionDataToUpdate[] = [
89+
self::REGION_KEY_REGION_ID => $regionItem->getData(self::REGION_KEY_REGION_ID),
90+
self::REGION_KEY_COUNTRY_ID => $regionItem->getData(self::REGION_KEY_COUNTRY_ID),
91+
self::REGION_KEY_CODE => $code,
92+
self::REGION_KEY_DEFAULT_NAME => $newRegionName,
93+
];
94+
// Collect data to insert into the 'directory_country_region_name' table
95+
$countryRegionNameDataToUpdate[] = [
96+
self::REGION_NAME_KEY_LOCALE => AppInterface::DISTRO_LOCALE_CODE,
97+
self::REGION_NAME_REGION_ID => $regionItem->getData(self::REGION_KEY_REGION_ID),
98+
self::REGION_NAME_KEY_NAME => $newRegionName
99+
];
100+
}
101+
102+
// Update region tables with new region names
103+
$connection->insertOnDuplicate(
104+
$this->moduleDataSetup->getTable('directory_country_region'),
105+
$countryRegionDataToUpdate,
106+
[self::REGION_KEY_DEFAULT_NAME]
107+
);
108+
$connection->insertOnDuplicate(
109+
$this->moduleDataSetup->getTable('directory_country_region_name'),
110+
$countryRegionNameDataToUpdate,
111+
[self::REGION_NAME_KEY_NAME]
112+
);
113+
114+
return $this;
115+
}
116+
117+
/**
118+
* @inheritdoc
119+
*/
120+
public static function getDependencies(): array
121+
{
122+
return [
123+
InitializeDirectoryData::class,
124+
];
125+
}
126+
127+
/**
128+
* @inheritdoc
129+
*/
130+
public function getAliases(): array
131+
{
132+
return [];
133+
}
134+
135+
/**
136+
* Get region items filtered by 'CH' country and region codes (data to update).
137+
*
138+
* @return DataObject[]
139+
*/
140+
private function getRegionItemsToUpdate(): array
141+
{
142+
$regionCollection = $this->regionCollectionFactory->create();
143+
$regionCollection->addCountryFilter(self::SWITZERLAND_COUNTRY_CODE);
144+
$regionCollection->addRegionCodeFilter(array_keys(self::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE));
145+
146+
return $regionCollection->getItems();
147+
}
148+
}

dev/tests/integration/testsuite/Magento/Directory/Model/RegionTest.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,36 +7,52 @@
77

88
namespace Magento\Directory\Model;
99

10+
use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
11+
use Magento\Directory\Setup\Patch\Data\UpdateRegionNamesForSwitzerland as SwitzerlandRegionData;
12+
use Magento\Framework\AppInterface;
1013
use Magento\TestFramework\Helper\Bootstrap;
14+
use PHPUnit\Framework\ExpectationFailedException;
1115
use PHPUnit\Framework\TestCase;
16+
use SebastianBergmann\RecursionContext\InvalidArgumentException;
17+
use Magento\Framework\Exception\LocalizedException;
1218

1319
class RegionTest extends TestCase
1420
{
1521
/**
1622
* @var Country
1723
*/
18-
protected $country;
24+
private $country;
25+
26+
/**
27+
* @var RegionCollectionFactory
28+
*/
29+
private $regionCollectionFactory;
1930

2031
/**
2132
* @inheritDoc
2233
*/
2334
protected function setUp(): void
2435
{
2536
$this->country = Bootstrap::getObjectManager()->create(Country::class);
37+
$this->regionCollectionFactory = Bootstrap::getObjectManager()->create(RegionCollectionFactory::class);
2638
}
2739

2840
/**
2941
* Verify country has regions.
3042
*
31-
* @var string $countryId
43+
* @param string $countryId
3244
* @dataProvider getCountryIdDataProvider
45+
*
46+
* @throws ExpectationFailedException
47+
* @throws InvalidArgumentException
48+
* @throws LocalizedException
3349
*/
34-
public function testCountryHasRegions($countryId)
50+
public function testCountryHasRegions(string $countryId): void
3551
{
3652
$country = $this->country->loadByCode($countryId);
3753
$region = $country->getRegions()->getItems();
3854

39-
$this->assertTrue(!empty($region), 'Country ' . $countryId . ' not have regions');
55+
$this->assertNotEmpty($region, 'Country ' . $countryId . ' not have regions');
4056
}
4157

4258
/**
@@ -75,4 +91,26 @@ public function getCountryIdDataProvider():array
7591
['countryId' => 'AL']
7692
];
7793
}
94+
95+
/**
96+
* Verify updated Switzerland regions
97+
*
98+
* @throws ExpectationFailedException
99+
* @throws InvalidArgumentException
100+
*/
101+
public function testUpdatedSwitzerlandRegions(): void
102+
{
103+
$regionCollection = $this->regionCollectionFactory->create();
104+
$regionCollection->addCountryFilter(SwitzerlandRegionData::SWITZERLAND_COUNTRY_CODE);
105+
$regionCollection->addRegionCodeFilter(
106+
array_keys(SwitzerlandRegionData::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE)
107+
);
108+
$regionCollection->addBindParam(':region_locale', AppInterface::DISTRO_LOCALE_CODE);
109+
foreach ($regionCollection->getItems() as $regionItem) {
110+
$code = $regionItem->getData('code');
111+
$expectRegionName = SwitzerlandRegionData::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE[$code] ?? null;
112+
$this->assertEquals($expectRegionName, $regionItem->getData('default_name'));
113+
$this->assertEquals($expectRegionName, $regionItem->getData('name'));
114+
}
115+
}
78116
}

0 commit comments

Comments
 (0)