Add option to display money tokens without formatting
authorEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 17 Jan 2022 23:08:06 +0000 (12:08 +1300)
committerEileen McNaughton <emcnaughton@wikimedia.org>
Mon, 17 Jan 2022 23:09:01 +0000 (12:09 +1300)
In order to provide compatibility we have implemented our formatting with no
format being 'format as money' rather than 'no format'. This means
we still don't have a way to do no format....

obviously we can bikeshed whether 'raw' is an appropriate
format name for the raw value....

Civi/Token/TokenProcessor.php
tests/phpunit/CRM/Utils/TokenConsistencyTest.php

index 97e4bd3647542819f3d283d459f5ff791fb707b8..7ffdc64ffb1f949b8963acdb5936eee7d1636827 100644 (file)
@@ -470,8 +470,18 @@ class TokenProcessor {
       }
     }
 
-    if ($value instanceof Money && $filter === NULL) {
-      $filter = ['crmMoney'];
+    if ($value instanceof Money) {
+      switch ($filter[0] ?? NULL) {
+        case NULL:
+        case 'crmMoney':
+          return \Civi::format()->money($value->getAmount(), $value->getCurrency());
+
+        case 'raw':
+          return $value->getAmount();
+
+        default:
+          throw new \CRM_Core_Exception("Invalid token filter: $filter");
+      }
     }
 
     switch ($filter[0] ?? NULL) {
@@ -484,11 +494,6 @@ class TokenProcessor {
       case 'lower':
         return mb_strtolower($value);
 
-      case 'crmMoney':
-        if ($value instanceof Money) {
-          return \Civi::format()->money($value->getAmount(), $value->getCurrency());
-        }
-
       case 'crmDate':
         if ($value instanceof \DateTime) {
           // @todo cludgey.
index c72c6b8733b803e76637471565568ed4088e66c4..8e47a1ee25015338071850383ec57484ba1c5d93 100644 (file)
@@ -212,6 +212,26 @@ case.custom_1 :' . '
     $this->assertEquals($this->getExpectedContributionRecurTokenOutPut(), $tokenProcessor->getRow(0)->render('html'));
   }
 
+  /**
+   * Test that contribution recur tokens are consistently rendered.
+   */
+  public function testContributionRecurTokenRaw(): void {
+    $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
+      'controller' => __CLASS__,
+      'smarty' => FALSE,
+      'schema' => ['contribution_recurId'],
+    ]);
+    $tokenProcessor->addMessage('not_specified', '{contribution_recur.amount}', 'text/plain');
+    $tokenProcessor->addMessage('money', '{contribution_recur.amount|crmMoney}', 'text/plain');
+    $tokenProcessor->addMessage('raw', '{contribution_recur.amount|raw}', 'text/plain');
+    $tokenProcessor->addMessage('moneyNumber', '{contribution_recur.amount|crmMoneyNumber}', 'text/plain');
+    $tokenProcessor->addRow(['contribution_recurId' => $this->getContributionRecurID()]);
+    $tokenProcessor->evaluate();
+    $this->assertEquals('€5,990.99', $tokenProcessor->getRow(0)->render('not_specified'));
+    $this->assertEquals('€5,990.99', $tokenProcessor->getRow(0)->render('money'));
+    $this->assertEquals('5990.99', $tokenProcessor->getRow(0)->render('raw'));
+  }
+
   /**
    * Test money format tokens can respect passed in locale.
    */