Merge pull request #16624 from mattwire/tokencompatsubscriber
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 2 Mar 2020 01:23:03 +0000 (14:23 +1300)
committerGitHub <noreply@github.com>
Mon, 2 Mar 2020 01:23:03 +0000 (14:23 +1300)
TokenProcessor - fix greetings tokens

Civi/Token/TokenCompatSubscriber.php
tests/phpunit/CRM/Utils/TokenTest.php

index 672080d29ba43d09c93390678803ea4f68597d42..d9b186e1f32131ea9fafe1cac8711bde94b71c3e 100644 (file)
@@ -120,12 +120,11 @@ class TokenCompatSubscriber implements EventSubscriberInterface {
     $e->string = \CRM_Utils_Token::replaceDomainTokens($e->string, $domain, $isHtml, $e->message['tokens'], $useSmarty);
 
     if (!empty($e->context['contact'])) {
+      \CRM_Utils_Token::replaceGreetingTokens($e->string, $e->context['contact'], $e->context['contact']['contact_id'], NULL, $useSmarty);
       $e->string = \CRM_Utils_Token::replaceContactTokens($e->string, $e->context['contact'], $isHtml, $e->message['tokens'], TRUE, $useSmarty);
 
       // FIXME: This may depend on $contact being merged with hook values.
       $e->string = \CRM_Utils_Token::replaceHookTokens($e->string, $e->context['contact'], $e->context['hookTokenCategories'], $isHtml, $useSmarty);
-
-      \CRM_Utils_Token::replaceGreetingTokens($e->string, $e->context['contact'], $e->context['contact']['contact_id'], NULL, $useSmarty);
     }
 
     if ($useSmarty) {
index 9366181288617e947adda21cd67db1ed2b6de001..450397298c5c7b9b49a42c29fdd43607cd161039 100644 (file)
@@ -147,4 +147,53 @@ class CRM_Utils_TokenTest extends CiviUnitTestCase {
     }
   }
 
+  /**
+   * This is a basic test of the token processor (currently testing TokenCompatSubscriber)
+   *   and makes sure that greeting + contact tokens are replaced.
+   * This is a good example to copy/expand when creating additional tests for token processor
+   *   in "real" situations.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testTokenProcessor() {
+    $params['contact_id'] = $this->individualCreate();
+
+    // Prepare the processor and general context.
+    $tokenProc = new \Civi\Token\TokenProcessor(\Civi::dispatcher(), [
+      // Unique(ish) identifier for our controller/use-case.
+      'controller' => 'civicrm_tokentest',
+
+      // Provide hints about what data will be available for each row.
+      // Ex: 'schema' => ['contactId', 'activityId', 'caseId'],
+      'schema' => ['contactId'],
+
+      // Whether to enable Smarty evaluation.
+      'smarty' => (defined('CIVICRM_MAIL_SMARTY') && CIVICRM_MAIL_SMARTY),
+    ]);
+
+    // Define message templates.
+    $tokenProc->addMessage('body_html', 'Good morning, <p>{contact.email_greeting} {contact.display_name}</p>. {custom.foobar} Bye!', 'text/html');
+    $tokenProc->addMessage('body_text', 'Good morning, {contact.email_greeting} {contact.display_name} Bye!', 'text/plain');
+
+    $expect[$params['contact_id']]['html'] = 'Good morning, <p>Dear Anthony Mr. Anthony Anderson II</p>.  Bye!';
+    $expect[$params['contact_id']]['text'] = 'Good morning, Dear Anthony Mr. Anthony Anderson II Bye!';
+
+    // Define row data.
+    foreach (explode(',', $params['contact_id']) as $contactId) {
+      $context = ['contactId' => $contactId];
+      $tokenProc->addRow()->context($context);
+    }
+
+    $tokenProc->evaluate();
+
+    $this->assertNotEmpty($tokenProc->getRows());
+    foreach ($tokenProc->getRows() as $tokenRow) {
+      /** @var \Civi\Token\TokenRow $tokenRow */
+      $html = $tokenRow->render('body_html');
+      $text = $tokenRow->render('body_text');
+      $this->assertEquals($expect[$params['contact_id']]['html'], $html);
+      $this->assertEquals($expect[$params['contact_id']]['text'], $text);
+    }
+  }
+
 }