Merge pull request #16529 from mattwire/altermailing_templatetype
[civicrm-core.git] / tests / phpunit / CRM / Contribute / BAO / ContributionRecurTest.php
CommitLineData
bbf58b03
EM
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
bbf58b03 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 |
bbf58b03 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
bbf58b03 11
7fe37828
EM
12/**
13 * Class CRM_Contribute_BAO_ContributionRecurTest
acb109b7 14 * @group headless
7fe37828 15 */
bbf58b03 16class CRM_Contribute_BAO_ContributionRecurTest extends CiviUnitTestCase {
9099cab3 17 protected $_params = [];
bbf58b03 18
00be9182 19 public function setUp() {
bbf58b03
EM
20 parent::setUp();
21 $this->_ids['payment_processor'] = $this->paymentProcessorCreate();
9099cab3 22 $this->_params = [
bbf58b03
EM
23 'contact_id' => $this->individualCreate(),
24 'amount' => 3.00,
25 'frequency_unit' => 'week',
26 'frequency_interval' => 1,
27 'installments' => 2,
28 'start_date' => 'yesterday',
29 'create_date' => 'yesterday',
30 'modified_date' => 'yesterday',
31 'cancel_date' => NULL,
32 'end_date' => '+ 2 weeks',
33 'processor_id' => '643411460836',
34 'trxn_id' => 'e0d0808e26f3e661c6c18eb7c039d363',
35 'invoice_id' => 'e0d0808e26f3e661c6c18eb7c039d363',
36 'contribution_status_id' => 1,
37 'is_test' => 0,
38 'cycle_day' => 1,
39 'next_sched_contribution_date' => '+ 1 week',
40 'failure_count' => 0,
41 'failure_retry_date' => NULL,
42 'auto_renew' => 0,
43 'currency' => 'USD',
44 'payment_processor_id' => $this->_ids['payment_processor'],
45 'is_email_receipt' => 1,
46 'financial_type_id' => 1,
47 'payment_instrument_id' => 1,
48 'campaign_id' => NULL,
9099cab3 49 ];
bbf58b03
EM
50 }
51
00be9182 52 public function teardown() {
9099cab3 53 $this->quickCleanup(['civicrm_contribution_recur', 'civicrm_payment_processor']);
bbf58b03
EM
54 }
55
56 /**
fe482240
EM
57 * Test that an object can be retrieved & saved (per CRM-14986).
58 *
59 * This has been causing a DB error so we are checking for absence of error
bbf58b03 60 */
00be9182 61 public function testFindSave() {
bbf58b03
EM
62 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
63 $dao = new CRM_Contribute_BAO_ContributionRecur();
64 $dao->id = $contributionRecur['id'];
65 $dao->find(TRUE);
66 $dao->is_email_receipt = 0;
67 $dao->save();
68 }
69
70 /**
fe482240
EM
71 * Test cancellation works per CRM-14986.
72 *
73 * We are checking for absence of error.
bbf58b03 74 */
00be9182 75 public function testCancelRecur() {
bbf58b03 76 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
9cfc631e 77 CRM_Contribute_BAO_ContributionRecur::cancelRecurContribution(['id' => $contributionRecur['id']]);
bbf58b03
EM
78 }
79
748bcfb2 80 /**
0b2f1f29 81 * Test checking if contribution recur object can allow for changes to financial types.
748bcfb2
SL
82 *
83 */
84 public function testSupportFinancialTypeChange() {
85 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
9099cab3 86 $this->callAPISuccess('Contribution', 'create', [
748bcfb2
SL
87 'contribution_recur_id' => $contributionRecur['id'],
88 'total_amount' => '3.00',
89 'financial_type_id' => 1,
90 'payment_instrument_id' => 1,
91 'currency' => 'USD',
92 'contact_id' => $this->individualCreate(),
93 'contribution_status_id' => 1,
46f459f2 94 'receive_date' => 'yesterday',
9099cab3 95 ]);
748bcfb2
SL
96 $this->assertTrue(CRM_Contribute_BAO_ContributionRecur::supportsFinancialTypeChange($contributionRecur['id']));
97 }
98
3d6bf1a7
EE
99 /**
100 * Test we don't change unintended fields on API edit
101 */
102 public function testUpdateRecur() {
103 $createParams = $this->_params;
104 $createParams['currency'] = 'XAU';
105 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $createParams);
9099cab3 106 $editParams = [
3d6bf1a7
EE
107 'id' => $contributionRecur['id'],
108 'end_date' => '+ 4 weeks',
9099cab3 109 ];
3d6bf1a7
EE
110 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $editParams);
111 $dao = new CRM_Contribute_BAO_ContributionRecur();
112 $dao->id = $contributionRecur['id'];
113 $dao->find(TRUE);
114 $this->assertEquals('XAU', $dao->currency, 'Edit clobbered recur currency');
115 }
116
0b2f1f29
AS
117 /**
118 * Check test contributions aren't picked up as template for non-test recurs
119 *
120 */
121 public function testGetTemplateContributionMatchTest1() {
122 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
123 // Create a first contrib
124 $firstContrib = $this->callAPISuccess('Contribution', 'create', [
125 'contribution_recur_id' => $contributionRecur['id'],
126 'total_amount' => '3.00',
127 'financial_type_id' => 1,
128 'payment_instrument_id' => 1,
129 'currency' => 'USD',
130 'contact_id' => $this->individualCreate(),
131 'contribution_status_id' => 1,
132 'receive_date' => 'yesterday',
133 ]);
134 // Create a test contrib - should not be picked up as template for non-test recur
135 $this->callAPISuccess('Contribution', 'create', [
136 'contribution_recur_id' => $contributionRecur['id'],
137 'total_amount' => '3.00',
138 'financial_type_id' => 1,
139 'payment_instrument_id' => 1,
140 'currency' => 'USD',
141 'contact_id' => $this->individualCreate(),
142 'contribution_status_id' => 1,
143 'receive_date' => 'yesterday',
144 'is_test' => 1,
145 ]);
146 $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
147 $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']);
148 }
149
150 /**
151 * Check non-test contributions aren't picked up as template for test recurs
152 *
153 */
154 public function testGetTemplateContributionMatchTest() {
155 $params = $this->_params;
156 $params['is_test'] = 1;
157 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $params);
158 // Create a first test contrib
159 $firstContrib = $this->callAPISuccess('Contribution', 'create', [
160 'contribution_recur_id' => $contributionRecur['id'],
161 'total_amount' => '3.00',
162 'financial_type_id' => 1,
163 'payment_instrument_id' => 1,
164 'currency' => 'USD',
165 'contact_id' => $this->individualCreate(),
166 'contribution_status_id' => 1,
167 'receive_date' => 'yesterday',
168 'is_test' => 1,
169 ]);
170 // Create a non-test contrib - should not be picked up as template for non-test recur
171 // This shouldn't occur - a live contrib against a test recur, but that's not the point...
172 $this->callAPISuccess('Contribution', 'create', [
173 'contribution_recur_id' => $contributionRecur['id'],
174 'total_amount' => '3.00',
175 'financial_type_id' => 1,
176 'payment_instrument_id' => 1,
177 'currency' => 'USD',
178 'contact_id' => $this->individualCreate(),
179 'contribution_status_id' => 1,
180 'receive_date' => 'yesterday',
181 'is_test' => 0,
182 ]);
183 $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
184 $this->assertEquals($firstContrib['id'], $fetchedTemplate['id']);
185 }
186
f306dbeb
AS
187 /**
188 * Test that is_template contribution is used where available
189 *
190 */
191 public function testGetTemplateContributionNewTemplate() {
192 $contributionRecur = $this->callAPISuccess('contribution_recur', 'create', $this->_params);
193 // Create the template
194 $templateContrib = $this->callAPISuccess('Contribution', 'create', [
195 'contribution_recur_id' => $contributionRecur['id'],
196 'total_amount' => '3.00',
197 'financial_type_id' => 1,
198 'payment_instrument_id' => 1,
199 'currency' => 'USD',
200 'contact_id' => $this->individualCreate(),
201 'contribution_status_id' => 1,
202 'receive_date' => 'yesterday',
203 'is_template' => 1,
204 ]);
205 // Create another normal contrib
206 $this->callAPISuccess('Contribution', 'create', [
207 'contribution_recur_id' => $contributionRecur['id'],
208 'total_amount' => '3.00',
209 'financial_type_id' => 1,
210 'payment_instrument_id' => 1,
211 'currency' => 'USD',
212 'contact_id' => $this->individualCreate(),
213 'contribution_status_id' => 1,
214 'receive_date' => 'yesterday',
215 ]);
216 $fetchedTemplate = CRM_Contribute_BAO_ContributionRecur::getTemplateContribution($contributionRecur['id']);
217 // Fetched template should be the is_template, not the latest contrib
218 $this->assertEquals($fetchedTemplate['id'], $templateContrib['id']);
219 }
220
bbf58b03 221}