Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
10 | | CiviCRM is free software; you can copy, modify, and distribute it | | |
11 | | under the terms of the GNU Affero General Public License | | |
12 | | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. | | |
13 | | | | |
14 | | CiviCRM is distributed in the hope that it will be useful, but | | |
15 | | WITHOUT ANY WARRANTY; without even the implied warranty of | | |
16 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | | |
17 | | See the GNU Affero General Public License for more details. | | |
18 | | | | |
19 | | You should have received a copy of the GNU Affero General Public | | |
20 | | License and the CiviCRM Licensing Exception along | | |
21 | | with this program; if not, contact CiviCRM LLC | | |
22 | | at info[AT]civicrm[DOT]org. If you have questions about the | | |
23 | | GNU Affero General Public License or the licensing of CiviCRM, | | |
24 | | see the CiviCRM license FAQ at http://civicrm.org/licensing | | |
25 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
e9479dcf EM |
28 | /** |
29 | * Class CRM_Core_Payment_BaseIPNTest | |
acb109b7 | 30 | * @group headless |
e9479dcf | 31 | */ |
6a488035 | 32 | class CRM_Core_Payment_BaseIPNTest extends CiviUnitTestCase { |
00e3870f | 33 | |
2b57dd9f | 34 | protected $_financialTypeId; |
6a488035 TO |
35 | protected $_contributionParams; |
36 | protected $_contactId; | |
37 | protected $_contributionId; | |
38 | protected $_participantId; | |
39 | protected $_pledgeId; | |
40 | protected $_eventId; | |
41 | protected $_processorId; | |
42 | protected $_contributionRecurParams; | |
43 | protected $_paymentProcessor; | |
3aaa68fb | 44 | |
45 | /** | |
46 | * Parameters to create a membership. | |
47 | * | |
48 | * @var array | |
49 | */ | |
9099cab3 | 50 | protected $_membershipParams = []; |
3aaa68fb | 51 | |
52 | /** | |
53 | * IPN instance. | |
54 | * | |
55 | * @var CRM_Core_Payment_BaseIPN | |
56 | */ | |
6a488035 TO |
57 | protected $IPN; |
58 | protected $_recurId; | |
59 | protected $_membershipId; | |
60 | protected $input; | |
61 | protected $ids; | |
62 | protected $objects; | |
c3543e4a | 63 | |
64 | /** | |
65 | * @var int | |
66 | */ | |
67 | protected $_membershipTypeID; | |
68 | ||
69 | /** | |
70 | * @var int | |
71 | */ | |
72 | protected $_membershipStatusID; | |
430ae6dd TO |
73 | public $DBResetRequired = FALSE; |
74 | ||
3aaa68fb | 75 | /** |
76 | * Setup function. | |
77 | */ | |
00be9182 | 78 | public function setUp() { |
6a488035 | 79 | parent::setUp(); |
9099cab3 CW |
80 | $this->_processorId = $this->paymentProcessorAuthorizeNetCreate(['is_test' => 0]); |
81 | $this->input = $this->ids = $this->objects = []; | |
0dbefed3 | 82 | $this->IPN = new CRM_Core_Payment_AuthorizeNetIPN($this->input); |
6a488035 TO |
83 | |
84 | $this->_contactId = $this->individualCreate(); | |
85 | $this->ids['contact'] = $this->_contactId; | |
2b57dd9f | 86 | $this->_financialTypeId = 1; |
6a488035 | 87 | |
9099cab3 | 88 | $this->_contributionParams = [ |
6a488035 | 89 | 'contact_id' => $this->_contactId, |
2b57dd9f | 90 | 'financial_type_id' => $this->_financialTypeId, |
1c34cb9b | 91 | 'receive_date' => date('Ymd'), |
6a488035 TO |
92 | 'total_amount' => 150.00, |
93 | 'invoice_id' => 'c8acb91e080ad7bd8a2adc119c192885', | |
94 | 'currency' => 'USD', | |
95 | 'contribution_recur_id' => $this->_recurId, | |
6a488035 | 96 | 'contribution_status_id' => 2, |
9099cab3 | 97 | ]; |
aeeaba66 | 98 | $contribution = $this->callAPISuccess('contribution', 'create', $this->_contributionParams); |
6a488035 TO |
99 | $this->_contributionId = $contribution['id']; |
100 | ||
101 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
102 | $contribution->id = $this->_contributionId; | |
92915c55 | 103 | $contribution->find(TRUE); |
6a488035 TO |
104 | $this->objects['contribution'] = $contribution; |
105 | } | |
106 | ||
3aaa68fb | 107 | /** |
108 | * Tear down after class. | |
109 | */ | |
00be9182 | 110 | public function tearDown() { |
b38530f2 | 111 | $this->quickCleanUpFinancialEntities(); |
cc0c30cc | 112 | CRM_Member_PseudoConstant::membershipType(NULL, TRUE); |
6a488035 TO |
113 | CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); |
114 | } | |
115 | ||
116 | /** | |
eceb18cc | 117 | * Test the LoadObjects function with recurring membership data. |
6a488035 | 118 | */ |
00be9182 | 119 | public function testLoadMembershipObjects() { |
6a488035 TO |
120 | $this->_setUpMembershipObjects(); |
121 | $this->_setUpRecurringContribution(); | |
122 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
ba4a1892 TM |
123 | $this->assertFalse(empty($this->objects['membership'])); |
124 | $this->assertArrayHasKey($this->_membershipTypeID, $this->objects['membership']); | |
6a488035 | 125 | $this->assertTrue(is_a($this->objects['membership'][$this->_membershipTypeID], 'CRM_Member_BAO_Membership')); |
2b57dd9f | 126 | $this->assertTrue(is_a($this->objects['financialType'], 'CRM_Financial_BAO_FinancialType')); |
1c34cb9b EM |
127 | $this->assertFalse(empty($this->objects['contributionRecur'])); |
128 | $this->assertFalse(empty($this->objects['paymentProcessor'])); | |
6a488035 | 129 | } |
92915c55 | 130 | |
c3543e4a | 131 | /** |
132 | * Test the LoadObjects function with recurring membership data. | |
133 | */ | |
134 | public function testLoadMembershipObjectsNoLeakage() { | |
135 | $this->_setUpMembershipObjects(); | |
136 | $this->_setUpRecurringContribution(); | |
137 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
138 | $this->assertEquals('Anthony', $this->objects['contact']->first_name); | |
139 | ||
9099cab3 | 140 | $this->ids['contact'] = $this->_contactId = $this->individualCreate([ |
3aaa68fb | 141 | 'first_name' => 'Donald', |
142 | 'last_name' => 'Duck', | |
39b959db | 143 | 'email' => 'the-don@duckville.com', |
9099cab3 CW |
144 | ]); |
145 | $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_contributionParams, ['invoice_id' => 'abc'])); | |
c3543e4a | 146 | $this->_contributionId = $contribution['id']; |
147 | $this->_setUpMembershipObjects(); | |
148 | $this->input['invoiceID'] = 'abc'; | |
149 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
150 | $this->assertEquals('Donald', $this->objects['contact']->first_name); | |
151 | } | |
152 | ||
6a488035 | 153 | /** |
eceb18cc | 154 | * Test the LoadObjects function with recurring membership data. |
6a488035 | 155 | */ |
00be9182 | 156 | public function testLoadMembershipObjectsLoadAll() { |
6a488035 TO |
157 | $this->_setUpMembershipObjects(); |
158 | $this->_setUpRecurringContribution(); | |
159 | unset($this->ids['membership']); | |
160 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
161 | $contribution->id = $this->_contributionId; | |
92915c55 | 162 | $contribution->find(TRUE); |
276e3ec6 | 163 | $contribution->loadRelatedObjects($this->input, $this->ids, TRUE); |
ba4a1892 TM |
164 | $this->assertFalse(empty($contribution->_relatedObjects['membership'])); |
165 | $this->assertArrayHasKey($this->_membershipTypeID, $contribution->_relatedObjects['membership']); | |
6a488035 | 166 | $this->assertTrue(is_a($contribution->_relatedObjects['membership'][$this->_membershipTypeID], 'CRM_Member_BAO_Membership')); |
2b57dd9f | 167 | $this->assertTrue(is_a($contribution->_relatedObjects['financialType'], 'CRM_Financial_BAO_FinancialType')); |
1c34cb9b EM |
168 | $this->assertFalse(empty($contribution->_relatedObjects['contributionRecur'])); |
169 | $this->assertFalse(empty($contribution->_relatedObjects['paymentProcessor'])); | |
6a488035 | 170 | } |
92915c55 | 171 | |
6a488035 | 172 | /** |
eceb18cc | 173 | * Test the LoadObjects function with recurring membership data. |
6a488035 | 174 | */ |
00be9182 | 175 | public function testsendMailMembershipObjects() { |
6a488035 | 176 | $this->_setUpMembershipObjects(); |
9099cab3 | 177 | $values = []; |
6a488035 TO |
178 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); |
179 | $msg = $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, TRUE); | |
1c34cb9b | 180 | $this->assertTrue(is_array($msg), "Message returned as an array in line"); |
6a488035 | 181 | $this->assertEquals('Mr. Anthony Anderson II', $msg['to']); |
6a488035 TO |
182 | $this->assertContains('Membership Type: General', $msg['body']); |
183 | } | |
184 | ||
c3543e4a | 185 | /** |
186 | * Test the LoadObjects function data does not leak. | |
187 | * | |
188 | * If more than one iteration takes place the variables should not leak. | |
189 | */ | |
190 | public function testSendMailMembershipObjectsNoLeakage() { | |
191 | $this->_setUpMembershipObjects(); | |
9099cab3 | 192 | $values = []; |
c3543e4a | 193 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); |
194 | $msg = $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, TRUE); | |
195 | $this->assertEquals('Mr. Anthony Anderson II', $msg['to']); | |
196 | $this->assertContains('Membership Type: General', $msg['body']); | |
197 | ||
9099cab3 CW |
198 | $this->ids['contact'] = $this->_contactId = $this->individualCreate(['prefix_id' => 'Dr.', 'first_name' => 'Donald', 'last_name' => 'Duck', 'email' => 'the-don@duckville.com']); |
199 | $contribution = $this->callAPISuccess('contribution', 'create', array_merge($this->_contributionParams, ['invoice_id' => 'abc'])); | |
c3543e4a | 200 | $this->_contributionId = $contribution['id']; |
201 | ||
9099cab3 | 202 | $this->_membershipTypeID = $this->membershipTypeCreate(['name' => 'Fowl']); |
c3543e4a | 203 | $this->_setUpMembershipObjects(); |
204 | $this->input['invoiceID'] = 'abc'; | |
205 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
206 | $this->assertEquals('Donald', $this->objects['contact']->first_name); | |
207 | $msg = $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, TRUE); | |
208 | $this->assertEquals('Dr. Donald Duck II', $msg['to']); | |
209 | $this->assertContains('Membership Type: Fowl', $msg['body']); | |
210 | } | |
211 | ||
6a488035 | 212 | /** |
eceb18cc | 213 | * Test the LoadObjects function with recurring membership data. |
6a488035 | 214 | */ |
00be9182 | 215 | public function testsendMailMembershipWithoutLoadObjects() { |
6a488035 | 216 | $this->_setUpMembershipObjects(); |
9099cab3 | 217 | $values = []; |
6a488035 TO |
218 | $msg = $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, TRUE); |
219 | $this->assertTrue(is_array($msg), "Message returned as an array in line" . __LINE__); | |
220 | $this->assertEquals('Mr. Anthony Anderson II', $msg['to']); | |
6a488035 TO |
221 | $this->assertContains('Membership Type: General', $msg['body']); |
222 | } | |
c490a46a CW |
223 | |
224 | /** | |
eceb18cc | 225 | * Test that loadObjects works with participant values. |
c490a46a | 226 | */ |
00be9182 | 227 | public function testLoadParticipantObjects() { |
6a488035 TO |
228 | $this->_setUpParticipantObjects(); |
229 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
ba4a1892 | 230 | $this->assertFalse(empty($this->objects['participant'])); |
6a488035 | 231 | $this->assertTrue(is_a($this->objects['participant'], 'CRM_Event_BAO_Participant')); |
2b57dd9f | 232 | $this->assertTrue(is_a($this->objects['financialType'], 'CRM_Financial_BAO_FinancialType')); |
6a488035 TO |
233 | $this->assertFalse(empty($this->objects['event'])); |
234 | $this->assertTrue(is_a($this->objects['event'], 'CRM_Event_BAO_Event')); | |
235 | $this->assertTrue(is_a($this->objects['contribution'], 'CRM_Contribute_BAO_Contribution')); | |
236 | $this->assertFalse(empty($this->objects['event']->id)); | |
237 | } | |
238 | ||
239 | /** | |
eceb18cc | 240 | * Test the LoadObjects function with a participant. |
6a488035 | 241 | */ |
00be9182 | 242 | public function testComposeMailParticipant() { |
6a488035 TO |
243 | $this->_setUpParticipantObjects(); |
244 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
9099cab3 | 245 | $values = []; |
6a488035 | 246 | $this->assertFalse(empty($this->objects['event'])); |
92915c55 | 247 | $msg = $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, TRUE); |
afc59fef | 248 | $this->assertContains('registration has been received and your status has been updated to Attended.', $msg['body']); |
6a488035 TO |
249 | $this->assertContains('Annual CiviCRM meet', $msg['html']); |
250 | } | |
251 | ||
252 | /** | |
6a488035 | 253 | */ |
00be9182 | 254 | public function testComposeMailParticipantObjects() { |
6a488035 | 255 | $this->_setUpParticipantObjects(); |
9099cab3 | 256 | $values = []; |
6a488035 TO |
257 | $msg = $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, TRUE); |
258 | $this->assertTrue(is_array($msg), "Message returned as an array in line" . __LINE__); | |
259 | $this->assertEquals('Mr. Anthony Anderson II', $msg['to']); | |
12ff7379 | 260 | $this->assertContains('Thank you for your registration', $msg['body']); |
6a488035 TO |
261 | } |
262 | ||
263 | /** | |
eceb18cc | 264 | * Test the LoadObjects function with recurring membership data. |
6a488035 | 265 | */ |
00be9182 | 266 | public function testsendMailParticipantObjectsCheckLog() { |
6a488035 | 267 | $this->_setUpParticipantObjects(); |
9099cab3 | 268 | $values = []; |
92915c55 | 269 | $mut = new CiviMailUtils($this, TRUE); |
6a488035 TO |
270 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); |
271 | $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, FALSE); | |
9099cab3 | 272 | $mut->checkMailLog([ |
12ff7379 | 273 | 'Thank you for your registration', |
39b959db SL |
274 | 'Annual CiviCRM meet', |
275 | 'Mr. Anthony Anderson II', | |
9099cab3 | 276 | ]); |
6a488035 TO |
277 | $mut->stop(); |
278 | } | |
c490a46a | 279 | |
6a488035 | 280 | /** |
eceb18cc | 281 | * Test the LoadObjects function with recurring membership data. |
6a488035 | 282 | */ |
00be9182 | 283 | public function testsendMailParticipantObjectsNoMail() { |
6a488035 TO |
284 | $this->_setUpParticipantObjects(); |
285 | $event = new CRM_Event_BAO_Event(); | |
286 | $event->id = $this->_eventId; | |
287 | $event->is_email_confirm = FALSE; | |
288 | $event->save(); | |
9099cab3 CW |
289 | $values = []; |
290 | $tablesToTruncate = [ | |
6a488035 | 291 | 'civicrm_mailing_spool', |
9099cab3 | 292 | ]; |
6a488035 | 293 | $this->quickCleanup($tablesToTruncate, FALSE); |
92915c55 | 294 | $mut = new CiviMailUtils($this, TRUE); |
6a488035 TO |
295 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); |
296 | $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, FALSE); | |
297 | $mut->assertMailLogEmpty('no mail should have been send as event set to no confirm'); | |
298 | $mut->stop(); | |
299 | } | |
300 | ||
c490a46a | 301 | /** |
eceb18cc | 302 | * Test that loadObjects works with participant values. |
c490a46a | 303 | */ |
00be9182 | 304 | public function testLoadPledgeObjects() { |
6a488035 TO |
305 | $this->_setUpPledgeObjects(); |
306 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
ba4a1892 | 307 | $this->assertFalse(empty($this->objects['pledge_payment'][0])); |
2b57dd9f | 308 | $this->assertTrue(is_a($this->objects['financialType'], 'CRM_Financial_BAO_FinancialType')); |
6a488035 TO |
309 | $this->assertTrue(is_a($this->objects['contribution'], 'CRM_Contribute_BAO_Contribution')); |
310 | $this->assertTrue(is_a($this->objects['pledge_payment'][0], 'CRM_Pledge_BAO_PledgePayment')); | |
311 | $this->assertFalse(empty($this->objects['pledge_payment'][0]->id)); | |
2b57dd9f | 312 | $this->assertEquals($this->_financialTypeId, $this->objects['financialType']->id); |
6a488035 TO |
313 | $this->assertEquals($this->_processorId, $this->objects['paymentProcessor']['id']); |
314 | $this->assertEquals($this->_contributionId, $this->objects['contribution']->id); | |
315 | $this->assertEquals($this->_contactId, $this->objects['contact']->id); | |
316 | $this->assertEquals($this->_pledgeId, $this->objects['pledge_payment'][0]->pledge_id); | |
317 | } | |
318 | ||
c490a46a | 319 | /** |
eceb18cc | 320 | * Test that loadObjects works with participant values. |
c490a46a | 321 | */ |
00be9182 | 322 | public function testLoadPledgeObjectsInvalidPledgeID() { |
6a488035 TO |
323 | $this->_setUpPledgeObjects(); |
324 | $this->ids['pledge_payment'][0] = 0; | |
9099cab3 | 325 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['return_error' => 1]); |
6a488035 TO |
326 | $this->assertArrayHasKey('error_message', $result); |
327 | $this->assertArrayNotHasKey('pledge_payment', $this->objects); | |
328 | $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']); | |
329 | $this->ids['pledge_payment'][0] = NULL; | |
9099cab3 | 330 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['return_error' => 1]); |
6a488035 TO |
331 | $this->assertArrayHasKey('error_message', $result); |
332 | $this->assertArrayNotHasKey('pledge_payment', $this->objects); | |
333 | $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']); | |
334 | $this->ids['pledge_payment'][0] = ''; | |
9099cab3 | 335 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['return_error' => 1]); |
6a488035 TO |
336 | $this->assertArrayHasKey('error_message', $result); |
337 | $this->assertArrayNotHasKey('pledge_payment', $this->objects); | |
338 | $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']); | |
339 | ||
6a488035 | 340 | $this->ids['pledge_payment'][0] = 999; |
9099cab3 | 341 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, $this->_processorId, ['return_error' => 1]); |
6a488035 TO |
342 | $this->assertArrayHasKey('error_message', $result); |
343 | $this->assertEquals('Could not find pledge payment record: 999', $result['error_message']); | |
344 | } | |
345 | ||
346 | /** | |
eceb18cc | 347 | * Test the LoadObjects function with a pledge. |
6a488035 | 348 | */ |
00be9182 | 349 | public function testsendMailPledge() { |
6a488035 | 350 | $this->_setUpPledgeObjects(); |
9099cab3 | 351 | $values = []; |
6a488035 TO |
352 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, NULL); |
353 | $msg = $this->IPN->sendMail($this->input, $this->ids, $this->objects, $values, FALSE, TRUE); | |
354 | $this->assertContains('Contribution Information', $msg['html']); | |
355 | } | |
356 | ||
357 | /** | |
3aaa68fb | 358 | * Test that an error is returned if required set & no contribution page. |
6a488035 | 359 | */ |
00be9182 | 360 | public function testRequiredWithoutProcessorID() { |
6a488035 | 361 | $this->_setUpPledgeObjects(); |
9099cab3 | 362 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['return_error' => 1]); |
6a488035 TO |
363 | $this->assertArrayHasKey('error_message', $result); |
364 | $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']); | |
365 | // error is only returned if $required set to True | |
9099cab3 | 366 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, NULL, ['return_error' => 1]); |
6a488035 TO |
367 | $this->assertFalse(is_array($result)); |
368 | //check that error is not returned if error checking not set | |
9099cab3 | 369 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['log_error' => 1]); |
6a488035 TO |
370 | $this->assertFalse(is_array($result)); |
371 | } | |
372 | ||
373 | /** | |
6a488035 TO |
374 | * Test that an error is not if required set & no processor ID |
375 | */ | |
00be9182 | 376 | public function testRequiredWithContributionPage() { |
6a488035 | 377 | $this->_setUpContributionObjects(TRUE); |
9099cab3 | 378 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['return_error' => 1]); |
276e3ec6 | 379 | $this->assertEquals(1, $result['is_error']); |
380 | ; | |
381 | } | |
382 | ||
383 | /** | |
384 | * Test that if part of $input the payment processor loads OK. | |
385 | * | |
386 | * It's preferable to pass it in as it cannot be correctly calculated. | |
387 | */ | |
388 | public function testPaymentProcessorLoadsAsParam() { | |
389 | $this->_setUpContributionObjects(); | |
9099cab3 CW |
390 | $this->input = array_merge($this->input, ['payment_processor_id' => $this->_processorId]); |
391 | $this->assertTrue($this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['return_error' => 1])); | |
6a488035 TO |
392 | } |
393 | ||
394 | /** | |
395 | * Test that an error is returned if required set & contribution page exists | |
6a488035 | 396 | */ |
00be9182 | 397 | public function testRequiredWithContributionPageError() { |
6a488035 | 398 | $this->_setUpContributionObjects(); |
9099cab3 | 399 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['return_error' => 1]); |
6a488035 | 400 | $this->assertArrayHasKey('error_message', $result); |
276e3ec6 | 401 | $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']); |
6a488035 | 402 | // error is only returned if $required set to True |
9099cab3 | 403 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, NULL, ['return_error' => 1]); |
6a488035 TO |
404 | $this->assertFalse(is_array($result)); |
405 | //check that error is not returned if error checking not set | |
9099cab3 | 406 | $result = $this->IPN->loadObjects($this->input, $this->ids, $this->objects, TRUE, NULL, ['log_error' => 1]); |
6a488035 TO |
407 | $this->assertFalse(is_array($result)); |
408 | } | |
409 | ||
5f5e7a76 O |
410 | public function testThatCancellingEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities() { |
411 | $this->_setUpParticipantObjects('Pending from incomplete transaction'); | |
412 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
9099cab3 | 413 | $additionalParticipantId = $this->participantCreate([ |
5f5e7a76 O |
414 | 'event_id' => $this->_eventId, |
415 | 'registered_by_id' => $this->_participantId, | |
416 | 'status_id' => 'Pending from incomplete transaction', | |
9099cab3 | 417 | ]); |
5f5e7a76 O |
418 | |
419 | $transaction = new CRM_Core_Transaction(); | |
420 | $this->IPN->cancelled($this->objects, $transaction); | |
421 | ||
422 | $cancelledParticipantsCount = civicrm_api3('Participant', 'get', [ | |
423 | 'sequential' => 1, | |
424 | 'id' => ['IN' => [$this->_participantId, $additionalParticipantId]], | |
425 | 'status_id' => 'Cancelled', | |
426 | ])['count']; | |
427 | $this->assertEquals(2, $cancelledParticipantsCount); | |
428 | ||
429 | $cancelledActivatesCount = civicrm_api3('Activity', 'get', [ | |
430 | 'sequential' => 1, | |
431 | 'activity_type_id' => 'Event Registration', | |
432 | 'subject' => ['LIKE' => '%Cancelled%'], | |
433 | 'source_record_id' => ['IN' => [$this->_participantId, $additionalParticipantId]], | |
434 | ]); | |
435 | ||
436 | $this->assertEquals(2, $cancelledActivatesCount['count']); | |
437 | } | |
438 | ||
439 | public function testThatFailedEventPaymentWillCancelAllAdditionalPendingParticipantsAndCreateCancellationActivities() { | |
440 | $this->_setUpParticipantObjects('Pending from incomplete transaction'); | |
441 | $this->IPN->loadObjects($this->input, $this->ids, $this->objects, FALSE, $this->_processorId); | |
9099cab3 | 442 | $additionalParticipantId = $this->participantCreate([ |
5f5e7a76 O |
443 | 'event_id' => $this->_eventId, |
444 | 'registered_by_id' => $this->_participantId, | |
445 | 'status_id' => 'Pending from incomplete transaction', | |
9099cab3 | 446 | ]); |
5f5e7a76 O |
447 | |
448 | $transaction = new CRM_Core_Transaction(); | |
449 | $this->IPN->failed($this->objects, $transaction); | |
450 | ||
451 | $cancelledParticipantsCount = civicrm_api3('Participant', 'get', [ | |
452 | 'sequential' => 1, | |
453 | 'id' => ['IN' => [$this->_participantId, $additionalParticipantId]], | |
454 | 'status_id' => 'Cancelled', | |
455 | ])['count']; | |
456 | $this->assertEquals(2, $cancelledParticipantsCount); | |
457 | ||
458 | $cancelledActivatesCount = civicrm_api3('Activity', 'get', [ | |
459 | 'sequential' => 1, | |
460 | 'activity_type_id' => 'Event Registration', | |
461 | 'subject' => ['LIKE' => '%Cancelled%'], | |
462 | 'source_record_id' => ['IN' => [$this->_participantId, $additionalParticipantId]], | |
463 | ]); | |
464 | ||
465 | $this->assertEquals(2, $cancelledActivatesCount['count']); | |
466 | } | |
6a488035 | 467 | |
4cbe18b8 | 468 | /** |
c490a46a CW |
469 | * Prepare for contribution Test - involving only contribution objects |
470 | * | |
4cbe18b8 EM |
471 | * @param bool $contributionPage |
472 | */ | |
00be9182 | 473 | public function _setUpContributionObjects($contributionPage = FALSE) { |
6a488035 | 474 | |
6a488035 TO |
475 | $contribution = new CRM_Contribute_BAO_Contribution(); |
476 | $contribution->id = $this->_contributionId; | |
477 | $contribution->find(TRUE); | |
cc0c30cc | 478 | $contributionPageID = NULL; |
1c34cb9b | 479 | |
6a488035 TO |
480 | if (!empty($contributionPage)) { |
481 | $dao = new CRM_Core_DAO(); | |
482 | $contribution_page = $dao->createTestObject('CRM_Contribute_DAO_ContributionPage'); | |
3d363977 EM |
483 | $contribution_page->payment_processor = 1; |
484 | $contribution_page->save(); | |
cc0c30cc | 485 | $contribution->contribution_page_id = $contributionPageID = $contribution_page->id; |
d5d148ab EM |
486 | //for unknown reasons trying to do a find & save on a contribution with a receive_date set |
487 | // doesn't work. This seems of minimal relevance to this test so ignoring | |
488 | // note that in tests it worked sometimes & not others - dependent on which other tests run. | |
489 | // running all CRM_Core tests caused failure as did just the single failing test. But running | |
490 | // just this class succeeds - because it actually doesn't do a mysql update on the following save | |
491 | // (unknown reason) | |
492 | unset($contribution->receive_date); | |
cc0c30cc | 493 | $contribution->save(); |
6a488035 TO |
494 | } |
495 | ||
496 | $this->objects['contribution'] = $contribution; | |
9099cab3 | 497 | $this->input = [ |
6a488035 | 498 | 'component' => 'contribute', |
cc0c30cc | 499 | 'contribution_page_id' => $contributionPageID, |
6a488035 TO |
500 | 'total_amount' => 110.00, |
501 | 'invoiceID' => "c8acb91e080ad7777a2adc119c192885", | |
502 | 'contactID' => $this->_contactId, | |
503 | 'contributionID' => $this->objects['contribution']->id, | |
9099cab3 | 504 | ]; |
6a488035 TO |
505 | } |
506 | ||
c490a46a | 507 | /** |
eceb18cc | 508 | * Prepare for membership test. |
c490a46a | 509 | */ |
00be9182 | 510 | public function _setUpMembershipObjects() { |
6a488035 | 511 | try { |
c3543e4a | 512 | if (!$this->_membershipTypeID) { |
513 | $this->_membershipTypeID = $this->membershipTypeCreate(); | |
514 | } | |
515 | if (!$this->_membershipStatusID) { | |
516 | $this->_membershipStatusID = $this->membershipStatusCreate('test status'); | |
517 | } | |
6a488035 | 518 | } |
92915c55 | 519 | catch (Exception$e) { |
6a488035 TO |
520 | echo $e->getMessage(); |
521 | } | |
522 | CRM_Member_PseudoConstant::membershipType($this->_membershipTypeID, TRUE); | |
523 | CRM_Member_PseudoConstant::membershipStatus(NULL, NULL, 'name', TRUE); | |
9099cab3 | 524 | $this->_membershipParams = [ |
6a488035 TO |
525 | 'contact_id' => $this->_contactId, |
526 | 'membership_type_id' => $this->_membershipTypeID, | |
527 | 'join_date' => '2009-01-21', | |
528 | 'start_date' => '2009-01-21', | |
529 | 'end_date' => '2009-12-21', | |
530 | 'source' => 'Payment', | |
531 | 'is_override' => 1, | |
532 | 'status_id' => $this->_membershipStatusID, | |
533 | 'version' => 3, | |
9099cab3 | 534 | ]; |
6a488035 | 535 | |
c1135109 | 536 | $membership = $this->callAPISuccess('membership', 'create', $this->_membershipParams); |
c3543e4a | 537 | if ($this->objects['contribution']->id != $this->_contributionId) { |
538 | $contribution = new CRM_Contribute_BAO_Contribution(); | |
539 | $contribution->id = $this->_contributionId; | |
540 | $contribution->find(TRUE); | |
9099cab3 | 541 | $this->objects = ['contribution' => $contribution]; |
c3543e4a | 542 | } |
6a488035 TO |
543 | $this->_membershipId = $membership['id']; |
544 | //we'll create membership payment here because to make setup more re-usable | |
9099cab3 | 545 | $this->callAPISuccess('membership_payment', 'create', [ |
92915c55 TO |
546 | 'contribution_id' => $this->_contributionId, |
547 | 'membership_id' => $this->_membershipId, | |
9099cab3 | 548 | ]); |
6a488035 | 549 | |
9099cab3 | 550 | $this->input = [ |
6a488035 TO |
551 | 'component' => 'contribute', |
552 | 'total_amount' => 150.00, | |
553 | 'invoiceID' => "c8acb91e080ad7bd8a2adc119c192885", | |
554 | 'contactID' => $this->_contactId, | |
c3543e4a | 555 | 'contributionID' => $this->_contributionId, |
6a488035 | 556 | 'membershipID' => $this->_membershipId, |
9099cab3 | 557 | ]; |
6a488035 TO |
558 | |
559 | $this->ids['membership'] = $this->_membershipId; | |
560 | } | |
561 | ||
00be9182 | 562 | public function _setUpRecurringContribution() { |
9099cab3 | 563 | $this->_contributionRecurParams = [ |
6a488035 TO |
564 | 'contact_id' => $this->_contactId, |
565 | 'amount' => 150.00, | |
566 | 'currency' => 'USD', | |
567 | 'frequency_unit' => 'week', | |
568 | 'frequency_interval' => 1, | |
569 | 'installments' => 2, | |
570 | 'start_date' => date('Ymd'), | |
571 | 'create_date' => date('Ymd'), | |
572 | 'invoice_id' => 'c8acb91e080ad7bd8a2adc119c192885', | |
573 | 'contribution_status_id' => 2, | |
2b57dd9f | 574 | 'financial_type_id' => $this->_financialTypeId, |
6a488035 TO |
575 | 'version' => 3, |
576 | 'payment_processor_id' => $this->_processorId, | |
9099cab3 | 577 | ]; |
aeeaba66 | 578 | $this->_recurId = $this->callAPISuccess('contribution_recur', 'create', $this->_contributionRecurParams); |
6a488035 TO |
579 | $this->_recurId = $this->_recurId['id']; |
580 | $this->input['contributionRecurId'] = $this->_recurId; | |
581 | $this->ids['contributionRecur'] = $this->_recurId; | |
582 | } | |
583 | ||
c490a46a | 584 | /** |
eceb18cc | 585 | * Set up participant requirements for test. |
5f5e7a76 O |
586 | * |
587 | * @param string $participantStatus | |
588 | * The participant to create status | |
c490a46a | 589 | */ |
5f5e7a76 | 590 | public function _setUpParticipantObjects($participantStatus = 'Attended') { |
9099cab3 | 591 | $event = $this->eventCreate(['is_email_confirm' => 1]); |
aeeaba66 | 592 | |
6a488035 | 593 | $this->_eventId = $event['id']; |
9099cab3 | 594 | $this->_participantId = $this->participantCreate([ |
92915c55 TO |
595 | 'event_id' => $this->_eventId, |
596 | 'contact_id' => $this->_contactId, | |
5f5e7a76 | 597 | 'status_id' => $participantStatus, |
9099cab3 | 598 | ]); |
3aaa68fb | 599 | |
9099cab3 | 600 | $this->callAPISuccess('participant_payment', 'create', [ |
92915c55 TO |
601 | 'contribution_id' => $this->_contributionId, |
602 | 'participant_id' => $this->_participantId, | |
9099cab3 | 603 | ]); |
aeeaba66 | 604 | |
6a488035 TO |
605 | $contribution = new CRM_Contribute_BAO_Contribution(); |
606 | $contribution->id = $this->_contributionId; | |
607 | $contribution->find(); | |
608 | $this->objects['contribution'] = $contribution; | |
9099cab3 | 609 | $this->input = [ |
6a488035 TO |
610 | 'component' => 'event', |
611 | 'total_amount' => 150.00, | |
612 | 'invoiceID' => "c8acb91e080ad7bd8a2adc119c192885", | |
613 | 'contactID' => $this->_contactId, | |
614 | 'contributionID' => $contribution->id, | |
615 | 'participantID' => $this->_participantId, | |
9099cab3 | 616 | ]; |
6a488035 TO |
617 | |
618 | $this->ids['participant'] = $this->_participantId; | |
619 | $this->ids['event'] = $this->_eventId; | |
620 | } | |
621 | ||
c490a46a | 622 | /** |
eceb18cc | 623 | * Set up participant requirements for test. |
c490a46a | 624 | */ |
00be9182 | 625 | public function _setUpPledgeObjects() { |
9099cab3 | 626 | $this->_pledgeId = $this->pledgeCreate(['contact_id' => $this->_contactId]); |
6a488035 | 627 | //we'll create membership payment here because to make setup more re-usable |
9099cab3 | 628 | $pledgePayment = $this->callAPISuccess('pledge_payment', 'create', [ |
92915c55 TO |
629 | 'version' => 3, |
630 | 'pledge_id' => $this->_pledgeId, | |
631 | 'contribution_id' => $this->_contributionId, | |
632 | 'status_id' => 1, | |
633 | 'actual_amount' => 50, | |
9099cab3 | 634 | ]); |
aeeaba66 | 635 | |
9099cab3 | 636 | $this->input = [ |
6a488035 TO |
637 | 'component' => 'contribute', |
638 | 'total_amount' => 150.00, | |
639 | 'invoiceID' => "c8acb91e080ad7bd8a2adc119c192885", | |
640 | 'contactID' => $this->_contactId, | |
641 | 'contributionID' => $this->_contributionId, | |
642 | 'pledgeID' => $this->_pledgeId, | |
9099cab3 | 643 | ]; |
6a488035 TO |
644 | |
645 | $this->ids['pledge_payment'][] = $pledgePayment['id']; | |
646 | } | |
96025800 | 647 | |
6a488035 | 648 | } |