From 9e5c06a0b897df2cfee03cf9b60d448ac4cff927 Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Sat, 24 Jun 2023 17:25:06 -0700 Subject: [PATCH] Fix PetitionTest on group frontend_title, erm by doing all the stuff --- CRM/Campaign/BAO/Petition.php | 51 +++++++------------ CRM/Mailing/Event/BAO/MailingEventConfirm.php | 5 +- CRM/Upgrade/Incremental/MessageTemplates.php | 9 ++++ CRM/Utils/Token.php | 3 ++ .../civicrm_mailing_component.sqldata.php | 2 +- .../phpunit/CRM/Campaign/BAO/PetitionTest.php | 35 ++++++++----- .../message_templates/petition_sign_html.tpl | 5 +- .../petition_sign_subject.tpl | 2 +- .../message_templates/petition_sign_text.tpl | 2 +- 9 files changed, 61 insertions(+), 53 deletions(-) diff --git a/CRM/Campaign/BAO/Petition.php b/CRM/Campaign/BAO/Petition.php index 8c7ea0bb72..d75e2b612f 100644 --- a/CRM/Campaign/BAO/Petition.php +++ b/CRM/Campaign/BAO/Petition.php @@ -9,6 +9,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\Group; + /** * * @package CRM @@ -514,32 +516,21 @@ AND tag_id = ( SELECT id FROM civicrm_tag WHERE name = %2 )"; * (reference ) an assoc array of name/value pairs. * * @param int $sendEmailMode + * CRM_Campaign_Form_Petition_Signature::EMAIL_THANK or CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM * - * @throws Exception + * @throws CRM_Core_Exception */ - public static function sendEmail($params, $sendEmailMode) { - - /* sendEmailMode - * CRM_Campaign_Form_Petition_Signature::EMAIL_THANK - * connected user via login/pwd - thank you - * or dedupe contact matched who doesn't have a tag CIVICRM_TAG_UNCONFIRMED - thank you - * or login using fb connect - thank you + click to add msg to fb wall - * - * CRM_Campaign_Form_Petition_Signature::EMAIL_CONFIRM - * send a confirmation request email - */ - - // check if the group defined by CIVICRM_PETITION_CONTACTS exists, else create it - $petitionGroupName = Civi::settings()->get('petition_contacts'); - - $dao = new CRM_Contact_DAO_Group(); - $dao->title = $petitionGroupName; - if (!$dao->find(TRUE)) { - $dao->is_active = 1; - $dao->visibility = 'User and User Admin Only'; - $dao->save(); + public static function sendEmail(array $params, int $sendEmailMode): void { + $surveyID = $params['sid']; + $contactID = $params['contactId']; + $activityID = $params['activityId'] ?? NULL; + $group_id = Group::get(FALSE)->addWhere('title', '=', Civi::settings()->get('petition_contacts'))->addSelect('id')->execute()->first()['id'] ?? NULL; + if (!$group_id) { + $group_id = Group::create(FALSE)->setValues([ + 'title' => Civi::settings()->get('petition_contacts'), + 'visibility' => 'User and User Admin Only', + ])->execute()->first()['id']; } - $group_id = $dao->id; // get petition info $petitionParams['id'] = $params['sid']; @@ -550,7 +541,7 @@ AND tag_id = ( SELECT id FROM civicrm_tag WHERE name = %2 )"; } //get the default domain email address. - list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail(); + [$domainEmailName, $domainEmailAddress] = CRM_Core_BAO_Domain::getNameAndEmail(); $emailDomain = CRM_Core_BAO_MailSettings::defaultDomain(); @@ -568,25 +559,17 @@ AND tag_id = ( SELECT id FROM civicrm_tag WHERE name = %2 )"; switch ($sendEmailMode) { case CRM_Campaign_Form_Petition_Signature::EMAIL_THANK: - - // add this contact to the CIVICRM_PETITION_CONTACTS group - // Cannot pass parameter 1 by reference - $p = [$params['contactId']]; - CRM_Contact_BAO_GroupContact::addContactsToGroup($p, $group_id, 'API'); + CRM_Contact_BAO_GroupContact::addContactsToGroup([$contactID], $group_id, 'API'); if ($params['email-Primary']) { CRM_Core_BAO_MessageTemplate::sendTemplate( [ - 'groupName' => 'msg_tpl_workflow_petition', 'workflow' => 'petition_sign', - 'contactId' => $params['contactId'], - 'tplParams' => $tplParams, + 'modelProps' => ['surveyID' => $surveyID, 'contactID' => $contactID], 'from' => "\"{$domainEmailName}\" <{$domainEmailAddress}>", 'toName' => $toName, 'toEmail' => $params['email-Primary'], 'replyTo' => $replyTo, - 'petitionId' => $params['sid'], - 'petitionTitle' => $petitionInfo['title'], ] ); } diff --git a/CRM/Mailing/Event/BAO/MailingEventConfirm.php b/CRM/Mailing/Event/BAO/MailingEventConfirm.php index 33f5f6d9ce..b9f0c8d101 100644 --- a/CRM/Mailing/Event/BAO/MailingEventConfirm.php +++ b/CRM/Mailing/Event/BAO/MailingEventConfirm.php @@ -113,8 +113,9 @@ class CRM_Mailing_Event_BAO_MailingEventConfirm extends CRM_Mailing_Event_DAO_Ma $bao->body_html = $html; $templates = $bao->getTemplates(); - $html = CRM_Utils_Token::replaceWelcomeTokens($templates['html'], $group->title, TRUE); - $text = CRM_Utils_Token::replaceWelcomeTokens($templates['text'], $group->title, FALSE); + // We can stop doing this here once it has been done in an upgrade script. + $html = str_replace('{welcome.group}', '{group.frontend_title}', $templates['html']); + $text = str_replace('{welcome.group}', '{group.frontend_title}', $templates['text']); $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ 'controller' => __CLASS__, diff --git a/CRM/Upgrade/Incremental/MessageTemplates.php b/CRM/Upgrade/Incremental/MessageTemplates.php index 67fd553e54..d750f67ea7 100644 --- a/CRM/Upgrade/Incremental/MessageTemplates.php +++ b/CRM/Upgrade/Incremental/MessageTemplates.php @@ -350,6 +350,15 @@ class CRM_Upgrade_Incremental_MessageTemplates { ['name' => 'contribution_offline_receipt', 'type' => 'html'], ], ], + [ + 'version' => '5.56.alpha1', + 'upgrade_descriptor' => ts('Update to use tokens'), + 'templates' => [ + ['name' => 'petition_sign', 'type' => 'text'], + ['name' => 'petition_sign', 'type' => 'html'], + ['name' => 'petition_sign', 'type' => 'subject'], + ], + ], ]; } diff --git a/CRM/Utils/Token.php b/CRM/Utils/Token.php index bc4bfd9838..af8539b1de 100644 --- a/CRM/Utils/Token.php +++ b/CRM/Utils/Token.php @@ -926,6 +926,8 @@ class CRM_Utils_Token { /** * Replace welcome/confirmation tokens * + * @deprecated since 5.64 will be removed around 5.70 + * * @param string $str * The string with tokens to be replaced. * @param string $group @@ -937,6 +939,7 @@ class CRM_Utils_Token { * The processed string */ public static function &replaceWelcomeTokens($str, $group, $html) { + CRM_Core_Error::deprecatedFunctionWarning('use the token processor'); if (self::token_match('welcome', 'group', $str)) { self::token_replace('welcome', 'group', $group, $str); } diff --git a/sql/civicrm_data/civicrm_mailing_component.sqldata.php b/sql/civicrm_data/civicrm_mailing_component.sqldata.php index bd1283618a..4ba0f76a2b 100644 --- a/sql/civicrm_data/civicrm_mailing_component.sqldata.php +++ b/sql/civicrm_data/civicrm_mailing_component.sqldata.php @@ -2,7 +2,7 @@ $subgroup = '{subscribe.group}'; $suburl = '{subscribe.url}'; -$welgroup = '{welcome.group}'; +$welgroup = '{group.frontend_title}'; $unsubgroup = '{unsubscribe.group}'; $actresub = '{action.resubscribe}'; $actresuburl = '{action.resubscribeUrl}'; diff --git a/tests/phpunit/CRM/Campaign/BAO/PetitionTest.php b/tests/phpunit/CRM/Campaign/BAO/PetitionTest.php index 33e8ae5242..eaf0a6a4f5 100644 --- a/tests/phpunit/CRM/Campaign/BAO/PetitionTest.php +++ b/tests/phpunit/CRM/Campaign/BAO/PetitionTest.php @@ -10,6 +10,8 @@ +--------------------------------------------------------------------+ */ +use Civi\Api4\MessageTemplate; + /** * * @package CRM @@ -17,10 +19,17 @@ */ class CRM_Campaign_BAO_PetitionTest extends CiviUnitTestCase { + public function tearDown(): void { + $this->revertTemplateToReservedTemplate(); + parent::tearDown(); + } + /** * Test Petition Email Sending using Domain tokens + * + * @throws \CRM_Core_Exception */ - public function testPetitionEmailWithDomainTokens() { + public function testPetitionEmailWithDomainTokens(): void { $mut = new CiviMailUtils($this, TRUE); $domain = $this->callAPISuccess('Domain', 'getsingle', ['id' => CRM_Core_Config::domainID()]); $this->callAPISuccess('Address', 'create', [ @@ -32,13 +41,13 @@ class CRM_Campaign_BAO_PetitionTest extends CiviUnitTestCase { 'country_id' => 'US', 'postal_code' => '20500', ]); - $template_contact = CRM_Core_DAO::singleValueQuery("SELECT msg_html FROM civicrm_msg_template WHERE workflow_name = 'petition_sign' AND is_default = 1"); - $template_contact .= ' - {domain.address}'; - CRM_Core_DAO::executeQuery("UPDATE civicrm_msg_template SET msg_html = '{$template_contact}' WHERE workflow_name = 'petition_sign' AND is_default = 1"); - $contact = $this->individualCreate(); - $email = $this->callAPISuccess('email', 'create', [ - 'contact_id' => $contact, + $templateContent = CRM_Core_DAO::singleValueQuery("SELECT msg_html FROM civicrm_msg_template WHERE workflow_name = 'petition_sign' AND is_default = 1"); + MessageTemplate::update()->addWhere('workflow_name', '=', 'petition_sign') + ->addWhere('is_default', '=', 1) + ->setValues(['msg_html' => $templateContent . '{domain.address}'])->execute(); + $contactID = $this->individualCreate(); + $this->callAPISuccess('Email', 'create', [ + 'contact_id' => $contactID, 'email' => 'testpetitioncontact@civicrm.org', ]); $survey = $this->callAPISuccess('Survey', 'create', [ @@ -46,15 +55,17 @@ class CRM_Campaign_BAO_PetitionTest extends CiviUnitTestCase { 'activity_type_id' => 'Petition', 'bypass_confirm' => 1, ]); - $params = [ + CRM_Campaign_BAO_Petition::sendEmail([ 'sid' => $survey['id'], - 'contactId' => $contact, + 'contactId' => $contactID, 'email-Primary' => 'testpetitioncontact@civicrm.org', - ]; - CRM_Campaign_BAO_Petition::sendEmail($params, CRM_Campaign_Form_Petition_Signature::EMAIL_THANK); + ], CRM_Campaign_Form_Petition_Signature::EMAIL_THANK); $mut->checkMailLog([ '1600 Pennsylvania Avenue', 'Washington', + 'Dear Anthony, +Thank you for signing Test Petition. +', ]); $mut->stop(); } diff --git a/xml/templates/message_templates/petition_sign_html.tpl b/xml/templates/message_templates/petition_sign_html.tpl index 0c83e5ceea..5fdf1215b1 100644 --- a/xml/templates/message_templates/petition_sign_html.tpl +++ b/xml/templates/message_templates/petition_sign_html.tpl @@ -1,5 +1,6 @@ {assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}

{$greeting},

{/if} -

Thank you for signing {$petition.title}.

+

Thank you for signing {survey.title}.

-{include file="CRM/Campaign/Page/Petition/SocialNetwork.tpl" petition_id=$survey_id noscript=true emailMode=true} +{capture assign=petitionURL}{crmURL p='civicrm/petition/sign' q="sid={survey.id}" a=1 fe=1 h=1}{/capture} +{include file="CRM/common/SocialNetwork.tpl" url=$petitionURL title='{survey.title}' pageURL=$petitionURL petition_id='{survey.id}' noscript=true emailMode=true} diff --git a/xml/templates/message_templates/petition_sign_subject.tpl b/xml/templates/message_templates/petition_sign_subject.tpl index cbf65b26a3..2698d2f155 100644 --- a/xml/templates/message_templates/petition_sign_subject.tpl +++ b/xml/templates/message_templates/petition_sign_subject.tpl @@ -1 +1 @@ -Thank you for signing {$petition.title} - {contact.display_name} +Thank you for signing {survey.title} - {contact.display_name} diff --git a/xml/templates/message_templates/petition_sign_text.tpl b/xml/templates/message_templates/petition_sign_text.tpl index 936b312c1e..90dbd6f8be 100644 --- a/xml/templates/message_templates/petition_sign_text.tpl +++ b/xml/templates/message_templates/petition_sign_text.tpl @@ -1,3 +1,3 @@ {assign var="greeting" value="{contact.email_greeting_display}"}{if $greeting}{$greeting},{/if} -Thank you for signing {$petition.title}. +Thank you for signing {survey.title}. -- 2.25.1