Fix token subscriber to format the display of the custom tokens
authoreileen <emcnaughton@wikimedia.org>
Mon, 15 Mar 2021 05:55:33 +0000 (18:55 +1300)
committereileen <emcnaughton@wikimedia.org>
Tue, 23 Mar 2021 23:03:25 +0000 (12:03 +1300)
Civi/Token/TokenCompatSubscriber.php
tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php

index bdfaa5994eb58ec665b56cb51a8b3566527a760d..bf291635c620b972f32cb470d0b34ea5ff50dcc1 100644 (file)
@@ -47,6 +47,8 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $e->getTokenProcessor()->context['hookTokenCategories'] = $categories;
 
     $messageTokens = $e->getTokenProcessor()->getMessageTokens();
+    $returnProperties = array_fill_keys($messageTokens['contact'] ?? [], 1);
+    $returnProperties = array_merge(\CRM_Contact_BAO_Query::defaultReturnProperties(), $returnProperties);
 
     foreach ($e->getRows() as $row) {
       if (empty($row->context['contactId'])) {
@@ -58,12 +60,16 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
         $params = [
           ['contact_id', '=', $contactId, 0, 0],
         ];
-        [$contact] = \CRM_Contact_BAO_Query::apiQuery($params);
+        [$contact] = \CRM_Contact_BAO_Query::apiQuery($params, $returnProperties ?? NULL);
         //CRM-4524
         $contact = reset($contact);
+        // Test cover for greeting in CRM_Core_BAO_ActionScheduleTest::testMailer
+        $contact['email_greeting'] = $contact['email_greeting_display'] ?? '';
+        $contact['postal_greeting'] = $contact['postal_greeting_display'] ?? '';
+        $contact['addressee'] = $contact['address_display'] ?? '';
         if (!$contact || is_a($contact, 'CRM_Core_Error')) {
           // FIXME: Need to differentiate errors which kill the batch vs the individual row.
-          \Civi::log()->debug("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']);
+          \Civi::log()->debug('Failed to generate token data. Invalid contact ID: ' . $row->context['contactId']);
           continue;
         }
 
@@ -71,10 +77,7 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
         if (!empty($messageTokens['contact'])) {
           foreach ($messageTokens['contact'] as $token) {
             if (\CRM_Core_BAO_CustomField::getKeyID($token)) {
-              $contact[$token] = civicrm_api3('Contact', 'getvalue', [
-                'return' => $token,
-                'id' => $contactId,
-              ]);
+              $contact[$token] = \CRM_Core_BAO_CustomField::displayValue($contact[$token], \CRM_Core_BAO_CustomField::getKeyID($token));
             }
           }
         }
index 38d9c994503082004c3ac58d33b02f217f4784d8..34311a1485761ef02ee6dc834391a1543a988694 100644 (file)
@@ -182,10 +182,8 @@ Default Domain Name
   public function testContactTokensRenderedByTokenProcessor(): void {
     $this->createCustomGroupWithFieldsOfAllTypes([]);
     $tokenData = $this->getAllContactTokens();
-    // @todo - these 2 still need fixing/ syncing.
-    unset($tokenData['preferred_communication_method'], $tokenData['addressee']);
     $address = $this->setupContactFromTokeData($tokenData);
-    $tokenString ='';
+    $tokenString = '';
     foreach (array_keys($tokenData) as $key) {
       $tokenString .= "{$key}:{contact.{$key}}\n";
     }
@@ -198,8 +196,9 @@ Default Domain Name
       $rendered = (string) $row->render('html');
     }
     $expected = $this->getExpectedContactOutput($address['id'], $tokenData, $rendered);
-    // @todo - fix these 2 & stop stripping them out.
-    $expected = str_replace(["preferred_communication_method:\n", "addressee:Mr. Robert Frank Smith II\n"], '', $expected);
+    // @todo - this works better in token processor than in CRM_Core_Token.
+    // once synced we can fix $this->getExpectedContactOutput to return the right thing.
+    $expected = str_replace("preferred_communication_method:\n", "preferred_communication_method:Phone\n", $expected);
     $this->assertEquals($expected, $rendered);
   }
 
@@ -364,23 +363,25 @@ Default Domain Name
       'is_primary' => TRUE,
       'name' => $tokenData['im'],
       'provider_id' => $tokenData['im_provider'],
-      'contact_id' => $tokenData['contact_id']
+      'contact_id' => $tokenData['contact_id'],
     ]);
     $this->callAPISuccess('OpenID', 'create', array_merge($tokenData, [
       'is_primary' => TRUE,
       'contact_id' => $tokenData['contact_id'],
-      'openid' => $tokenData['openid']
+      'openid' => $tokenData['openid'],
     ]));
     return $address;
   }
 
   /**
-   * @param array|null $contact
-   * @param $id
+   * Get the expected rendered string.
+   *
+   * @param int $id
    * @param array $tokenData
-   * @param bool $checksum
+   * @param string $actualOutput
    *
    * @return string
+   * @throws \API_Exception
    */
   protected function getExpectedContactOutput($id, array $tokenData, string $actualOutput): string {
     $checksum = substr($actualOutput, (strpos($actualOutput, 'cs=') + 3), 47);