From 3676b8b4e01151c168db5e2d0920cb3e571fab8c Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 15 Mar 2021 18:55:33 +1300 Subject: [PATCH] Fix token subscriber to format the display of the custom tokens --- Civi/Token/TokenCompatSubscriber.php | 15 ++++++----- .../CRM/Core/BAO/ActionScheduleTest.php | 13 ++++------ .../CRM/Core/BAO/MessageTemplateTest.php | 25 +++++++++++-------- 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/Civi/Token/TokenCompatSubscriber.php b/Civi/Token/TokenCompatSubscriber.php index bdfaa5994eb5..bf291635c620 100644 --- a/Civi/Token/TokenCompatSubscriber.php +++ b/Civi/Token/TokenCompatSubscriber.php @@ -47,6 +47,8 @@ public function onEvaluate(TokenValueEvent $e) { $e->getTokenProcessor()->context['hookTokenCategories'] = $categories; $messageTokens = $e->getTokenProcessor()->getMessageTokens(); + $returnProperties = array_fill_keys($messageTokens['contact'] ?? [], 1); + $returnProperties = array_merge(\CRM_Contact_BAO_Query::defaultReturnProperties(), $returnProperties); foreach ($e->getRows() as $row) { if (empty($row->context['contactId'])) { @@ -58,12 +60,16 @@ public function onEvaluate(TokenValueEvent $e) { $params = [ ['contact_id', '=', $contactId, 0, 0], ]; - [$contact] = \CRM_Contact_BAO_Query::apiQuery($params); + [$contact] = \CRM_Contact_BAO_Query::apiQuery($params, $returnProperties ?? NULL); //CRM-4524 $contact = reset($contact); + // Test cover for greeting in CRM_Core_BAO_ActionScheduleTest::testMailer + $contact['email_greeting'] = $contact['email_greeting_display'] ?? ''; + $contact['postal_greeting'] = $contact['postal_greeting_display'] ?? ''; + $contact['addressee'] = $contact['address_display'] ?? ''; if (!$contact || is_a($contact, 'CRM_Core_Error')) { // FIXME: Need to differentiate errors which kill the batch vs the individual row. - \Civi::log()->debug("Failed to generate token data. Invalid contact ID: " . $row->context['contactId']); + \Civi::log()->debug('Failed to generate token data. Invalid contact ID: ' . $row->context['contactId']); continue; } @@ -71,10 +77,7 @@ public function onEvaluate(TokenValueEvent $e) { if (!empty($messageTokens['contact'])) { foreach ($messageTokens['contact'] as $token) { if (\CRM_Core_BAO_CustomField::getKeyID($token)) { - $contact[$token] = civicrm_api3('Contact', 'getvalue', [ - 'return' => $token, - 'id' => $contactId, - ]); + $contact[$token] = \CRM_Core_BAO_CustomField::displayValue($contact[$token], \CRM_Core_BAO_CustomField::getKeyID($token)); } } } diff --git a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php index e46626e3467e..103d58e3a857 100644 --- a/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php +++ b/tests/phpunit/CRM/Core/BAO/ActionScheduleTest.php @@ -39,7 +39,7 @@ class CRM_Core_BAO_ActionScheduleTest extends CiviUnitTestCase { * * @throws CRM_Core_Exception */ - public function setUp() { + public function setUp(): void { parent::setUp(); $this->mut = new CiviMailUtils($this, TRUE); @@ -769,7 +769,7 @@ public function setUp() { * * @throws \CRM_Core_Exception */ - public function tearDown() { + public function tearDown(): void { parent::tearDown(); $this->mut->clearMessages(); $this->mut->stop(); @@ -900,18 +900,15 @@ public function mailerExamples() { * * @throws \API_Exception * @throws \CRM_Core_Exception - * @throws \Civi\API\Exception\UnauthorizedException - * * @dataProvider mailerExamples - * */ - public function testMailer($schedule, $patterns) { + public function testMailer(array $schedule, array $patterns): void { $actionSchedule = array_merge($this->fixtures['sched_activity_1day'], $schedule); $actionScheduleDao = CRM_Core_BAO_ActionSchedule::add($actionSchedule); - $this->assertInternalType('numeric', $actionScheduleDao->id); + $this->assertIsNumeric($actionScheduleDao->id); $activity = $this->createTestObject('CRM_Activity_DAO_Activity', $this->fixtures['phonecall']); - $this->assertInternalType('numeric', $activity->id); + $this->assertIsNumeric($activity->id); $contact = $this->callAPISuccess('contact', 'create', array_merge( $this->fixtures['contact'], [ diff --git a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php index 38d9c9945030..ad36d68f7065 100644 --- a/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php +++ b/tests/phpunit/CRM/Core/BAO/MessageTemplateTest.php @@ -182,10 +182,10 @@ public function testContactTokens(): void { public function testContactTokensRenderedByTokenProcessor(): void { $this->createCustomGroupWithFieldsOfAllTypes([]); $tokenData = $this->getAllContactTokens(); - // @todo - these 2 still need fixing/ syncing. - unset($tokenData['preferred_communication_method'], $tokenData['addressee']); + // @todo - these still need fixing/ syncing. + unset($tokenData['preferred_communication_method'], $tokenData['addressee'], $tokenData['email_greeting'], $tokenData['postal_greeting']); $address = $this->setupContactFromTokeData($tokenData); - $tokenString =''; + $tokenString = ''; foreach (array_keys($tokenData) as $key) { $tokenString .= "{$key}:{contact.{$key}}\n"; } @@ -198,8 +198,11 @@ public function testContactTokensRenderedByTokenProcessor(): void { $rendered = (string) $row->render('html'); } $expected = $this->getExpectedContactOutput($address['id'], $tokenData, $rendered); - // @todo - fix these 2 & stop stripping them out. - $expected = str_replace(["preferred_communication_method:\n", "addressee:Mr. Robert Frank Smith II\n"], '', $expected); + // @todo - fix these & stop stripping them out. + $expected = str_replace([ + "preferred_communication_method:\n", + "addressee:Mr. Robert Frank Smith II\n", + ], '', $expected); $this->assertEquals($expected, $rendered); } @@ -364,23 +367,25 @@ protected function setupContactFromTokeData(array $tokenData) { 'is_primary' => TRUE, 'name' => $tokenData['im'], 'provider_id' => $tokenData['im_provider'], - 'contact_id' => $tokenData['contact_id'] + 'contact_id' => $tokenData['contact_id'], ]); $this->callAPISuccess('OpenID', 'create', array_merge($tokenData, [ 'is_primary' => TRUE, 'contact_id' => $tokenData['contact_id'], - 'openid' => $tokenData['openid'] + 'openid' => $tokenData['openid'], ])); return $address; } /** - * @param array|null $contact - * @param $id + * Get the expected rendered string. + * + * @param int $id * @param array $tokenData - * @param bool $checksum + * @param string $actualOutput * * @return string + * @throws \API_Exception */ protected function getExpectedContactOutput($id, array $tokenData, string $actualOutput): string { $checksum = substr($actualOutput, (strpos($actualOutput, 'cs=') + 3), 47);