From 965ff8f1e415944a100ffd0f9e9b3040b492b10a Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 10 Sep 2021 10:43:37 +1200 Subject: [PATCH] Replace expensive query with cheaper one Rather than retrieve everything with a legacy function, just get what we want.... Note I commented on how silly I think it is but no change to outcome --- CRM/Core/BAO/MessageTemplate.php | 18 +++++----- .../CRM/Core/BAO/MessageTemplateTest.php | 36 ++++++++++++++++++- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CRM/Core/BAO/MessageTemplate.php b/CRM/Core/BAO/MessageTemplate.php index 0fa7c268e3..50ea77fbac 100644 --- a/CRM/Core/BAO/MessageTemplate.php +++ b/CRM/Core/BAO/MessageTemplate.php @@ -15,6 +15,7 @@ * @copyright CiviCRM LLC https://civicrm.org/licensing */ +use Civi\Api4\Email; use Civi\Api4\MessageTemplate; use Civi\WorkflowMessage\WorkflowMessage; @@ -369,16 +370,17 @@ class CRM_Core_BAO_MessageTemplate extends CRM_Core_DAO_MessageTemplate { $params['html'] = $mailContent['html']; if ($params['toEmail']) { - $contactParams = [['email', 'LIKE', $params['toEmail'], 0, 1]]; - [$contact] = CRM_Contact_BAO_Query::apiQuery($contactParams); - - $prefs = array_pop($contact); - - if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] === 'HTML') { + // @todo - consider whether we really should be loading + // this based on 'the first email in the db that matches'. + // when we likely have the contact id. OTOH people probably barely + // use preferredMailFormat these days - the good fight against html + // emails was lost a decade ago... + $preferredMailFormat = Email::get(FALSE)->addWhere('email', '=', $params['toEmail'])->addSelect('contact_id.preferred_mail_format')->execute()->first()['contact_id.preferred_mail_format']; + + if ($preferredMailFormat === 'HTML') { $params['text'] = NULL; } - - if (isset($prefs['preferred_mail_format']) and $prefs['preferred_mail_format'] === 'Text') { + if ($preferredMailFormat === 'Text') { $params['html'] = NULL; } diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 3edb7e6c23..1866db446c 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -45,7 +45,41 @@ class CRM_Core_BAO_MessageTemplateTest extends CiviUnitTestCase { $this->assertStringContainsString('

Hello testRenderTemplate Abba Baab!

', $rendered['html']); } - public function testSendTemplate_RenderMode_OpenTemplate() { + /** + * Check detection of contact's preferred mail. + * + * - ie if text then no html output is sent. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function testSendTemplateNoHtml(): void { + $contactId = $this->individualCreate([ + 'preferred_mail_format' => 'Text', + 'first_name' => 'Mary', + 'email' => 'mary_anderson@civicrm.org', + ]); + $mut = new CiviMailUtils($this, TRUE); + CRM_Core_BAO_MessageTemplate::sendTemplate( + [ + 'contactId' => $contactId, + 'from' => 'admin@example.com', + 'toEmail' => 'mary_anderson@civicrm.org', + 'messageTemplate' => [ + 'msg_subject' => 'My subject', + 'msg_text' => 'My text', + 'msg_html' => 'My html', + ], + ] + ); + $mut->checkMailLog(['My text', 'My subject'], ['My html']); + } + + /** + * @throws \API_Exception + * @throws \CRM_Core_Exception + */ + public function testSendTemplate_RenderMode_OpenTemplate(): void { $contactId = $this->individualCreate([ 'first_name' => 'Abba', 'last_name' => 'Baab', -- 2.25.1