-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
VACMS-19227: Adds Philippines as a US state for facility purposes (#2…
- Loading branch information
Showing
6 changed files
with
171 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...oot/modules/custom/va_gov_address/src/EventSubscriber/AddPhilippinesAsStateSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
* | ||
* 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) { | ||
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
7
docroot/modules/custom/va_gov_address/va_gov_address.info.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
6 changes: 6 additions & 0 deletions
6
docroot/modules/custom/va_gov_address/va_gov_address.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
102 changes: 102 additions & 0 deletions
102
tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |