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