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

TokenProcessor - fix greetings tokens #16624

Merged
merged 3 commits into from
Mar 2, 2020

Conversation

mattwire
Copy link
Contributor

Overview

replaceGreetingsTokens must be called before replaceContactTokens because replaceContactTokens removes all 'invalid' tokens (including the {contact.email_greeting} etc. so they don't get replaced.

Found while working on emailapi extension and tokenprocessor.

Before

Greetings tokens not replaced.

After

Greetings tokens replaced.

Technical Details

TokenCompatSubscriber is a compatibility layer between contact tokens and the new token processor. So this change only affects code that uses the tokenProcessor (which is just scheduled reminders).

Also see CRM/Core/BAO/MessageTemplate.php where it is called in the same order as this PR.

Aside, I don't understand why we have separate functions for contact greetings and contact tokens - and why replaceContactTokens can't just call replaceGreetingTokens or similar (currently it's the other way around and replaceGreetingTokens calls replaceContactTokens internally! Ugh!

Comments

@aydun any chance of a quick review so we can get this into 5.24 with all the other tokenProcessor improvements that have already gone in. @eileenmcnaughton

…ause replaceContactTokens removes all 'invalid' tokens
@civibot
Copy link

civibot bot commented Feb 24, 2020

(Standard links)

@civibot civibot bot added the master label Feb 24, 2020
@eileenmcnaughton
Copy link
Contributor

@mattwire can we replicate in a test?

@aydun
Copy link
Contributor

aydun commented Feb 25, 2020

@mattwire This seems right.

Note also that the docblock for replaceGreetingTokens() says: "::replaceContactTokens() may need to be called after this method, to replace tokens supplied from this method." - which agrees with the reordering in this PR.

As you note, very little currently uses this.

@totten
Copy link
Member

totten commented Feb 25, 2020

I'm going to +1 the need for some test coverage to demonstrate the greeting token scenario.

  • These tokens are edge-casey/obscure.
  • The patch changes the mechanics of iterative substitution used in TokenCompatSubscriber. Iterative substitution is risky/hard to do well. If one is to maintain such a thing, you need test-coverage.
  • TokenCompatSubscriber was/is a stop-gap until someone can fully remove iterative substitution. If one aims to replace it (eventually), then you need test-coverage to validate the replacement.

@mattwire
Copy link
Contributor Author

@eileenmcnaughton @totten @aydun It appears in some cases greetings tokens are replaced when called after replaceContactTokens but in other cases (like my real-world testing) not. This test proves that it does work per this PR - but the test actually seems to work without the PR too!

Ok to merge?

@totten
Copy link
Member

totten commented Feb 28, 2020

@mattwire The test looks sensible to me.

Guess we've rolled a hard dice on figuring that edge-case, and I don't want to drag this out more. On the upside: even if the test doesn't reproduce the exact bug, it does still mitigate the risk of forgetting to fix greeting-tokens during a replacement of TokenCompatSubscriber (which, for me, seems more valuable than patching-up the current implementation).

I made a small edit - when doing assertions in a loop, it helps to have some assertion outside the loop which checks the size/quantity/number of iterations. (If getRows() returned [], then the loop wouldn't do any assertions...)

@totten totten added the merge ready PR will be merged after a few days if there are no objections label Feb 28, 2020
@eileenmcnaughton eileenmcnaughton merged commit 8171fba into civicrm:master Mar 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has-test master merge on pass merge ready PR will be merged after a few days if there are no objections
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants