Merge pull request #17719 from civicrm/5.27
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / MembershipRenewalTest.php
CommitLineData
4187b12f
EM
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
4187b12f 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 |
4187b12f
EM
9 +--------------------------------------------------------------------+
10 */
11
4187b12f
EM
12/**
13 * Test CRM_Member_Form_Membership functions.
14 *
15 * @package CiviCRM
acb109b7 16 * @group headless
4187b12f 17 */
bce3b15b 18class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
4187b12f 19
4187b12f
EM
20 protected $_individualId;
21 protected $_contribution;
22 protected $_financialTypeId = 1;
4187b12f
EM
23 protected $_entity = 'Membership';
24 protected $_params;
4187b12f
EM
25 protected $_paymentProcessorID;
26
27 /**
28 * Membership type ID for annual fixed membership.
29 *
30 * @var int
31 */
32 protected $membershipTypeAnnualFixedID;
33
34 /**
35 * Parameters to create payment processor.
36 *
37 * @var array
38 */
08d620bc 39 protected $_processorParams = [];
4187b12f
EM
40
41 /**
42 * ID of created membership.
43 *
44 * @var int
45 */
46 protected $_membershipID;
47
48 /**
49 * Payment instrument mapping.
50 *
51 * @var array
52 */
08d620bc 53 protected $paymentInstruments = [];
4187b12f 54
1f31bbc4 55
56 /**
57 * @var CiviMailUtils
58 */
59 protected $mut;
60
e5c0aee0 61 /**
62 * @var int
63 */
64 private $financialTypeID;
65
4187b12f
EM
66 /**
67 * Test setup for every test.
68 *
69 * Connect to the database, truncate the tables that will be used
70 * and redirect stdin to a temporary file.
e5c0aee0 71 *
72 * @throws \CRM_Core_Exception
4187b12f
EM
73 */
74 public function setUp() {
4187b12f
EM
75 parent::setUp();
76
77 $this->_individualId = $this->individualCreate();
8950ecdc 78 $this->_paymentProcessorID = $this->processorCreate();
e5c0aee0 79 $this->financialTypeID = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Member Dues');
08d620bc 80
81 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/data.xml');
e5c0aee0 82 $this->membershipTypeAnnualFixedID = $this->callAPISuccess('membership_type', 'create', [
4187b12f 83 'domain_id' => 1,
e5c0aee0 84 'name' => 'AnnualFixed',
4187b12f 85 'member_of_contact_id' => 23,
e5c0aee0 86 'duration_unit' => 'year',
4187b12f 87 'duration_interval' => 1,
e5c0aee0 88 'period_type' => 'fixed',
89 'fixed_period_start_day' => '101',
90 'fixed_period_rollover_day' => '1231',
4187b12f 91 'relationship_type_id' => 20,
7aa78908 92 'min_fee' => 100,
e5c0aee0 93 'financial_type_id' => $this->financialTypeID,
94 ])['id'];
95
96 $this->_membershipID = $this->callAPISuccess('Membership', 'create', [
4187b12f
EM
97 'contact_id' => $this->_individualId,
98 'membership_type_id' => $this->membershipTypeAnnualFixedID,
5ea9c2be 99 'join_date' => '2020-04-13',
100 'source' => 'original_source',
e5c0aee0 101 ])['id'];
4187b12f 102
e5c0aee0 103 $this->paymentInstruments = $this->callAPISuccess('Contribution', 'getoptions', ['field' => 'payment_instrument_id'])['values'];
4187b12f
EM
104 }
105
106 /**
107 * Clean up after each test.
e5c0aee0 108 *
109 * @throws \CRM_Core_Exception
4187b12f
EM
110 */
111 public function tearDown() {
3fc37a30 112 $this->validateAllPayments();
113 $this->validateAllContributions();
4187b12f
EM
114 $this->quickCleanUpFinancialEntities();
115 $this->quickCleanup(
9099cab3 116 [
4187b12f
EM
117 'civicrm_relationship',
118 'civicrm_membership_type',
119 'civicrm_membership',
120 'civicrm_uf_match',
0816949d 121 'civicrm_address',
9099cab3 122 ]
4187b12f 123 );
9099cab3
CW
124 foreach ([17, 18, 23, 32] as $contactID) {
125 $this->callAPISuccess('contact', 'delete', ['id' => $contactID, 'skip_undelete' => TRUE]);
e2ce0d88 126 }
9099cab3 127 $this->callAPISuccess('relationship_type', 'delete', ['id' => 20]);
4187b12f
EM
128 }
129
4187b12f
EM
130 /**
131 * Test the submit function of the membership form.
e5c0aee0 132 *
133 * @throws \CRM_Core_Exception
134 * @throws \CiviCRM_API3_Exception
4187b12f
EM
135 */
136 public function testSubmit() {
137 $form = $this->getForm();
138 $this->createLoggedInUser();
08d620bc 139 $params = [
4187b12f
EM
140 'cid' => $this->_individualId,
141 'join_date' => date('m/d/Y', time()),
142 'start_date' => '',
143 'end_date' => '',
144 // This format reflects the 23 being the organisation & the 25 being the type.
9099cab3 145 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
4187b12f
EM
146 'auto_renew' => '0',
147 'max_related' => '',
148 'num_terms' => '1',
149 'source' => '',
150 'total_amount' => '50.00',
1a00ea3c
EM
151 //Member dues, see data.xml
152 'financial_type_id' => '2',
4187b12f
EM
153 'soft_credit_type_id' => '',
154 'soft_credit_contact_id' => '',
155 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
156 'receipt_text_signup' => 'Thank you text',
157 'payment_processor_id' => $this->_paymentProcessorID,
158 'credit_card_number' => '4111111111111111',
159 'cvv2' => '123',
9099cab3 160 'credit_card_exp_date' => [
4187b12f 161 'M' => '9',
39b959db
SL
162 // TODO: Future proof
163 'Y' => '2024',
9099cab3 164 ],
4187b12f
EM
165 'credit_card_type' => 'Visa',
166 'billing_first_name' => 'Test',
167 'billing_middlename' => 'Last',
168 'billing_street_address-5' => '10 Test St',
169 'billing_city-5' => 'Test',
170 'billing_state_province_id-5' => '1003',
171 'billing_postal_code-5' => '90210',
172 'billing_country_id-5' => '1228',
08d620bc 173 ];
4187b12f
EM
174 $form->_contactID = $this->_individualId;
175
176 $form->testSubmit($params);
24d9c528 177 $form->setRenewalMessage();
9099cab3
CW
178 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
179 $this->callAPISuccessGetCount('ContributionRecur', ['contact_id' => $this->_individualId], 0);
180 $contribution = $this->callAPISuccess('Contribution', 'get', [
4187b12f
EM
181 'contact_id' => $this->_individualId,
182 'is_test' => TRUE,
9099cab3 183 ]);
4187b12f 184
9099cab3 185 $this->callAPISuccessGetCount('LineItem', [
4187b12f
EM
186 'entity_id' => $membership['id'],
187 'entity_table' => 'civicrm_membership',
188 'contribution_id' => $contribution['id'],
9099cab3
CW
189 ], 1);
190 $this->_checkFinancialRecords([
16f3bd02 191 'id' => $contribution['id'],
192 'total_amount' => 50,
193 'financial_account_id' => 2,
9099cab3 194 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', [
16f3bd02 195 'id' => $this->_paymentProcessorID,
196 'return' => 'payment_instrument_id',
9099cab3
CW
197 ]),
198 ], 'online');
24d9c528
JG
199 $this->assertEquals([
200 [
201 'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been renewed.',
202 'title' => 'Complete',
203 'type' => 'success',
204 'options' => NULL,
205 ],
206 ], CRM_Core_Session::singleton()->getStatus());
4187b12f
EM
207 }
208
7aa78908 209 /**
210 * Test submitting with tax enabled.
211 *
212 * @throws \CRM_Core_Exception
213 * @throws \CiviCRM_API3_Exception
214 */
215 public function testSubmitWithTax() {
216 $this->enableTaxAndInvoicing();
217 $this->relationForFinancialTypeWithFinancialAccount($this->financialTypeID);
218 $form = $this->getForm();
219
220 $form->testSubmit([
221 'cid' => $this->_individualId,
222 'join_date' => date('Y-m-d'),
223 'start_date' => '',
224 'end_date' => '',
225 // This format reflects the 23 being the organisation & the 25 being the type.
226 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
227 'auto_renew' => '0',
228 'max_related' => '',
229 'num_terms' => '1',
230 'source' => '',
231 'total_amount' => '50.00',
232 'financial_type_id' => $this->financialTypeID,
233 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
234 'receipt_text_signup' => 'Thank you text',
235 'payment_processor_id' => $this->_paymentProcessorID,
236 'credit_card_number' => '4111111111111111',
237 'cvv2' => '123',
238 'credit_card_exp_date' => [
239 'M' => '9',
240 'Y' => date('Y') + 2,
241 ],
242 'credit_card_type' => 'Visa',
243 'billing_first_name' => 'Test',
244 'billing_middlename' => 'Last',
245 'billing_street_address-5' => '10 Test St',
246 'billing_city-5' => 'Test',
247 'billing_state_province_id-5' => '1003',
248 'billing_postal_code-5' => '90210',
249 'billing_country_id-5' => '1228',
250 ]);
251 $contribution = $this->callAPISuccessGetSingle('Contribution', ['contact_id' => $this->_individualId, 'is_test' => TRUE, 'return' => ['total_amount', 'tax_amount']]);
252 $this->assertEquals(50, $contribution['total_amount']);
253 $this->assertEquals(5, $contribution['tax_amount']);
254 }
255
4187b12f
EM
256 /**
257 * Test the submit function of the membership form.
91786f44 258 *
259 * @throws \CRM_Core_Exception
e5c0aee0 260 * @throws \CiviCRM_API3_Exception
4187b12f
EM
261 */
262 public function testSubmitRecur() {
263 $form = $this->getForm();
264
9099cab3 265 $this->callAPISuccess('MembershipType', 'create', [
4187b12f
EM
266 'id' => $this->membershipTypeAnnualFixedID,
267 'duration_unit' => 'month',
268 'duration_interval' => 1,
269 'auto_renew' => TRUE,
9099cab3 270 ]);
4187b12f
EM
271 $form->preProcess();
272 $this->createLoggedInUser();
9099cab3 273 $params = [
4187b12f
EM
274 'cid' => $this->_individualId,
275 'price_set_id' => 0,
276 'join_date' => date('m/d/Y', time()),
277 'start_date' => '',
278 'end_date' => '',
279 'campaign_id' => '',
280 // This format reflects the 23 being the organisation & the 25 being the type.
9099cab3 281 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
4187b12f
EM
282 'auto_renew' => '1',
283 'is_recur' => 1,
284 'max_related' => 0,
285 'num_terms' => '1',
286 'source' => '',
287 'total_amount' => '77.00',
bb069c16 288 //Member dues, see data.xml
289 'financial_type_id' => '2',
4187b12f
EM
290 'soft_credit_type_id' => 11,
291 'soft_credit_contact_id' => '',
292 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
293 'receipt_text' => 'Thank you text',
294 'payment_processor_id' => $this->_paymentProcessorID,
295 'credit_card_number' => '4111111111111111',
296 'cvv2' => '123',
9099cab3 297 'credit_card_exp_date' => [
4187b12f 298 'M' => '9',
90530b87 299 'Y' => date('Y') + 1,
9099cab3 300 ],
4187b12f
EM
301 'credit_card_type' => 'Visa',
302 'billing_first_name' => 'Test',
303 'billing_middlename' => 'Last',
304 'billing_street_address-5' => '10 Test St',
305 'billing_city-5' => 'Test',
306 'billing_state_province_id-5' => '1003',
307 'billing_postal_code-5' => '90210',
308 'billing_country_id-5' => '1228',
bb069c16 309 'send_receipt' => 1,
9099cab3 310 ];
4187b12f
EM
311 $form->_mode = 'test';
312 $form->_contactID = $this->_individualId;
313
314 $form->testSubmit($params);
9099cab3
CW
315 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
316 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
bb069c16 317 $this->assertEquals(1, $contributionRecur['is_email_receipt']);
318 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contributionRecur['modified_date'])));
319 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contributionRecur['modified_date'])));
320 $this->assertNotEmpty($contributionRecur['invoice_id']);
321 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id',
322 'Pending'), $contributionRecur['contribution_status_id']);
9099cab3 323 $this->assertEquals($this->callAPISuccessGetValue('PaymentProcessor', [
16f3bd02 324 'id' => $this->_paymentProcessorID,
325 'return' => 'payment_instrument_id',
9099cab3 326 ]), $contributionRecur['payment_instrument_id']);
bb069c16 327
9099cab3 328 $contribution = $this->callAPISuccess('Contribution', 'getsingle', [
4187b12f
EM
329 'contact_id' => $this->_individualId,
330 'is_test' => TRUE,
9099cab3 331 ]);
4187b12f 332
9099cab3 333 $this->assertEquals($this->callAPISuccessGetValue('PaymentProcessor', [
16f3bd02 334 'id' => $this->_paymentProcessorID,
335 'return' => 'payment_instrument_id',
9099cab3 336 ]), $contribution['payment_instrument_id']);
e136edcf 337 $this->assertEquals($contributionRecur['id'], $contribution['contribution_recur_id']);
bb069c16 338
9099cab3 339 $this->callAPISuccessGetCount('LineItem', [
4187b12f
EM
340 'entity_id' => $membership['id'],
341 'entity_table' => 'civicrm_membership',
342 'contribution_id' => $contribution['id'],
9099cab3 343 ], 1);
4187b12f 344
9099cab3 345 $this->callAPISuccessGetSingle('address', [
0816949d
EM
346 'contact_id' => $this->_individualId,
347 'street_address' => '10 Test St',
39b959db 348 'postal_code' => 90210,
9099cab3 349 ]);
0816949d
EM
350 }
351
b3f6108a 352 /**
353 * Test the submit function of the membership form.
e5c0aee0 354 *
355 * @throws \CRM_Core_Exception
356 * @throws \CiviCRM_API3_Exception
b3f6108a 357 */
358 public function testSubmitRecurCompleteInstant() {
359 $form = $this->getForm();
1f31bbc4 360 /** @var \CRM_Core_Payment_Dummy $processor */
b3f6108a 361 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID);
9099cab3 362 $processor->setDoDirectPaymentResult([
b3f6108a 363 'payment_status_id' => 1,
364 'trxn_id' => 'kettles boil water',
365 'fee_amount' => .29,
9099cab3 366 ]);
b3f6108a 367
9099cab3 368 $this->callAPISuccess('MembershipType', 'create', [
b3f6108a 369 'id' => $this->membershipTypeAnnualFixedID,
370 'duration_unit' => 'month',
371 'duration_interval' => 1,
372 'auto_renew' => TRUE,
9099cab3 373 ]);
b3f6108a 374 $this->createLoggedInUser();
375 $form->preProcess();
376
377 $form->_contactID = $this->_individualId;
378 $params = $this->getBaseSubmitParams();
379 $form->_mode = 'test';
380
381 $form->testSubmit($params);
9099cab3 382 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
5ea9c2be 383 $this->assertEquals('2020-04-13', $membership['join_date']);
384 $this->assertEquals(date('Y-01-01'), $membership['start_date']);
385 $nextYear = date('Y') + 1;
386 $this->assertEquals(date($nextYear . '-01-31'), $membership['end_date']);
387 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'New'), $membership['status_id']);
388 $this->assertNotEmpty($membership['contribution_recur_id']);
389 $this->assertNotEmpty('original_source', $membership['source']);
390
391 $log = $this->callAPISuccessGetSingle('MembershipLog', ['membership_id' => $membership['id'], 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
392 $this->assertEquals(date($nextYear . '-01-01'), $log['start_date']);
393 $this->assertEquals(date($nextYear . '-01-31'), $log['end_date']);
394 $this->assertEquals(date('Y-m-d'), $log['modified_date']);
395
9099cab3 396 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
3a1f9fd3 397 $this->assertEquals($contributionRecur['id'], $membership['contribution_recur_id']);
bb069c16 398 $this->assertEquals(0, $contributionRecur['is_email_receipt']);
399 $this->assertEquals(date('Y-m-d'), date('Y-m-d', strtotime($contributionRecur['modified_date'])));
400 $this->assertNotEmpty($contributionRecur['invoice_id']);
bb069c16 401 // @todo fix this part!
402 /*
403 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id',
b1b2796c 404 'In Progress'), $contributionRecur['contribution_status_id']);
bb069c16 405 $this->assertNotEmpty($contributionRecur['next_sched_contribution_date']);
b1b2796c 406 */
9099cab3 407 $paymentInstrumentID = $this->callAPISuccessGetValue('PaymentProcessor', [
16f3bd02 408 'id' => $this->_paymentProcessorID,
409 'return' => 'payment_instrument_id',
9099cab3 410 ]);
16f3bd02 411 $this->assertEquals($paymentInstrumentID, $contributionRecur['payment_instrument_id']);
bb069c16 412
9099cab3 413 $contribution = $this->callAPISuccess('Contribution', 'getsingle', [
b3f6108a 414 'contact_id' => $this->_individualId,
415 'is_test' => TRUE,
9099cab3 416 ]);
16f3bd02 417 $this->assertEquals($paymentInstrumentID, $contribution['payment_instrument_id']);
b3f6108a 418
419 $this->assertEquals('kettles boil water', $contribution['trxn_id']);
420 $this->assertEquals(.29, $contribution['fee_amount']);
1f31bbc4 421 $this->assertEquals(7800.90, $contribution['total_amount']);
422 $this->assertEquals(7800.61, $contribution['net_amount']);
b3f6108a 423
9099cab3 424 $this->callAPISuccessGetCount('LineItem', [
b3f6108a 425 'entity_id' => $membership['id'],
426 'entity_table' => 'civicrm_membership',
427 'contribution_id' => $contribution['id'],
9099cab3 428 ], 1);
1f31bbc4 429 }
430
431 /**
432 * Test the submit function of the membership form.
433 *
434 * @param string $thousandSeparator
435 *
436 * @dataProvider getThousandSeparators
90530b87 437 *
438 * @throws \CiviCRM_API3_Exception
439 * @throws \CRM_Core_Exception
1f31bbc4 440 */
441 public function testSubmitRecurCompleteInstantWithMail($thousandSeparator) {
442 $this->setCurrencySeparators($thousandSeparator);
443 $form = $this->getForm();
444 $this->mut = new CiviMailUtils($this, TRUE);
445 /** @var \CRM_Core_Payment_Dummy $processor */
446 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID);
9099cab3 447 $processor->setDoDirectPaymentResult([
1f31bbc4 448 'payment_status_id' => 1,
449 'trxn_id' => 'kettles boil water',
450 'fee_amount' => .29,
9099cab3 451 ]);
1f31bbc4 452
9099cab3 453 $this->callAPISuccess('MembershipType', 'create', [
1f31bbc4 454 'id' => $this->membershipTypeAnnualFixedID,
455 'duration_unit' => 'month',
456 'duration_interval' => 1,
457 'auto_renew' => TRUE,
9099cab3 458 ]);
1f31bbc4 459 $this->createLoggedInUser();
460 $form->preProcess();
461
462 $form->_contactID = $this->_individualId;
463 $params = $this->getBaseSubmitParams();
464 $params['send_receipt'] = 1;
465 $form->_mode = 'test';
b3f6108a 466
1f31bbc4 467 $form->testSubmit($params);
9099cab3 468 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', ['contact_id' => $this->_individualId]);
1f31bbc4 469 $this->assertEquals(1, $contributionRecur['is_email_receipt']);
470 $this->mut->checkMailLog([
39b959db 471 '$ ' . $this->formatMoneyInput(7800.90),
1f31bbc4 472 ]);
473 $this->mut->stop();
474 $this->setCurrencySeparators(',');
b3f6108a 475 }
3a1f9fd3 476
0816949d
EM
477 /**
478 * Test the submit function of the membership form.
91786f44 479 *
480 * @throws \CRM_Core_Exception
e5c0aee0 481 * @throws \CiviCRM_API3_Exception
0816949d
EM
482 */
483 public function testSubmitPayLater() {
484 $form = $this->getForm(NULL);
485 $this->createLoggedInUser();
9099cab3
CW
486 $originalMembership = $this->callAPISuccessGetSingle('membership', []);
487 $params = [
0816949d
EM
488 'cid' => $this->_individualId,
489 'join_date' => date('m/d/Y', time()),
490 'start_date' => '',
491 'end_date' => '',
492 // This format reflects the 23 being the organisation & the 25 being the type.
9099cab3 493 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
0816949d
EM
494 'auto_renew' => '0',
495 'max_related' => '',
496 'num_terms' => '2',
0816949d
EM
497 'total_amount' => '50.00',
498 //Member dues, see data.xml
499 'financial_type_id' => '2',
500 'soft_credit_type_id' => '',
501 'soft_credit_contact_id' => '',
502 'payment_instrument_id' => 4,
503 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
504 'receipt_text_signup' => 'Thank you text',
505 'payment_processor_id' => $this->_paymentProcessorID,
506 'record_contribution' => TRUE,
507 'trxn_id' => 777,
508 'contribution_status_id' => 2,
9099cab3 509 ];
0816949d
EM
510 $form->_contactID = $this->_individualId;
511
512 $form->testSubmit($params);
9099cab3 513 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
0816949d 514 $this->assertEquals(strtotime($membership['end_date']), strtotime($originalMembership['end_date']));
9099cab3 515 $contribution = $this->callAPISuccessGetSingle('Contribution', [
0816949d
EM
516 'contact_id' => $this->_individualId,
517 'contribution_status_id' => 2,
e5c0aee0 518 'return' => ['tax_amount', 'trxn_id'],
9099cab3 519 ]);
0816949d 520 $this->assertEquals($contribution['trxn_id'], 777);
7aa78908 521 $this->assertEquals(NULL, $contribution['tax_amount']);
0816949d 522
9099cab3 523 $this->callAPISuccessGetCount('LineItem', [
0816949d
EM
524 'entity_id' => $membership['id'],
525 'entity_table' => 'civicrm_membership',
526 'contribution_id' => $contribution['id'],
9099cab3 527 ], 1);
0816949d
EM
528 }
529
530 /**
531 * Test the submit function of the membership form.
91786f44 532 *
533 * @throws \CRM_Core_Exception
e5c0aee0 534 * @throws \CiviCRM_API3_Exception
0816949d
EM
535 */
536 public function testSubmitPayLaterWithBilling() {
537 $form = $this->getForm(NULL);
538 $this->createLoggedInUser();
9099cab3
CW
539 $originalMembership = $this->callAPISuccessGetSingle('membership', []);
540 $params = [
0816949d
EM
541 'cid' => $this->_individualId,
542 'join_date' => date('m/d/Y', time()),
543 'start_date' => '',
544 'end_date' => '',
545 // This format reflects the 23 being the organisation & the 25 being the type.
9099cab3 546 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
0816949d
EM
547 'auto_renew' => '0',
548 'max_related' => '',
549 'num_terms' => '2',
0816949d
EM
550 'total_amount' => '50.00',
551 //Member dues, see data.xml
552 'financial_type_id' => '2',
553 'soft_credit_type_id' => '',
554 'soft_credit_contact_id' => '',
555 'payment_instrument_id' => 4,
556 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
557 'receipt_text_signup' => 'Thank you text',
558 'payment_processor_id' => $this->_paymentProcessorID,
559 'record_contribution' => TRUE,
560 'trxn_id' => 777,
561 'contribution_status_id' => 2,
562 'billing_first_name' => 'Test',
563 'billing_middlename' => 'Last',
564 'billing_street_address-5' => '10 Test St',
565 'billing_city-5' => 'Test',
566 'billing_state_province_id-5' => '1003',
567 'billing_postal_code-5' => '90210',
568 'billing_country_id-5' => '1228',
9099cab3 569 ];
0816949d
EM
570 $form->_contactID = $this->_individualId;
571
572 $form->testSubmit($params);
9099cab3 573 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
0816949d 574 $this->assertEquals(strtotime($membership['end_date']), strtotime($originalMembership['end_date']));
9099cab3 575 $contribution = $this->callAPISuccessGetSingle('Contribution', [
0816949d
EM
576 'contact_id' => $this->_individualId,
577 'contribution_status_id' => 2,
9099cab3 578 ]);
0816949d
EM
579 $this->assertEquals($contribution['trxn_id'], 777);
580
9099cab3 581 $this->callAPISuccessGetCount('LineItem', [
0816949d
EM
582 'entity_id' => $membership['id'],
583 'entity_table' => 'civicrm_membership',
584 'contribution_id' => $contribution['id'],
9099cab3
CW
585 ], 1);
586 $this->callAPISuccessGetSingle('address', [
0816949d
EM
587 'contact_id' => $this->_individualId,
588 'street_address' => '10 Test St',
589 'postal_code' => 90210,
9099cab3 590 ]);
0816949d
EM
591 }
592
593 /**
594 * Test the submit function of the membership form.
e5c0aee0 595 *
596 * @throws \CRM_Core_Exception
597 * @throws \CiviCRM_API3_Exception
0816949d
EM
598 */
599 public function testSubmitComplete() {
600 $form = $this->getForm(NULL);
601 $this->createLoggedInUser();
9099cab3
CW
602 $originalMembership = $this->callAPISuccessGetSingle('membership', []);
603 $params = [
0816949d
EM
604 'cid' => $this->_individualId,
605 'join_date' => date('m/d/Y', time()),
606 'start_date' => '',
607 'end_date' => '',
608 // This format reflects the 23 being the organisation & the 25 being the type.
9099cab3 609 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
0816949d
EM
610 'auto_renew' => '0',
611 'max_related' => '',
612 'num_terms' => '2',
0816949d
EM
613 'total_amount' => '50.00',
614 //Member dues, see data.xml
615 'financial_type_id' => '2',
616 'soft_credit_type_id' => '',
617 'soft_credit_contact_id' => '',
618 'payment_instrument_id' => 4,
619 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
620 'receipt_text_signup' => 'Thank you text',
621 'payment_processor_id' => $this->_paymentProcessorID,
622 'record_contribution' => TRUE,
623 'trxn_id' => 777,
624 'contribution_status_id' => 1,
77623a96 625 'fee_amount' => .5,
9099cab3 626 ];
0816949d
EM
627 $form->_contactID = $this->_individualId;
628
629 $form->testSubmit($params);
9099cab3 630 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
0816949d
EM
631 $this->assertEquals(strtotime($membership['end_date']), strtotime('+ 2 years',
632 strtotime($originalMembership['end_date'])));
9099cab3 633 $contribution = $this->callAPISuccessGetSingle('Contribution', [
0816949d
EM
634 'contact_id' => $this->_individualId,
635 'contribution_status_id' => 1,
9099cab3 636 ]);
0816949d
EM
637
638 $this->assertEquals($contribution['trxn_id'], 777);
77623a96 639 $this->assertEquals(.5, $contribution['fee_amount']);
9099cab3 640 $this->callAPISuccessGetCount('LineItem', [
0816949d
EM
641 'entity_id' => $membership['id'],
642 'entity_table' => 'civicrm_membership',
643 'contribution_id' => $contribution['id'],
9099cab3 644 ], 1);
4187b12f
EM
645 }
646
647 /**
648 * Get a membership form object.
649 *
650 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
651 *
0816949d
EM
652 * @param string $mode
653 *
4187b12f 654 * @return \CRM_Member_Form_MembershipRenewal
e5c0aee0 655 *
656 * @throws \CRM_Core_Exception
657 * @throws \CiviCRM_API3_Exception
4187b12f 658 */
0816949d 659 protected function getForm($mode = 'test') {
4187b12f
EM
660 $form = new CRM_Member_Form_MembershipRenewal();
661 $_SERVER['REQUEST_METHOD'] = 'GET';
662 $form->controller = new CRM_Core_Controller();
663 $form->_bltID = 5;
0816949d 664 $form->_mode = $mode;
d05aa358 665 $form->setEntityId($this->_membershipID);
4187b12f
EM
666 $form->preProcess();
667 return $form;
668 }
669
b3f6108a 670 /**
671 * Get some re-usable parameters for the submit function.
672 *
673 * @return array
674 */
675 protected function getBaseSubmitParams() {
e5c0aee0 676 return [
b3f6108a 677 'cid' => $this->_individualId,
678 'price_set_id' => 0,
679 'join_date' => date('m/d/Y', time()),
680 'start_date' => '',
681 'end_date' => '',
682 'campaign_id' => '',
683 // This format reflects the 23 being the organisation & the 25 being the type.
9099cab3 684 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
b3f6108a 685 'auto_renew' => '1',
686 'is_recur' => 1,
687 'max_related' => 0,
688 'num_terms' => '1',
1f31bbc4 689 'total_amount' => $this->formatMoneyInput('7800.90'),
39b959db
SL
690 //Member dues, see data.xml
691 'financial_type_id' => '2',
b3f6108a 692 'soft_credit_type_id' => 11,
693 'soft_credit_contact_id' => '',
694 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
695 'receipt_text' => 'Thank you text',
696 'payment_processor_id' => $this->_paymentProcessorID,
697 'credit_card_number' => '4111111111111111',
698 'cvv2' => '123',
9099cab3 699 'credit_card_exp_date' => [
b3f6108a 700 'M' => '9',
90530b87 701 'Y' => date('Y') + 1,
9099cab3 702 ],
b3f6108a 703 'credit_card_type' => 'Visa',
704 'billing_first_name' => 'Test',
705 'billing_middlename' => 'Last',
706 'billing_street_address-5' => '10 Test St',
707 'billing_city-5' => 'Test',
708 'billing_state_province_id-5' => '1003',
709 'billing_postal_code-5' => '90210',
710 'billing_country_id-5' => '1228',
9099cab3 711 ];
b3f6108a 712 }
713
d05aa358 714 /**
715 * Test renewing an expired membership.
716 *
717 * @throws \CRM_Core_Exception
718 * @throws \CiviCRM_API3_Exception
719 */
720 public function testSubmitRenewExpired() {
721 $form = $this->getForm(NULL);
722 $this->createLoggedInUser();
723 $originalMembership = $this->callAPISuccessGetSingle('membership', []);
724 $this->callAPISuccess('Membership', 'create', [
725 'status_id' => 'Expired',
726 'id' => $originalMembership['id'],
727 'start_date' => '2019-03-01',
728 'join_date' => '2019-03-01',
729 'end_date' => '2020-03-24',
730 'source' => 'sauce',
731 ]);
732
733 $params = [
734 'contact_id' => $this->_individualId,
735 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
736 'renewal_date' => '2020-06-10',
737 'financial_type_id' => '2',
738 'num_terms' => '1',
739 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
740 'record_contribution' => '1',
741 'total_amount' => '100.00',
742 'receive_date' => '2020-06-05 06:05:00',
743 'payment_instrument_id' => '4',
744 'contribution_status_id' => '1',
745 'send_receipt' => '1',
746 ];
747 $form->testSubmit($params);
748 $renewedMembership = $this->callAPISuccessGetSingle('Membership', ['id' => $originalMembership['id']]);
749 $this->assertEquals('sauce', $renewedMembership['source']);
750 $this->assertEquals(date('Y-01-01'), $renewedMembership['start_date']);
751 $this->assertEquals(date('2019-03-01'), $renewedMembership['join_date']);
752 $this->assertEquals(date('Y-12-31'), $renewedMembership['end_date']);
753 $log = $this->callAPISuccessGetSingle('MembershipLog', ['membership_id' => $renewedMembership['id'], 'options' => ['limit' => 1, 'sort' => 'id DESC']]);
754 $this->assertEquals(date('Y-01-01'), $log['start_date']);
755 $this->assertEquals(date('Y-12-31'), $log['end_date']);
756 $this->assertEquals(date('Y-m-d'), $log['modified_date']);
757 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'Current'), $log['status_id']);
758 }
759
4187b12f 760}