default token filter
authorJon Goldberg <jon@megaphonetech.com>
Tue, 22 Nov 2022 21:29:26 +0000 (16:29 -0500)
committerJon Goldberg <jon@megaphonetech.com>
Tue, 22 Nov 2022 21:29:26 +0000 (16:29 -0500)
Civi/Token/TokenProcessor.php
tests/phpunit/Civi/Token/TokenProcessorTest.php

index 1ed89df5fc6016b3578505c5656d735d82bcea0d..a5434e138d19a5db8dd23fc8eb7dafce935b31d5 100644 (file)
@@ -508,6 +508,14 @@ class TokenProcessor {
           return $value;
         }
 
+      case 'default':
+        if (!$value) {
+          return $filter[1];
+        }
+        else {
+          return $value;
+        }
+
       default:
         throw new \CRM_Core_Exception('Invalid token filter: ' . json_encode($filter, JSON_UNESCAPED_SLASHES));
     }
index b606ca4c28b6db82437d7dd357d697720a1a591f..d3eff21d4c85e80989a26d9b453dbe1ea3ddce5e 100644 (file)
@@ -350,9 +350,11 @@ class TokenProcessorTest extends \CiviUnitTestCase {
       'This is {foo_bar.whiz_bang|upper}!' => 'This is SOME TEXT!',
       'This is {foo_bar.whiz_bang|boolean}!' => 'This is 1!',
       'This is {foo_bar.whiz_bop|boolean}!' => 'This is 0!',
+      'This is {foo_bar.whiz_bang|default:"bang"}.' => 'This is Some Text.',
+      'This is {foo_bar.whiz_bop|default:"bop"}.' => 'This is bop.',
     ];
-    // We expect 5 messages to be parsed 2 times each - ie 10 times.
-    $expectExampleCount = 10;
+    // We expect 7 messages to be parsed 2 times each - ie 14 times.
+    $expectExampleCount = 14;
     $actualExampleCount = 0;
 
     foreach ($exampleMessages as $inputMessage => $expectOutput) {