Skip to content
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

VACMS-19227: Adds Philippines as a US state for facility purposes #20068

Merged
merged 9 commits into from
Dec 19, 2024
1 change: 1 addition & 0 deletions config/sync/core.extension.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ module:
upgrade_status: 0
user: 0
user_history: 0
va_gov_address: 0
va_gov_api: 0
va_gov_backend: 0
va_gov_banner: 0
Expand Down
8 changes: 6 additions & 2 deletions config/sync/field.field.node.press_release.field_address.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ dependencies:
- node.type.press_release
module:
- address
- tmgmt_content
third_party_settings:
tmgmt_content:
excluded: false
id: node.press_release.field_address
field_name: field_address
entity_type: node
Expand All @@ -17,7 +21,7 @@ required: false
translatable: false
default_value:
-
langcode: ''
langcode: en
country_code: US
administrative_area: ''
locality: ''
Expand All @@ -26,7 +30,7 @@ default_value:
sorting_code: null
address_line1: null
address_line2: null
address_line3: ''
address_line3: null
organization: null
given_name: null
additional_name: null
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Drupal\va_gov_address\EventSubscriber;

use Drupal\address\Event\AddressEvents;
use Drupal\address\Event\SubdivisionsEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Adds a Philippines to the US States.
dsasser marked this conversation as resolved.
Show resolved Hide resolved
*
* 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 {

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AddressEvents::SUBDIVISIONS][] = ['onSubdivisions'];
return $events;
}

/**
* Provides the states of the US (plus the Philippines).
*
* @param \Drupal\address\Event\SubdivisionsEvent $event
* The subdivisions event.
*/
public function onSubdivisions(SubdivisionsEvent $event) {
dsasser marked this conversation as resolved.
Show resolved Hide resolved
if ($event->getParents() !== ['US']) {
return;
}

$definitions = $event->getDefinitions();

// Add the Philippines as a state.
$definitions['subdivisions']['PH'] = [
'code' => 'PH',
'name' => 'Philippines',
'country_code' => 'US',
'id' => 'PH',
];
ksort($definitions['subdivisions']);
$event->setDefinitions($definitions);
}

}
7 changes: 7 additions & 0 deletions docroot/modules/custom/va_gov_address/va_gov_address.info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name: 'VA.gov Address'
type: module
description: 'Custom code used for the Address field.'
core_version_requirement: ^9 || ^10
dependencies:
- address
package: 'Custom'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
va_gov_address.add_philippines_as_state_subscriber:
class: Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber
arguments: ['@address.subdivision_repository']
tags:
- { name: event_subscriber }
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace tests\phpunit\va_gov_address\unit\EventSubscriber;

use Drupal\address\Event\AddressEvents;
use Drupal\address\Event\SubdivisionsEvent;
use Drupal\va_gov_address\EventSubscriber\AddPhilippinesAsStateSubscriber;
use Drupal\Tests\UnitTestCase;
use Prophecy\Argument;

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

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

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

/**
* Tests the getSubscribedEvents method.
*
* @covers ::getSubscribedEvents
*/
public function testGetSubscribedEvents() {
$events = AddPhilippinesAsStateSubscriber::getSubscribedEvents();
$this->assertArrayHasKey(AddressEvents::SUBDIVISIONS, $events);
$this->assertIsArray($events[AddressEvents::SUBDIVISIONS]);
$this->assertEquals(['onSubdivisions'], array_column($events[AddressEvents::SUBDIVISIONS], 0));
}

/**
* Tests the onSubdivisions method.
*
* @covers ::onSubdivisions
*/
public function testOnSubdivisions() {
// Create a mock event.
/** @var \Prophecy\Prophecy\ObjectProphecy|\Drupal\address\Event\SubdivisionsEvent $event */
$event = $this->prophesize(SubdivisionsEvent::class);

// Configure the prophecy to return specific values for getDefinitions().
$definitions = [
'subdivisions' => [
'CA' => [
'code' => 'CA',
'name' => 'California',
'country_code' => 'US',
'id' => 'CA',
],
],
];
$event->getDefinitions()->willReturn($definitions);

// 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.
return isset($definitions['subdivisions']['PH'])
&& $definitions['subdivisions']['PH']['name'] === 'Philippines';
}))->shouldBeCalled();

// Call the onSubdivisions method.
$this->subscriber->onSubdivisions($event->reveal());
}

/**
* Tests that onSubdivisions does not modify definitions for non-US parents.
*
* @covers ::onSubdivisions
*/
public function testOnSubdivisionsNonUs() {
// Create a mock event.
/** @var \Prophecy\Prophecy\ObjectProphecy|\Drupal\address\Event\SubdivisionsEvent $event */
$event = $this->prophesize(SubdivisionsEvent::class);

// Mock the getParents method to return a non-US parent.
$event->getParents()->willReturn(['CA']);

// Assert that setDefinitions is never called.
$event->setDefinitions()->shouldNotBeCalled();

// Call the onSubdivisions method.
$this->subscriber->onSubdivisions($event->reveal());
}

}
Loading