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. | |
d0ce76fd EM |
47 | */ |
48 | public function tearDown(): void { | |
e80f2261 | 49 | $this->quickCleanup(['civicrm_case', 'civicrm_case_type', 'civicrm_participant', 'civicrm_event'], TRUE); |
d0ce76fd EM |
50 | parent::tearDown(); |
51 | } | |
52 | ||
53 | /** | |
54 | * Test that case tokens are consistently rendered. | |
55 | * | |
d0ce76fd EM |
56 | * @throws \CiviCRM_API3_Exception |
57 | */ | |
58 | public function testCaseTokenConsistency(): void { | |
59 | $this->createLoggedInUser(); | |
60 | CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase'); | |
61 | $this->createCustomGroupWithFieldOfType(['extends' => 'Case']); | |
62 | $tokens = CRM_Core_SelectValues::caseTokens(); | |
63 | $this->assertEquals($this->getCaseTokens(), $tokens); | |
64 | $caseID = $this->getCaseID(); | |
044c0ad1 | 65 | $tokenString = $this->getTokenString(array_keys($this->getCaseTokens())); |
78ffc4d7 | 66 | $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, $tokenString, ['case' => $this->getCaseTokenKeys()]); |
d0ce76fd EM |
67 | $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml); |
68 | // Now do the same without passing in 'knownTokens' | |
78ffc4d7 | 69 | $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, $tokenString); |
d0ce76fd | 70 | $this->assertEquals($this->getExpectedCaseTokenOutput(), $tokenHtml); |
2f5c0408 EM |
71 | |
72 | // And check our deprecated tokens still work. | |
73 | $tokenHtml = CRM_Utils_Token::replaceCaseTokens($caseID, '{case.case_type_id} {case.status_id}'); | |
74 | $this->assertEquals('Housing Support Ongoing', $tokenHtml); | |
78ffc4d7 EM |
75 | |
76 | $additionalTokensFromProcessor = [ | |
77 | '{case.case_type_id}' => 'Case Type ID', | |
78 | '{case.status_id}' => 'Case Status', | |
79 | '{case.case_type_id:name}' => 'Machine name: Case Type', | |
80 | '{case.status_id:name}' => 'Machine name: Case Status', | |
81 | ]; | |
82 | $expectedTokens = array_merge($this->getCaseTokens(), $additionalTokensFromProcessor); | |
83 | ||
84 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
85 | 'controller' => __CLASS__, | |
86 | 'smarty' => FALSE, | |
87 | 'schema' => ['caseId'], | |
88 | ]); | |
3c78698e | 89 | $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); |
78ffc4d7 EM |
90 | $tokenProcessor->addRow([ |
91 | 'caseId' => $this->getCaseID(), | |
92 | ]); | |
93 | $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); | |
94 | ||
95 | $tokenProcessor->evaluate(); | |
96 | foreach ($tokenProcessor->getRows() as $row) { | |
97 | $text = $row->render('html'); | |
98 | } | |
99 | $this->assertEquals($this->getExpectedCaseTokenOutput(), $text); | |
d0ce76fd EM |
100 | } |
101 | ||
102 | /** | |
103 | * Get expected output from token parsing. | |
104 | * | |
105 | * @return string | |
106 | */ | |
107 | protected function getExpectedCaseTokenOutput(): string { | |
044c0ad1 EM |
108 | return 'case.id :1 |
109 | case.case_type_id:label :Housing Support | |
110 | case.subject :Case Subject | |
111 | case.start_date :July 23rd, 2021 | |
112 | case.end_date :July 26th, 2021 | |
113 | case.details :case details | |
114 | case.status_id:label :Ongoing | |
115 | case.is_deleted:label :No | |
116 | case.created_date :' . CRM_Utils_Date::customFormat($this->case['created_date']) . ' | |
117 | case.modified_date :' . CRM_Utils_Date::customFormat($this->case['modified_date']) . ' | |
118 | case.custom_1 :' . ' | |
d0ce76fd EM |
119 | '; |
120 | } | |
121 | ||
122 | /** | |
123 | * @return int | |
124 | */ | |
125 | protected function getContactID(): int { | |
126 | if (!isset($this->ids['Contact'][0])) { | |
127 | $this->ids['Contact'][0] = $this->individualCreate(); | |
128 | } | |
129 | return $this->ids['Contact'][0]; | |
130 | } | |
131 | ||
132 | /** | |
133 | * Get the keys for the case tokens. | |
134 | * | |
135 | * @return array | |
136 | */ | |
137 | public function getCaseTokenKeys(): array { | |
138 | $return = []; | |
139 | foreach (array_keys($this->getCaseTokens()) as $key) { | |
140 | $return[] = substr($key, 6, -1); | |
141 | } | |
142 | return $return; | |
143 | } | |
144 | ||
145 | /** | |
146 | * Get declared tokens. | |
147 | * | |
148 | * @return string[] | |
149 | */ | |
150 | public function getCaseTokens(): array { | |
151 | return [ | |
152 | '{case.id}' => 'Case ID', | |
2f5c0408 | 153 | '{case.case_type_id:label}' => 'Case Type', |
d0ce76fd EM |
154 | '{case.subject}' => 'Case Subject', |
155 | '{case.start_date}' => 'Case Start Date', | |
156 | '{case.end_date}' => 'Case End Date', | |
157 | '{case.details}' => 'Details', | |
2f5c0408 | 158 | '{case.status_id:label}' => 'Case Status', |
78ffc4d7 | 159 | '{case.is_deleted:label}' => 'Case is in the Trash', |
d0ce76fd EM |
160 | '{case.created_date}' => 'Created Date', |
161 | '{case.modified_date}' => 'Modified Date', | |
162 | '{case.custom_1}' => 'Enter text here :: Group with field text', | |
163 | ]; | |
164 | } | |
165 | ||
166 | /** | |
167 | * Get case ID. | |
168 | * | |
169 | * @return int | |
170 | */ | |
171 | protected function getCaseID(): int { | |
172 | if (!isset($this->case)) { | |
a0fb3068 | 173 | $case_id = $this->callAPISuccess('Case', 'create', [ |
d0ce76fd EM |
174 | 'case_type_id' => 'housing_support', |
175 | 'activity_subject' => 'Case Subject', | |
176 | 'client_id' => $this->getContactID(), | |
177 | 'status_id' => 1, | |
178 | 'subject' => 'Case Subject', | |
179 | 'start_date' => '2021-07-23 15:39:20', | |
a0fb3068 | 180 | // Note end_date is inconsistent with status Ongoing but for the |
181 | // purposes of testing tokens is ok. Creating it with status Resolved | |
182 | // then ignores our known fixed end date. | |
d0ce76fd EM |
183 | 'end_date' => '2021-07-26 18:07:20', |
184 | 'medium_id' => 2, | |
185 | 'details' => 'case details', | |
186 | 'activity_details' => 'blah blah', | |
187 | 'sequential' => 1, | |
a0fb3068 | 188 | ])['id']; |
189 | // Need to retrieve the case again because modified date might be updated a | |
190 | // split-second later than the original return value because of activity | |
191 | // triggers when the timeline is populated. The returned array from create | |
192 | // is determined before that happens. | |
193 | $this->case = $this->callAPISuccess('Case', 'getsingle', ['id' => $case_id]); | |
d0ce76fd EM |
194 | } |
195 | return $this->case['id']; | |
196 | } | |
197 | ||
0f4031da EM |
198 | /** |
199 | * Test that contribution recur tokens are consistently rendered. | |
200 | */ | |
201 | public function testContributionRecurTokenConsistency(): void { | |
202 | $this->createLoggedInUser(); | |
203 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
204 | 'controller' => __CLASS__, | |
205 | 'smarty' => FALSE, | |
206 | 'schema' => ['contribution_recurId'], | |
207 | ]); | |
3c78698e | 208 | $this->assertEquals(array_merge($this->getContributionRecurTokens(), $this->getDomainTokens()), $tokenProcessor->listTokens()); |
044c0ad1 | 209 | $tokenString = $this->getTokenString(array_keys($this->getContributionRecurTokens())); |
0f4031da EM |
210 | |
211 | $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); | |
212 | $tokenProcessor->addRow(['contribution_recurId' => $this->getContributionRecurID()]); | |
213 | $tokenProcessor->evaluate(); | |
214 | $this->assertEquals($this->getExpectedContributionRecurTokenOutPut(), $tokenProcessor->getRow(0)->render('html')); | |
215 | } | |
216 | ||
e9841a51 EM |
217 | /** |
218 | * Get tokens that are not advertised via listTokens. | |
219 | * | |
220 | * @return string[] | |
221 | */ | |
222 | public function getUnadvertisedTokens(): array { | |
223 | return [ | |
224 | '{membership.status_id}' => 'Status ID', | |
225 | '{membership.membership_type_id}' => 'Membership Type ID', | |
226 | '{membership.status_id:name}' => 'Machine name: Status', | |
227 | '{membership.membership_type_id:name}' => 'Machine name: Membership Type', | |
228 | ]; | |
229 | } | |
230 | ||
0f4031da EM |
231 | /** |
232 | * Get the contribution recur tokens keyed by the token. | |
233 | * | |
234 | * e.g {contribution_recur.id} | |
235 | * | |
236 | * @return array | |
237 | */ | |
238 | protected function getContributionRecurTokens(): array { | |
239 | $return = []; | |
240 | foreach ($this->getContributionRecurTokensByField() as $key => $value) { | |
241 | $return['{contribution_recur.' . $key . '}'] = $value; | |
242 | } | |
243 | return $return; | |
244 | } | |
245 | ||
246 | protected function getContributionRecurTokensByField(): array { | |
247 | return [ | |
248 | 'id' => 'Recurring Contribution ID', | |
249 | 'amount' => 'Amount', | |
250 | 'currency' => 'Currency', | |
251 | 'frequency_unit' => 'Frequency Unit', | |
252 | 'frequency_interval' => 'Interval (number of units)', | |
253 | 'installments' => 'Number of Installments', | |
254 | 'start_date' => 'Start Date', | |
255 | 'create_date' => 'Created Date', | |
256 | 'modified_date' => 'Modified Date', | |
257 | 'cancel_date' => 'Cancel Date', | |
258 | 'cancel_reason' => 'Cancellation Reason', | |
259 | 'end_date' => 'Recurring Contribution End Date', | |
260 | 'processor_id' => 'Processor ID', | |
261 | 'payment_token_id' => 'Payment Token ID', | |
262 | 'trxn_id' => 'Transaction ID', | |
263 | 'invoice_id' => 'Invoice ID', | |
264 | 'contribution_status_id' => 'Status', | |
97ca72e4 | 265 | 'is_test:label' => 'Test', |
0f4031da EM |
266 | 'cycle_day' => 'Cycle Day', |
267 | 'next_sched_contribution_date' => 'Next Scheduled Contribution Date', | |
268 | 'failure_count' => 'Number of Failures', | |
269 | 'failure_retry_date' => 'Retry Failed Attempt Date', | |
97ca72e4 | 270 | 'auto_renew:label' => 'Auto Renew', |
0f4031da EM |
271 | 'payment_processor_id' => 'Payment Processor ID', |
272 | 'financial_type_id' => 'Financial Type ID', | |
273 | 'payment_instrument_id' => 'Payment Method', | |
97ca72e4 | 274 | 'is_email_receipt:label' => 'Send email Receipt?', |
0f4031da EM |
275 | 'frequency_unit:label' => 'Frequency Unit', |
276 | 'frequency_unit:name' => 'Machine name: Frequency Unit', | |
277 | 'contribution_status_id:label' => 'Status', | |
278 | 'contribution_status_id:name' => 'Machine name: Status', | |
279 | 'payment_processor_id:label' => 'Payment Processor', | |
280 | 'payment_processor_id:name' => 'Machine name: Payment Processor', | |
281 | 'financial_type_id:label' => 'Financial Type', | |
282 | 'financial_type_id:name' => 'Machine name: Financial Type', | |
283 | 'payment_instrument_id:label' => 'Payment Method', | |
284 | 'payment_instrument_id:name' => 'Machine name: Payment Method', | |
285 | ]; | |
286 | } | |
287 | ||
288 | /** | |
289 | * Get contributionRecur ID. | |
290 | * | |
291 | * @return int | |
292 | */ | |
293 | protected function getContributionRecurID(): int { | |
294 | if (!isset($this->contributionRecur)) { | |
295 | $paymentProcessorID = $this->processorCreate(); | |
296 | $this->contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', [ | |
297 | 'contact_id' => $this->getContactID(), | |
298 | 'status_id' => 1, | |
299 | 'is_email_receipt' => 1, | |
300 | 'start_date' => '2021-07-23 15:39:20', | |
301 | 'end_date' => '2021-07-26 18:07:20', | |
302 | 'cancel_date' => '2021-08-19 09:12:45', | |
34795e7a | 303 | 'next_sched_contribution_date' => '2021-09-08', |
0f4031da EM |
304 | 'cancel_reason' => 'Because', |
305 | 'amount' => 5990.99, | |
306 | 'currency' => 'EUR', | |
307 | 'frequency_unit' => 'year', | |
308 | 'frequency_interval' => 2, | |
309 | 'installments' => 24, | |
310 | 'payment_instrument_id' => 'Check', | |
311 | 'financial_type_id' => 'Member dues', | |
312 | 'processor_id' => 'abc', | |
313 | 'payment_processor_id' => $paymentProcessorID, | |
314 | 'trxn_id' => 123, | |
315 | 'invoice_id' => 'inv123', | |
316 | 'sequential' => 1, | |
317 | 'failure_retry_date' => '2020-01-03', | |
318 | 'auto_renew' => 1, | |
319 | 'cycle_day' => '15', | |
320 | 'is_test' => TRUE, | |
321 | 'payment_token_id' => $this->callAPISuccess('PaymentToken', 'create', [ | |
322 | 'contact_id' => $this->getContactID(), | |
323 | 'token' => 456, | |
324 | 'payment_processor_id' => $paymentProcessorID, | |
325 | ])['id'], | |
326 | ])['values'][0]; | |
327 | } | |
328 | return $this->contributionRecur['id']; | |
329 | } | |
330 | ||
331 | /** | |
332 | * Get rendered output for contribution tokens. | |
333 | * | |
334 | * @return string | |
335 | */ | |
336 | protected function getExpectedContributionRecurTokenOutPut(): string { | |
044c0ad1 EM |
337 | return 'contribution_recur.id :' . $this->getContributionRecurID() . ' |
338 | contribution_recur.amount :€ 5,990.99 | |
339 | contribution_recur.currency :EUR | |
340 | contribution_recur.frequency_unit :year | |
341 | contribution_recur.frequency_interval :2 | |
342 | contribution_recur.installments :24 | |
343 | contribution_recur.start_date :July 23rd, 2021 3:39 PM | |
344 | contribution_recur.create_date :' . CRM_Utils_Date::customFormat($this->contributionRecur['create_date']) . ' | |
345 | contribution_recur.modified_date :' . CRM_Utils_Date::customFormat($this->contributionRecur['modified_date']) . ' | |
346 | contribution_recur.cancel_date :August 19th, 2021 9:12 AM | |
347 | contribution_recur.cancel_reason :Because | |
348 | contribution_recur.end_date :July 26th, 2021 6:07 PM | |
349 | contribution_recur.processor_id :abc | |
350 | contribution_recur.payment_token_id :1 | |
351 | contribution_recur.trxn_id :123 | |
352 | contribution_recur.invoice_id :inv123 | |
353 | contribution_recur.contribution_status_id :2 | |
354 | contribution_recur.is_test:label :Yes | |
355 | contribution_recur.cycle_day :15 | |
356 | contribution_recur.next_sched_contribution_date :September 8th, 2021 | |
357 | contribution_recur.failure_count :0 | |
358 | contribution_recur.failure_retry_date :January 3rd, 2020 | |
359 | contribution_recur.auto_renew:label :Yes | |
360 | contribution_recur.payment_processor_id :1 | |
361 | contribution_recur.financial_type_id :2 | |
362 | contribution_recur.payment_instrument_id :4 | |
363 | contribution_recur.is_email_receipt:label :Yes | |
364 | contribution_recur.frequency_unit:label :year | |
365 | contribution_recur.frequency_unit:name :year | |
366 | contribution_recur.contribution_status_id:label :Pending Label** | |
367 | contribution_recur.contribution_status_id:name :Pending | |
368 | contribution_recur.payment_processor_id:label :Dummy (test) | |
369 | contribution_recur.payment_processor_id:name :Dummy (test) | |
370 | contribution_recur.financial_type_id:label :Member Dues | |
371 | contribution_recur.financial_type_id:name :Member Dues | |
372 | contribution_recur.payment_instrument_id:label :Check | |
373 | contribution_recur.payment_instrument_id:name :Check | |
374 | '; | |
375 | ||
0f4031da EM |
376 | } |
377 | ||
d41a5d53 EM |
378 | /** |
379 | * Test that membership tokens are consistently rendered. | |
380 | * | |
381 | * @throws \API_Exception | |
382 | */ | |
383 | public function testMembershipTokenConsistency(): void { | |
384 | $this->createLoggedInUser(); | |
385 | $this->restoreMembershipTypes(); | |
386 | $this->createCustomGroupWithFieldOfType(['extends' => 'Membership']); | |
387 | $tokens = CRM_Core_SelectValues::membershipTokens(); | |
d568dbe0 EM |
388 | $expectedTokens = $this->getMembershipTokens(); |
389 | $this->assertEquals($expectedTokens, $tokens); | |
d41a5d53 EM |
390 | $newStyleTokens = "\n{membership.status_id:label}\n{membership.membership_type_id:label}\n"; |
391 | $tokenString = $newStyleTokens . implode("\n", array_keys($this->getMembershipTokens())); | |
b024d6a1 | 392 | |
d41a5d53 EM |
393 | $memberships = CRM_Utils_Token::getMembershipTokenDetails([$this->getMembershipID()]); |
394 | $messageToken = CRM_Utils_Token::getTokens($tokenString); | |
395 | $tokenHtml = CRM_Utils_Token::replaceEntityTokens('membership', $memberships[$this->getMembershipID()], $tokenString, $messageToken); | |
396 | $this->assertEquals($this->getExpectedMembershipTokenOutput(), $tokenHtml); | |
dd2f879a | 397 | |
b024d6a1 EM |
398 | // Custom fields work in the processor so test it.... |
399 | $tokenString .= "\n{membership." . $this->getCustomFieldName('text') . '}'; | |
dd2f879a EM |
400 | // Now compare with scheduled reminder |
401 | $mut = new CiviMailUtils($this); | |
402 | CRM_Utils_Time::setTime('2007-01-22 15:00:00'); | |
403 | $this->callAPISuccess('action_schedule', 'create', [ | |
404 | 'title' => 'job', | |
405 | 'subject' => 'job', | |
406 | 'entity_value' => 1, | |
407 | 'mapping_id' => 4, | |
408 | 'start_action_date' => 'membership_join_date', | |
409 | 'start_action_offset' => 1, | |
410 | 'start_action_condition' => 'after', | |
411 | 'start_action_unit' => 'day', | |
412 | 'body_html' => $tokenString, | |
413 | ]); | |
414 | $this->callAPISuccess('job', 'send_reminder', []); | |
d568dbe0 EM |
415 | $expected = $this->getExpectedMembershipTokenOutput(); |
416 | // Unlike the legacy method custom fields are resolved by the processor. | |
417 | $expected .= "\nmy field"; | |
418 | $mut->checkMailLog([$expected]); | |
419 | ||
420 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
421 | 'controller' => __CLASS__, | |
422 | 'smarty' => FALSE, | |
423 | 'schema' => ['membershipId'], | |
424 | ]); | |
425 | $tokens = $tokenProcessor->listTokens(); | |
426 | // Add in custom tokens as token processor supports these. | |
427 | $expectedTokens['{membership.custom_1}'] = 'Enter text here :: Group with field text'; | |
e9841a51 EM |
428 | // Add in unadvertised tokens - for now they are included. |
429 | $expectedTokens = array_merge($expectedTokens, $this->getUnadvertisedTokens()); | |
3c78698e | 430 | $this->assertEquals(array_merge($expectedTokens, $this->getDomainTokens()), $tokens); |
d568dbe0 EM |
431 | $tokenProcessor->addMessage('html', $tokenString, 'text/plain'); |
432 | $tokenProcessor->addRow(['membershipId' => $this->getMembershipID()]); | |
433 | $tokenProcessor->evaluate(); | |
434 | $this->assertEquals($expected, $tokenProcessor->getRow(0)->render('html')); | |
435 | ||
d41a5d53 EM |
436 | } |
437 | ||
438 | /** | |
439 | * Get declared membership tokens. | |
440 | * | |
441 | * @return string[] | |
442 | */ | |
443 | public function getMembershipTokens(): array { | |
444 | return [ | |
445 | '{membership.id}' => 'Membership ID', | |
044c0ad1 | 446 | '{membership.status_id:label}' => 'Status', |
eac0a5bf | 447 | '{membership.membership_type_id:label}' => 'Membership Type', |
d41a5d53 | 448 | '{membership.start_date}' => 'Membership Start Date', |
044c0ad1 EM |
449 | '{membership.join_date}' => 'Member Since', |
450 | '{membership.end_date}' => 'Membership Expiration Date', | |
d41a5d53 EM |
451 | '{membership.fee}' => 'Membership Fee', |
452 | ]; | |
453 | } | |
454 | ||
455 | /** | |
456 | * Get case ID. | |
457 | * | |
458 | * @return int | |
459 | */ | |
460 | protected function getMembershipID(): int { | |
461 | if (!isset($this->ids['Membership'][0])) { | |
b024d6a1 EM |
462 | $this->ids['Membership'][0] = $this->contactMembershipCreate([ |
463 | 'contact_id' => $this->getContactID(), | |
464 | $this->getCustomFieldName('text') => 'my field', | |
465 | ]); | |
d41a5d53 EM |
466 | } |
467 | return $this->ids['Membership'][0]; | |
468 | } | |
469 | ||
f399fbd8 EM |
470 | /** |
471 | * Get expected output from token parsing. | |
472 | * | |
473 | * @return string | |
474 | */ | |
e80f2261 | 475 | protected function getExpectedParticipantTokenOutput(): string { |
044c0ad1 EM |
476 | return 'participant.status_id :2 |
477 | participant.role_id :1 | |
478 | participant.register_date :February 19th, 2007 | |
479 | participant.source :Wimbeldon | |
480 | participant.fee_level :steep | |
481 | participant.fee_amount :$ 50.00 | |
482 | participant.registered_by_id : | |
483 | participant.transferred_to_contact_id : | |
484 | participant.role_id:label :Attendee | |
485 | participant.balance : | |
486 | participant.custom_2 :99999 | |
487 | participant.id :2 | |
488 | participant.fee_currency :USD | |
489 | participant.discount_amount : | |
490 | participant.status_id:label :Attended | |
491 | participant.status_id:name :Attended | |
492 | participant.role_id:name :Attendee | |
493 | participant.is_test:label :No | |
494 | participant.must_wait : | |
e80f2261 EM |
495 | '; |
496 | } | |
497 | ||
498 | /** | |
499 | * Get expected output from token parsing. | |
500 | * | |
501 | * @return string | |
502 | */ | |
503 | protected function getExpectedEventTokenOutput(): string { | |
044c0ad1 EM |
504 | return 'event.id :' . $this->ids['event'][0] . ' |
505 | event.title :Annual CiviCRM meet | |
506 | event.start_date :October 21st, 2008 | |
507 | event.end_date :October 23rd, 2008 | |
508 | event.event_type_id:label :Conference | |
509 | event.summary :If you have any CiviCRM related issues or want to track where CiviCRM is heading, Sign up now | |
510 | event.contact_email :event@example.com | |
511 | event.contact_phone :456 789 | |
512 | event.description :event description | |
513 | event.location :15 Walton St | |
f399fbd8 EM |
514 | Emerald City, Maine 90210 |
515 | ||
044c0ad1 EM |
516 | event.info_url :' . CRM_Utils_System::url('civicrm/event/info', NULL, TRUE) . '&reset=1&id=1 |
517 | event.registration_url :' . CRM_Utils_System::url('civicrm/event/register', NULL, TRUE) . '&reset=1&id=1 | |
518 | event.custom_1 :my field | |
519 | '; | |
f399fbd8 EM |
520 | } |
521 | ||
d41a5d53 EM |
522 | /** |
523 | * Get expected output from token parsing. | |
524 | * | |
525 | * @return string | |
526 | */ | |
527 | protected function getExpectedMembershipTokenOutput(): string { | |
528 | return ' | |
529 | Expired | |
530 | General | |
531 | 1 | |
532 | Expired | |
533 | General | |
534 | January 21st, 2007 | |
535 | January 21st, 2007 | |
536 | December 21st, 2007 | |
537 | 100.00'; | |
538 | } | |
539 | ||
1ed50dc6 EM |
540 | /** |
541 | * Test that membership tokens are consistently rendered. | |
542 | * | |
543 | * @throws \API_Exception | |
544 | */ | |
545 | public function testParticipantTokenConsistency(): void { | |
546 | $this->createLoggedInUser(); | |
e80f2261 EM |
547 | $this->setupParticipantScheduledReminder(); |
548 | ||
1ed50dc6 EM |
549 | $tokens = CRM_Core_SelectValues::participantTokens(); |
550 | $this->assertEquals($this->getParticipantTokens(), $tokens); | |
e80f2261 EM |
551 | |
552 | $mut = new CiviMailUtils($this); | |
553 | ||
554 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
555 | 'controller' => __CLASS__, | |
556 | 'smarty' => FALSE, | |
557 | 'schema' => ['participantId'], | |
558 | ]); | |
559 | $this->assertEquals(array_merge($tokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); | |
560 | ||
561 | $this->callAPISuccess('job', 'send_reminder', []); | |
562 | $expected = $this->getExpectedParticipantTokenOutput(); | |
563 | $mut->checkMailLog([$expected]); | |
564 | ||
044c0ad1 | 565 | $tokenProcessor->addMessage('html', $this->getTokenString(array_keys($this->getParticipantTokens())), 'text/plain'); |
e80f2261 EM |
566 | $tokenProcessor->addRow(['participantId' => $this->ids['participant'][0]]); |
567 | $tokenProcessor->evaluate(); | |
568 | $this->assertEquals($expected, $tokenProcessor->getRow(0)->render('html')); | |
569 | ||
1ed50dc6 EM |
570 | } |
571 | ||
572 | /** | |
3c78698e | 573 | * Get declared participant tokens. |
1ed50dc6 EM |
574 | * |
575 | * @return string[] | |
576 | */ | |
577 | public function getParticipantTokens(): array { | |
578 | return [ | |
b7472bd6 | 579 | '{participant.status_id}' => 'Status ID', |
e80f2261 | 580 | '{participant.role_id}' => 'Participant Role ID', |
b7472bd6 EM |
581 | '{participant.register_date}' => 'Register date', |
582 | '{participant.source}' => 'Participant Source', | |
583 | '{participant.fee_level}' => 'Fee level', | |
584 | '{participant.fee_amount}' => 'Fee Amount', | |
585 | '{participant.registered_by_id}' => 'Registered By Participant ID', | |
1ed50dc6 | 586 | '{participant.transferred_to_contact_id}' => 'Transferred to Contact ID', |
e80f2261 EM |
587 | '{participant.role_id:label}' => 'Participant Role', |
588 | '{participant.balance}' => 'Event Balance', | |
589 | '{participant.' . $this->getCustomFieldName('participant_int') . '}' => 'Enter integer here :: participant_Group with field int', | |
590 | '{participant.id}' => 'Participant ID', | |
591 | '{participant.fee_currency}' => 'Fee Currency', | |
592 | '{participant.discount_amount}' => 'Discount Amount', | |
593 | '{participant.status_id:label}' => 'Status', | |
594 | '{participant.status_id:name}' => 'Machine name: Status', | |
595 | '{participant.role_id:name}' => 'Machine name: Participant Role', | |
596 | '{participant.is_test:label}' => 'Test', | |
597 | '{participant.must_wait}' => 'Must Wait on List', | |
1ed50dc6 EM |
598 | ]; |
599 | } | |
600 | ||
3c78698e EM |
601 | /** |
602 | * Test that domain tokens are consistently rendered. | |
603 | */ | |
604 | public function testDomainTokenConsistency(): void { | |
605 | $tokens = CRM_Core_SelectValues::domainTokens(); | |
606 | $this->assertEquals($this->getDomainTokens(), $tokens); | |
607 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
608 | 'controller' => __CLASS__, | |
609 | 'smarty' => FALSE, | |
610 | ]); | |
611 | $tokens['{domain.id}'] = 'Domain ID'; | |
612 | $tokens['{domain.description}'] = 'Domain Description'; | |
dfe53edd | 613 | $tokens['{domain.now}'] = 'Current time/date'; |
3c78698e EM |
614 | $this->assertEquals($tokens, $tokenProcessor->listTokens()); |
615 | } | |
616 | ||
defba8ff EM |
617 | /** |
618 | * @throws \API_Exception | |
619 | * @throws \CRM_Core_Exception | |
620 | */ | |
621 | public function testDomainNow(): void { | |
622 | putenv('TIME_FUNC=frozen'); | |
5e62af3d | 623 | CRM_Utils_Time::setTime('2021-09-18 23:58:00'); |
44dd64f0 EM |
624 | $modifiers = [ |
625 | 'shortdate' => '09/18/2021', | |
f85e1a4b | 626 | '%B %Y' => 'September 2021', |
44dd64f0 EM |
627 | ]; |
628 | foreach ($modifiers as $filter => $expected) { | |
629 | $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([ | |
630 | 'messageTemplate' => [ | |
f85e1a4b | 631 | 'msg_text' => '{domain.now|crmDate:"' . $filter . '"}', |
44dd64f0 EM |
632 | ], |
633 | ])['text']; | |
f85e1a4b | 634 | $this->assertEquals($expected, $resolved); |
44dd64f0 | 635 | } |
defba8ff EM |
636 | $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([ |
637 | 'messageTemplate' => [ | |
638 | 'msg_text' => '{domain.now}', | |
639 | ], | |
640 | ])['text']; | |
641 | $this->assertEquals('September 18th, 2021 11:58 PM', $resolved); | |
f85e1a4b TO |
642 | |
643 | // This example is malformed - no quotes | |
644 | try { | |
645 | $resolved = CRM_Core_BAO_MessageTemplate::renderTemplate([ | |
646 | 'messageTemplate' => [ | |
647 | 'msg_text' => '{domain.now|crmDate:shortdate}', | |
648 | ], | |
649 | ])['text']; | |
e80f2261 | 650 | $this->fail('Expected unquoted parameter to fail'); |
f85e1a4b TO |
651 | } |
652 | catch (\CRM_Core_Exception $e) { | |
653 | $this->assertRegExp(';Malformed token param;', $e->getMessage()); | |
654 | } | |
defba8ff EM |
655 | } |
656 | ||
3c78698e EM |
657 | /** |
658 | * Get declared participant tokens. | |
659 | * | |
660 | * @return string[] | |
661 | */ | |
662 | public function getDomainTokens(): array { | |
663 | return [ | |
664 | '{domain.name}' => ts('Domain name'), | |
665 | '{domain.address}' => ts('Domain (organization) address'), | |
666 | '{domain.phone}' => ts('Domain (organization) phone'), | |
667 | '{domain.email}' => 'Domain (organization) email', | |
668 | '{domain.id}' => ts('Domain ID'), | |
669 | '{domain.description}' => ts('Domain Description'), | |
defba8ff | 670 | '{domain.now}' => 'Current time/date', |
3c78698e EM |
671 | ]; |
672 | } | |
673 | ||
ce971869 | 674 | /** |
e80f2261 | 675 | * Test that event tokens are consistently rendered. |
34795e7a EM |
676 | * |
677 | * @throws \API_Exception | |
ce971869 EM |
678 | */ |
679 | public function testEventTokenConsistency(): void { | |
f399fbd8 EM |
680 | $mut = new CiviMailUtils($this); |
681 | $this->setupParticipantScheduledReminder(); | |
682 | ||
ce971869 | 683 | $tokens = CRM_Core_SelectValues::eventTokens(); |
e9841a51 EM |
684 | $unadvertisedTokens = [ |
685 | '{event.event_type_id}' => 'Event Type', | |
686 | '{event.event_type_id:name}' => 'Machine name: Event Type', | |
687 | ]; | |
688 | $this->assertEquals(array_merge($this->getEventTokens(), $unadvertisedTokens), $tokens); | |
ce971869 EM |
689 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ |
690 | 'controller' => __CLASS__, | |
691 | 'smarty' => FALSE, | |
692 | 'schema' => ['eventId'], | |
693 | ]); | |
694 | $this->assertEquals(array_merge($tokens, $this->getDomainTokens()), $tokenProcessor->listTokens()); | |
f399fbd8 | 695 | |
044c0ad1 | 696 | $expectedEventString = $this->getExpectedEventTokenOutput(); |
e80f2261 | 697 | $this->callAPISuccess('job', 'send_reminder', []); |
044c0ad1 EM |
698 | $expectedParticipantString = $this->getExpectedParticipantTokenOutput(); |
699 | $toCheck = array_merge(explode("\n", $expectedEventString), explode("\n", $expectedParticipantString)); | |
700 | $toCheck[] = $expectedEventString; | |
701 | $toCheck[] = $expectedParticipantString; | |
702 | $mut->checkMailLog($toCheck); | |
703 | $tokens = array_keys($this->getEventTokens()); | |
704 | $html = $this->getTokenString($tokens); | |
705 | $tokenProcessor->addMessage('html', $html, 'text/plain'); | |
e80f2261 EM |
706 | $tokenProcessor->addRow(['eventId' => $this->ids['event'][0]]); |
707 | $tokenProcessor->evaluate(); | |
044c0ad1 | 708 | $this->assertEquals($expectedEventString, $tokenProcessor->getRow(0)->render('html')); |
e80f2261 EM |
709 | } |
710 | ||
711 | /** | |
712 | * Test that event tokens work absent participant tokens. | |
713 | * | |
714 | * @throws \API_Exception | |
715 | */ | |
716 | public function testEventTokenConsistencyNoParticipantTokens(): void { | |
717 | $mut = new CiviMailUtils($this); | |
718 | $this->setupParticipantScheduledReminder(FALSE); | |
719 | ||
f399fbd8 EM |
720 | $this->callAPISuccess('job', 'send_reminder', []); |
721 | $expected = $this->getExpectedEventTokenOutput(); | |
044c0ad1 EM |
722 | // Checking these individually is easier to decipher discrepancies |
723 | // but we also want to check in entirety. | |
724 | $toCheck = explode("\n", $expected); | |
725 | $toCheck[] = $expected; | |
726 | $mut->checkMailLog($toCheck); | |
e80f2261 EM |
727 | |
728 | $tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [ | |
729 | 'controller' => __CLASS__, | |
730 | 'smarty' => FALSE, | |
731 | 'schema' => ['eventId'], | |
732 | ]); | |
044c0ad1 | 733 | $html = $this->getTokenString(array_keys($this->getEventTokens())); |
e80f2261 | 734 | |
044c0ad1 | 735 | $tokenProcessor->addMessage('html', $html, 'text/plain'); |
e80f2261 EM |
736 | $tokenProcessor->addRow(['eventId' => $this->ids['event'][0]]); |
737 | $tokenProcessor->evaluate(); | |
738 | $this->assertEquals($expected, $tokenProcessor->getRow(0)->render('html')); | |
739 | ||
f399fbd8 EM |
740 | } |
741 | ||
742 | /** | |
743 | * Set up scheduled reminder for participants. | |
744 | * | |
745 | * @throws \API_Exception | |
746 | */ | |
e80f2261 | 747 | public function setupParticipantScheduledReminder($includeParticipant = TRUE): void { |
f399fbd8 | 748 | $this->createCustomGroupWithFieldOfType(['extends' => 'Event']); |
e80f2261 | 749 | $this->createCustomGroupWithFieldOfType(['extends' => 'Participant'], 'int', 'participant_'); |
044c0ad1 EM |
750 | $html = ''; |
751 | $tokens = array_keys($this->getEventTokens()); | |
e80f2261 | 752 | if ($includeParticipant) { |
044c0ad1 | 753 | $tokens = array_keys(array_merge($this->getEventTokens(), $this->getParticipantTokens())); |
e80f2261 | 754 | } |
044c0ad1 | 755 | $html = $this->getTokenString($tokens); |
f399fbd8 EM |
756 | $emailID = Email::create()->setValues(['email' => 'event@example.com'])->execute()->first()['id']; |
757 | $addressID = Address::create()->setValues([ | |
758 | 'street_address' => '15 Walton St', | |
759 | 'supplemental_address_1' => 'up the road', | |
760 | 'city' => 'Emerald City', | |
761 | 'state_province_id:label' => 'Maine', | |
762 | 'postal_code' => 90210, | |
763 | ])->execute()->first()['id']; | |
764 | $phoneID = Phone::create()->setValues(['phone' => '456 789'])->execute()->first()['id']; | |
765 | ||
766 | $locationBlockID = LocBlock::save(FALSE)->setRecords([ | |
767 | [ | |
768 | 'email_id' => $emailID, | |
769 | 'address_id' => $addressID, | |
770 | 'phone_id' => $phoneID, | |
771 | ], | |
772 | ])->execute()->first()['id']; | |
e80f2261 | 773 | $this->ids['event'][0] = $this->eventCreate([ |
f399fbd8 EM |
774 | 'description' => 'event description', |
775 | $this->getCustomFieldName('text') => 'my field', | |
776 | 'loc_block_id' => $locationBlockID, | |
e80f2261 | 777 | ])['id']; |
f399fbd8 EM |
778 | // Create an unrelated participant record so that the ids don't match. |
779 | // this prevents things working just because the id 'happens to be valid' | |
e80f2261 EM |
780 | $this->participantCreate(['register_date' => '2020-01-01', 'event_id' => $this->ids['event'][0]]); |
781 | $this->ids['participant'][0] = $this->participantCreate([ | |
782 | 'event_id' => $this->ids['event'][0], | |
783 | 'fee_amount' => 50, | |
784 | 'fee_level' => 'steep', | |
785 | $this->getCustomFieldName('participant_int') => '99999', | |
786 | ]); | |
f399fbd8 EM |
787 | CRM_Utils_Time::setTime('2007-02-20 15:00:00'); |
788 | $this->callAPISuccess('action_schedule', 'create', [ | |
789 | 'title' => 'job', | |
790 | 'subject' => 'job', | |
791 | 'entity_value' => 1, | |
792 | 'mapping_id' => 2, | |
793 | 'start_action_date' => 'register_date', | |
794 | 'start_action_offset' => 1, | |
795 | 'start_action_condition' => 'after', | |
796 | 'start_action_unit' => 'day', | |
e80f2261 | 797 | 'body_html' => $html, |
f399fbd8 | 798 | ]); |
ce971869 EM |
799 | } |
800 | ||
801 | /** | |
802 | * Get expected event tokens. | |
803 | * | |
804 | * @return string[] | |
805 | */ | |
806 | protected function getEventTokens(): array { | |
807 | return [ | |
e80f2261 | 808 | '{event.id}' => 'Event ID', |
ce971869 EM |
809 | '{event.title}' => 'Event Title', |
810 | '{event.start_date}' => 'Event Start Date', | |
811 | '{event.end_date}' => 'Event End Date', | |
e80f2261 | 812 | '{event.event_type_id:label}' => 'Event Type', |
ce971869 EM |
813 | '{event.summary}' => 'Event Summary', |
814 | '{event.contact_email}' => 'Event Contact Email', | |
815 | '{event.contact_phone}' => 'Event Contact Phone', | |
816 | '{event.description}' => 'Event Description', | |
817 | '{event.location}' => 'Event Location', | |
ce971869 EM |
818 | '{event.info_url}' => 'Event Info URL', |
819 | '{event.registration_url}' => 'Event Registration URL', | |
f399fbd8 | 820 | '{event.' . $this->getCustomFieldName('text') . '}' => 'Enter text here :: Group with field text', |
ce971869 EM |
821 | ]; |
822 | } | |
823 | ||
044c0ad1 EM |
824 | /** |
825 | * @param array $tokens | |
826 | * | |
827 | * @return string | |
828 | */ | |
829 | protected function getTokenString(array $tokens): string { | |
830 | $html = ''; | |
831 | foreach ($tokens as $token) { | |
832 | $html .= substr($token, 1, -1) . ' :' . $token . "\n"; | |
833 | } | |
834 | return $html; | |
835 | } | |
836 | ||
d0ce76fd | 837 | } |