Skip to content

Commit

Permalink
Merge pull request #25031 from MegaphoneJon/default-token-filter
Browse files Browse the repository at this point in the history
default token filter
  • Loading branch information
seamuslee001 authored Nov 22, 2022
2 parents 0d836fc + c58e19e commit bacbb7c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 8 additions & 0 deletions Civi/Token/TokenProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ private function filterTokenValue($value, ?array $filter, TokenRow $row) {
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));
}
Expand Down
6 changes: 4 additions & 2 deletions tests/phpunit/Civi/Token/TokenProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,9 +350,11 @@ public function testFilter(): void {
'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) {
Expand Down

0 comments on commit bacbb7c

Please sign in to comment.