3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
12 use Civi\Token\TokenProcessor
;
13 use Civi\Api4\LocBlock
;
16 use Civi\Api4\Address
;
19 * CRM_Utils_TokenConsistencyTest
21 * Class for ensuring tokens have internal consistency.
27 class CRM_Utils_TokenConsistencyTest
extends CiviUnitTestCase
{
29 use CRMTraits_Custom_CustomDataTrait
;
39 * Recurring contribution.
43 protected $contributionRecur;
48 * @throws \API_Exception
49 * @throws \CRM_Core_Exception
51 public function tearDown(): void
{
52 $this->quickCleanup(['civicrm_case', 'civicrm_case_type'], TRUE);
57 * Test that case tokens are consistently rendered.
59 * @throws \CiviCRM_API3_Exception
61 public function testCaseTokenConsistency(): void
{
62 $this->createLoggedInUser();
63 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
64 $this->createCustomGroupWithFieldOfType(['extends' => 'Case']);
65 $tokens = CRM_Core_SelectValues
::caseTokens();
66 $this->assertEquals($this->getCaseTokens(), $tokens);
67 $caseID = $this->getCaseID();
68 $tokenString = implode("\n", array_keys($this->getCaseTokens()));
69 $tokenHtml = CRM_Utils_Token
::replaceCaseTokens($caseID, $tokenString, ['case' => $this->getCaseTokenKeys()]);
70 $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml);
71 // Now do the same without passing in 'knownTokens'
72 $tokenHtml = CRM_Utils_Token
::replaceCaseTokens($caseID, $tokenString);
73 $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml);
75 // And check our deprecated tokens still work.
76 $tokenHtml = CRM_Utils_Token
::replaceCaseTokens($caseID, '{case.case_type_id} {case.status_id}');
77 $this->assertEquals('Housing Support Ongoing', $tokenHtml);
79 $additionalTokensFromProcessor = [
80 '{case.case_type_id}' => 'Case Type ID',
81 '{case.status_id}' => 'Case Status',
82 '{case.case_type_id:name}' => 'Machine name: Case Type',
83 '{case.status_id:name}' => 'Machine name: Case Status',
85 $expectedTokens = array_merge($this->getCaseTokens(), $additionalTokensFromProcessor);
87 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
88 'controller' => __CLASS__
,
90 'schema' => ['caseId'],
92 $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokenProcessor->listTokens());
93 $tokenProcessor->addRow([
94 'caseId' => $this->getCaseID(),
96 $tokenProcessor->addMessage('html', $tokenString, 'text/plain');
98 $tokenProcessor->evaluate();
99 foreach ($tokenProcessor->getRows() as $row) {
100 $text = $row->render('html');
102 $this->assertEquals($this->getExpectedCaseTokenOutput(), $text);
106 * Get expected output from token parsing.
110 protected function getExpectedCaseTokenOutput(): string {
119 ' . CRM_Utils_Date
::customFormat($this->case['created_date']) . '
120 ' . CRM_Utils_Date
::customFormat($this->case['modified_date']) . '
127 protected function getContactID(): int {
128 if (!isset($this->ids
['Contact'][0])) {
129 $this->ids
['Contact'][0] = $this->individualCreate();
131 return $this->ids
['Contact'][0];
135 * Get the keys for the case tokens.
139 public function getCaseTokenKeys(): array {
141 foreach (array_keys($this->getCaseTokens()) as $key) {
142 $return[] = substr($key, 6, -1);
148 * Get declared tokens.
152 public function getCaseTokens(): array {
154 '{case.id}' => 'Case ID',
155 '{case.case_type_id:label}' => 'Case Type',
156 '{case.subject}' => 'Case Subject',
157 '{case.start_date}' => 'Case Start Date',
158 '{case.end_date}' => 'Case End Date',
159 '{case.details}' => 'Details',
160 '{case.status_id:label}' => 'Case Status',
161 '{case.is_deleted:label}' => 'Case is in the Trash',
162 '{case.created_date}' => 'Created Date',
163 '{case.modified_date}' => 'Modified Date',
164 '{case.custom_1}' => 'Enter text here :: Group with field text',
173 protected function getCaseID(): int {
174 if (!isset($this->case)) {
175 $case_id = $this->callAPISuccess('Case', 'create', [
176 'case_type_id' => 'housing_support',
177 'activity_subject' => 'Case Subject',
178 'client_id' => $this->getContactID(),
180 'subject' => 'Case Subject',
181 'start_date' => '2021-07-23 15:39:20',
182 // Note end_date is inconsistent with status Ongoing but for the
183 // purposes of testing tokens is ok. Creating it with status Resolved
184 // then ignores our known fixed end date.
185 'end_date' => '2021-07-26 18:07:20',
187 'details' => 'case details',
188 'activity_details' => 'blah blah',
191 // Need to retrieve the case again because modified date might be updated a
192 // split-second later than the original return value because of activity
193 // triggers when the timeline is populated. The returned array from create
194 // is determined before that happens.
195 $this->case = $this->callAPISuccess('Case', 'getsingle', ['id' => $case_id]);
197 return $this->case['id'];
201 * Test that contribution recur tokens are consistently rendered.
203 public function testContributionRecurTokenConsistency(): void
{
204 $this->createLoggedInUser();
205 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
206 'controller' => __CLASS__
,
208 'schema' => ['contribution_recurId'],
210 $this->assertEquals(array_merge($this->getContributionRecurTokens(), $this->getDomainTokens()), $tokenProcessor->listTokens());
211 $tokenString = implode("\n", array_keys($this->getContributionRecurTokens()));
213 $tokenProcessor->addMessage('html', $tokenString, 'text/plain');
214 $tokenProcessor->addRow(['contribution_recurId' => $this->getContributionRecurID()]);
215 $tokenProcessor->evaluate();
216 $this->assertEquals($this->getExpectedContributionRecurTokenOutPut(), $tokenProcessor->getRow(0)->render('html'));
220 * Get the contribution recur tokens keyed by the token.
222 * e.g {contribution_recur.id}
226 protected function getContributionRecurTokens(): array {
228 foreach ($this->getContributionRecurTokensByField() as $key => $value) {
229 $return['{contribution_recur.' . $key . '}'] = $value;
234 protected function getContributionRecurTokensByField(): array {
236 'id' => 'Recurring Contribution ID',
237 'amount' => 'Amount',
238 'currency' => 'Currency',
239 'frequency_unit' => 'Frequency Unit',
240 'frequency_interval' => 'Interval (number of units)',
241 'installments' => 'Number of Installments',
242 'start_date' => 'Start Date',
243 'create_date' => 'Created Date',
244 'modified_date' => 'Modified Date',
245 'cancel_date' => 'Cancel Date',
246 'cancel_reason' => 'Cancellation Reason',
247 'end_date' => 'Recurring Contribution End Date',
248 'processor_id' => 'Processor ID',
249 'payment_token_id' => 'Payment Token ID',
250 'trxn_id' => 'Transaction ID',
251 'invoice_id' => 'Invoice ID',
252 'contribution_status_id' => 'Status',
253 'is_test:label' => 'Test',
254 'cycle_day' => 'Cycle Day',
255 'next_sched_contribution_date' => 'Next Scheduled Contribution Date',
256 'failure_count' => 'Number of Failures',
257 'failure_retry_date' => 'Retry Failed Attempt Date',
258 'auto_renew:label' => 'Auto Renew',
259 'payment_processor_id' => 'Payment Processor ID',
260 'financial_type_id' => 'Financial Type ID',
261 'payment_instrument_id' => 'Payment Method',
262 'is_email_receipt:label' => 'Send email Receipt?',
263 'frequency_unit:label' => 'Frequency Unit',
264 'frequency_unit:name' => 'Machine name: Frequency Unit',
265 'contribution_status_id:label' => 'Status',
266 'contribution_status_id:name' => 'Machine name: Status',
267 'payment_processor_id:label' => 'Payment Processor',
268 'payment_processor_id:name' => 'Machine name: Payment Processor',
269 'financial_type_id:label' => 'Financial Type',
270 'financial_type_id:name' => 'Machine name: Financial Type',
271 'payment_instrument_id:label' => 'Payment Method',
272 'payment_instrument_id:name' => 'Machine name: Payment Method',
277 * Get contributionRecur ID.
281 protected function getContributionRecurID(): int {
282 if (!isset($this->contributionRecur
)) {
283 $paymentProcessorID = $this->processorCreate();
284 $this->contributionRecur
= $this->callAPISuccess('ContributionRecur', 'create', [
285 'contact_id' => $this->getContactID(),
287 'is_email_receipt' => 1,
288 'start_date' => '2021-07-23 15:39:20',
289 'end_date' => '2021-07-26 18:07:20',
290 'cancel_date' => '2021-08-19 09:12:45',
291 'cancel_reason' => 'Because',
294 'frequency_unit' => 'year',
295 'frequency_interval' => 2,
296 'installments' => 24,
297 'payment_instrument_id' => 'Check',
298 'financial_type_id' => 'Member dues',
299 'processor_id' => 'abc',
300 'payment_processor_id' => $paymentProcessorID,
302 'invoice_id' => 'inv123',
304 'failure_retry_date' => '2020-01-03',
308 'payment_token_id' => $this->callAPISuccess('PaymentToken', 'create', [
309 'contact_id' => $this->getContactID(),
311 'payment_processor_id' => $paymentProcessorID,
315 return $this->contributionRecur
['id'];
319 * Get rendered output for contribution tokens.
323 protected function getExpectedContributionRecurTokenOutPut(): string {
324 return $this->getContributionRecurID() . '
330 July 23rd, 2021 3:39 PM
331 ' . CRM_Utils_Date
::customFormat($this->contributionRecur
['create_date']) . '
332 ' . CRM_Utils_Date
::customFormat($this->contributionRecur
['modified_date']) . '
333 August 19th, 2021 9:12 AM
335 July 26th, 2021 6:07 PM
345 January 3rd, 2020 12:00 AM
364 * Test that membership tokens are consistently rendered.
366 * @throws \API_Exception
368 public function testMembershipTokenConsistency(): void
{
369 $this->createLoggedInUser();
370 $this->restoreMembershipTypes();
371 $this->createCustomGroupWithFieldOfType(['extends' => 'Membership']);
372 $tokens = CRM_Core_SelectValues
::membershipTokens();
373 $expectedTokens = $this->getMembershipTokens();
374 $this->assertEquals($expectedTokens, $tokens);
375 $newStyleTokens = "\n{membership.status_id:label}\n{membership.membership_type_id:label}\n";
376 $tokenString = $newStyleTokens . implode("\n", array_keys($this->getMembershipTokens()));
378 $memberships = CRM_Utils_Token
::getMembershipTokenDetails([$this->getMembershipID()]);
379 $messageToken = CRM_Utils_Token
::getTokens($tokenString);
380 $tokenHtml = CRM_Utils_Token
::replaceEntityTokens('membership', $memberships[$this->getMembershipID()], $tokenString, $messageToken);
381 $this->assertEquals($this->getExpectedMembershipTokenOutput(), $tokenHtml);
383 // Custom fields work in the processor so test it....
384 $tokenString .= "\n{membership." . $this->getCustomFieldName('text') . '}';
385 // Now compare with scheduled reminder
386 $mut = new CiviMailUtils($this);
387 CRM_Utils_Time
::setTime('2007-01-22 15:00:00');
388 $this->callAPISuccess('action_schedule', 'create', [
393 'start_action_date' => 'membership_join_date',
394 'start_action_offset' => 1,
395 'start_action_condition' => 'after',
396 'start_action_unit' => 'day',
397 'body_html' => $tokenString,
399 $this->callAPISuccess('job', 'send_reminder', []);
400 $expected = $this->getExpectedMembershipTokenOutput();
401 // Unlike the legacy method custom fields are resolved by the processor.
402 $expected .= "\nmy field";
403 $mut->checkMailLog([$expected]);
405 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
406 'controller' => __CLASS__
,
408 'schema' => ['membershipId'],
410 $tokens = $tokenProcessor->listTokens();
411 // Add in custom tokens as token processor supports these.
412 $expectedTokens['{membership.custom_1}'] = 'Enter text here :: Group with field text';
413 $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokens);
414 $tokenProcessor->addMessage('html', $tokenString, 'text/plain');
415 $tokenProcessor->addRow(['membershipId' => $this->getMembershipID()]);
416 $tokenProcessor->evaluate();
417 $this->assertEquals($expected, $tokenProcessor->getRow(0)->render('html'));
422 * Get declared membership tokens.
426 public function getMembershipTokens(): array {
428 '{membership.id}' => 'Membership ID',
429 '{membership.status_id:label}' => 'Membership Status',
430 '{membership.membership_type_id:label}' => 'Membership Type',
431 '{membership.start_date}' => 'Membership Start Date',
432 '{membership.join_date}' => 'Membership Join Date',
433 '{membership.end_date}' => 'Membership End Date',
434 '{membership.fee}' => 'Membership Fee',
443 protected function getMembershipID(): int {
444 if (!isset($this->ids
['Membership'][0])) {
445 $this->ids
['Membership'][0] = $this->contactMembershipCreate([
446 'contact_id' => $this->getContactID(),
447 $this->getCustomFieldName('text') => 'my field',
450 return $this->ids
['Membership'][0];
454 * Get expected output from token parsing.
458 protected function getExpectedEventTokenOutput(): string {
462 October 21st, 2008 12:00 AM
463 October 23rd, 2008 12:00 AM
465 If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now
470 Emerald City, Maine 90210
473 ' . CRM_Utils_System
::url('civicrm/event/info', NULL, TRUE) . '&reset=1&id=1
474 ' . CRM_Utils_System
::url('civicrm/event/register', NULL, TRUE) . '&reset=1&id=1
480 * Get expected output from token parsing.
484 protected function getExpectedMembershipTokenOutput(): string {
498 * Test that membership tokens are consistently rendered.
500 * @throws \API_Exception
502 public function testParticipantTokenConsistency(): void
{
503 $this->createLoggedInUser();
504 $this->createCustomGroupWithFieldOfType(['extends' => 'Participant']);
505 $tokens = CRM_Core_SelectValues
::participantTokens();
506 $this->assertEquals($this->getParticipantTokens(), $tokens);
510 * Get declared participant tokens.
514 public function getParticipantTokens(): array {
516 '{participant.status_id}' => 'Status ID',
517 '{participant.role_id}' => 'Participant Role (ID)',
518 '{participant.register_date}' => 'Register date',
519 '{participant.source}' => 'Participant Source',
520 '{participant.fee_level}' => 'Fee level',
521 '{participant.fee_amount}' => 'Fee Amount',
522 '{participant.registered_by_id}' => 'Registered By Participant ID',
523 '{participant.transferred_to_contact_id}' => 'Transferred to Contact ID',
524 '{participant.role_id:label}' => 'Participant Role (label)',
525 '{participant.fee_label}' => 'Fee Label',
526 '{participant.' . $this->getCustomFieldName('text') . '}' => 'Enter text here :: Group with field text',
531 * Test that domain tokens are consistently rendered.
533 public function testDomainTokenConsistency(): void
{
534 $tokens = CRM_Core_SelectValues
::domainTokens();
535 $this->assertEquals($this->getDomainTokens(), $tokens);
536 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
537 'controller' => __CLASS__
,
540 $tokens['{domain.id}'] = 'Domain ID';
541 $tokens['{domain.description}'] = 'Domain Description';
542 $tokens['{domain.now}'] = 'Current time/date';
543 $this->assertEquals($tokens, $tokenProcessor->listTokens());
547 * @throws \API_Exception
548 * @throws \CRM_Core_Exception
550 public function testDomainNow(): void
{
551 putenv('TIME_FUNC=frozen');
552 CRM_Utils_Time
::setTime('2021-09-18 23:58:00');
554 'shortdate' => '09/18/2021',
555 '%B %Y' => 'September 2021',
557 foreach ($modifiers as $filter => $expected) {
558 $resolved = CRM_Core_BAO_MessageTemplate
::renderTemplate([
559 'messageTemplate' => [
560 'msg_text' => '{domain.now|crmDate:"' . $filter . '"}',
563 $this->assertEquals($expected, $resolved);
565 $resolved = CRM_Core_BAO_MessageTemplate
::renderTemplate([
566 'messageTemplate' => [
567 'msg_text' => '{domain.now}',
570 $this->assertEquals('September 18th, 2021 11:58 PM', $resolved);
572 // This example is malformed - no quotes
574 $resolved = CRM_Core_BAO_MessageTemplate
::renderTemplate([
575 'messageTemplate' => [
576 'msg_text' => '{domain.now|crmDate:shortdate}',
579 $this->fail("Expected unquoted parameter to fail");
581 catch (\CRM_Core_Exception
$e) {
582 $this->assertRegExp(';Malformed token param;', $e->getMessage());
587 * Get declared participant tokens.
591 public function getDomainTokens(): array {
593 '{domain.name}' => ts('Domain name'),
594 '{domain.address}' => ts('Domain (organization) address'),
595 '{domain.phone}' => ts('Domain (organization) phone'),
596 '{domain.email}' => 'Domain (organization) email',
597 '{domain.id}' => ts('Domain ID'),
598 '{domain.description}' => ts('Domain Description'),
599 '{domain.now}' => 'Current time/date',
604 * Test that domain tokens are consistently rendered.
606 public function testEventTokenConsistency(): void
{
607 $mut = new CiviMailUtils($this);
608 $this->setupParticipantScheduledReminder();
610 $tokens = CRM_Core_SelectValues
::eventTokens();
611 $this->assertEquals($this->getEventTokens(), $tokens);
612 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
613 'controller' => __CLASS__
,
615 'schema' => ['eventId'],
617 $this->assertEquals(array_merge($tokens, $this->getDomainTokens()), $tokenProcessor->listTokens());
619 $this->callAPISuccess('job', 'send_reminder', []);
620 $expected = $this->getExpectedEventTokenOutput();
621 $mut->checkMailLog([$expected]);
625 * Set up scheduled reminder for participants.
627 * @throws \API_Exception
629 public function setupParticipantScheduledReminder(): void
{
630 $this->createCustomGroupWithFieldOfType(['extends' => 'Event']);
631 $emailID = Email
::create()->setValues(['email' => 'event@example.com'])->execute()->first()['id'];
632 $addressID = Address
::create()->setValues([
633 'street_address' => '15 Walton St',
634 'supplemental_address_1' => 'up the road',
635 'city' => 'Emerald City',
636 'state_province_id:label' => 'Maine',
637 'postal_code' => 90210,
638 ])->execute()->first()['id'];
639 $phoneID = Phone
::create()->setValues(['phone' => '456 789'])->execute()->first()['id'];
641 $locationBlockID = LocBlock
::save(FALSE)->setRecords([
643 'email_id' => $emailID,
644 'address_id' => $addressID,
645 'phone_id' => $phoneID,
647 ])->execute()->first()['id'];
648 $event = $this->eventCreate([
649 'description' => 'event description',
650 $this->getCustomFieldName('text') => 'my field',
651 'loc_block_id' => $locationBlockID,
653 // Create an unrelated participant record so that the ids don't match.
654 // this prevents things working just because the id 'happens to be valid'
655 $this->participantCreate(['register_date' => '2020-01-01', 'event_id' => $event['id']]);
656 $this->participantCreate(['event_id' => $event['id'], 'fee_amount' => 50]);
657 CRM_Utils_Time
::setTime('2007-02-20 15:00:00');
658 $this->callAPISuccess('action_schedule', 'create', [
663 'start_action_date' => 'register_date',
664 'start_action_offset' => 1,
665 'start_action_condition' => 'after',
666 'start_action_unit' => 'day',
667 'body_html' => implode("\n", array_keys($this->getEventTokens())),
672 * Get expected event tokens.
676 protected function getEventTokens(): array {
678 '{event.event_id}' => 'Event ID',
679 '{event.title}' => 'Event Title',
680 '{event.start_date}' => 'Event Start Date',
681 '{event.end_date}' => 'Event End Date',
682 '{event.event_type}' => 'Event Type',
683 '{event.summary}' => 'Event Summary',
684 '{event.contact_email}' => 'Event Contact Email',
685 '{event.contact_phone}' => 'Event Contact Phone',
686 '{event.description}' => 'Event Description',
687 '{event.location}' => 'Event Location',
688 '{event.fee_amount}' => 'Event Fee',
689 '{event.info_url}' => 'Event Info URL',
690 '{event.registration_url}' => 'Event Registration URL',
691 '{event.balance}' => 'Event Balance',
692 '{event.' . $this->getCustomFieldName('text') . '}' => 'Enter text here :: Group with field text',