Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev/core#2814 fix tokenCompat to be consistent with unresolved tokens #21568

Merged
merged 4 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions Civi/Token/TokenCompatSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,17 +414,10 @@ protected function getFieldMetadata(): array {
*/
public function onRender(TokenRenderEvent $e): void {
$useSmarty = !empty($e->context['smarty']);
$remainingTokens = \CRM_Utils_Token::getTokens($e->string);

if ($remainingTokens) {
foreach ($remainingTokens as $part1 => $part2) {
$e->string = preg_replace(
'/(?<!\{|\\\\)\{' . $part1 . '\.([\w]+(:|\.)?\w*(\-[\w\s]+)?)\}(?!\})/',
'',
$e->string
);
}
}
$e->string = $e->getTokenProcessor()->visitTokens($e->string, function() {
// For historical consistency, we filter out unrecognized tokens.
return '';
});

if ($useSmarty) {
$smartyVars = [];
Expand Down
34 changes: 28 additions & 6 deletions Civi/Token/TokenProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,37 @@ public function render($name, $row) {
return $event->string;
}

private function visitTokens(string $expression, callable $callback): string {
/**
* Examine a token string and filter each token expression.
*
* @internal
* This function is only intended for use within civicrm-core. The name/location/callback-signature may change.
* @param string $expression
* Ex: 'Hello {foo.bar} and {whiz.bang|filter:"arg"}!'
* @param callable $callback
* A function which visits (and substitutes) each token.
* function(?string $fullToken, ?string $entity, ?string $field, ?array $modifier)
* @return string
*/
public function visitTokens(string $expression, callable $callback): string {
// Regex examples: '{foo.bar}', '{foo.bar|whiz}', '{foo.bar|whiz:"bang"}', '{foo.bar|whiz:"bang":"bang"}'
// Regex counter-examples: '{foobar}', '{foo bar}', '{$foo.bar}', '{$foo.bar|whiz}', '{foo.bar|whiz{bang}}'
// Key observations: Civi tokens MUST have a `.` and MUST NOT have a `$`. Civi filters MUST NOT have `{}`s or `$`s.
$tokRegex = '([\w]+)\.([\w:\.]+)'; /* EX: 'foo.bar' in '{foo.bar|whiz:"bang":"bang"}' */
$argRegex = ':[\w": %\-_()\[\]\+/#@!,\.\?]*'; /* EX: ':"bang":"bang"' in '{foo.bar|whiz:"bang":"bang"}' */
// Debatable: Maybe relax to this: $argRegex = ':[^{}\n]*'; /* EX: ':"bang":"bang"' in '{foo.bar|whiz:"bang":"bang"}' */
$filterRegex = "(\w+(?:$argRegex)?)"; /* EX: 'whiz:"bang"' in '{foo.bar|whiz:"bang"' */
return preg_replace_callback(";\{$tokRegex(?:\|$filterRegex)?\};", function($m) use ($callback) {

static $fullRegex = NULL;
if ($fullRegex === NULL) {
// The regex is a bit complicated, we so break it down into fragments.
// Consider the example '{foo.bar|whiz:"bang":"bang"}'. Each fragment matches the following:

$tokenRegex = '([\w]+)\.([\w:\.]+)'; /* MATCHES: 'foo.bar' */
$filterArgRegex = ':[\w": %\-_()\[\]\+/#@!,\.\?]*'; /* MATCHES: ':"bang":"bang"' */
// Key rule of filterArgRegex is to prohibit '{}'s because they may parse ambiguously. So you *might* relax it to:
// $filterArgRegex = ':[^{}\n]*'; /* MATCHES: ':"bang":"bang"' */
$filterNameRegex = "\w+"; /* MATCHES: 'whiz' */
$filterRegex = "\|($filterNameRegex(?:$filterArgRegex)?)"; /* MATCHES: '|whiz:"bang":"bang"' */
$fullRegex = ";\{$tokenRegex(?:$filterRegex)?\};";
}
return preg_replace_callback($fullRegex, function($m) use ($callback) {
$filterParts = NULL;
if (isset($m[3])) {
$filterParts = [];
Expand Down
19 changes: 11 additions & 8 deletions tests/phpunit/Civi/Token/TokenProcessorTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace Civi\Token;

use Civi\Test\Invasive;
use Civi\Token\Event\TokenRegisterEvent;
use Civi\Token\Event\TokenValueEvent;
use Symfony\Component\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -45,19 +44,23 @@ public function testVisitTokens() {
'{foo.bar}' => ['foo', 'bar', NULL],
'{foo.bar|whiz}' => ['foo', 'bar', ['whiz']],
'{foo.bar|whiz:"bang"}' => ['foo', 'bar', ['whiz', 'bang']],
'{love.shack|place:"bang":"b@ng, on +he/([do0r])?!"}' => ['love', 'shack', ['place', 'bang', 'b@ng, on +he/([do0r])?!']],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you didn't like this one?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I wouldn't want anyone to accuse of desecrating the art of the B-52s...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol

'{FoO.bAr|whiz:"bang"}' => ['FoO', 'bAr', ['whiz', 'bang']],
'{oo_f.ra_b|b_52:"bang":"b@ng, on +he/([do0r])?!"}' => ['oo_f', 'ra_b', ['b_52', 'bang', 'b@ng, on +he/([do0r])?!']],
'{foo.bar.whiz}' => ['foo', 'bar.whiz', NULL],
'{foo.bar.whiz|bang}' => ['foo', 'bar.whiz', ['bang']],
'{foo.bar:label}' => ['foo', 'bar:label', NULL],
'{foo.bar:label|truncate:"10"}' => ['foo', 'bar:label', ['truncate', '10']],
];
foreach ($examples as $input => $expected) {
array_unshift($expected, $input);
$log = [];
Invasive::call([$p, 'visitTokens'], [
$input,
function (?string $fullToken, ?string $entity, ?string $field, ?array $modifier) use (&$log) {
$log[] = [$fullToken, $entity, $field, $modifier];
},
]);
$filtered = $p->visitTokens($input, function (?string $fullToken, ?string $entity, ?string $field, ?array $modifier) use (&$log) {
$log[] = [$fullToken, $entity, $field, $modifier];
return 'Replaced!';
});
$this->assertEquals(1, count($log), "Should receive one callback on expression: $input");
$this->assertEquals($expected, $log[0]);
$this->assertEquals('Replaced!', $filtered);
}
}

Expand Down