From 65bbba0bc5af3aa77faf05b23f89de1d9128743c Mon Sep 17 00:00:00 2001 From: Eileen McNaughton Date: Fri, 8 Dec 2023 17:59:13 +1300 Subject: [PATCH] Add token {membership.membership_status_id.is_new} --- CRM/Core/EntityTokens.php | 3 ++ CRM/Member/Tokens.php | 44 ++++++++++--------- .../CRM/Member/Form/Task/PDFLetterTest.php | 11 +++-- .../CRM/Utils/TokenConsistencyTest.php | 14 ++++-- 4 files changed, 44 insertions(+), 28 deletions(-) diff --git a/CRM/Core/EntityTokens.php b/CRM/Core/EntityTokens.php index ce76c40e23..e038dc49d5 100644 --- a/CRM/Core/EntityTokens.php +++ b/CRM/Core/EntityTokens.php @@ -713,6 +713,9 @@ class CRM_Core_EntityTokens extends AbstractTokenSubscriber { * @throws \CRM_Core_Exception */ protected function getRelatedTokensForEntity(string $entity, string $joinField, array $tokenList, $hiddenTokens = []): array { + if (!array_key_exists($entity, \Civi::service('action_object_provider')->getEntities())) { + return []; + } $apiParams = ['checkPermissions' => FALSE]; if ($tokenList !== ['*']) { $apiParams['where'] = [['name', 'IN', $tokenList]]; diff --git a/CRM/Member/Tokens.php b/CRM/Member/Tokens.php index 6def09ac86..f696764421 100644 --- a/CRM/Member/Tokens.php +++ b/CRM/Member/Tokens.php @@ -10,8 +10,6 @@ +--------------------------------------------------------------------+ */ -use Civi\Api4\ContributionRecur; - /** * Class CRM_Member_Tokens * @@ -63,8 +61,8 @@ class CRM_Member_Tokens extends CRM_Core_EntityTokens { */ public function evaluateToken(\Civi\Token\TokenRow $row, $entity, $field, $prefetch = NULL) { if ($field === 'fee') { - $membershipType = CRM_Member_BAO_MembershipType::getMembershipType($this->getFieldValue($row, 'membership_type_id')); - $row->tokens($entity, $field, \CRM_Utils_Money::formatLocaleNumericRoundedForDefaultCurrency($membershipType['minimum_fee'] ?? 0)); + parent::evaluateToken($row, $entity, 'membership_type_id.minimum_fee', $prefetch); + $row->format('text/plain')->tokens($entity, $field, $row->tokens['membership']['membership_type_id.minimum_fee']); } else { parent::evaluateToken($row, $entity, $field, $prefetch); @@ -95,13 +93,30 @@ class CRM_Member_Tokens extends CRM_Core_EntityTokens { ]; } + /** + * These tokens still work but we don't advertise them. + * + * We will actively remove from the following places + * - scheduled reminders + * - add to 'blocked' on pdf letter & email + * + * & then at some point start issuing warnings for them. + * + * @return string[] + */ + protected function getDeprecatedTokens(): array { + return [ + 'fee' => 'membership_type_id.minimum_fee', + ]; + } + /** * Get fields which need to be returned to render another token. * * @return array */ public function getDependencies(): array { - return ['fee' => 'membership_type_id']; + return ['fee' => 'membership_type_id.minimum_fee']; } /** @@ -119,7 +134,7 @@ class CRM_Member_Tokens extends CRM_Core_EntityTokens { 'type' => 'calculated', 'options' => NULL, 'data_type' => 'integer', - 'audience' => 'user', + 'audience' => 'hidden', ], ]; } @@ -129,21 +144,10 @@ class CRM_Member_Tokens extends CRM_Core_EntityTokens { */ protected function getRelatedTokens(): array { $tokens = []; - if (!in_array('ContributionRecur', array_keys(\Civi::service('action_object_provider')->getEntities()))) { - return $tokens; - } $hiddenTokens = ['modified_date', 'create_date', 'trxn_id', 'invoice_id', 'is_test', 'payment_token_id', 'payment_processor_id', 'payment_instrument_id', 'cycle_day', 'installments', 'processor_id', 'next_sched_contribution_date', 'failure_count', 'failure_retry_date', 'auto_renew', 'is_email_receipt', 'contribution_status_id']; - $contributionRecurFields = ContributionRecur::getFields(FALSE)->setLoadOptions(TRUE)->execute(); - foreach ($contributionRecurFields as $contributionRecurField) { - $tokens['contribution_recur_id.' . $contributionRecurField['name']] = [ - 'title' => $contributionRecurField['title'], - 'name' => 'contribution_recur_id.' . $contributionRecurField['name'], - 'type' => 'mapped', - 'options' => $contributionRecurField['options'] ?? NULL, - 'data_type' => $contributionRecurField['data_type'], - 'audience' => in_array($contributionRecurField['name'], $hiddenTokens) ? 'hidden' : 'user', - ]; - } + $tokens += $this->getRelatedTokensForEntity('ContributionRecur', 'contribution_recur_id', ['*'], $hiddenTokens); + $tokens += $this->getRelatedTokensForEntity('MembershipType', 'membership_type_id', ['minimum_fee']); + $tokens += $this->getRelatedTokensForEntity('MembershipStatus', 'status_id', ['is_new']); return $tokens; } diff --git a/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php b/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php index 2c3999d35f..3cca177ffe 100644 --- a/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php +++ b/tests/phpunit/CRM/Member/Form/Task/PDFLetterTest.php @@ -27,6 +27,8 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { /** * Test token replacement for Print/Merge Task + * + * @throws \CRM_Core_Exception */ public function testMembershipTokenReplacementInPDF(): void { $this->createLoggedInUser(); @@ -35,7 +37,7 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { $searchFormValues = [ 'radio_ts' => 'ts_sel', - 'task' => CRM_Member_Task::PDF_LETTER, + 'task' => CRM_Core_Task::PDF_LETTER, ]; $membershipDates = [ @@ -56,7 +58,7 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { 'start_date' => $date, 'end_date' => date('Y-m-d', strtotime("{$date} +1 year")), ]; - $result = $this->callAPISuccess('membership', 'create', $params); + $result = $this->callAPISuccess('Membership', 'create', $params); $searchFormValues['mark_x_' . $result['id']] = 1; $params = array_merge($params, [ @@ -85,6 +87,7 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { ], NULL, $searchFormValues); $form->buildForm(); try { + $testHTML = ''; $form->postProcess(); } catch (CRM_Core_Exception_PrematureExitException $e) { @@ -93,7 +96,7 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { // Assert all membership tokens are replaced correctly. $expected = array_values($expected); - foreach ($expected as $key => $dateVal) { + foreach ($expected as $dateVal) { $this->assertStringContainsString('Anthony', $testHTML); foreach ($tokens as $token) { $this->assertStringContainsString($dateVal[$token], $testHTML); @@ -104,7 +107,7 @@ class CRM_Member_Form_Task_PDFLetterTest extends CiviUnitTestCase { /** * Generate sample HTML for testing. */ - public static function getSampleHTML() { + public static function getSampleHTML(): array { $tokens = [ 'Test Fee' => 'fee', 'Test Type' => 'membership_type_id:label', diff --git a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php index e9d2b0bcaf..24be9cf284 100644 --- a/tests/phpunit/CRM/Utils/TokenConsistencyTest.php +++ b/tests/phpunit/CRM/Utils/TokenConsistencyTest.php @@ -573,6 +573,7 @@ contribution_recur.payment_instrument_id:name :Check * */ public function testMembershipTokenConsistency(): void { + CRM_Utils_Time::setTime('2007-01-22 15:00:00'); $this->createLoggedInUser(); $this->restoreMembershipTypes(); $this->createCustomGroupWithFieldOfType(['extends' => 'Membership']); @@ -586,7 +587,6 @@ contribution_recur.payment_instrument_id:name :Check $tokenString .= "\n{membership." . $this->getCustomFieldName('text') . '}'; // Now compare with scheduled reminder $mut = new CiviMailUtils($this); - CRM_Utils_Time::setTime('2007-01-22 15:00:00'); $this->callAPISuccess('ActionSchedule', 'create', [ 'title' => 'job', 'subject' => 'job', @@ -612,6 +612,8 @@ contribution_recur.payment_instrument_id:name :Check $tokens = $tokenProcessor->listTokens(); // Add in custom tokens as token processor supports these. $expectedTokens = array_merge($expectedTokens, $this->getTokensAdvertisedByTokenProcessorButNotLegacy()); + // Token 'fee' is deprecated & no longer advertised. + unset($expectedTokens['{membership.fee}']); $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens(), $this->getRecurEntityTokens('membership')), $tokens); $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); $tokenProcessor->addRow(['membershipId' => $this->getMembershipID()]); @@ -677,7 +679,9 @@ contribution_recur.payment_instrument_id:name :Check '{membership.start_date}' => 'Membership Start Date', '{membership.join_date}' => 'Member Since', '{membership.end_date}' => 'Membership Expiration Date', + '{membership.membership_type_id.minimum_fee}' => 'Minimum Fee', '{membership.fee}' => 'Membership Fee', + '{membership.status_id.is_new}' => 'Is new membership status', ]; } @@ -760,15 +764,17 @@ event.fee_label :Event fees */ protected function getExpectedMembershipTokenOutput(): string { return ' -Expired +New General 1 -Expired +New General January 21st, 2007 January 21st, 2007 December 21st, 2007 -100.00'; +$100.00 +$100.00 +1'; } /** -- 2.25.1