Commit | Line | Data |
---|---|---|
d0ce76fd EM |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | Copyright CiviCRM LLC. All rights reserved. | | |
5 | | | | |
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 | +--------------------------------------------------------------------+ | |
10 | */ | |
11 | ||
0f4031da | 12 | use Civi\Token\TokenProcessor; |
f399fbd8 EM |
13 | use Civi\Api4\LocBlock; |
14 | use Civi\Api4\Email; | |
15 | use Civi\Api4\Phone; | |
16 | use Civi\Api4\Address; | |
0f4031da | 17 | |
d0ce76fd EM |
18 | /** |
19 | * CRM_Utils_TokenConsistencyTest | |
20 | * | |
21 | * Class for ensuring tokens have internal consistency. | |
22 | * | |
23 | * @group Tokens | |
24 | * | |
25 | * @group headless | |
26 | */ | |
27 | class CRM_Utils_TokenConsistencyTest extends CiviUnitTestCase { | |
28 | ||
29 | use CRMTraits_Custom_CustomDataTrait; | |
30 | ||
31 | /** | |
32 | * Created case. | |
33 | * | |
34 | * @var array | |
35 | */ | |
36 | protected $case; | |
37 | ||
0f4031da EM |
38 | /** |
39 | * Recurring contribution. | |
40 | * | |
41 | * @var array | |
42 | */ | |
43 | protected $contributionRecur; | |
44 | ||
d0ce76fd EM |
45 | /** |
46 | * Post test cleanup. | |
47 | * | |
48 | * @throws \API_Exception | |
49 | * @throws \CRM_Core_Exception | |
50 | */ | |
51 | public function tearDown(): void { | |
d568dbe0 | 52 | $this->quickCleanup(['civicrm_case', 'civicrm_case_type'], TRUE); |
d0ce76fd EM |
53 | parent::tearDown(); |
54 | } | |
55 | ||
56 | /** | |
57 | * Test that case tokens are consistently rendered. | |
58 | * | |
d0ce76fd EM |
59 | * @throws \CiviCRM_API3_Exception |
60 | */ | |
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(); | |
78ffc4d7 EM |
68 | $tokenString = implode("\n", array_keys($this->getCaseTokens())); |
69 | $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, $tokenString, ['case' => $this->getCaseTokenKeys()]); | |
d0ce76fd EM |
70 | $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml); |
71 | // Now do the same without passing in 'knownTokens' | |
78ffc4d7 | 72 | $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, $tokenString); |
d0ce76fd | 73 | $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml); |
2f5c0408 EM |
74 | |
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); | |
78ffc4d7 EM |
78 | |
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', | |
84 | ]; | |
85 | $expectedTokens = array_merge($this->getCaseTokens(), $additionalTokensFromProcessor); | |
86 | ||
87 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
88 | 'controller' => __CLASS__, | |
89 | 'smarty' => FALSE, | |
90 | 'schema' => ['caseId'], | |
91 | ]); | |
3c78698e | 92 | $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); |
78ffc4d7 EM |
93 | $tokenProcessor->addRow([ |
94 | 'caseId' => $this->getCaseID(), | |
95 | ]); | |
96 | $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); | |
97 | ||
98 | $tokenProcessor->evaluate(); | |
99 | foreach ($tokenProcessor->getRows() as $row) { | |
100 | $text = $row->render('html'); | |
101 | } | |
102 | $this->assertEquals($this->getExpectedCaseTokenOutput(), $text); | |
d0ce76fd EM |
103 | } |
104 | ||
105 | /** | |
106 | * Get expected output from token parsing. | |
107 | * | |
108 | * @return string | |
109 | */ | |
110 | protected function getExpectedCaseTokenOutput(): string { | |
111 | return '1 | |
112 | Housing Support | |
113 | Case Subject | |
114 | July 23rd, 2021 | |
115 | July 26th, 2021 | |
116 | case details | |
117 | Ongoing | |
118 | No | |
78ffc4d7 EM |
119 | ' . CRM_Utils_Date::customFormat($this->case['created_date']) . ' |
120 | ' . CRM_Utils_Date::customFormat($this->case['modified_date']) . ' | |
d0ce76fd EM |
121 | '; |
122 | } | |
123 | ||
124 | /** | |
125 | * @return int | |
126 | */ | |
127 | protected function getContactID(): int { | |
128 | if (!isset($this->ids['Contact'][0])) { | |
129 | $this->ids['Contact'][0] = $this->individualCreate(); | |
130 | } | |
131 | return $this->ids['Contact'][0]; | |
132 | } | |
133 | ||
134 | /** | |
135 | * Get the keys for the case tokens. | |
136 | * | |
137 | * @return array | |
138 | */ | |
139 | public function getCaseTokenKeys(): array { | |
140 | $return = []; | |
141 | foreach (array_keys($this->getCaseTokens()) as $key) { | |
142 | $return[] = substr($key, 6, -1); | |
143 | } | |
144 | return $return; | |
145 | } | |
146 | ||
147 | /** | |
148 | * Get declared tokens. | |
149 | * | |
150 | * @return string[] | |
151 | */ | |
152 | public function getCaseTokens(): array { | |
153 | return [ | |
154 | '{case.id}' => 'Case ID', | |
2f5c0408 | 155 | '{case.case_type_id:label}' => 'Case Type', |
d0ce76fd EM |
156 | '{case.subject}' => 'Case Subject', |
157 | '{case.start_date}' => 'Case Start Date', | |
158 | '{case.end_date}' => 'Case End Date', | |
159 | '{case.details}' => 'Details', | |
2f5c0408 | 160 | '{case.status_id:label}' => 'Case Status', |
78ffc4d7 | 161 | '{case.is_deleted:label}' => 'Case is in the Trash', |
d0ce76fd EM |
162 | '{case.created_date}' => 'Created Date', |
163 | '{case.modified_date}' => 'Modified Date', | |
164 | '{case.custom_1}' => 'Enter text here :: Group with field text', | |
165 | ]; | |
166 | } | |
167 | ||
168 | /** | |
169 | * Get case ID. | |
170 | * | |
171 | * @return int | |
172 | */ | |
173 | protected function getCaseID(): int { | |
174 | if (!isset($this->case)) { | |
a0fb3068 | 175 | $case_id = $this->callAPISuccess('Case', 'create', [ |
d0ce76fd EM |
176 | 'case_type_id' => 'housing_support', |
177 | 'activity_subject' => 'Case Subject', | |
178 | 'client_id' => $this->getContactID(), | |
179 | 'status_id' => 1, | |
180 | 'subject' => 'Case Subject', | |
181 | 'start_date' => '2021-07-23 15:39:20', | |
a0fb3068 | 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. | |
d0ce76fd EM |
185 | 'end_date' => '2021-07-26 18:07:20', |
186 | 'medium_id' => 2, | |
187 | 'details' => 'case details', | |
188 | 'activity_details' => 'blah blah', | |
189 | 'sequential' => 1, | |
a0fb3068 | 190 | ])['id']; |
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]); | |
d0ce76fd EM |
196 | } |
197 | return $this->case['id']; | |
198 | } | |
199 | ||
0f4031da EM |
200 | /** |
201 | * Test that contribution recur tokens are consistently rendered. | |
202 | */ | |
203 | public function testContributionRecurTokenConsistency(): void { | |
204 | $this->createLoggedInUser(); | |
205 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
206 | 'controller' => __CLASS__, | |
207 | 'smarty' => FALSE, | |
208 | 'schema' => ['contribution_recurId'], | |
209 | ]); | |
3c78698e | 210 | $this->assertEquals(array_merge($this->getContributionRecurTokens(), $this->getDomainTokens()), $tokenProcessor->listTokens()); |
0f4031da EM |
211 | $tokenString = implode("\n", array_keys($this->getContributionRecurTokens())); |
212 | ||
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')); | |
217 | } | |
218 | ||
219 | /** | |
220 | * Get the contribution recur tokens keyed by the token. | |
221 | * | |
222 | * e.g {contribution_recur.id} | |
223 | * | |
224 | * @return array | |
225 | */ | |
226 | protected function getContributionRecurTokens(): array { | |
227 | $return = []; | |
228 | foreach ($this->getContributionRecurTokensByField() as $key => $value) { | |
229 | $return['{contribution_recur.' . $key . '}'] = $value; | |
230 | } | |
231 | return $return; | |
232 | } | |
233 | ||
234 | protected function getContributionRecurTokensByField(): array { | |
235 | return [ | |
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', | |
97ca72e4 | 253 | 'is_test:label' => 'Test', |
0f4031da EM |
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', | |
97ca72e4 | 258 | 'auto_renew:label' => 'Auto Renew', |
0f4031da EM |
259 | 'payment_processor_id' => 'Payment Processor ID', |
260 | 'financial_type_id' => 'Financial Type ID', | |
261 | 'payment_instrument_id' => 'Payment Method', | |
97ca72e4 | 262 | 'is_email_receipt:label' => 'Send email Receipt?', |
0f4031da EM |
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', | |
273 | ]; | |
274 | } | |
275 | ||
276 | /** | |
277 | * Get contributionRecur ID. | |
278 | * | |
279 | * @return int | |
280 | */ | |
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(), | |
286 | 'status_id' => 1, | |
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', | |
292 | 'amount' => 5990.99, | |
293 | 'currency' => 'EUR', | |
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, | |
301 | 'trxn_id' => 123, | |
302 | 'invoice_id' => 'inv123', | |
303 | 'sequential' => 1, | |
304 | 'failure_retry_date' => '2020-01-03', | |
305 | 'auto_renew' => 1, | |
306 | 'cycle_day' => '15', | |
307 | 'is_test' => TRUE, | |
308 | 'payment_token_id' => $this->callAPISuccess('PaymentToken', 'create', [ | |
309 | 'contact_id' => $this->getContactID(), | |
310 | 'token' => 456, | |
311 | 'payment_processor_id' => $paymentProcessorID, | |
312 | ])['id'], | |
313 | ])['values'][0]; | |
314 | } | |
315 | return $this->contributionRecur['id']; | |
316 | } | |
317 | ||
318 | /** | |
319 | * Get rendered output for contribution tokens. | |
320 | * | |
321 | * @return string | |
322 | */ | |
323 | protected function getExpectedContributionRecurTokenOutPut(): string { | |
324 | return $this->getContributionRecurID() . ' | |
325 | € 5,990.99 | |
326 | EUR | |
327 | year | |
328 | 2 | |
329 | 24 | |
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 | |
334 | Because | |
335 | July 26th, 2021 6:07 PM | |
336 | abc | |
337 | 1 | |
338 | 123 | |
339 | inv123 | |
340 | 2 | |
97ca72e4 | 341 | Yes |
0f4031da EM |
342 | 15 |
343 | ||
344 | 0 | |
345 | January 3rd, 2020 12:00 AM | |
97ca72e4 | 346 | Yes |
0f4031da EM |
347 | 1 |
348 | 2 | |
349 | 4 | |
97ca72e4 | 350 | Yes |
0f4031da EM |
351 | year |
352 | year | |
353 | Pending Label** | |
354 | Pending | |
97ca72e4 EM |
355 | Dummy (test) |
356 | Dummy (test) | |
0f4031da EM |
357 | Member Dues |
358 | Member Dues | |
359 | Check | |
360 | Check'; | |
361 | } | |
362 | ||
d41a5d53 EM |
363 | /** |
364 | * Test that membership tokens are consistently rendered. | |
365 | * | |
366 | * @throws \API_Exception | |
367 | */ | |
368 | public function testMembershipTokenConsistency(): void { | |
369 | $this->createLoggedInUser(); | |
370 | $this->restoreMembershipTypes(); | |
371 | $this->createCustomGroupWithFieldOfType(['extends' => 'Membership']); | |
372 | $tokens = CRM_Core_SelectValues::membershipTokens(); | |
d568dbe0 EM |
373 | $expectedTokens = $this->getMembershipTokens(); |
374 | $this->assertEquals($expectedTokens, $tokens); | |
d41a5d53 EM |
375 | $newStyleTokens = "\n{membership.status_id:label}\n{membership.membership_type_id:label}\n"; |
376 | $tokenString = $newStyleTokens . implode("\n", array_keys($this->getMembershipTokens())); | |
b024d6a1 | 377 | |
d41a5d53 EM |
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); | |
dd2f879a | 382 | |
b024d6a1 EM |
383 | // Custom fields work in the processor so test it.... |
384 | $tokenString .= "\n{membership." . $this->getCustomFieldName('text') . '}'; | |
dd2f879a EM |
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', [ | |
389 | 'title' => 'job', | |
390 | 'subject' => 'job', | |
391 | 'entity_value' => 1, | |
392 | 'mapping_id' => 4, | |
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, | |
398 | ]); | |
399 | $this->callAPISuccess('job', 'send_reminder', []); | |
d568dbe0 EM |
400 | $expected = $this->getExpectedMembershipTokenOutput(); |
401 | // Unlike the legacy method custom fields are resolved by the processor. | |
402 | $expected .= "\nmy field"; | |
403 | $mut->checkMailLog([$expected]); | |
404 | ||
405 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
406 | 'controller' => __CLASS__, | |
407 | 'smarty' => FALSE, | |
408 | 'schema' => ['membershipId'], | |
409 | ]); | |
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'; | |
3c78698e | 413 | $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokens); |
d568dbe0 EM |
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')); | |
418 | ||
d41a5d53 EM |
419 | } |
420 | ||
421 | /** | |
422 | * Get declared membership tokens. | |
423 | * | |
424 | * @return string[] | |
425 | */ | |
426 | public function getMembershipTokens(): array { | |
427 | return [ | |
428 | '{membership.id}' => 'Membership ID', | |
eac0a5bf EM |
429 | '{membership.status_id:label}' => 'Membership Status', |
430 | '{membership.membership_type_id:label}' => 'Membership Type', | |
d41a5d53 EM |
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', | |
435 | ]; | |
436 | } | |
437 | ||
438 | /** | |
439 | * Get case ID. | |
440 | * | |
441 | * @return int | |
442 | */ | |
443 | protected function getMembershipID(): int { | |
444 | if (!isset($this->ids['Membership'][0])) { | |
b024d6a1 EM |
445 | $this->ids['Membership'][0] = $this->contactMembershipCreate([ |
446 | 'contact_id' => $this->getContactID(), | |
447 | $this->getCustomFieldName('text') => 'my field', | |
448 | ]); | |
d41a5d53 EM |
449 | } |
450 | return $this->ids['Membership'][0]; | |
451 | } | |
452 | ||
f399fbd8 EM |
453 | /** |
454 | * Get expected output from token parsing. | |
455 | * | |
456 | * @return string | |
457 | */ | |
458 | protected function getExpectedEventTokenOutput(): string { | |
459 | return ' | |
460 | 1 | |
461 | Annual CiviCRM meet | |
462 | October 21st, 2008 12:00 AM | |
463 | October 23rd, 2008 12:00 AM | |
464 | Conference | |
465 | If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now | |
466 | event@example.com | |
467 | 456 789 | |
468 | event description | |
469 | 15 Walton St | |
470 | Emerald City, Maine 90210 | |
471 | ||
472 | $ 50.00 | |
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 | |
475 | ||
476 | my field'; | |
477 | } | |
478 | ||
d41a5d53 EM |
479 | /** |
480 | * Get expected output from token parsing. | |
481 | * | |
482 | * @return string | |
483 | */ | |
484 | protected function getExpectedMembershipTokenOutput(): string { | |
485 | return ' | |
486 | Expired | |
487 | General | |
488 | 1 | |
489 | Expired | |
490 | General | |
491 | January 21st, 2007 | |
492 | January 21st, 2007 | |
493 | December 21st, 2007 | |
494 | 100.00'; | |
495 | } | |
496 | ||
1ed50dc6 EM |
497 | /** |
498 | * Test that membership tokens are consistently rendered. | |
499 | * | |
500 | * @throws \API_Exception | |
501 | */ | |
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); | |
507 | } | |
508 | ||
509 | /** | |
3c78698e | 510 | * Get declared participant tokens. |
1ed50dc6 EM |
511 | * |
512 | * @return string[] | |
513 | */ | |
514 | public function getParticipantTokens(): array { | |
515 | return [ | |
b7472bd6 EM |
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', | |
1ed50dc6 | 523 | '{participant.transferred_to_contact_id}' => 'Transferred to Contact ID', |
b7472bd6 | 524 | '{participant.role_id:label}' => 'Participant Role (label)', |
1ed50dc6 | 525 | '{participant.fee_label}' => 'Fee Label', |
1ed50dc6 EM |
526 | '{participant.' . $this->getCustomFieldName('text') . '}' => 'Enter text here :: Group with field text', |
527 | ]; | |
528 | } | |
529 | ||
3c78698e EM |
530 | /** |
531 | * Test that domain tokens are consistently rendered. | |
532 | */ | |
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__, | |
538 | 'smarty' => FALSE, | |
539 | ]); | |
540 | $tokens['{domain.id}'] = 'Domain ID'; | |
541 | $tokens['{domain.description}'] = 'Domain Description'; | |
dfe53edd | 542 | $tokens['{domain.now}'] = 'Current time/date'; |
3c78698e EM |
543 | $this->assertEquals($tokens, $tokenProcessor->listTokens()); |
544 | } | |
545 | ||
defba8ff EM |
546 | /** |
547 | * @throws \API_Exception | |
548 | * @throws \CRM_Core_Exception | |
549 | */ | |
550 | public function testDomainNow(): void { | |
551 | putenv('TIME_FUNC=frozen'); | |
5e62af3d | 552 | CRM_Utils_Time::setTime('2021-09-18 23:58:00'); |
44dd64f0 EM |
553 | $modifiers = [ |
554 | 'shortdate' => '09/18/2021', | |
f85e1a4b | 555 | '%B %Y' => 'September 2021', |
44dd64f0 EM |
556 | ]; |
557 | foreach ($modifiers as $filter => $expected) { | |
558 | $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([ | |
559 | 'messageTemplate' => [ | |
f85e1a4b | 560 | 'msg_text' => '{domain.now|crmDate:"' . $filter . '"}', |
44dd64f0 EM |
561 | ], |
562 | ])['text']; | |
f85e1a4b | 563 | $this->assertEquals($expected, $resolved); |
44dd64f0 | 564 | } |
defba8ff EM |
565 | $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([ |
566 | 'messageTemplate' => [ | |
567 | 'msg_text' => '{domain.now}', | |
568 | ], | |
569 | ])['text']; | |
570 | $this->assertEquals('September 18th, 2021 11:58 PM', $resolved); | |
f85e1a4b TO |
571 | |
572 | // This example is malformed - no quotes | |
573 | try { | |
574 | $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([ | |
575 | 'messageTemplate' => [ | |
576 | 'msg_text' => '{domain.now|crmDate:shortdate}', | |
577 | ], | |
578 | ])['text']; | |
579 | $this->fail("Expected unquoted parameter to fail"); | |
580 | } | |
581 | catch (\CRM_Core_Exception $e) { | |
582 | $this->assertRegExp(';Malformed token param;', $e->getMessage()); | |
583 | } | |
defba8ff EM |
584 | } |
585 | ||
3c78698e EM |
586 | /** |
587 | * Get declared participant tokens. | |
588 | * | |
589 | * @return string[] | |
590 | */ | |
591 | public function getDomainTokens(): array { | |
592 | return [ | |
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'), | |
defba8ff | 599 | '{domain.now}' => 'Current time/date', |
3c78698e EM |
600 | ]; |
601 | } | |
602 | ||
ce971869 EM |
603 | /** |
604 | * Test that domain tokens are consistently rendered. | |
605 | */ | |
606 | public function testEventTokenConsistency(): void { | |
f399fbd8 EM |
607 | $mut = new CiviMailUtils($this); |
608 | $this->setupParticipantScheduledReminder(); | |
609 | ||
ce971869 EM |
610 | $tokens = CRM_Core_SelectValues::eventTokens(); |
611 | $this->assertEquals($this->getEventTokens(), $tokens); | |
612 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
613 | 'controller' => __CLASS__, | |
614 | 'smarty' => FALSE, | |
615 | 'schema' => ['eventId'], | |
616 | ]); | |
617 | $this->assertEquals(array_merge($tokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); | |
f399fbd8 EM |
618 | |
619 | $this->callAPISuccess('job', 'send_reminder', []); | |
620 | $expected = $this->getExpectedEventTokenOutput(); | |
621 | $mut->checkMailLog([$expected]); | |
622 | } | |
623 | ||
624 | /** | |
625 | * Set up scheduled reminder for participants. | |
626 | * | |
627 | * @throws \API_Exception | |
628 | */ | |
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']; | |
640 | ||
641 | $locationBlockID = LocBlock::save(FALSE)->setRecords([ | |
642 | [ | |
643 | 'email_id' => $emailID, | |
644 | 'address_id' => $addressID, | |
645 | 'phone_id' => $phoneID, | |
646 | ], | |
647 | ])->execute()->first()['id']; | |
648 | $event = $this->eventCreate([ | |
649 | 'description' => 'event description', | |
650 | $this->getCustomFieldName('text') => 'my field', | |
651 | 'loc_block_id' => $locationBlockID, | |
652 | ]); | |
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', [ | |
659 | 'title' => 'job', | |
660 | 'subject' => 'job', | |
661 | 'entity_value' => 1, | |
662 | 'mapping_id' => 2, | |
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())), | |
668 | ]); | |
ce971869 EM |
669 | } |
670 | ||
671 | /** | |
672 | * Get expected event tokens. | |
673 | * | |
674 | * @return string[] | |
675 | */ | |
676 | protected function getEventTokens(): array { | |
677 | return [ | |
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', | |
f399fbd8 | 692 | '{event.' . $this->getCustomFieldName('text') . '}' => 'Enter text here :: Group with field text', |
ce971869 EM |
693 | ]; |
694 | } | |
695 | ||
d0ce76fd | 696 | } |