Skip to content
This repository is currently being migrated. It's locked while the migration is in progress.
Open
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
Expand Up @@ -7,12 +7,12 @@
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Adds a Philippines to the US States.
* Adds select countries to the US States.
*
* This class follows the Centarro guidelines:
* https://docs.drupalcommerce.org/v2/developer-guide/customers/addresses/#how-do-i-add-or-modify-subdivisions-for-a-country.
*/
class AddPhilippinesAsStateSubscriber implements EventSubscriberInterface {
class AddCountriesAsStatesSubscriber implements EventSubscriberInterface {

/**
* {@inheritdoc}
Expand Down Expand Up @@ -42,7 +42,25 @@ public function onSubdivisions(SubdivisionsEvent $event) {
'country_code' => 'US',
'id' => 'PH',
];
ksort($definitions['subdivisions']);
// Add Germany as a state.
$definitions['subdivisions']['DEU'] = [
'code' => 'DEU',
'name' => 'Germany',
'country_code' => 'US',
'id' => 'DEU',
];
// Add South Korea as a state.
$definitions['subdivisions']['KR'] = [
'code' => 'KR',
'name' => 'South Korea',
'country_code' => 'US',
'id' => 'KR',
];

// Sort subdivisions by name.
uasort($definitions['subdivisions'], function ($a, $b) {
return strcmp($a['name'], $b['name']);
});
$event->setDefinitions($definitions);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
va_gov_address.add_philippines_as_state_subscriber:
class: Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber
va_gov_address.add_countries_as_states_subscriber:
class: Drupal\va_gov_address\EventSubscriber\AddCountriesAsStatesSubscriber
arguments: ['@address.subdivision_repository']
Comment thread
Becapa marked this conversation as resolved.
tags:
- { name: event_subscriber }
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
use Drupal\address\Event\AddressEvents;
use Drupal\address\Event\SubdivisionsEvent;
use Drupal\Tests\UnitTestCase;
use Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber;
use Drupal\va_gov_address\EventSubscriber\AddCountriesAsStatesSubscriber;
use Prophecy\Argument;

/**
* Tests custom US address group.
*
* @coversDefaultClass \Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber
* @coversDefaultClass \Drupal\va_gov_address\EventSubscriber\AddCountriesAsStatesSubscriber
*
* @group va_gov_address
*/
class AddPhilippinesAsStateSubscriberTest extends UnitTestCase {
class AddCountriesAsStatesSubscriberTest extends UnitTestCase {

/**
* The event subscriber under test.
*
* @var \Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber
* @var \Drupal\va_gov_address\EventSubscriber\AddCountriesAsStatesSubscriber
*/
protected AddPhilippinesAsStateSubscriber $subscriber;
protected AddCountriesAsStatesSubscriber $subscriber;

/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->subscriber = new AddPhilippinesAsStateSubscriber();
$this->subscriber = new AddCountriesAsStatesSubscriber();
}

/**
Expand All @@ -38,7 +38,7 @@ protected function setUp(): void {
* @covers ::getSubscribedEvents
*/
public function testGetSubscribedEvents() {
$events = AddPhilippinesAsStateSubscriber::getSubscribedEvents();
$events = AddCountriesAsStatesSubscriber::getSubscribedEvents();
$this->assertArrayHasKey(AddressEvents::SUBDIVISIONS, $events);
$this->assertIsArray($events[AddressEvents::SUBDIVISIONS]);
$this->assertEquals(['onSubdivisions'], array_column($events[AddressEvents::SUBDIVISIONS], 0));
Expand Down Expand Up @@ -70,9 +70,14 @@ public function testOnSubdivisions() {
// Set up expected calls for getParents() and setDefinitions().
$event->getParents()->willReturn(['US']);
$event->setDefinitions(Argument::that(function ($definitions) {
// Validate that Philippines (PH) is added as a subdivision.
// Validate that Philippines (PH), Germany (DEU), and South Korea (KR)
// are added as subdivisions.
return isset($definitions['subdivisions']['PH'])
&& $definitions['subdivisions']['PH']['name'] === 'Philippines';
&& isset($definitions['subdivisions']['DEU'])
&& isset($definitions['subdivisions']['KR'])
&& $definitions['subdivisions']['PH']['name'] === 'Philippines'
&& $definitions['subdivisions']['DEU']['name'] === 'Germany'
&& $definitions['subdivisions']['KR']['name'] === 'South Korea';
}))->shouldBeCalled();

// Call the onSubdivisions method.
Expand Down
Loading