From: Eileen McNaughton Date: Mon, 25 Sep 2023 21:55:08 +0000 (+1300) Subject: Fix event token to support all address fields X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=c167fa8c8a67e078856bee6b791ed902527ed236;p=civicrm-core.git Fix event token to support all address fields --- diff --git a/CRM/Event/Tokens.php b/CRM/Event/Tokens.php index 6bc9db1aae..2a51923ba0 100644 --- a/CRM/Event/Tokens.php +++ b/CRM/Event/Tokens.php @@ -177,14 +177,9 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens { if (!Civi::cache('metadata')->has($cacheKey)) { $event = Event::get($this->checkPermissions)->addWhere('id', '=', $eventID) ->setSelect(array_merge([ - 'loc_block_id.address_id.name', - 'loc_block_id.address_id.street_address', - 'loc_block_id.address_id.supplemental_address_1', - 'loc_block_id.address_id.supplemental_address_2', - 'loc_block_id.address_id.supplemental_address_3', - 'loc_block_id.address_id.city', + 'loc_block_id.address_id.*', 'loc_block_id.address_id.state_province_id:label', - 'loc_block_id.address_id.postal_code', + 'loc_block_id.address_id.country_id:label', 'loc_block_id.email_id.email', 'loc_block_id.email_2_id.email', 'loc_block_id.phone_id.phone', @@ -209,16 +204,17 @@ class CRM_Event_Tokens extends CRM_Core_EntityTokens { 'custom.*', ], $this->getExposedFields())) ->execute()->first(); - $tokens['location']['text/plain'] = \CRM_Utils_Address::format([ + $addressValues = [ 'address_name' => $event['loc_block_id.address_id.name'], - 'street_address' => $event['loc_block_id.address_id.street_address'], - 'supplemental_address_1' => $event['loc_block_id.address_id.supplemental_address_1'], - 'supplemental_address_2' => $event['loc_block_id.address_id.supplemental_address_2'], - 'supplemental_address_3' => $event['loc_block_id.address_id.supplemental_address_3'], - 'city' => $event['loc_block_id.address_id.city'], 'state_province' => $event['loc_block_id.address_id.state_province_id:label'], - 'postal_code' => $event['loc_block_id.address_id.postal_code'], - ]); + 'country' => $event['loc_block_id.address_id.country_id:label'], + ]; + foreach ($event as $key => $value) { + if (strpos($key, 'loc_block_id.address_id.') === 0) { + $addressValues[str_replace('loc_block_id.address_id.', '', $key)] = $value; + } + } + $tokens['location']['text/plain'] = \CRM_Utils_Address::format($addressValues); $tokens['info_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/info', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE, TRUE); $tokens['registration_url']['text/html'] = \CRM_Utils_System::url('civicrm/event/register', 'reset=1&id=' . $eventID, TRUE, NULL, FALSE, TRUE); $tokens['start_date']['text/html'] = !empty($event['start_date']) ? new DateTime($event['start_date']) : ''; diff --git a/CRM/Utils/Address.php b/CRM/Utils/Address.php index d311828a76..24eb889461 100644 --- a/CRM/Utils/Address.php +++ b/CRM/Utils/Address.php @@ -55,7 +55,7 @@ class CRM_Utils_Address { $fullPostalCode = $fields['postal_code'] ?? NULL; if (!empty($fields['postal_code_suffix'])) { - $fullPostalCode .= "-$fields[postal_code_suffix]"; + $fullPostalCode .= '-' . $fields['postal_code_suffix']; } // make sure that some of the fields do have values diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index 6d6bbeddec..7daf6f8b60 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -52,7 +52,7 @@ class CRM_Utils_TokenConsistencyTest extends CiviUnitTestCase { // WORKAROUND: CRM_Event_Tokens copies `civicrm_event` data into metadata cache. That should probably change, but that's a different scope-of-work. // `clear()` works around it. This should be removed if that's updated, but it will be safe either way. Civi::cache('metadata')->clear(); - + $this->revertSetting('mailing_format'); parent::tearDown(); } @@ -716,7 +716,8 @@ event.loc_block_id.phone_id.phone :456 789 event.description :event description event.location :15 Walton St
up the road
-Emerald City, Maine 90210
+Emerald City, Maine 90210-1234
+United States
event.info_url :' . CRM_Utils_System::url('civicrm/event/info', NULL, TRUE) . '&reset=1&id=1 event.registration_url :' . CRM_Utils_System::url('civicrm/event/register', NULL, TRUE) . '&reset=1&id=1 event.pay_later_receipt :Please transfer funds to our bank account. @@ -1078,7 +1079,9 @@ United States', $tokenProcessor->getRow(0)->render('message')); 'city' => 'Emerald City', 'state_province_id:label' => 'Maine', 'postal_code' => 90210, + 'postal_code_suffix' => 1234, ])->execute()->first()['id']; + \Civi::settings()->set('mailing_format', '{contact.street_address}{ }{contact.supplemental_address_1}{ }{contact.supplemental_address_2}{ }{contact.city}{ }{contact.state_province}{ }{contact.postal_code}'); $phoneID = Phone::create() ->setValues(['phone' => '456 789']) ->execute() diff --git a/tests/phpunit/CiviTest/CiviUnitTestCase.php b/tests/phpunit/CiviTest/CiviUnitTestCase.php index 72ac8e794e..0f2d9a9af0 100644 --- a/tests/phpunit/CiviTest/CiviUnitTestCase.php +++ b/tests/phpunit/CiviTest/CiviUnitTestCase.php @@ -108,9 +108,14 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { protected $tempDirs; /** - * @var CRM_Core_Transaction|null + * @var CRM_Core_Transaction */ - private $tx = NULL; + private $tx; + + /** + * @var array + */ + protected $originalSettings = []; /** * Array of IDs created to support the test. @@ -333,6 +338,7 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { $this->renameLabels(); $this->ensureMySQLMode(['IGNORE_SPACE', 'ERROR_FOR_DIVISION_BY_ZERO', 'STRICT_TRANS_TABLES']); putenv('CIVICRM_SMARTY_DEFAULT_ESCAPE=1'); + $this->originalSettings = \Civi::settings()->all(); } /** @@ -497,6 +503,13 @@ class CiviUnitTestCase extends PHPUnit\Framework\TestCase { parent::tearDown(); } + /** + * @param string $setting + */ + protected function revertSetting(string $setting): void { + \Civi::settings()->set($setting, $this->originalSettings[$setting]); + } + /** * CHeck that all tests that have created payments have created them with the right financial entities. *