Skip to content

Commit

Permalink
Merge pull request #19598 from eileenmcnaughton/msg_tpl_less
Browse files Browse the repository at this point in the history
Convert Smarty & domain token processing to use token processor
  • Loading branch information
mattwire authored Feb 18, 2021
2 parents 47fa1f0 + 82e5b7a commit b5145c1
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 39 deletions.
46 changes: 23 additions & 23 deletions CRM/Core/BAO/MessageTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Token\TokenProcessor;

/**
*
* @package CRM
Expand Down Expand Up @@ -425,7 +427,7 @@ public static function sendTemplate($params) {
$mailContent['subject'] = $params['subject'];
}

$mailContent = self::renderMessageTemplate($mailContent, $params['disableSmarty'], $params['contactId'] ?? NULL, $params['tplParams']);
$mailContent = self::renderMessageTemplate($mailContent, (bool) $params['disableSmarty'], $params['contactId'] ?? NULL, $params['tplParams']);

// send the template, honouring the target user’s preferences (if any)
$sent = FALSE;
Expand Down Expand Up @@ -692,35 +694,33 @@ protected static function parseThroughSmarty(array $mailContent, $tplParams): ar
*
* @param array $mailContent
* @param bool $disableSmarty
* @param int $contactID
* @param int|NULL $contactID
* @param array $smartyAssigns
*
* @return array
* @throws \CRM_Core_Exception
*/
public static function renderMessageTemplate(array $mailContent, $disableSmarty, $contactID, $smartyAssigns): array {
$tokens = self::getTokensToResolve($mailContent);

// When using Smarty we need to pass the $escapeSmarty parameter.
$escapeSmarty = !$disableSmarty;

$mailContent = self::resolveDomainTokens($mailContent, $tokens, $escapeSmarty);

public static function renderMessageTemplate(array $mailContent, bool $disableSmarty, $contactID, array $smartyAssigns): array {
if ($contactID) {
$mailContent = self::resolveContactTokens($contactID, $tokens, $mailContent, $escapeSmarty);
// @todo resolve contact ID below - see https://github.com/civicrm/civicrm-core/pull/19550
// for things to resolve first.
$tokens = self::getTokensToResolve($mailContent);
$mailContent = self::resolveContactTokens($contactID, $tokens, $mailContent, !$disableSmarty);
}

// Normally Smarty is run, but it can be disabled using the disableSmarty
// parameter, which may be useful for non-core uses of MessageTemplate.send
// In particular it helps with the mosaicomsgtpl extension.
if (!$disableSmarty) {
$mailContent = self::parseThroughSmarty($mailContent, $smartyAssigns);
}
else {
// Since we're not relying on Smarty for this function, we DIY.
// strip whitespace from ends and turn into a single line
$mailContent['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $mailContent['subject']));
CRM_Core_Smarty::singleton()->pushScope($smartyAssigns);
$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), ['smarty' => !$disableSmarty]);
$tokenProcessor->addMessage('html', $mailContent['html'], 'text/html');
$tokenProcessor->addMessage('text', $mailContent['text'], 'text/plain');
$tokenProcessor->addMessage('subject', $mailContent['subject'], 'text/plain');
$tokenProcessor->addRow([]);
$tokenProcessor->evaluate();
foreach ($tokenProcessor->getRows() as $row) {
$mailContent['html'] = $row->render('html');
$mailContent['text'] = $row->render('text');
$mailContent['subject'] = $row->render('subject');
}
CRM_Core_Smarty::singleton()->popScope();

$mailContent['subject'] = trim(preg_replace('/[\r\n]+/', ' ', $mailContent['subject']));
return $mailContent;
}

Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testDomainTokens(): void {
Up the road
London, 90210
~ [email protected] ~ 1 ~ rather nice', $messageContent['text']);
$this->assertEquals('Default Domain Name ~ ~ Buckingham palaceUp the roadLondon, 90210~ [email protected] ~ 1 ~ rather nice', $messageContent['subject']);
$this->assertEquals('Default Domain Name ~ ~ Buckingham palace Up the road London, 90210 ~ [email protected] ~ 1 ~ rather nice', $messageContent['subject']);
}

/**
Expand Down Expand Up @@ -246,7 +246,7 @@ public function testContactTokens(): void {
';
$this->assertEquals($expected, $messageContent['html']);
$this->assertEquals($expected, $messageContent['text']);
$this->assertEquals(str_replace("\n", '', $expected), $messageContent['subject']);
$this->assertEquals(rtrim(str_replace("\n", ' ', $expected)), $messageContent['subject']);
}

/**
Expand Down
20 changes: 9 additions & 11 deletions tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function setUp() {
*
* @throws \Exception
*/
public function testSubmit() {
public function testSubmit(): void {
$event = $this->eventCreate();
$mut = new CiviMailUtils($this, TRUE);
CRM_Event_Form_Registration_Confirm::testSubmit([
Expand Down Expand Up @@ -77,15 +77,11 @@ public function testSubmit() {
],
]);

$participant = $this->callAPISuccessGetSingle('Participant', []);
$mut->checkMailLog([
'Dear Logged In, Thank you for your registration. This is a confirmation that your registration has been received and your status has been updated to Registered.',
]);
$mut->stop();
$mut->clearMessages();
$tplVars = CRM_Core_Smarty::singleton()->get_template_vars();
$this->assertEquals($participant['id'], $tplVars['participantID']);

}

/**
Expand Down Expand Up @@ -486,8 +482,6 @@ private function submitWithNote($event, $contact_id) {
$mut->checkMailLog(['Comment: ' . $event['note'] . chr(0x0A)]);
$mut->stop();
$mut->clearMessages();
$tplVars = CRM_Core_Smarty::singleton()->get_template_vars();
$this->assertEquals($participant['id'], $tplVars['participantID']);
//return ['contact_id' => $contact_id, 'participant_id' => $participant['id']];
return [$contact_id, $participant['id']];
}
Expand Down Expand Up @@ -557,21 +551,25 @@ private function uf_field_add($uf_group_id, $field_name, $field_type, $field_lab
* /dev/event#10
* Test submission with a note in the profile, ensuring the confirmation
* email reflects the submitted value
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
* @throws \Exception
*/
public function testNoteSubmission() {
public function testNoteSubmission(): void {
//create an event with an attached profile containing a note
$event = $this->creatEventWithProfile(NULL);
$event['custom_pre_id'] = $this->ids["UFGroup"]["our profile"];
$event['custom_pre_id'] = $this->ids['UFGroup']['our profile'];
$event['note'] = 'This is note 1';
list($contact_id, $participant_id) = $this->submitWithNote($event, NULL);
[$contact_id, $participant_id] = $this->submitWithNote($event, NULL);
civicrm_api3('Participant', 'delete', ['id' => $participant_id]);

//now that the contact has one note, register this contact again with a different note
//and confirm that the note shown in the email is the current one
$event = $this->creatEventWithProfile($event);
$event['custom_pre_id'] = $this->ids["UFGroup"]["our profile"];
$event['note'] = 'This is note 2';
list($contact_id, $participant_id) = $this->submitWithNote($event, $contact_id);
[$contact_id, $participant_id] = $this->submitWithNote($event, $contact_id);
civicrm_api3('Participant', 'delete', ['id' => $participant_id]);

//finally, submit a blank note and confirm that the note shown in the email is blank
Expand Down
4 changes: 1 addition & 3 deletions tests/phpunit/api/v3/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3666,9 +3666,8 @@ public function setUpPendingContribution($priceFieldValueID, $contriParams = [])
* Test sending a mail via the API.
*
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testSendMail() {
public function testSendMail(): void {
$mut = new CiviMailUtils($this, TRUE);
$orderParams = $this->_params;
$orderParams['contribution_status_id'] = 'Pending';
Expand Down Expand Up @@ -3698,7 +3697,6 @@ public function testSendMail() {
$mut->stop();
$tplVars = CRM_Core_Smarty::singleton()->get_template_vars();
$this->assertEquals('bob', $tplVars['billingName']);
$this->assertEquals("bob\nblah\n", $tplVars['address']);
}

/**
Expand Down

0 comments on commit b5145c1

Please sign in to comment.