Fix Address test failure by passing the hook through to copyCustomFields and fix...
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionRecurTest.php
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
12 use Civi\Api4\ContributionRecur;
13
14 /**
15 * Class CRM_Contribute_BAO_ContributionRecurTest
16 * @group headless
17 */
18 class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase {
19
20 use CRMTraits_Financial_OrderTrait;
21
22 protected $isValidateFinancialsOnPostAssert = TRUE;
23
24 /**
25 * Set up for test.
26 *
27 * @throws \CiviCRM_API3_Exception
28 */
29 public function setUp(): void {
30 parent::setUp();
31 $this->ids['payment_processor'] = $this->paymentProcessorCreate();
32 $this->_params = [
33 'contact_id' => $this->individualCreate(),
34 'amount' => 3.00,
35 'frequency_unit' => 'week',
36 'frequency_interval' => 1,
37 'installments' => 2,
38 'start_date' => 'yesterday',
39 'create_date' => 'yesterday',
40 'modified_date' => 'yesterday',
41 'cancel_date' => NULL,
42 'end_date' => '+ 2 weeks',
43 'processor_id' => '643411460836',
44 'trxn_id' => 'e0d0808e26f3e661c6c18eb7c039d363',
45 'invoice_id' => 'e0d0808e26f3e661c6c18eb7c039d363',
46 'contribution_status_id' => 1,
47 'is_test' => 0,
48 'cycle_day' => 1,
49 'next_sched_contribution_date' => '+ 1 week',
50 'failure_count' => 0,
51 'failure_retry_date' => NULL,
52 'auto_renew' => 0,
53 'currency' => 'USD',
54 'payment_processor_id' => $this->ids['payment_processor'],
55 'is_email_receipt' => 1,
56 'financial_type_id' => 1,
57 'payment_instrument_id' => 1,
58 'campaign_id' => NULL,
59 ];
60 }
61
62 /**
63 * Cleanup after test.
64 *
65 * @throws \CRM_Core_Exception
66 */
67 public function teardown():void {
68 $this->quickCleanUpFinancialEntities();
69 }
70
71 /**
72 * Test that an object can be retrieved & saved (per CRM-14986).
73 *
74 * This has been causing a DB error so we are checking for absence of error
75 *
76 * @throws \CRM_Core_Exception
77 */
78 public function testFindSave(): void {
79 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
80 $dao = new CRM_Contribute_BAO_ContributionRecur();
81 $dao->id = $contributionRecur['id'];
82 $dao->find(TRUE);
83 $dao->is_email_receipt = 0;
84 $dao->save();
85 }
86
87 /**
88 * Test cancellation works per CRM-14986.
89 *
90 * We are checking for absence of error.
91 *
92 * @throws \CRM_Core_Exception
93 */
94 public function testCancelRecur(): void {
95 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
96 CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution(['id' => $contributionRecur['id']]);
97 }
98
99 /**
100 * Test checking if contribution recur object can allow for changes to financial types.
101 *
102 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
103 */
104 public function testSupportFinancialTypeChange(): void {
105 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
106 $this->callAPISuccess('Contribution', 'create', [
107 'contribution_recur_id' => $contributionRecur['id'],
108 'total_amount' => '3.00',
109 'financial_type_id' => 1,
110 'payment_instrument_id' => 1,
111 'currency' => 'USD',
112 'contact_id' => $this->individualCreate(),
113 'contribution_status_id' => 1,
114 'receive_date' => 'yesterday',
115 ]);
116 $this->assertTrue(CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($contributionRecur['id']));
117 }
118
119 /**
120 * Test we don't change unintended fields on API edit
121 *
122 * @throws \CRM_Core_Exception
123 */
124 public function testUpdateRecur(): void {
125 $createParams = $this->_params;
126 $createParams['currency'] = 'XAU';
127 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $createParams);
128 $editParams = [
129 'id' => $contributionRecur['id'],
130 'end_date' => '+ 4 weeks',
131 ];
132 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $editParams);
133 $dao = new CRM_Contribute_BAO_ContributionRecur();
134 $dao->id = $contributionRecur['id'];
135 $dao->find(TRUE);
136 $this->assertEquals('XAU', $dao->currency, 'Edit clobbered recur currency');
137 }
138
139 /**
140 * Check test contributions aren't picked up as template for non-test recurs
141 *
142 * @throws \API_Exception
143 * @throws \CRM_Core_Exception
144 * @throws \CiviCRM_API3_Exception
145 * @throws \Civi\API\Exception\UnauthorizedException
146 */
147 public function testGetTemplateContributionMatchTest1(): void {
148 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
149 // Create a first contrib
150 $firstContrib = $this->callAPISuccess('Contribution', 'create', [
151 'contribution_recur_id' => $contributionRecur['id'],
152 'total_amount' => '3.00',
153 'financial_type_id' => 1,
154 'payment_instrument_id' => 1,
155 'currency' => 'USD',
156 'contact_id' => $this->individualCreate(),
157 'contribution_status_id' => 1,
158 'receive_date' => 'yesterday',
159 ]);
160 // Create a test contrib - should not be picked up as template for non-test recur
161 $this->callAPISuccess('Contribution', 'create', [
162 'contribution_recur_id' => $contributionRecur['id'],
163 'total_amount' => '3.00',
164 'financial_type_id' => 1,
165 'payment_instrument_id' => 1,
166 'currency' => 'USD',
167 'contact_id' => $this->individualCreate(),
168 'contribution_status_id' => 1,
169 'receive_date' => 'yesterday',
170 'is_test' => 1,
171 ]);
172 $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
173 $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']);
174 }
175
176 /**
177 * Check non-test contributions aren't picked up as template for test recurs
178 *
179 * @throws \API_Exception
180 * @throws \CRM_Core_Exception
181 * @throws \CiviCRM_API3_Exception
182 * @throws \Civi\API\Exception\UnauthorizedException
183 */
184 public function testGetTemplateContributionMatchTest(): void {
185 $params = $this->_params;
186 $params['is_test'] = 1;
187 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $params);
188 // Create a first test contrib
189 $firstContrib = $this->callAPISuccess('Contribution', 'create', [
190 'contribution_recur_id' => $contributionRecur['id'],
191 'total_amount' => '3.00',
192 'financial_type_id' => 1,
193 'payment_instrument_id' => 1,
194 'currency' => 'USD',
195 'contact_id' => $this->individualCreate(),
196 'contribution_status_id' => 1,
197 'receive_date' => 'yesterday',
198 'is_test' => 1,
199 ]);
200 // Create a non-test contrib - should not be picked up as template for non-test recur
201 // This shouldn't occur - a live contrib against a test recur, but that's not the point...
202 $this->callAPISuccess('Contribution', 'create', [
203 'contribution_recur_id' => $contributionRecur['id'],
204 'total_amount' => '3.00',
205 'financial_type_id' => 1,
206 'payment_instrument_id' => 1,
207 'currency' => 'USD',
208 'contact_id' => $this->individualCreate(),
209 'contribution_status_id' => 1,
210 'receive_date' => 'yesterday',
211 'is_test' => 0,
212 ]);
213 $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
214 $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']);
215 }
216
217 /**
218 * Check whether template contribution is created based on the first contribution.
219 *
220 * There are three contributions created. Each of them with a different value at a custom field.
221 * The first contribution created should be copied as a template contribution.
222 * The other two should not be used as a template.
223 *
224 * Then we delete the template contribution and make sure a new one exists.
225 * At that time the second contribution should be used a template as that is the most recent one (according to the date).
226 *
227 * @throws \API_Exception
228 * @throws \CRM_Core_Exception
229 * @throws \CiviCRM_API3_Exception
230 * @throws \Civi\API\Exception\UnauthorizedException
231 */
232 public function testCreateTemplateContributionFromFirstContributionTest(): void {
233 $custom_group = $this->customGroupCreate(['extends' => 'Contribution', 'name' => 'template']);
234 $custom_field = $this->customFieldCreate(['custom_group_id' => $custom_group['id'], 'name' => 'field']);
235
236 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
237 // Create a first test contrib
238 $date = new DateTime();
239 $firstContrib = $this->callAPISuccess('Contribution', 'create', [
240 'contribution_recur_id' => $contributionRecur['id'],
241 'total_amount' => '3.00',
242 'financial_type_id' => 1,
243 'payment_instrument_id' => 1,
244 'currency' => 'USD',
245 'contact_id' => $this->_params['contact_id'],
246 'contribution_status_id' => 1,
247 'receive_date' => $date->format('YmdHis'),
248 'custom_' . $custom_field['id'] => 'First Contribution',
249 ]);
250 $date->modify('+2 days');
251 $secondContrib = $this->callAPISuccess('Contribution', 'create', [
252 'contribution_recur_id' => $contributionRecur['id'],
253 'total_amount' => '3.00',
254 'financial_type_id' => 1,
255 'payment_instrument_id' => 1,
256 'currency' => 'USD',
257 'contact_id' => $this->_params['contact_id'],
258 'contribution_status_id' => 1,
259 'receive_date' => $date->format('YmdHis'),
260 'custom_' . $custom_field['id'] => 'Second and most recent Contribution',
261 ]);
262
263 $date->modify('-1 week');
264 $thirdContrib = $this->callAPISuccess('Contribution', 'create', [
265 'contribution_recur_id' => $contributionRecur['id'],
266 'total_amount' => '3.00',
267 'financial_type_id' => 1,
268 'payment_instrument_id' => 1,
269 'currency' => 'USD',
270 'contact_id' => $this->_params['contact_id'],
271 'contribution_status_id' => 1,
272 'receive_date' => $date->format('YmdHis'),
273 'custom_' . $custom_field['id'] => 'Third Contribution',
274 ]);
275
276 // Make sure a template contribution exists.
277 $templateContributionId = CRM_Contribute_BAO_ContributionRecur::ensureTemplateContributionExists($contributionRecur['id']);
278 $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
279 $templateContribution = \Civi\Api4\Contribution::get(FALSE)
280 ->addSelect('*', 'custom.*')
281 ->addWhere('contribution_recur_id', '=', $contributionRecur['id'])
282 ->addWhere('is_template', '=', 1)
283 ->addWhere('is_test', '=', 0)
284 ->addOrderBy('id', 'DESC')
285 ->execute();
286
287 $this->assertNotEquals($firstContrib['id'], $fetchedTemplate['id']);
288 $this->assertNotEquals($secondContrib['id'], $fetchedTemplate['id']);
289 $this->assertNotEquals($thirdContrib['id'], $fetchedTemplate['id']);
290 $this->assertEquals($templateContributionId, $fetchedTemplate['id']);
291 $this->assertTrue($fetchedTemplate['is_template']);
292 $this->assertFalse($fetchedTemplate['is_test']);
293 $this->assertEquals(1, $templateContribution->count());
294 $templateContribution = $templateContribution->first();
295 $this->assertNotNull($templateContribution['template.field']);
296 $this->assertEquals('Second and most recent Contribution', $templateContribution['template.field']);
297 $this->callAPISuccess('CustomField', 'delete', ['id' => $custom_field['id']]);
298 $this->callAPISuccess('CustomGroup', 'delete', ['id' => $custom_group['id']]);
299 }
300
301 /**
302 * Test that is_template contribution is used where available
303 *
304 * @throws \API_Exception
305 * @throws \CiviCRM_API3_Exception
306 * @throws \Civi\API\Exception\UnauthorizedException
307 */
308 public function testGetTemplateContributionNewTemplate(): void {
309 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
310 // Create the template
311 $templateContrib = $this->callAPISuccess('Contribution', 'create', [
312 'contribution_recur_id' => $contributionRecur['id'],
313 'total_amount' => '3.00',
314 'financial_type_id' => 1,
315 'source' => 'Template Contribution',
316 'payment_instrument_id' => 1,
317 'currency' => 'AUD',
318 'contact_id' => $this->individualCreate(),
319 'contribution_status_id' => 1,
320 'receive_date' => 'yesterday',
321 'is_template' => 1,
322 ]);
323 // Create another normal contrib
324 $this->callAPISuccess('Contribution', 'create', [
325 'contribution_recur_id' => $contributionRecur['id'],
326 'total_amount' => '3.00',
327 'financial_type_id' => 1,
328 'source' => 'Non-template Contribution',
329 'payment_instrument_id' => 1,
330 'currency' => 'USD',
331 'contact_id' => $this->individualCreate(),
332 'contribution_status_id' => 1,
333 'receive_date' => 'yesterday',
334 ]);
335 $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
336 // Fetched template should be the is_template, not the latest contrib
337 $this->assertEquals($fetchedTemplate['id'], $templateContrib['id']);
338
339 $repeatContribution = $this->callAPISuccess('Contribution', 'repeattransaction', [
340 'contribution_status_id' => 'Completed',
341 'contribution_recur_id' => $contributionRecur['id'],
342 ]);
343 $this->assertEquals('Template Contribution', $repeatContribution['values'][$repeatContribution['id']]['source']);
344 $this->assertEquals('AUD', $repeatContribution['values'][$repeatContribution['id']]['currency']);
345 }
346
347 /**
348 * Test to check if correct membership is auto renewed.
349 *
350 * @throws \CRM_Core_Exception|\CiviCRM_API3_Exception
351 */
352 public function testAutoRenewalWhenOneMemberIsDeceased(): void {
353 $contactId1 = $this->individualCreate();
354 $contactId2 = $this->individualCreate();
355 $membershipOrganizationId = $this->organizationCreate();
356
357 $this->createExtraneousContribution();
358 $this->callAPISuccess('Contribution', 'create', [
359 'contact_id' => $contactId1,
360 'receive_date' => '2010-01-20',
361 'financial_type_id' => 'Member Dues',
362 'contribution_status_id' => 'Completed',
363 'total_amount' => 150,
364 ]);
365
366 // create membership type
367 $membershipTypeId1 = (int) $this->callAPISuccess('MembershipType', 'create', [
368 'domain_id' => 1,
369 'member_of_contact_id' => $membershipOrganizationId,
370 'financial_type_id' => 'Member Dues',
371 'duration_unit' => 'month',
372 'duration_interval' => 1,
373 'period_type' => 'rolling',
374 'minimum_fee' => 100,
375 'name' => 'Parent',
376 ])['id'];
377
378 $membershipTypeID = (int) $this->callAPISuccess('MembershipType', 'create', [
379 'domain_id' => 1,
380 'member_of_contact_id' => $membershipOrganizationId,
381 'financial_type_id' => 'Member Dues',
382 'duration_unit' => 'month',
383 'duration_interval' => 1,
384 'period_type' => 'rolling',
385 'minimum_fee' => 50,
386 'name' => 'Child',
387 ])['id'];
388
389 $contactIDs = [
390 $contactId1 => $membershipTypeId1,
391 $contactId2 => $membershipTypeID,
392 ];
393
394 $contributionRecurId = $this->callAPISuccess('contribution_recur', 'create', $this->_params)['id'];
395
396 $priceFields = CRM_Price_BAO_PriceSet::getDefaultPriceSet('membership');
397
398 // prepare order api params.
399 $params = [
400 'contact_id' => $contactId1,
401 'receive_date' => '2010-01-20',
402 'financial_type_id' => 'Member Dues',
403 'contribution_recur_id' => $contributionRecurId,
404 'total_amount' => 150,
405 'api.Payment.create' => ['total_amount' => 150],
406 ];
407
408 foreach ($priceFields as $priceField) {
409 $lineItems = [];
410 $contactId = array_search((int) $priceField['membership_type_id'], $contactIDs, TRUE);
411 $lineItems[1] = [
412 'price_field_id' => $priceField['priceFieldID'],
413 'price_field_value_id' => $priceField['priceFieldValueID'],
414 'label' => $priceField['label'],
415 'field_title' => $priceField['label'],
416 'qty' => 1,
417 'unit_price' => $priceField['amount'],
418 'line_total' => $priceField['amount'],
419 'financial_type_id' => $priceField['financial_type_id'],
420 'entity_table' => 'civicrm_membership',
421 'membership_type_id' => $priceField['membership_type_id'],
422 ];
423 $params['line_items'][] = [
424 'line_item' => $lineItems,
425 'params' => [
426 'contact_id' => $contactId,
427 'membership_type_id' => $priceField['membership_type_id'],
428 'source' => 'Payment',
429 'join_date' => date('Y-m', strtotime('1 month ago')) . '-28',
430 'start_date' => date('Y-m') . '-28',
431 'contribution_recur_id' => $contributionRecurId,
432 'status_id' => 'Pending',
433 'is_override' => 1,
434 ],
435 ];
436 }
437 $order = $this->callAPISuccess('Order', 'create', $params);
438 $contributionId = $order['id'];
439 $membershipId1 = $this->callAPISuccessGetValue('Membership', [
440 'contact_id' => $contactId1,
441 'membership_type_id' => $membershipTypeId1,
442 'return' => 'id',
443 ]);
444
445 $membershipId2 = $this->callAPISuccessGetValue('Membership', [
446 'contact_id' => $contactId2,
447 'membership_type_id' => $membershipTypeID,
448 'return' => 'id',
449 ]);
450
451 // First renewal (2nd payment).
452 $this->callAPISuccess('Contribution', 'repeattransaction', [
453 'original_contribution_id' => $contributionId,
454 'contribution_status_id' => 'Completed',
455 ]);
456
457 // Second Renewal (3rd payment).
458 $this->callAPISuccess('Contribution', 'repeattransaction', [
459 'original_contribution_id' => $contributionId,
460 'contribution_status_id' => 'Completed',
461 ]);
462
463 // Third renewal (4th payment).
464 $this->callAPISuccess('Contribution', 'repeattransaction', ['original_contribution_id' => $contributionId, 'contribution_status_id' => 'Completed']);
465
466 // check line item and membership payment count.
467 $this->validateAllCounts($membershipId1, 4);
468 $this->validateAllCounts($membershipId2, 4);
469
470 $expectedDate = $this->getYearAndMonthFromOffset(4);
471 // check membership end date.
472 foreach ([$membershipId1, $membershipId2] as $mId) {
473 $endDate = $this->callAPISuccessGetValue('Membership', [
474 'id' => $mId,
475 'return' => 'end_date',
476 ]);
477 $this->assertEquals("{$expectedDate['year']}-{$expectedDate['month']}-27", $endDate, ts('End date incorrect.'));
478 }
479
480 // At this moment Contact 2 is deceased, but we wait until payment is recorded in civi before marking the contact deceased.
481 // At payment Gateway we update the amount from 150 to 100
482 // IPN is recorded for subsequent payment (5th payment).
483 $contribution = $this->callAPISuccess('Contribution', 'repeattransaction', [
484 'original_contribution_id' => $contributionId,
485 'contribution_status_id' => 'Completed',
486 'total_amount' => '100',
487 ]);
488
489 // now we mark the contact2 as deceased.
490 $this->callAPISuccess('Contact', 'create', [
491 'id' => $contactId2,
492 'is_deceased' => 1,
493 ]);
494
495 // set membership recurring to null.
496 $this->callAPISuccess('Membership', 'create', [
497 'id' => $membershipId2,
498 'contribution_recur_id' => NULL,
499 ]);
500
501 $this->callAPISuccess('Contribution', 'delete', ['id' => $contribution['id']]);
502 unset($params['line_items'][1]);
503 $params['total_amount'] = 100;
504 $params['line_items'][0]['params']['id'] = $membershipId1;
505 $params['api.Payment.create']['total_amount'] = 100;
506
507 $order = $this->callAPISuccess('Order', 'create', $params);
508
509 // check line item and membership payment count.
510 $this->validateAllCounts($membershipId1, 5);
511 $this->validateAllCounts($membershipId2, 4);
512
513 $checkAgainst = $this->callAPISuccessGetSingle('Membership', [
514 'id' => $membershipId2,
515 'return' => ['end_date', 'status_id'],
516 ]);
517
518 // record next subsequent payment (6th payment).
519 $this->callAPISuccess('Contribution', 'repeattransaction', [
520 'original_contribution_id' => $order['id'],
521 'contribution_status_id' => 'Completed',
522 'total_amount' => '100',
523 ]);
524
525 // check membership id 1 is renewed
526 $endDate = $this->callAPISuccessGetValue('Membership', [
527 'id' => $membershipId1,
528 'return' => 'end_date',
529 ]);
530 $expectedDate = $this->getYearAndMonthFromOffset(6);
531 $this->assertEquals("{$expectedDate['year']}-{$expectedDate['month']}-27", $endDate, ts('End date incorrect.'));
532 // check line item and membership payment count.
533 $this->validateAllCounts($membershipId1, 6);
534 $this->validateAllCounts($membershipId2, 4);
535
536 // check if membership status and end date is not changed.
537 $membership2 = $this->callAPISuccessGetSingle('Membership', [
538 'id' => $membershipId2,
539 'return' => ['end_date', 'status_id'],
540 ]);
541 $this->assertSame($membership2, $checkAgainst);
542 }
543
544 /**
545 * Check line item and membership payment count.
546 *
547 * @param int $membershipId
548 * @param int $count
549 *
550 * @throws \CRM_Core_Exception
551 */
552 public function validateAllCounts(int $membershipId, int $count): void {
553 $memPayParams = [
554 'membership_id' => $membershipId,
555 ];
556 $lineItemParams = [
557 'entity_id' => $membershipId,
558 'entity_table' => 'civicrm_membership',
559 'contribution_id' => ['>' => 0],
560 ];
561 $this->callAPISuccessGetCount('LineItem', $lineItemParams, $count);
562 $this->callAPISuccessGetCount('MembershipPayment', $memPayParams, $count);
563 }
564
565 /**
566 * Given a number of months offset, get the year and month.
567 * Note the way php arithmetic works, using strtotime('+x months') doesn't
568 * work because it will roll over the day accounting for different number
569 * of days in the month, but we want the same day of the month, x months
570 * from now.
571 * e.g. July 31 + 4 months will return Dec 1 if using php functions, but
572 * we want Nov 31.
573 *
574 * @param int $offset
575 * @param int|null $year Optional input year to start
576 * @param int|null $month Optional input month to start
577 *
578 * @return array
579 * ['year' => int, 'month' => int]
580 */
581 private function getYearAndMonthFromOffset(int $offset, int $year = NULL, int $month = NULL): array {
582 $dateInfo = [
583 'year' => $year ?? (int) date('Y'),
584 'month' => ($month ?? (int) date('m')) + $offset,
585 ];
586 if ($dateInfo['month'] > 12) {
587 $dateInfo['year']++;
588 $dateInfo['month'] -= 12;
589 }
590 if ($dateInfo['month'] < 10) {
591 $dateInfo['month'] = "0{$dateInfo['month']}";
592 }
593
594 return $dateInfo;
595 }
596
597 /**
598 * Test getYearAndMonthFromOffset
599 *
600 * @dataProvider yearMonthProvider
601 *
602 * @param array $input
603 * @param array $expected
604 */
605 public function testGetYearAndMonthFromOffset(array $input, array $expected): void {
606 $this->assertEquals($expected, $this->getYearAndMonthFromOffset($input[0], $input[1], $input[2]));
607 }
608
609 /**
610 * data provider for testGetYearAndMonthFromOffset
611 */
612 public function yearMonthProvider(): array {
613 return [
614 // input = offset, year, current month
615 ['input' => [4, 2020, 1], 'output' => ['year' => '2020', 'month' => '05']],
616 ['input' => [6, 2020, 1], 'output' => ['year' => '2020', 'month' => '07']],
617 ['input' => [4, 2020, 2], 'output' => ['year' => '2020', 'month' => '06']],
618 ['input' => [6, 2020, 2], 'output' => ['year' => '2020', 'month' => '08']],
619 ['input' => [4, 2020, 3], 'output' => ['year' => '2020', 'month' => '07']],
620 ['input' => [6, 2020, 3], 'output' => ['year' => '2020', 'month' => '09']],
621 ['input' => [4, 2020, 4], 'output' => ['year' => '2020', 'month' => '08']],
622 ['input' => [6, 2020, 4], 'output' => ['year' => '2020', 'month' => '10']],
623 ['input' => [4, 2020, 5], 'output' => ['year' => '2020', 'month' => '09']],
624 ['input' => [6, 2020, 5], 'output' => ['year' => '2020', 'month' => '11']],
625 ['input' => [4, 2020, 6], 'output' => ['year' => '2020', 'month' => '10']],
626 ['input' => [6, 2020, 6], 'output' => ['year' => '2020', 'month' => '12']],
627 ['input' => [4, 2020, 7], 'output' => ['year' => '2020', 'month' => '11']],
628 ['input' => [6, 2020, 7], 'output' => ['year' => '2021', 'month' => '01']],
629 ['input' => [4, 2020, 8], 'output' => ['year' => '2020', 'month' => '12']],
630 ['input' => [6, 2020, 8], 'output' => ['year' => '2021', 'month' => '02']],
631 ['input' => [4, 2020, 9], 'output' => ['year' => '2021', 'month' => '01']],
632 ['input' => [6, 2020, 9], 'output' => ['year' => '2021', 'month' => '03']],
633 ['input' => [4, 2020, 10], 'output' => ['year' => '2021', 'month' => '02']],
634 ['input' => [6, 2020, 10], 'output' => ['year' => '2021', 'month' => '04']],
635 ['input' => [4, 2020, 11], 'output' => ['year' => '2021', 'month' => '03']],
636 ['input' => [6, 2020, 11], 'output' => ['year' => '2021', 'month' => '05']],
637 ['input' => [4, 2020, 12], 'output' => ['year' => '2021', 'month' => '04']],
638 ['input' => [6, 2020, 12], 'output' => ['year' => '2021', 'month' => '06']],
639 ];
640 }
641
642 /**
643 * Test Recurring Contribution Email Receipt Flag
644 *
645 * @throws \CRM_Core_Exception
646 */
647 public function testContributionEmailReceipt(): void {
648 $createParams = $this->_params;
649 unset($createParams['trxn_id'], $createParams['invoice_id']);
650
651 // pass null value to is_email_receipt
652 $createParams['is_email_receipt'] = NULL;
653 $recurring1 = $this->callAPISuccess('ContributionRecur', 'create', $createParams);
654 $recurring1Get = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $recurring1['id']]);
655 // default is_email_receipt column value is 1
656 $this->assertEquals('1', $recurring1Get['is_email_receipt']);
657
658 // pass empty value to is_email_receipt
659 $createParams['is_email_receipt'] = '';
660 $recurring2 = $this->callAPISuccess('ContributionRecur', 'create', $createParams);
661 $recurring2 = ContributionRecur::get(FALSE)->addWhere('id', '=', $recurring2['id'])->addSelect('is_email_receipt')->execute()->first();
662 $this->assertEquals(NULL, $recurring2['is_email_receipt']);
663
664 // Pass 0 value to is_email_receipt.
665 $createParams['is_email_receipt'] = 0;
666 $recurring3 = $this->callAPISuccess('ContributionRecur', 'create', $createParams);
667 $recurring3Get = $this->callAPISuccess('ContributionRecur', 'getsingle', ['id' => $recurring3['id']]);
668 $this->assertEquals('0', $recurring3Get['is_email_receipt']);
669 }
670
671 }