Commit | Line | Data |
---|---|---|
e9379b58 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
e9379b58 | 5 | | | |
7d61e75f TO |
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 | | |
e9379b58 | 9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
12 | /** | |
13 | * Test APIv3 civicrm_contribute_* functions | |
14 | * | |
15 | * @package CiviCRM_APIv3 | |
16 | * @subpackage API_Contribution | |
17 | * @group headless | |
18 | */ | |
19 | class CRM_Contribute_Form_Task_PDFLetterCommonTest extends CiviUnitTestCase { | |
20 | ||
edcbcbb0 | 21 | use CRMTraits_Custom_CustomDataTrait; |
22 | ||
e9379b58 | 23 | protected $_individualId; |
24 | ||
102279e2 | 25 | protected $_docTypes; |
59d861cb | 26 | |
3017ca78 | 27 | protected $_contactIds; |
59d861cb | 28 | |
2fe8b920 | 29 | /** |
30 | * Count how many times the hookTokens is called. | |
31 | * | |
32 | * This only needs to be called once, check refactoring doesn't change this. | |
33 | * | |
34 | * @var int | |
35 | */ | |
36 | protected $hookTokensCalled = 0; | |
37 | ||
102279e2 | 38 | protected function setUp(): void { |
59d861cb | 39 | parent::setUp(); |
9099cab3 | 40 | $this->_individualId = $this->individualCreate(['first_name' => 'Anthony', 'last_name' => 'Collins']); |
59d861cb | 41 | $this->_docTypes = CRM_Core_SelectValues::documentApplicationType(); |
42 | } | |
43 | ||
e9379b58 | 44 | /** |
45 | * Clean up after each test. | |
3017ca78 | 46 | * |
edcbcbb0 | 47 | * @throws \API_Exception |
3017ca78 | 48 | * @throws \CRM_Core_Exception |
e9379b58 | 49 | */ |
102279e2 | 50 | public function tearDown(): void { |
e9379b58 | 51 | $this->quickCleanUpFinancialEntities(); |
edcbcbb0 | 52 | $this->quickCleanup(['civicrm_uf_match', 'civicrm_campaign'], TRUE); |
e9379b58 | 53 | CRM_Utils_Hook::singleton()->reset(); |
edcbcbb0 | 54 | parent::tearDown(); |
e9379b58 | 55 | } |
56 | ||
40022216 | 57 | /** |
58 | * Test thank you send with grouping. | |
59 | * | |
60 | * @throws \CRM_Core_Exception | |
61 | * @throws \CiviCRM_API3_Exception | |
62 | */ | |
63 | public function testGroupedThankYous(): void { | |
64 | $this->ids['Contact'][0] = $this->individualCreate(); | |
65 | $this->createLoggedInUser(); | |
66 | $contribution1ID = $this->callAPISuccess('Contribution', 'create', [ | |
67 | 'contact_id' => $this->ids['Contact'][0], | |
68 | 'total_amount' => '60', | |
69 | 'financial_type_id' => 'Donation', | |
70 | 'currency' => 'USD', | |
71 | 'receive_date' => '2021-01-01 13:21', | |
72 | ])['id']; | |
73 | $contribution2ID = $this->callAPISuccess('Contribution', 'create', [ | |
74 | 'contact_id' => $this->ids['Contact'][0], | |
75 | 'total_amount' => '70', | |
76 | 'financial_type_id' => 'Donation', | |
77 | 'receive_date' => '2021-02-01 2:21', | |
78 | 'currency' => 'USD', | |
79 | ])['id']; | |
31f2ebac | 80 | /* @var CRM_Contribute_Form_Task_PDFLetter $form */ |
40022216 | 81 | $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', [ |
82 | 'campaign_id' => '', | |
83 | 'subject' => '', | |
84 | 'format_id' => '', | |
85 | 'paper_size' => 'letter', | |
86 | 'orientation' => 'portrait', | |
87 | 'metric' => 'in', | |
88 | 'margin_left' => '0.75', | |
89 | 'margin_right' => '0.75', | |
90 | 'margin_top' => '0.75', | |
91 | 'margin_bottom' => '0.75', | |
92 | 'document_type' => 'pdf', | |
93 | 'html_message' => '{contribution.currency} * {contribution.total_amount} * {contribution.receive_date}', | |
94 | 'template' => '', | |
95 | 'saveTemplateName' => '', | |
96 | 'from_email_address' => '185', | |
97 | 'thankyou_update' => '1', | |
98 | 'group_by' => 'contact_id', | |
99 | 'group_by_separator' => 'comma', | |
100 | 'email_options' => '', | |
101 | ]); | |
102 | $this->setSearchSelection([$contribution1ID, $contribution2ID], $form); | |
103 | $form->preProcess(); | |
104 | try { | |
105 | $form->postProcess(); | |
106 | } | |
107 | catch (CRM_Core_Exception_PrematureExitException $e) { | |
f70a513f | 108 | $this->assertStringContainsString('USD, USD * $60.00, $70.00 * January 1st, 2021 1:21 PM, February 1st, 2021 2:21 AM', $e->errorData['html']); |
40022216 | 109 | } |
110 | } | |
111 | ||
e9379b58 | 112 | /** |
113 | * Test the buildContributionArray function. | |
3017ca78 | 114 | * |
102279e2 | 115 | * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception |
e9379b58 | 116 | */ |
3017ca78 | 117 | public function testBuildContributionArray(): void { |
e9379b58 | 118 | $this->_individualId = $this->individualCreate(); |
39b23159 | 119 | |
9099cab3 | 120 | $customGroup = $this->callAPISuccess('CustomGroup', 'create', [ |
39b23159 ERL |
121 | 'title' => 'Test Custom Set for Contribution', |
122 | 'extends' => 'Contribution', | |
123 | 'is_active' => TRUE, | |
9099cab3 CW |
124 | ]); |
125 | $params = [ | |
39b23159 ERL |
126 | 'custom_group_id' => $customGroup['id'], |
127 | 'label' => 'Text field', | |
128 | 'html_type' => 'Text', | |
129 | 'data_type' => 'String', | |
130 | 'weight' => 1, | |
131 | 'is_active' => 1, | |
9099cab3 | 132 | ]; |
39b23159 ERL |
133 | $customField = $this->callAPISuccess('CustomField', 'create', $params); |
134 | $customFieldKey = 'custom_' . $customField['id']; | |
3017ca78 | 135 | $campaignTitle = 'Test Campaign '; |
39b23159 | 136 | |
9099cab3 | 137 | $params = [ |
39b23159 ERL |
138 | 'contact_id' => $this->_individualId, |
139 | 'total_amount' => 6, | |
9099cab3 | 140 | 'campaign_id' => $this->campaignCreate(['title' => $campaignTitle], FALSE), |
39b23159 | 141 | 'financial_type_id' => 'Donation', |
31f2ebac | 142 | $customFieldKey => 'Text_', |
9099cab3 CW |
143 | ]; |
144 | $contributionIDs = $returnProperties = []; | |
e9379b58 | 145 | $result = $this->callAPISuccess('Contribution', 'create', $params); |
146 | $contributionIDs[] = $result['id']; | |
9099cab3 | 147 | $this->hookClass->setHook('civicrm_tokenValues', [$this, 'hookTokenValues']); |
e9379b58 | 148 | |
39b23159 ERL |
149 | // assume that there are two token {contribution.financial_type} and |
150 | // {contribution.custom_N} in message content | |
9099cab3 CW |
151 | $messageToken = [ |
152 | 'contribution' => [ | |
39b23159 | 153 | 'financial_type', |
d31d0888 | 154 | 'payment_instrument', |
155 | 'campaign', | |
39b23159 | 156 | $customFieldKey, |
9099cab3 CW |
157 | ], |
158 | ]; | |
39b23159 | 159 | |
3467497b | 160 | $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter'); |
161 | [$contributions, $contacts] = $form->buildContributionArray('contact_id', $contributionIDs, $returnProperties, TRUE, TRUE, $messageToken, 'test', '**', FALSE); | |
e9379b58 | 162 | |
163 | $this->assertEquals('Anthony', $contacts[$this->_individualId]['first_name']); | |
e9379b58 | 164 | $this->assertEquals('Donation', $contributions[$result['id']]['financial_type']); |
d31d0888 | 165 | $this->assertEquals($campaignTitle, $contributions[$result['id']]['campaign']); |
166 | $this->assertEquals('Check', $contributions[$result['id']]['payment_instrument']); | |
39b23159 ERL |
167 | // CRM-20359: assert that contribution custom field token is rightfully replaced by its value |
168 | $this->assertEquals($params[$customFieldKey], $contributions[$result['id']][$customFieldKey]); | |
169 | ||
170 | $this->customFieldDelete($customField['id']); | |
171 | $this->customGroupDelete($customGroup['id']); | |
e9379b58 | 172 | } |
173 | ||
174 | /** | |
175 | * Implement token values hook. | |
176 | * | |
177 | * @param array $details | |
178 | * @param array $contactIDs | |
179 | * @param int $jobID | |
180 | * @param array $tokens | |
181 | * @param string $className | |
182 | */ | |
102279e2 | 183 | public function hookTokenValues(&$details, $contactIDs, $jobID, $tokens, $className): void { |
e9379b58 | 184 | foreach ($details as $index => $detail) { |
185 | $details[$index]['favourite_emoticon'] = 'emo'; | |
186 | } | |
187 | } | |
188 | ||
59d861cb | 189 | /** |
190 | * Test contribution token replacement in | |
191 | * html returned by postProcess function. | |
3017ca78 | 192 | * |
193 | * @throws \CiviCRM_API3_Exception | |
194 | * @throws \CRM_Core_Exception | |
59d861cb | 195 | */ |
3017ca78 | 196 | public function testPostProcess(): void { |
21585345 | 197 | $this->createLoggedInUser(); |
9099cab3 CW |
198 | foreach (['docx', 'odt'] as $docType) { |
199 | $formValues = [ | |
59d861cb | 200 | 'group_by' => NULL, |
9099cab3 | 201 | 'document_file' => [ |
59d861cb | 202 | 'name' => __DIR__ . "/sample_documents/Template.$docType", |
203 | 'type' => $this->_docTypes[$docType], | |
9099cab3 CW |
204 | ], |
205 | ]; | |
59d861cb | 206 | |
9da59513 | 207 | $contributionId = $this->createContribution(); |
102279e2 | 208 | /* @var $form CRM_Contribute_Form_Task_PDFLetter */ |
b701d2e5 | 209 | $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); |
9099cab3 | 210 | $form->setContributionIds([$contributionId]); |
59d861cb | 211 | $format = Civi::settings()->get('dateformatFull'); |
212 | $date = CRM_Utils_Date::getToday(); | |
213 | $displayDate = CRM_Utils_Date::customFormat($date, $format); | |
214 | ||
cf56c730 EM |
215 | try { |
216 | $form->postProcess(); | |
217 | $this->fail('Exception expected'); | |
218 | } | |
219 | catch (CRM_Core_Exception_PrematureExitException $e) { | |
220 | $html = $e->errorData['html']; | |
221 | } | |
9099cab3 | 222 | $expectedValues = [ |
cb9b8644 | 223 | 'Hello Anthony', |
59d861cb | 224 | '$ 100.00', |
225 | $displayDate, | |
993a5b99 | 226 | 'Donation', |
cb9b8644 | 227 | 'Domain Name - Default Domain Name', |
9099cab3 | 228 | ]; |
59d861cb | 229 | |
230 | foreach ($expectedValues as $val) { | |
cf56c730 | 231 | $this->assertNotSame(strpos($html[$contributionId], $val), 0); |
59d861cb | 232 | } |
233 | } | |
234 | } | |
235 | ||
9da59513 | 236 | /** |
237 | * Test that no notice or errors occur if no contribution tokens are requested. | |
238 | * | |
239 | * @throws \CRM_Core_Exception | |
240 | * @throws \CiviCRM_API3_Exception | |
241 | */ | |
242 | public function testNoContributionTokens(): void { | |
243 | $this->createLoggedInUser(); | |
244 | $formValues = [ | |
245 | 'html_message' => '{contact.display_name}', | |
246 | 'document_type' => 'pdf', | |
247 | ]; | |
248 | /* @var $form CRM_Contribute_Form_Task_PDFLetter */ | |
249 | $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); | |
250 | $form->setContributionIds([$this->createContribution()]); | |
251 | try { | |
252 | $form->postProcess(); | |
253 | } | |
254 | catch (CRM_Core_Exception_PrematureExitException $e) { | |
255 | $html = $e->errorData['html']; | |
256 | } | |
257 | $this->assertStringContainsString('Mr. Anthony Anderson II', $html); | |
258 | } | |
259 | ||
edcbcbb0 | 260 | /** |
261 | * Test all contribution tokens. | |
262 | * | |
edcbcbb0 | 263 | * @throws \CRM_Core_Exception |
264 | * @throws \CiviCRM_API3_Exception | |
edcbcbb0 | 265 | */ |
266 | public function testAllContributionTokens(): void { | |
14145505 EM |
267 | $this->hookClass->setHook('civicrm_tokenValues', [$this, 'hookTokenValues']); |
268 | $this->hookClass->setHook('civicrm_tokens', [$this, 'hook_tokens']); | |
269 | ||
edcbcbb0 | 270 | $this->createLoggedInUser(); |
271 | $this->createCustomGroupWithFieldsOfAllTypes(['extends' => 'Contribution']); | |
14145505 | 272 | $this->campaignCreate(['name' => 'Big one', 'title' => 'Big one'], FALSE); |
edcbcbb0 | 273 | $tokens = $this->getAllContributionTokens(); |
274 | $formValues = [ | |
275 | 'document_type' => 'pdf', | |
276 | 'html_message' => '', | |
277 | ]; | |
278 | foreach (array_keys($this->getAllContributionTokens()) as $token) { | |
279 | $formValues['html_message'] .= "$token : {contribution.$token}\n"; | |
280 | } | |
14145505 | 281 | $formValues['html_message'] .= '{emoji.favourite_emoticon}'; |
edcbcbb0 | 282 | /* @var $form CRM_Contribute_Form_Task_PDFLetter */ |
283 | $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); | |
ec20b780 | 284 | $form->setContributionIds([$this->createContribution(array_merge(['campaign_id' => $tokens['campaign_id:label']], $tokens))]); |
edcbcbb0 | 285 | try { |
286 | $form->postProcess(); | |
287 | } | |
288 | catch (CRM_Core_Exception_PrematureExitException $e) { | |
289 | $html = $e->errorData['html']; | |
290 | } | |
291 | $this->assertEquals(' | |
292 | <html> | |
293 | <head> | |
294 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> | |
295 | <style>@page { margin: 0.75in 0.75in 0.75in 0.75in; }</style> | |
296 | <style type="text/css">@import url(' . CRM_Core_Config::singleton()->userFrameworkResourceURL . 'css/print.css);</style> | |
297 | ' . " \n" . ' </head> | |
298 | <body> | |
299 | <div id="crm-container"> | |
ec20b780 | 300 | id : 1 |
f70a513f EM |
301 | total_amount : €9,999.99 |
302 | fee_amount : €1,111.11 | |
303 | net_amount : €7,777.78 | |
304 | non_deductible_amount : €2,222.22 | |
31f2ebac | 305 | receive_date : July 20th, 2018 |
ec20b780 | 306 | payment_instrument_id:label : Check |
edcbcbb0 | 307 | trxn_id : 1234 |
308 | invoice_id : 568 | |
309 | currency : EUR | |
31f2ebac | 310 | cancel_date : December 30th, 2019 |
edcbcbb0 | 311 | cancel_reason : Contribution Cancel Reason |
31f2ebac EM |
312 | receipt_date : October 30th, 2019 |
313 | thankyou_date : November 30th, 2019 | |
ec20b780 | 314 | source : Contribution Source |
edcbcbb0 | 315 | amount_level : Amount Level |
316 | contribution_status_id : 2 | |
317 | check_number : 6789 | |
ec20b780 | 318 | campaign_id:label : Big one |
edcbcbb0 | 319 | ' . $this->getCustomFieldName('text') . ' : Bobsled |
320 | ' . $this->getCustomFieldName('select_string') . ' : Red | |
321 | ' . $this->getCustomFieldName('select_date') . ' : 01/20/2021 12:00AM | |
322 | ' . $this->getCustomFieldName('int') . ' : 999 | |
323 | ' . $this->getCustomFieldName('link') . ' : <a href="http://civicrm.org" target="_blank">http://civicrm.org</a> | |
324 | ' . $this->getCustomFieldName('country') . ' : New Zealand | |
325 | ' . $this->getCustomFieldName('multi_country') . ' : France, Canada | |
326 | ' . $this->getCustomFieldName('contact_reference') . ' : Mr. Spider Man II | |
327 | ' . $this->getCustomFieldName('state') . ' : Queensland | |
328 | ' . $this->getCustomFieldName('multi_state') . ' : Victoria, New South Wales | |
329 | ' . $this->getCustomFieldName('boolean') . ' : Yes | |
330 | ' . $this->getCustomFieldName('checkbox') . ' : Purple | |
14145505 | 331 | emo |
edcbcbb0 | 332 | </div> |
333 | </body> | |
334 | </html>', $html); | |
335 | } | |
336 | ||
337 | /** | |
338 | * Get all the tokens available to contributions. | |
339 | * | |
340 | * @return array | |
edcbcbb0 | 341 | */ |
342 | public function getAllContributionTokens(): array { | |
343 | return [ | |
ec20b780 | 344 | 'id' => '', |
edcbcbb0 | 345 | 'total_amount' => '9999.99', |
346 | 'fee_amount' => '1111.11', | |
347 | 'net_amount' => '7777.78', | |
348 | 'non_deductible_amount' => '2222.22', | |
349 | 'receive_date' => '2018-07-20', | |
ec20b780 | 350 | 'payment_instrument_id:label' => 'Check', |
edcbcbb0 | 351 | 'trxn_id' => '1234', |
352 | 'invoice_id' => '568', | |
353 | 'currency' => 'EUR', | |
354 | 'cancel_date' => '2019-12-30', | |
355 | 'cancel_reason' => 'Contribution Cancel Reason', | |
356 | 'receipt_date' => '2019-10-30', | |
357 | 'thankyou_date' => '2019-11-30', | |
ec20b780 | 358 | 'source' => 'Contribution Source', |
edcbcbb0 | 359 | 'amount_level' => 'Amount Level', |
360 | 'contribution_status_id' => 'Pending', | |
361 | 'check_number' => '6789', | |
ec20b780 | 362 | 'campaign_id:label' => 'Big one', |
edcbcbb0 | 363 | $this->getCustomFieldName('text') => 'Bobsled', |
364 | $this->getCustomFieldName('select_string') => 'R', | |
365 | $this->getCustomFieldName('select_date') => '2021-01-20', | |
366 | $this->getCustomFieldName('int') => 999, | |
367 | $this->getCustomFieldName('link') => 'http://civicrm.org', | |
368 | $this->getCustomFieldName('country') => 'New Zealand', | |
369 | $this->getCustomFieldName('multi_country') => ['France', 'Canada'], | |
370 | $this->getCustomFieldName('contact_reference') => $this->individualCreate(['first_name' => 'Spider', 'last_name' => 'Man']), | |
371 | $this->getCustomFieldName('state') => 'Queensland', | |
372 | $this->getCustomFieldName('multi_state') => ['Victoria', 'New South Wales'], | |
373 | $this->getCustomFieldName('boolean') => TRUE, | |
374 | $this->getCustomFieldName('checkbox') => 'P', | |
375 | $this->getCustomFieldName('contact_reference') => $this->individualCreate(['first_name' => 'Spider', 'last_name' => 'Man']), | |
edcbcbb0 | 376 | ]; |
377 | } | |
378 | ||
8de6622e | 379 | /** |
380 | * Test assignment of variables when using the group by function. | |
381 | * | |
3017ca78 | 382 | * We are looking to see that the contribution aggregate and contributions |
383 | * arrays reflect the most recent contact rather than a total aggregate, | |
384 | * since we are using group by. | |
385 | * | |
386 | * @throws \CiviCRM_API3_Exception | |
387 | * @throws \CRM_Core_Exception | |
8de6622e | 388 | */ |
3017ca78 | 389 | public function testPostProcessGroupByContact(): void { |
3280f327 | 390 | $this->createLoggedInUser(); |
9099cab3 CW |
391 | $this->hookClass->setHook('civicrm_tokenValues', [$this, 'hook_aggregateTokenValues']); |
392 | $this->hookClass->setHook('civicrm_tokens', [$this, 'hook_tokens']); | |
8de6622e | 393 | $this->mut = new CiviMailUtils($this, TRUE); |
394 | $this->_individualId = $this->individualCreate(); | |
395 | $this->_individualId2 = $this->individualCreate(); | |
0ceb63d9 | 396 | $htmlMessage = '{aggregate.rendered_token}'; |
9099cab3 | 397 | $formValues = [ |
8de6622e | 398 | 'group_by' => 'contact_id', |
399 | 'html_message' => $htmlMessage, | |
400 | 'email_options' => 'both', | |
401 | 'subject' => 'Testy test test', | |
beac1417 | 402 | 'from' => 'info@example.com', |
9099cab3 | 403 | ]; |
8de6622e | 404 | |
9099cab3 CW |
405 | $contributionIDs = []; |
406 | $contribution = $this->callAPISuccess('Contribution', 'create', [ | |
8de6622e | 407 | 'contact_id' => $this->_individualId, |
408 | 'total_amount' => 100, | |
409 | 'financial_type_id' => 'Donation', | |
3280f327 | 410 | 'receive_date' => '2016-12-25', |
9099cab3 | 411 | ]); |
8de6622e | 412 | $contributionIDs[] = $contribution['id']; |
9099cab3 | 413 | $contribution = $this->callAPISuccess('Contribution', 'create', [ |
8de6622e | 414 | 'contact_id' => $this->_individualId2, |
415 | 'total_amount' => 10, | |
416 | 'financial_type_id' => 'Donation', | |
3280f327 | 417 | 'receive_date' => '2016-12-25', |
9099cab3 | 418 | ]); |
8de6622e | 419 | $contributionIDs[] = $contribution['id']; |
420 | ||
9099cab3 | 421 | $contribution = $this->callAPISuccess('Contribution', 'create', [ |
8de6622e | 422 | 'contact_id' => $this->_individualId2, |
423 | 'total_amount' => 1, | |
424 | 'financial_type_id' => 'Donation', | |
3280f327 | 425 | 'receive_date' => '2016-12-25', |
9099cab3 | 426 | ]); |
8de6622e | 427 | $contributionIDs[] = $contribution['id']; |
428 | ||
102279e2 | 429 | /* @var \CRM_Contribute_Form_Task_PDFLetter $form */ |
b701d2e5 | 430 | $form = $this->getFormObject('CRM_Contribute_Form_Task_PDFLetter', $formValues); |
8de6622e | 431 | $form->setContributionIds($contributionIDs); |
432 | ||
cf56c730 EM |
433 | try { |
434 | $form->postProcess(); | |
435 | $this->fail('exception expected.'); | |
436 | } | |
437 | catch (CRM_Core_Exception_PrematureExitException $e) { | |
438 | $html = $e->errorData['html']; | |
439 | } | |
8de6622e | 440 | $this->assertEquals("<table border='1' cellpadding='2' cellspacing='0' class='table'> |
441 | <tbody> | |
442 | <tr> | |
443 | <th>Date</th> | |
444 | <th>Amount</th> | |
445 | <th>Financial Type</th> | |
446 | <th>Source</th> | |
447 | </tr> | |
448 | <!-- | |
449 | --> | |
450 | <tr> | |
3280f327 | 451 | <td>25 December 2016</td> |
8de6622e | 452 | <td>$ 100.00</td> |
3280f327 | 453 | <td>Donation</td> |
8de6622e | 454 | <td></td> |
455 | </tr> | |
456 | <!-- | |
12d88807 | 457 | --> |
8de6622e | 458 | <tr> |
459 | <td><strong>Total</strong></td> | |
460 | <td><strong>$ 100.00</strong></td> | |
461 | <td></td> | |
462 | <td></td> | |
463 | </tr> | |
464 | </tbody> | |
465 | </table>", $html[1]); | |
466 | $this->assertEquals("<table border='1' cellpadding='2' cellspacing='0' class='table'> | |
467 | <tbody> | |
468 | <tr> | |
469 | <th>Date</th> | |
470 | <th>Amount</th> | |
471 | <th>Financial Type</th> | |
472 | <th>Source</th> | |
473 | </tr> | |
474 | <!-- | |
12d88807 | 475 | --> |
8de6622e | 476 | <tr> |
3280f327 | 477 | <td>25 December 2016</td> |
8de6622e | 478 | <td>$ 10.00</td> |
3280f327 | 479 | <td>Donation</td> |
8de6622e | 480 | <td></td> |
481 | </tr> | |
482 | <!-- | |
483 | --> | |
484 | <tr> | |
3280f327 | 485 | <td>25 December 2016</td> |
8de6622e | 486 | <td>$ 1.00</td> |
3280f327 | 487 | <td>Donation</td> |
8de6622e | 488 | <td></td> |
489 | </tr> | |
490 | <!-- | |
491 | --> | |
492 | <tr> | |
493 | <td><strong>Total</strong></td> | |
494 | <td><strong>$ 11.00</strong></td> | |
495 | <td></td> | |
496 | <td></td> | |
497 | </tr> | |
498 | </tbody> | |
499 | </table>", $html[2]); | |
3280f327 | 500 | |
9099cab3 | 501 | $activities = $this->callAPISuccess('Activity', 'get', ['activity_type_id' => 'Print PDF Letter', 'sequential' => 1]); |
3280f327 | 502 | $this->assertEquals(2, $activities['count']); |
503 | $this->assertEquals($html[1], $activities['values'][0]['details']); | |
504 | $this->assertEquals($html[2], $activities['values'][1]['details']); | |
2fe8b920 | 505 | // Checking it is not called multiple times. |
506 | // once for each contact create + once for the activities. | |
12d88807 | 507 | // By calling the cached function we can get this down to 1 |
0ceb63d9 | 508 | $this->assertEquals(3, $this->hookTokensCalled); |
3280f327 | 509 | $this->mut->checkAllMailLog($html); |
8de6622e | 510 | |
511 | } | |
512 | ||
513 | /** | |
514 | * Implements civicrm_tokens(). | |
515 | */ | |
cf56c730 | 516 | public function hook_tokens(&$tokens): void { |
2fe8b920 | 517 | $this->hookTokensCalled++; |
9099cab3 | 518 | $tokens['aggregate'] = ['rendered_token' => 'rendered_token']; |
14145505 | 519 | $tokens['emoji'] = ['favourite_emoticon' => 'favourite_emoticon']; |
8de6622e | 520 | } |
521 | ||
522 | /** | |
523 | * Get the html message. | |
524 | * | |
525 | * @return string | |
526 | */ | |
527 | public function getHtmlMessage() { | |
528 | return '{assign var=\'contact_aggregate\' value=0} | |
529 | <table border=\'1\' cellpadding=\'2\' cellspacing=\'0\' class=\'table\'> | |
530 | <tbody> | |
531 | <tr> | |
532 | <th>Date</th> | |
533 | <th>Amount</th> | |
534 | <th>Financial Type</th> | |
535 | <th>Source</th> | |
536 | </tr> | |
537 | <!-- | |
538 | {foreach from=$contributions item=contribution} | |
539 | {if $contribution.contact_id == $messageContactID} | |
540 | {assign var=\'date\' value=$contribution.receive_date|date_format:\'%d %B %Y\'} | |
541 | {assign var=contact_aggregate | |
542 | value=$contact_aggregate+$contribution.total_amount} | |
543 | --> | |
544 | <tr> | |
545 | <td>{$date}</td> | |
546 | <td>{$contribution.total_amount|crmMoney}</td> | |
547 | <td>{$contribution.financial_type}</td> | |
3280f327 | 548 | <td></td> |
8de6622e | 549 | </tr> |
550 | <!-- | |
551 | {/if} | |
552 | {/foreach} | |
553 | --> | |
554 | <tr> | |
555 | <td><strong>Total</strong></td> | |
556 | <td><strong>{$contact_aggregate|crmMoney}</strong></td> | |
557 | <td></td> | |
558 | <td></td> | |
559 | </tr> | |
560 | </tbody> | |
561 | </table>'; | |
562 | } | |
563 | ||
564 | /** | |
565 | * Implements CiviCRM hook. | |
566 | * | |
567 | * @param array $values | |
568 | * @param array $contactIDs | |
569 | * @param null $job | |
570 | * @param array $tokens | |
571 | * @param null $context | |
572 | */ | |
cf56c730 | 573 | public function hook_aggregateTokenValues(array &$values, $contactIDs, $job = NULL, $tokens = [], $context = NULL) { |
8de6622e | 574 | foreach ($contactIDs as $contactID) { |
575 | CRM_Core_Smarty::singleton()->assign('messageContactID', $contactID); | |
576 | $values[$contactID]['aggregate.rendered_token'] = CRM_Core_Smarty::singleton() | |
577 | ->fetch('string:' . $this->getHtmlMessage()); | |
578 | } | |
579 | } | |
580 | ||
c2486e81 SM |
581 | /** |
582 | * @param string $token | |
583 | * @param string $entity | |
584 | * @param string $textToSearch | |
585 | * @param bool $expected | |
586 | * | |
587 | * @dataProvider isHtmlTokenInTableCellProvider | |
588 | */ | |
cf56c730 | 589 | public function testIsHtmlTokenInTableCell($token, $entity, $textToSearch, $expected): void { |
c2486e81 | 590 | $this->assertEquals($expected, |
9be8686d | 591 | CRM_Contribute_Form_Task_PDFLetter::isHtmlTokenInTableCell($token, $entity, $textToSearch) |
c2486e81 SM |
592 | ); |
593 | } | |
594 | ||
595 | public function isHtmlTokenInTableCellProvider() { | |
596 | return [ | |
597 | ||
598 | 'simplest TRUE' => [ | |
599 | 'token', | |
600 | 'entity', | |
601 | '<td>{entity.token}</td>', | |
602 | TRUE, | |
603 | ], | |
604 | ||
605 | 'simplest FALSE' => [ | |
606 | 'token', | |
607 | 'entity', | |
608 | '{entity.token}', | |
609 | FALSE, | |
610 | ], | |
611 | ||
612 | 'token between two tables' => [ | |
613 | 'token', | |
614 | 'entity', | |
615 | ' <table><tr><td>Top</td></tr></table> | |
616 | {entity.token} | |
617 | <table><tr><td>Bottom</td></tr></table>', | |
618 | FALSE, | |
619 | ], | |
620 | ||
621 | 'token in two tables' => [ | |
622 | 'token', | |
623 | 'entity', | |
624 | ' <table><tr><td>{entity.token}</td></tr><tr><td>foo</td></tr></table> | |
625 | <table><tr><td>{entity.token}</td></tr><tr><td>foo</td></tr></table>', | |
626 | TRUE, | |
627 | ], | |
628 | ||
629 | 'token outside of table and inside of table' => [ | |
630 | 'token', | |
631 | 'entity', | |
632 | ' {entity.token} | |
633 | <table><tr><td>{entity.token}</td></tr><tr><td>foo</td></tr></table>', | |
634 | FALSE, | |
635 | ], | |
636 | ||
637 | 'token inside more complicated table' => [ | |
638 | 'token', | |
639 | 'entity', | |
640 | ' <table><tr><td class="foo"><em>{entity.token}</em></td></tr></table>', | |
641 | TRUE, | |
642 | ], | |
643 | ||
644 | 'token inside something that looks like table cell' => [ | |
645 | 'token', | |
646 | 'entity', | |
647 | ' <tdata>{entity.token}</tdata> | |
648 | <table><tr><td>Bottom</td></tr></table>', | |
649 | FALSE, | |
650 | ], | |
651 | ||
652 | ]; | |
653 | } | |
654 | ||
40022216 | 655 | /** |
656 | * @param array $entities | |
657 | * @param \CRM_Core_Form $form | |
658 | */ | |
659 | protected function setSearchSelection(array $entities, CRM_Core_Form $form): void { | |
660 | $_SESSION['_' . $form->controller->_name . '_container']['values']['Search'] = [ | |
661 | 'radio_ts' => 'ts_sel', | |
662 | ]; | |
663 | foreach ($entities as $entityID) { | |
664 | $_SESSION['_' . $form->controller->_name . '_container']['values']['Search']['mark_x_' . $entityID] = TRUE; | |
665 | } | |
666 | } | |
667 | ||
9da59513 | 668 | /** |
cf56c730 EM |
669 | * @param array $contributionParams |
670 | * | |
9da59513 | 671 | * @return mixed |
9da59513 | 672 | */ |
cf56c730 | 673 | protected function createContribution(array $contributionParams = []) { |
edcbcbb0 | 674 | $contributionParams = array_merge([ |
9da59513 | 675 | 'contact_id' => $this->individualCreate(), |
676 | 'total_amount' => 100, | |
677 | 'financial_type_id' => 'Donation', | |
ec20b780 | 678 | 'source' => 'Contribution Source', |
edcbcbb0 | 679 | ], $contributionParams); |
680 | return $this->callAPISuccess('Contribution', 'create', $contributionParams)['id']; | |
9da59513 | 681 | } |
682 | ||
e9379b58 | 683 | } |