Fix event token to support all address fields
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 25 Sep 2023 21:55:08 +0000 (10:55 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Thu, 28 Sep 2023 23:42:48 +0000 (12:42 +1300)
CRM/Event/Tokens.php
CRM/Utils/Address.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php
tests/phpunit/CiviTest/CiviUnitTestCase.php

index 6bc9db1aae88d36a3d9272f4e1b9abd96a235cb6..2a51923ba0bc196575871773d60d8e29ea132d17 100644 (file)
@@ -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']) : '';
index d311828a76a2807f90b361af86d10e9b6866d52f..24eb88946112d0c8321ca3545d00fde79b0044d2 100644 (file)
@@ -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
index 6d6bbeddeca711c11f0b96086b6a37ab4642648d..7daf6f8b6039dc1f16d091c6ca4350033832ba56 100644 (file)
@@ -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<br />
 up the road<br />
-Emerald City, Maine 90210<br />
+Emerald City, Maine 90210-1234<br />
+United States<br />
 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()
index 72ac8e794ec8ead12073abd5ffbbc94ad9c756a9..0f2d9a9af053391631a1f13993f7e5c6538377e3 100644 (file)
@@ -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.
    *