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

Add unit test cover for the MessageTemplate::renderMessageTemplate function #19551

Merged
merged 1 commit into from
Feb 7, 2021

Conversation

eileenmcnaughton
Copy link
Contributor

@eileenmcnaughton eileenmcnaughton commented Feb 6, 2021

Overview

Test provides full cover for the extracted renderMessageTemplate function covering the 4 methods of token replacement

  • domain tokens
  • contact tokens
  • smarty assigns within the function
  • smarty assigns before the function

In the process I identified a typo from the extraction which is tested & fixed in this.

The test supports the further cleanup of this function in #19550 but as it raises a couple of issues on that change it makes sense to merge this first

Before

Test cover incomplete

After

All domain tokens tested for all 3 strings
All contact tokens tested for all 3 strings, including 1 custom field
Assigning a smarty Variable before the function tested
Assigning a smarty variable within the function tested

Technical Details

Comments

@civibot
Copy link

civibot bot commented Feb 6, 2021

(Standard links)

@civibot civibot bot added the master label Feb 6, 2021
@eileenmcnaughton eileenmcnaughton force-pushed the msg_tpl_convert_test branch 3 times, most recently from 9957b25 to dba415b Compare February 6, 2021 03:47
@eileenmcnaughton eileenmcnaughton changed the title Add unit test cover for the MessageTemplate::renderMessageTemplate fu… Add unit test cover for the MessageTemplate::renderMessageTemplate function Feb 6, 2021
@@ -1234,7 +1234,7 @@ public static function getTokenDetails(
if (!empty($contactDetails[$contactID]['preferred_communication_method'])
) {
$communicationPreferences = [];
foreach ($contactDetails[$contactID]['preferred_communication_method'] as $val) {
foreach ((array) $contactDetails[$contactID]['preferred_communication_method'] as $val) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ensuring it is an array fixed a test fail & makes sense when iterating

* @throws \CiviCRM_API3_Exception
*/
public function getAllContactTokens(): array {
return [
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@totten this & the function below (for domains) provide a full set of token data for the relevant entity. You might be able to extract to a json sample but there are a couple of gotchas around the rows that reference related entities

You can see how each token presents in https://github.com/civicrm/civicrm-core/pull/19551/files#diff-c3c60600f080b49f2a9029b82865098c2a2742fbcedce0ecd731376917d80e1bR162

Note this PR should be merged & I'll rebase #19550 because I think we will need to discuss the slight differences #19550 highlights in the test run & this actually fixes an error in the extraction (& adds tests)

@mattwire
Copy link
Contributor

mattwire commented Feb 6, 2021

This looks good. However test fails relate: CRM_Mailing_MailingSystemTest.testText

I think the hardcoded token list is a good compromise for the test but note as tokenProcessor preloads and caches values we may need to revisit the list (as currently it's creating contacts etc. on the fly eg. for contact_id). Also be good to get a html style tag in here somewhere as that trips things up unless escaped properly (though we could add that as a follow-up).

@eileenmcnaughton
Copy link
Contributor Author

eileenmcnaughton commented Feb 6, 2021

@mattwire hopefully I have fixed the caching bug causing the test fail - perhaps once this is merged you could follow up with a patch of the issue you mentioned - before we consider making any changes to the actual function (ie merging #19950 or similar)

I'm not 100% sure what the tokenProcessor caching is that you mention - as long as it uses Civi::cache() or Civi::statics (probably more appropriate for everything except domain) it should be flushed in between tests?

@mattwire
Copy link
Contributor

mattwire commented Feb 6, 2021

Ok, not strictly a cache but a "preload". Looks like contact tokens may not be using it though - see https://github.com/civicrm/civicrm-core/blob/master/CRM/Activity/Tokens.php#L88 for activities.

@eileenmcnaughton
Copy link
Contributor Author

@mattwire OK - that will probably not impact how we add test cover I don't think

…nction

This provides cover for the 4 methods
- domain tokens
- contact tokens
- smarty assigns within the function
- smarty assigns before the function
@mattwire mattwire merged commit af3c97e into civicrm:master Feb 7, 2021
@colemanw colemanw deleted the msg_tpl_convert_test branch February 7, 2021 17:24
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