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
;
15 * CRM_Utils_TokenConsistencyTest
17 * Class for ensuring tokens have internal consistency.
23 class CRM_Utils_TokenConsistencyTest
extends CiviUnitTestCase
{
25 use CRMTraits_Custom_CustomDataTrait
;
35 * Recurring contribution.
39 protected $contributionRecur;
44 * @throws \API_Exception
45 * @throws \CRM_Core_Exception
47 public function tearDown(): void
{
48 $this->quickCleanup(['civicrm_case', 'civicrm_case_type'], TRUE);
53 * Test that case tokens are consistently rendered.
55 * @throws \CiviCRM_API3_Exception
57 public function testCaseTokenConsistency(): void
{
58 $this->createLoggedInUser();
59 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCase');
60 $this->createCustomGroupWithFieldOfType(['extends' => 'Case']);
61 $tokens = CRM_Core_SelectValues
::caseTokens();
62 $this->assertEquals($this->getCaseTokens(), $tokens);
63 $caseID = $this->getCaseID();
64 $tokenString = implode("\n", array_keys($this->getCaseTokens()));
65 $tokenHtml = CRM_Utils_Token
::replaceCaseTokens($caseID, $tokenString, ['case' => $this->getCaseTokenKeys()]);
66 $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml);
67 // Now do the same without passing in 'knownTokens'
68 $tokenHtml = CRM_Utils_Token
::replaceCaseTokens($caseID, $tokenString);
69 $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml);
71 // And check our deprecated tokens still work.
72 $tokenHtml = CRM_Utils_Token
::replaceCaseTokens($caseID, '{case.case_type_id} {case.status_id}');
73 $this->assertEquals('Housing Support Ongoing', $tokenHtml);
75 $additionalTokensFromProcessor = [
76 '{case.case_type_id}' => 'Case Type ID',
77 '{case.status_id}' => 'Case Status',
78 '{case.case_type_id:name}' => 'Machine name: Case Type',
79 '{case.status_id:name}' => 'Machine name: Case Status',
81 $expectedTokens = array_merge($this->getCaseTokens(), $additionalTokensFromProcessor);
83 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
84 'controller' => __CLASS__
,
86 'schema' => ['caseId'],
88 $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokenProcessor->listTokens());
89 $tokenProcessor->addRow([
90 'caseId' => $this->getCaseID(),
92 $tokenProcessor->addMessage('html', $tokenString, 'text/plain');
94 $tokenProcessor->evaluate();
95 foreach ($tokenProcessor->getRows() as $row) {
96 $text = $row->render('html');
98 $this->assertEquals($this->getExpectedCaseTokenOutput(), $text);
102 * Get expected output from token parsing.
106 protected function getExpectedCaseTokenOutput(): string {
115 ' . CRM_Utils_Date
::customFormat($this->case['created_date']) . '
116 ' . CRM_Utils_Date
::customFormat($this->case['modified_date']) . '
123 protected function getContactID(): int {
124 if (!isset($this->ids
['Contact'][0])) {
125 $this->ids
['Contact'][0] = $this->individualCreate();
127 return $this->ids
['Contact'][0];
131 * Get the keys for the case tokens.
135 public function getCaseTokenKeys(): array {
137 foreach (array_keys($this->getCaseTokens()) as $key) {
138 $return[] = substr($key, 6, -1);
144 * Get declared tokens.
148 public function getCaseTokens(): array {
150 '{case.id}' => 'Case ID',
151 '{case.case_type_id:label}' => 'Case Type',
152 '{case.subject}' => 'Case Subject',
153 '{case.start_date}' => 'Case Start Date',
154 '{case.end_date}' => 'Case End Date',
155 '{case.details}' => 'Details',
156 '{case.status_id:label}' => 'Case Status',
157 '{case.is_deleted:label}' => 'Case is in the Trash',
158 '{case.created_date}' => 'Created Date',
159 '{case.modified_date}' => 'Modified Date',
160 '{case.custom_1}' => 'Enter text here :: Group with field text',
169 protected function getCaseID(): int {
170 if (!isset($this->case)) {
171 $case_id = $this->callAPISuccess('Case', 'create', [
172 'case_type_id' => 'housing_support',
173 'activity_subject' => 'Case Subject',
174 'client_id' => $this->getContactID(),
176 'subject' => 'Case Subject',
177 'start_date' => '2021-07-23 15:39:20',
178 // Note end_date is inconsistent with status Ongoing but for the
179 // purposes of testing tokens is ok. Creating it with status Resolved
180 // then ignores our known fixed end date.
181 'end_date' => '2021-07-26 18:07:20',
183 'details' => 'case details',
184 'activity_details' => 'blah blah',
187 // Need to retrieve the case again because modified date might be updated a
188 // split-second later than the original return value because of activity
189 // triggers when the timeline is populated. The returned array from create
190 // is determined before that happens.
191 $this->case = $this->callAPISuccess('Case', 'getsingle', ['id' => $case_id]);
193 return $this->case['id'];
197 * Test that contribution recur tokens are consistently rendered.
199 public function testContributionRecurTokenConsistency(): void
{
200 $this->createLoggedInUser();
201 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
202 'controller' => __CLASS__
,
204 'schema' => ['contribution_recurId'],
206 $this->assertEquals(array_merge($this->getContributionRecurTokens(), $this->getDomainTokens()), $tokenProcessor->listTokens());
207 $tokenString = implode("\n", array_keys($this->getContributionRecurTokens()));
209 $tokenProcessor->addMessage('html', $tokenString, 'text/plain');
210 $tokenProcessor->addRow(['contribution_recurId' => $this->getContributionRecurID()]);
211 $tokenProcessor->evaluate();
212 $this->assertEquals($this->getExpectedContributionRecurTokenOutPut(), $tokenProcessor->getRow(0)->render('html'));
216 * Get the contribution recur tokens keyed by the token.
218 * e.g {contribution_recur.id}
222 protected function getContributionRecurTokens(): array {
224 foreach ($this->getContributionRecurTokensByField() as $key => $value) {
225 $return['{contribution_recur.' . $key . '}'] = $value;
230 protected function getContributionRecurTokensByField(): array {
232 'id' => 'Recurring Contribution ID',
233 'amount' => 'Amount',
234 'currency' => 'Currency',
235 'frequency_unit' => 'Frequency Unit',
236 'frequency_interval' => 'Interval (number of units)',
237 'installments' => 'Number of Installments',
238 'start_date' => 'Start Date',
239 'create_date' => 'Created Date',
240 'modified_date' => 'Modified Date',
241 'cancel_date' => 'Cancel Date',
242 'cancel_reason' => 'Cancellation Reason',
243 'end_date' => 'Recurring Contribution End Date',
244 'processor_id' => 'Processor ID',
245 'payment_token_id' => 'Payment Token ID',
246 'trxn_id' => 'Transaction ID',
247 'invoice_id' => 'Invoice ID',
248 'contribution_status_id' => 'Status',
249 'is_test:label' => 'Test',
250 'cycle_day' => 'Cycle Day',
251 'next_sched_contribution_date' => 'Next Scheduled Contribution Date',
252 'failure_count' => 'Number of Failures',
253 'failure_retry_date' => 'Retry Failed Attempt Date',
254 'auto_renew:label' => 'Auto Renew',
255 'payment_processor_id' => 'Payment Processor ID',
256 'financial_type_id' => 'Financial Type ID',
257 'payment_instrument_id' => 'Payment Method',
258 'is_email_receipt:label' => 'Send email Receipt?',
259 'frequency_unit:label' => 'Frequency Unit',
260 'frequency_unit:name' => 'Machine name: Frequency Unit',
261 'contribution_status_id:label' => 'Status',
262 'contribution_status_id:name' => 'Machine name: Status',
263 'payment_processor_id:label' => 'Payment Processor',
264 'payment_processor_id:name' => 'Machine name: Payment Processor',
265 'financial_type_id:label' => 'Financial Type',
266 'financial_type_id:name' => 'Machine name: Financial Type',
267 'payment_instrument_id:label' => 'Payment Method',
268 'payment_instrument_id:name' => 'Machine name: Payment Method',
273 * Get contributionRecur ID.
277 protected function getContributionRecurID(): int {
278 if (!isset($this->contributionRecur
)) {
279 $paymentProcessorID = $this->processorCreate();
280 $this->contributionRecur
= $this->callAPISuccess('ContributionRecur', 'create', [
281 'contact_id' => $this->getContactID(),
283 'is_email_receipt' => 1,
284 'start_date' => '2021-07-23 15:39:20',
285 'end_date' => '2021-07-26 18:07:20',
286 'cancel_date' => '2021-08-19 09:12:45',
287 'cancel_reason' => 'Because',
290 'frequency_unit' => 'year',
291 'frequency_interval' => 2,
292 'installments' => 24,
293 'payment_instrument_id' => 'Check',
294 'financial_type_id' => 'Member dues',
295 'processor_id' => 'abc',
296 'payment_processor_id' => $paymentProcessorID,
298 'invoice_id' => 'inv123',
300 'failure_retry_date' => '2020-01-03',
304 'payment_token_id' => $this->callAPISuccess('PaymentToken', 'create', [
305 'contact_id' => $this->getContactID(),
307 'payment_processor_id' => $paymentProcessorID,
311 return $this->contributionRecur
['id'];
315 * Get rendered output for contribution tokens.
319 protected function getExpectedContributionRecurTokenOutPut(): string {
320 return $this->getContributionRecurID() . '
326 July 23rd, 2021 3:39 PM
327 ' . CRM_Utils_Date
::customFormat($this->contributionRecur
['create_date']) . '
328 ' . CRM_Utils_Date
::customFormat($this->contributionRecur
['modified_date']) . '
329 August 19th, 2021 9:12 AM
331 July 26th, 2021 6:07 PM
341 January 3rd, 2020 12:00 AM
360 * Test that membership tokens are consistently rendered.
362 * @throws \API_Exception
364 public function testMembershipTokenConsistency(): void
{
365 $this->createLoggedInUser();
366 $this->restoreMembershipTypes();
367 $this->createCustomGroupWithFieldOfType(['extends' => 'Membership']);
368 $tokens = CRM_Core_SelectValues
::membershipTokens();
369 $expectedTokens = $this->getMembershipTokens();
370 $this->assertEquals($expectedTokens, $tokens);
371 $newStyleTokens = "\n{membership.status_id:label}\n{membership.membership_type_id:label}\n";
372 $tokenString = $newStyleTokens . implode("\n", array_keys($this->getMembershipTokens()));
374 $memberships = CRM_Utils_Token
::getMembershipTokenDetails([$this->getMembershipID()]);
375 $messageToken = CRM_Utils_Token
::getTokens($tokenString);
376 $tokenHtml = CRM_Utils_Token
::replaceEntityTokens('membership', $memberships[$this->getMembershipID()], $tokenString, $messageToken);
377 $this->assertEquals($this->getExpectedMembershipTokenOutput(), $tokenHtml);
379 // Custom fields work in the processor so test it....
380 $tokenString .= "\n{membership." . $this->getCustomFieldName('text') . '}';
381 // Now compare with scheduled reminder
382 $mut = new CiviMailUtils($this);
383 CRM_Utils_Time
::setTime('2007-01-22 15:00:00');
384 $this->callAPISuccess('action_schedule', 'create', [
389 'start_action_date' => 'membership_join_date',
390 'start_action_offset' => 1,
391 'start_action_condition' => 'after',
392 'start_action_unit' => 'day',
393 'body_html' => $tokenString,
395 $this->callAPISuccess('job', 'send_reminder', []);
396 $expected = $this->getExpectedMembershipTokenOutput();
397 // Unlike the legacy method custom fields are resolved by the processor.
398 $expected .= "\nmy field";
399 $mut->checkMailLog([$expected]);
401 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
402 'controller' => __CLASS__
,
404 'schema' => ['membershipId'],
406 $tokens = $tokenProcessor->listTokens();
407 // Add in custom tokens as token processor supports these.
408 $expectedTokens['{membership.custom_1}'] = 'Enter text here :: Group with field text';
409 $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokens);
410 $tokenProcessor->addMessage('html', $tokenString, 'text/plain');
411 $tokenProcessor->addRow(['membershipId' => $this->getMembershipID()]);
412 $tokenProcessor->evaluate();
413 $this->assertEquals($expected, $tokenProcessor->getRow(0)->render('html'));
418 * Get declared membership tokens.
422 public function getMembershipTokens(): array {
424 '{membership.id}' => 'Membership ID',
425 '{membership.status_id:label}' => 'Membership Status',
426 '{membership.membership_type_id:label}' => 'Membership Type',
427 '{membership.start_date}' => 'Membership Start Date',
428 '{membership.join_date}' => 'Membership Join Date',
429 '{membership.end_date}' => 'Membership End Date',
430 '{membership.fee}' => 'Membership Fee',
439 protected function getMembershipID(): int {
440 if (!isset($this->ids
['Membership'][0])) {
441 $this->ids
['Membership'][0] = $this->contactMembershipCreate([
442 'contact_id' => $this->getContactID(),
443 $this->getCustomFieldName('text') => 'my field',
446 return $this->ids
['Membership'][0];
450 * Get expected output from token parsing.
454 protected function getExpectedMembershipTokenOutput(): string {
468 * Test that membership tokens are consistently rendered.
470 * @throws \API_Exception
472 public function testParticipantTokenConsistency(): void
{
473 $this->createLoggedInUser();
474 $this->createCustomGroupWithFieldOfType(['extends' => 'Participant']);
475 $tokens = CRM_Core_SelectValues
::participantTokens();
476 $this->assertEquals($this->getParticipantTokens(), $tokens);
480 * Get declared participant tokens.
484 public function getParticipantTokens(): array {
486 '{participant.status_id}' => 'Status ID',
487 '{participant.role_id}' => 'Participant Role (ID)',
488 '{participant.register_date}' => 'Register date',
489 '{participant.source}' => 'Participant Source',
490 '{participant.fee_level}' => 'Fee level',
491 '{participant.fee_amount}' => 'Fee Amount',
492 '{participant.registered_by_id}' => 'Registered By Participant ID',
493 '{participant.transferred_to_contact_id}' => 'Transferred to Contact ID',
494 '{participant.role_id:label}' => 'Participant Role (label)',
495 '{participant.fee_label}' => 'Fee Label',
496 '{participant.' . $this->getCustomFieldName('text') . '}' => 'Enter text here :: Group with field text',
501 * Test that domain tokens are consistently rendered.
503 public function testDomainTokenConsistency(): void
{
504 $tokens = CRM_Core_SelectValues
::domainTokens();
505 $this->assertEquals($this->getDomainTokens(), $tokens);
506 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
507 'controller' => __CLASS__
,
510 $tokens['{domain.id}'] = 'Domain ID';
511 $tokens['{domain.description}'] = 'Domain Description';
512 $this->assertEquals($tokens, $tokenProcessor->listTokens());
516 * Get declared participant tokens.
520 public function getDomainTokens(): array {
522 '{domain.name}' => ts('Domain name'),
523 '{domain.address}' => ts('Domain (organization) address'),
524 '{domain.phone}' => ts('Domain (organization) phone'),
525 '{domain.email}' => 'Domain (organization) email',
526 '{domain.id}' => ts('Domain ID'),
527 '{domain.description}' => ts('Domain Description'),
532 * Test that domain tokens are consistently rendered.
534 public function testEventTokenConsistency(): void
{
535 $tokens = CRM_Core_SelectValues
::eventTokens();
536 $this->assertEquals($this->getEventTokens(), $tokens);
537 $tokenProcessor = new TokenProcessor(\Civi
::dispatcher(), [
538 'controller' => __CLASS__
,
540 'schema' => ['eventId'],
542 $this->assertEquals(array_merge($tokens, $this->getDomainTokens()), $tokenProcessor->listTokens());
546 * Get expected event tokens.
550 protected function getEventTokens(): array {
552 '{event.event_id}' => 'Event ID',
553 '{event.title}' => 'Event Title',
554 '{event.start_date}' => 'Event Start Date',
555 '{event.end_date}' => 'Event End Date',
556 '{event.event_type}' => 'Event Type',
557 '{event.summary}' => 'Event Summary',
558 '{event.contact_email}' => 'Event Contact Email',
559 '{event.contact_phone}' => 'Event Contact Phone',
560 '{event.description}' => 'Event Description',
561 '{event.location}' => 'Event Location',
562 '{event.fee_amount}' => 'Event Fee',
563 '{event.info_url}' => 'Event Info URL',
564 '{event.registration_url}' => 'Event Registration URL',
565 '{event.balance}' => 'Event Balance',