From e33c2ce7b864fbf7c36c44972d9b9717fd2e485e Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Mon, 6 Sep 2021 14:09:06 +1200 Subject: [PATCH] dev/core#2814 Fix activity:sendSMS to use renderTemplate --- CRM/Activity/BAO/Activity.php | 3 +-- .../phpunit/CRM/Activity/BAO/ActivityTest.php | 18 ++++++++++++------ tests/phpunit/CiviTest/CiviTestSMSProvider.php | 12 +++++++++++- 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CRM/Activity/BAO/Activity.php b/CRM/Activity/BAO/Activity.php index 362f869824..c346148fc4 100644 --- a/CRM/Activity/BAO/Activity.php +++ b/CRM/Activity/BAO/Activity.php @@ -1327,8 +1327,7 @@ WHERE entity_id =%1 AND entity_table = %2"; unset($tokenDetails["{$contactId}"]['phone_type_id']); $contact = array_merge($contact, $tokenDetails["{$contactId}"]); } - $tokenText = CRM_Utils_Token::replaceContactTokens($text, $contact, FALSE, $messageToken, FALSE, FALSE); - $tokenText = CRM_Utils_Token::replaceHookTokens($tokenText, $contact, $categories, FALSE, FALSE); + $tokenText = CRM_Core_BAO_MessageTemplate::renderTemplate(['messageTemplate' => ['msg_text' => $text], 'contactId' => $contactId, 'disableSmarty' => TRUE])['text']; // Only send if the phone is of type mobile if ($contact['phone_type_id'] == CRM_Core_PseudoConstant::getKey('CRM_Core_BAO_Phone', 'phone_type_id', 'Mobile')) { diff --git a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php index e737a872dc..dc360ecaa5 100644 --- a/tests/phpunit/CRM/Activity/BAO/ActivityTest.php +++ b/tests/phpunit/CRM/Activity/BAO/ActivityTest.php @@ -1378,6 +1378,8 @@ $text } /** + * Test successful SMS send. + * * @throws \API_Exception * @throws \CRM_Core_Exception * @throws \CiviCRM_API3_Exception @@ -1385,7 +1387,10 @@ $text */ public function testSendSmsMobilePhoneNumber(): void { $sent = $this->createSendSmsTest(TRUE, 2); - $this->assertEquals(TRUE, $sent[0], "Expected sent should be true"); + $this->assertEquals(TRUE, $sent[0]); + /* @var CiviTestSMSProvider $provider $provider['id']*/ + $providerObj = CRM_SMS_Provider::singleton(['provider_id' => $this->ids['SmsProvider'][0]]); + $this->assertEquals('text Anthony', $providerObj->getSentMessage()); } /** @@ -1423,7 +1428,7 @@ $text * @throws \Civi\API\Exception\UnauthorizedException */ public function createSendSmsTest(bool $expectSuccess = TRUE, int $phoneType = 0, bool $passPhoneTypeInContactDetails = FALSE, array $additionalContactParams = []): array { - $provider = civicrm_api3('SmsProvider', 'create', [ + $this->ids['SmsProvider'][0] = civicrm_api3('SmsProvider', 'create', [ 'name' => 'CiviTestSMSProvider', 'api_type' => 1, 'username' => 1, @@ -1433,9 +1438,9 @@ $text 'is_default' => 1, 'is_active' => 1, 'domain_id' => 1, - ]); + ])['id']; - $smsProviderParams['provider_id'] = $provider['id']; + $smsProviderParams['provider_id'] = $this->ids['SmsProvider'][0]; // Create a contact $contactId = $this->individualCreate(); @@ -1450,7 +1455,7 @@ $text $contactIds[] = $contact['contact_id']; } - $activityParams['sms_text_message'] = 'text'; + $activityParams['sms_text_message'] = 'text {contact.first_name}'; $activityParams['activity_subject'] = 'subject'; // Get a "logged in" user to set as source of Sms. @@ -2663,7 +2668,8 @@ $textValue $this->assertEquals($outBoundSmsActivityId, $activity['activity_type_id'], 'Wrong activity type is set.'); $this->assertEquals($activityStatusCompleted, $activity['status_id'], 'Expected activity status Completed.'); $this->assertEquals('subject', $activity['subject'], 'Activity subject does not match.'); - $this->assertEquals('text', $activity['details'], 'Activity details does not match.'); + // Token is not resolved here. + $this->assertEquals('text {contact.first_name}', $activity['details'], 'Activity details does not match.'); } /** diff --git a/tests/phpunit/CiviTest/CiviTestSMSProvider.php b/tests/phpunit/CiviTest/CiviTestSMSProvider.php index 9bcffd0d53..d0e23e79f2 100644 --- a/tests/phpunit/CiviTest/CiviTestSMSProvider.php +++ b/tests/phpunit/CiviTest/CiviTestSMSProvider.php @@ -13,7 +13,7 @@ * Test SMS provider to allow for testing */ class CiviTestSMSProvider extends CRM_SMS_Provider { - protected $_providerInfo = []; + protected $sentMessage; protected $_id = 0; static private $_singleton = []; @@ -44,6 +44,16 @@ class CiviTestSMSProvider extends CRM_SMS_Provider { } public function send($recipients, $header, $message, $dncID = NULL) { + $this->sentMessage = $message; + } + + /** + * Get the message that was sent. + * + * @return string + */ + public function getSentMessage(): string { + return $this->sentMessage; } } -- 2.25.1