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 TokenCompatSubscriber - Evaluate tokens during "civi.token.eval" phase #21449

Merged
merged 3 commits into from
Sep 16, 2021

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Sep 13, 2021

Overview

dev/core#2814 Fix tokenCompatSubscriber to conditionally evaluate legacy hook tokens

Before

legacy hook tokens evaluated in render

After

legacy hook tokens loaded in evaluate

Technical Details

As discussed the tokens should be loaded in evaluate and the token processor class should handle the rendering - but currently that is not how it is working. This adds a function to evaluate for legacy hooks that runs before the main contact evaluate function. It loads the contact with all 'returnProperties' if hooks are to be called - because that is what they expect....

But if we get as far as the main evaluate and the contact isn't loaded we just load the values we actually need for tokens

It is pretty hard finding the right size chunk for this - it would be sensible to ask why I didn't go that bit further and fix the contact tokens to follow the same pattern. OTOH this might be so big it just stalls. I did create #21450 in order to potentially address the 'too big' - if it seems to not go far enough then IMHO the solution is to see it as a process

Comments

Test cover is excellent on this code by now

@civibot
Copy link

civibot bot commented Sep 13, 2021

(Standard links)

@civibot civibot bot added the master label Sep 13, 2021
@eileenmcnaughton eileenmcnaughton force-pushed the compat branch 2 times, most recently from f1d6cc9 to fdb1fc9 Compare September 13, 2021 02:00
@eileenmcnaughton eileenmcnaughton changed the title dev/core#2814 Fix tokenCompatSubscriber to conditionally evaluate leg… dev/core#2814 Fix tokenCompatSubscriber to conditionally evaluate legacy hook tokens Sep 13, 2021
@eileenmcnaughton eileenmcnaughton force-pushed the compat branch 4 times, most recently from b25c254 to c350856 Compare September 13, 2021 20:21
@@ -268,24 +320,11 @@ public function onEvaluate(TokenValueEvent $e) {
/** @var int $contactId */
$contactId = $row->context['contactId'];
if (empty($row->context['contact'])) {
$contact = $this->getContact($contactId, $messageTokens['contact'] ?? [], TRUE);
$contact = $this->getContact($contactId, $messageTokens);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No longer loading 'all' here - just the 'needed' since 'all' is only needed if legacy tokens are in play & they do it

@@ -615,15 +615,15 @@ protected static function sendReminderEmail($tokenRow, $schedule, $toContactID):
$mailParams = [
'groupName' => 'Scheduled Reminder Sender',
'from' => self::pickFromEmail($schedule),
'toName' => $tokenRow->context['contact']['display_name'],
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code was taking advantage of the fact the token compat subscriber was loading the full contact - I have switched to a 'pseudotoken' approach to 'direct' it to

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's clever to tell it to fetch display_name. 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm glad you think it's clever ....

@eileenmcnaughton
Copy link
Contributor Author

@totten this is working now - with one issue - I'm not sure how to handle the fact some tokens return html. I think there is probably an easy answer but that is what the failing tests are highlighting

@@ -382,7 +384,7 @@ public function testContactTokens(): void {
*/
public function hookTokenValues(array &$details): void {
foreach ($details as $index => $detail) {
$details[$index]['favourite_emoticon'] = 'emo';
$details[$index]['important_stuff.favourite_emoticon'] = 'emo';
Copy link
Member

@totten totten Sep 14, 2021

Choose a reason for hiding this comment

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

@eileenmcnaughton Is this a contract change? On one hand, it smells sorta like it on the surface. OTOH, this part of the unit-test seems to be fairly recent, and there should be other places with additional coverage of the same hook. Is this a place where maybe the test was incomplete/passing-for-the-wrong-reason before?

Copy link
Contributor Author

@eileenmcnaughton eileenmcnaughton Sep 14, 2021

Choose a reason for hiding this comment

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

I think the test was wrong - I only just added it in the context of making this change but then when I made it pass & all the other token tests failed..... so I figured I had gotten it wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From docs

image

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah & also here

Copy link
Member

Choose a reason for hiding this comment

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

Cool. Yeah, I think that's right (the revision seems to match-up more with the devdocs and other tests).

@totten totten changed the title dev/core#2814 Fix tokenCompatSubscriber to conditionally evaluate legacy hook tokens dev/core#2814 TokenCompatSubscriber - Evaluate tokens during "civi.token.eval" phase Sep 14, 2021
@eileenmcnaughton
Copy link
Contributor Author

@totten looks like that got it passing!

@totten
Copy link
Member

totten commented Sep 16, 2021

Change looks good. This functionality has decent test coverage and passes. Merging.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants