From: Matthew Wire <mjw@mjwconsult.co.uk>
Date: Fri, 28 Feb 2020 11:14:01 +0000 (+0000)
Subject: Add basic test for TokenCompatSubscriber
X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=30a231df2a84e8112c4f268d41bcf039347a030e;p=civicrm-core.git

Add basic test for TokenCompatSubscriber
---

diff --git a/tests/phpunit/CRM/Utils/TokenTest.php b/tests/phpunit/CRM/Utils/TokenTest.php
index 9366181288..88789a0ed2 100644
--- a/tests/phpunit/CRM/Utils/TokenTest.php
+++ b/tests/phpunit/CRM/Utils/TokenTest.php
@@ -147,4 +147,52 @@ 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();
+
+    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);
+    }
+  }
+
 }