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