diff --git a/config/sync/core.extension.yml b/config/sync/core.extension.yml index db1349c19f..3a27a631da 100644 --- a/config/sync/core.extension.yml +++ b/config/sync/core.extension.yml @@ -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 diff --git a/config/sync/field.field.node.press_release.field_address.yml b/config/sync/field.field.node.press_release.field_address.yml index 5db976f164..f35b3f1d58 100644 --- a/config/sync/field.field.node.press_release.field_address.yml +++ b/config/sync/field.field.node.press_release.field_address.yml @@ -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 @@ -17,7 +21,7 @@ required: false translatable: false default_value: - - langcode: '' + langcode: en country_code: US administrative_area: '' locality: '' @@ -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 diff --git a/docroot/modules/custom/va_gov_address/src/EventSubscriber/AddPhilippinesAsStateSubscriber.php b/docroot/modules/custom/va_gov_address/src/EventSubscriber/AddPhilippinesAsStateSubscriber.php new file mode 100644 index 0000000000..3c5be68d8e --- /dev/null +++ b/docroot/modules/custom/va_gov_address/src/EventSubscriber/AddPhilippinesAsStateSubscriber.php @@ -0,0 +1,49 @@ +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); + } + +} diff --git a/docroot/modules/custom/va_gov_address/va_gov_address.info.yml b/docroot/modules/custom/va_gov_address/va_gov_address.info.yml new file mode 100644 index 0000000000..3b32ecfc82 --- /dev/null +++ b/docroot/modules/custom/va_gov_address/va_gov_address.info.yml @@ -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' diff --git a/docroot/modules/custom/va_gov_address/va_gov_address.services.yml b/docroot/modules/custom/va_gov_address/va_gov_address.services.yml new file mode 100644 index 0000000000..6d61211083 --- /dev/null +++ b/docroot/modules/custom/va_gov_address/va_gov_address.services.yml @@ -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 } diff --git a/tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php b/tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php new file mode 100644 index 0000000000..e96797f47d --- /dev/null +++ b/tests/phpunit/va_gov_address/unit/EventSubscriber/AddPhilippinesAsStateSubscriberTest.php @@ -0,0 +1,102 @@ +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()); + } + +}