Skip to content

Updated Switzerland regions #33787

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Directory\Setup\Patch\Data;

use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
use Magento\Framework\AppInterface;
use Magento\Framework\DataObject;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
* Update region names for Switzerland.
*/
class UpdateRegionNamesForSwitzerland implements DataPatchInterface
{
public const SWITZERLAND_COUNTRY_CODE = 'CH';
/**
* Array structure:
* - key - region code (like the 'code' field in the 'directory_country_region' table)
* - value - new default name for the region (like the 'default_name' field in the 'directory_country_region' table)
* @var array
*/
public const SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE = [
'FR' => 'Friburg',
'GE' => 'Geneva',
'LU' => 'Lucerne',
'NE' => 'Neuchâtel',
'TI' => 'Ticino',
'VD' => 'Vaud',
];

private const REGION_KEY_REGION_ID = 'region_id';
private const REGION_KEY_COUNTRY_ID = 'country_id';
private const REGION_KEY_CODE = 'code';
private const REGION_KEY_DEFAULT_NAME = 'default_name';

private const REGION_NAME_REGION_ID = 'region_id';
private const REGION_NAME_KEY_LOCALE = 'locale';
private const REGION_NAME_KEY_NAME = 'name';

/**
* @var ModuleDataSetupInterface
*/
private $moduleDataSetup;

/**
* @var RegionCollectionFactory
*/
private $regionCollectionFactory;

/**
* @param ModuleDataSetupInterface $moduleDataSetup
* @param RegionCollectionFactory $regionCollectionFactory
*/
public function __construct(
ModuleDataSetupInterface $moduleDataSetup,
RegionCollectionFactory $regionCollectionFactory
) {
$this->moduleDataSetup = $moduleDataSetup;
$this->regionCollectionFactory = $regionCollectionFactory;
}

/**
* @inheritdoc
*/
public function apply(): DataPatchInterface
{
$regionItems = $this->getRegionItemsToUpdate();
if (!count($regionItems)) {
return $this;
}

$countryRegionDataToUpdate = [];
$countryRegionNameDataToUpdate = [];
$connection = $this->moduleDataSetup->getConnection();
foreach ($regionItems as $regionItem) {
$code = $regionItem->getData(self::REGION_KEY_CODE);
$newRegionName = self::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE[$code] ?? null;
if ($newRegionName === null) {
continue;
}
// Collect data to insert into the 'directory_country_region' table
$countryRegionDataToUpdate[] = [
self::REGION_KEY_REGION_ID => $regionItem->getData(self::REGION_KEY_REGION_ID),
self::REGION_KEY_COUNTRY_ID => $regionItem->getData(self::REGION_KEY_COUNTRY_ID),
self::REGION_KEY_CODE => $code,
self::REGION_KEY_DEFAULT_NAME => $newRegionName,
];
// Collect data to insert into the 'directory_country_region_name' table
$countryRegionNameDataToUpdate[] = [
self::REGION_NAME_KEY_LOCALE => AppInterface::DISTRO_LOCALE_CODE,
self::REGION_NAME_REGION_ID => $regionItem->getData(self::REGION_KEY_REGION_ID),
self::REGION_NAME_KEY_NAME => $newRegionName
];
}

// Update region tables with new region names
$connection->insertOnDuplicate(
$this->moduleDataSetup->getTable('directory_country_region'),
$countryRegionDataToUpdate,
[self::REGION_KEY_DEFAULT_NAME]
);
$connection->insertOnDuplicate(
$this->moduleDataSetup->getTable('directory_country_region_name'),
$countryRegionNameDataToUpdate,
[self::REGION_NAME_KEY_NAME]
);

return $this;
}

/**
* @inheritdoc
*/
public static function getDependencies(): array
{
return [
InitializeDirectoryData::class,
];
}

/**
* @inheritdoc
*/
public function getAliases(): array
{
return [];
}

/**
* Get region items filtered by 'CH' country and region codes (data to update).
*
* @return DataObject[]
*/
private function getRegionItemsToUpdate(): array
{
$regionCollection = $this->regionCollectionFactory->create();
$regionCollection->addCountryFilter(self::SWITZERLAND_COUNTRY_CODE);
$regionCollection->addRegionCodeFilter(array_keys(self::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE));

return $regionCollection->getItems();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,52 @@

namespace Magento\Directory\Model;

use Magento\Directory\Model\ResourceModel\Region\CollectionFactory as RegionCollectionFactory;
use Magento\Directory\Setup\Patch\Data\UpdateRegionNamesForSwitzerland as SwitzerlandRegionData;
use Magento\Framework\AppInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\ExpectationFailedException;
use PHPUnit\Framework\TestCase;
use SebastianBergmann\RecursionContext\InvalidArgumentException;
use Magento\Framework\Exception\LocalizedException;

class RegionTest extends TestCase
{
/**
* @var Country
*/
protected $country;
private $country;

/**
* @var RegionCollectionFactory
*/
private $regionCollectionFactory;

/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->country = Bootstrap::getObjectManager()->create(Country::class);
$this->regionCollectionFactory = Bootstrap::getObjectManager()->create(RegionCollectionFactory::class);
}

/**
* Verify country has regions.
*
* @var string $countryId
* @param string $countryId
* @dataProvider getCountryIdDataProvider
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
* @throws LocalizedException
*/
public function testCountryHasRegions($countryId)
public function testCountryHasRegions(string $countryId): void
{
$country = $this->country->loadByCode($countryId);
$region = $country->getRegions()->getItems();

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

/**
Expand Down Expand Up @@ -75,4 +91,26 @@ public function getCountryIdDataProvider():array
['countryId' => 'AL']
];
}

/**
* Verify updated Switzerland regions
*
* @throws ExpectationFailedException
* @throws InvalidArgumentException
*/
public function testUpdatedSwitzerlandRegions(): void
{
$regionCollection = $this->regionCollectionFactory->create();
$regionCollection->addCountryFilter(SwitzerlandRegionData::SWITZERLAND_COUNTRY_CODE);
$regionCollection->addRegionCodeFilter(
array_keys(SwitzerlandRegionData::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE)
);
$regionCollection->addBindParam(':region_locale', AppInterface::DISTRO_LOCALE_CODE);
foreach ($regionCollection->getItems() as $regionItem) {
$code = $regionItem->getData('code');
$expectRegionName = SwitzerlandRegionData::SWITZERLAND_COUNTRY_REGION_DATA_TO_UPDATE[$code] ?? null;
$this->assertEquals($expectRegionName, $regionItem->getData('default_name'));
$this->assertEquals($expectRegionName, $regionItem->getData('name'));
}
}
}