Merge pull request #15517 from jitendrapurohit/tax-recur-test
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * File for the MembershipTest class
30 *
31 * (PHP 5)
32 *
33 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
34 */
35
36 /**
37 * Test CRM_Member_Form_Membership functions.
38 *
39 * @package CiviCRM
40 * @group headless
41 */
42 class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
43
44 protected $_individualId;
45 protected $_contribution;
46 protected $_financialTypeId = 1;
47 protected $_entity = 'Membership';
48 protected $_params;
49 protected $_ids = [];
50 protected $_paymentProcessorID;
51
52 /**
53 * Membership type ID for annual fixed membership.
54 *
55 * @var int
56 */
57 protected $membershipTypeAnnualFixedID;
58
59 /**
60 * Parameters to create payment processor.
61 *
62 * @var array
63 */
64 protected $_processorParams = [];
65
66 /**
67 * ID of created membership.
68 *
69 * @var int
70 */
71 protected $_membershipID;
72
73 /**
74 * Payment instrument mapping.
75 *
76 * @var array
77 */
78 protected $paymentInstruments = [];
79
80 /**
81 * @var CiviMailUtils
82 */
83 protected $mut;
84
85 /**
86 * Test setup for every test.
87 *
88 * Connect to the database, truncate the tables that will be used
89 * and redirect stdin to a temporary file.
90 *
91 * @throws \CRM_Core_Exception
92 */
93 public function setUp() {
94 $this->_apiversion = 3;
95 parent::setUp();
96
97 $this->_individualId = $this->individualCreate();
98 $this->_paymentProcessorID = $this->processorCreate();
99
100 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/data.xml');
101
102 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', [
103 'domain_id' => 1,
104 'name' => "AnnualFixed",
105 'member_of_contact_id' => 23,
106 'duration_unit' => "year",
107 'minimum_fee' => 50,
108 'duration_interval' => 1,
109 'period_type' => "fixed",
110 'fixed_period_start_day' => "101",
111 'fixed_period_rollover_day' => "1231",
112 'relationship_type_id' => 20,
113 'financial_type_id' => 2,
114 ]);
115 $this->membershipTypeAnnualFixedID = $membershipTypeAnnualFixed['id'];
116
117 $instruments = $this->callAPISuccess('contribution', 'getoptions', ['field' => 'payment_instrument_id']);
118 $this->paymentInstruments = $instruments['values'];
119 }
120
121 /**
122 * Clean up after each test.
123 */
124 public function tearDown() {
125 $this->quickCleanUpFinancialEntities();
126 $this->quickCleanup(
127 [
128 'civicrm_relationship',
129 'civicrm_membership_type',
130 'civicrm_membership',
131 'civicrm_uf_match',
132 'civicrm_email',
133 ]
134 );
135 foreach ([17, 18, 23, 32] as $contactID) {
136 $this->callAPISuccess('contact', 'delete', ['id' => $contactID, 'skip_undelete' => TRUE]);
137 }
138 $this->callAPISuccess('relationship_type', 'delete', ['id' => 20]);
139 }
140
141 /**
142 * Test CRM_Member_Form_Membership::formRule() with a parameter
143 * that has an empty contact_select_id value
144 *
145 * @throws \CiviCRM_API3_Exception
146 */
147 public function testFormRuleEmptyContact() {
148 $params = [
149 'contact_select_id' => 0,
150 'membership_type_id' => [1 => NULL],
151 ];
152 $files = [];
153 $obj = new CRM_Member_Form_Membership();
154 $rc = $obj->formRule($params, $files, $obj);
155 $this->assertType('array', $rc);
156 $this->assertTrue(array_key_exists('membership_type_id', $rc));
157
158 $params['membership_type_id'] = [1 => 3];
159 $rc = $obj->formRule($params, $files, $obj);
160 $this->assertType('array', $rc);
161 $this->assertTrue(array_key_exists('join_date', $rc));
162 }
163
164 /**
165 * Test that form rule fails if start date is before join date.
166 *
167 * Test CRM_Member_Form_Membership::formRule() with a parameter
168 * that has an start date before the join date and a rolling
169 * membership type.
170 */
171 public function testFormRuleRollingEarlyStart() {
172 $unixNow = time();
173 $unixYesterday = $unixNow - (24 * 60 * 60);
174 $ymdYesterday = date('Y-m-d', $unixYesterday);
175 $params = [
176 'join_date' => date('Y-m-d'),
177 'start_date' => $ymdYesterday,
178 'end_date' => '',
179 'membership_type_id' => ['23', '15'],
180 ];
181 $files = [];
182 $obj = new CRM_Member_Form_Membership();
183 $rc = call_user_func(['CRM_Member_Form_Membership', 'formRule'],
184 $params, $files, $obj
185 );
186 $this->assertType('array', $rc);
187 $this->assertTrue(array_key_exists('start_date', $rc));
188 }
189
190 /**
191 * Test CRM_Member_Form_Membership::formRule() with a parameter
192 * that has an end date before the start date and a rolling
193 * membership type
194 */
195 public function testFormRuleRollingEarlyEnd() {
196 $unixNow = time();
197 $unixYesterday = $unixNow - (24 * 60 * 60);
198 $ymdYesterday = date('Y-m-d', $unixYesterday);
199 $params = [
200 'join_date' => date('Y-m-d'),
201 'start_date' => date('Y-m-d'),
202 'end_date' => $ymdYesterday,
203 'membership_type_id' => ['23', '15'],
204 ];
205 $files = [];
206 $obj = new CRM_Member_Form_Membership();
207 $rc = $obj->formRule($params, $files, $obj);
208 $this->assertType('array', $rc);
209 $this->assertTrue(array_key_exists('end_date', $rc));
210 }
211
212 /**
213 * Test CRM_Member_Form_Membership::formRule() with end date but no start date and a rolling membership type.
214 */
215 public function testFormRuleRollingEndNoStart() {
216 $unixNow = time();
217 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
218 $ymdYearFromNow = date('Y-m-d', $unixYearFromNow);
219 $params = [
220 'join_date' => date('Y-m-d'),
221 'start_date' => '',
222 'end_date' => $ymdYearFromNow,
223 'membership_type_id' => ['23', '15'],
224 ];
225 $files = [];
226 $obj = new CRM_Member_Form_Membership();
227 $rc = $obj->formRule($params, $files, $obj);
228 $this->assertType('array', $rc);
229 $this->assertTrue(array_key_exists('start_date', $rc));
230 }
231
232 /**
233 * Test CRM_Member_Form_Membership::formRule() with a parameter
234 * that has an end date and a lifetime membership type
235 */
236 public function testFormRuleRollingLifetimeEnd() {
237 $unixNow = time();
238 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
239 $params = [
240 'join_date' => date('Y-m-d'),
241 'start_date' => date('Y-m-d'),
242 'end_date' => date('Y-m-d',
243 $unixYearFromNow
244 ),
245 'membership_type_id' => ['23', '25'],
246 ];
247 $files = [];
248 $obj = new CRM_Member_Form_Membership();
249 $rc = $obj->formRule($params, $files, $obj);
250 $this->assertType('array', $rc);
251 $this->assertTrue(array_key_exists('status_id', $rc));
252 }
253
254 /**
255 * Test CRM_Member_Form_Membership::formRule() with a parameter
256 * that has permanent override and no status
257 */
258 public function testFormRulePermanentOverrideWithNoStatus() {
259 $unixNow = time();
260 $params = [
261 'join_date' => date('Y-m-d'),
262 'membership_type_id' => ['23', '25'],
263 'is_override' => TRUE,
264 ];
265 $files = [];
266 $obj = new CRM_Member_Form_Membership();
267 $rc = $obj->formRule($params, $files, $obj);
268 $this->assertType('array', $rc);
269 $this->assertTrue(array_key_exists('status_id', $rc));
270 }
271
272 public function testFormRuleUntilDateOverrideWithValidOverrideEndDate() {
273 $params = [
274 'join_date' => date('Y-m-d'),
275 'membership_type_id' => ['23', '25'],
276 'is_override' => TRUE,
277 'status_id' => 1,
278 'status_override_end_date' => date('Y-m-d'),
279 ];
280 $files = [];
281 $membershipForm = new CRM_Member_Form_Membership();
282 $validationResponse = $membershipForm->formRule($params, $files, $membershipForm);
283 $this->assertTrue($validationResponse);
284 }
285
286 public function testFormRuleUntilDateOverrideWithNoOverrideEndDate() {
287 $params = [
288 'join_date' => date('Y-m-d'),
289 'membership_type_id' => ['23', '25'],
290 'is_override' => CRM_Member_StatusOverrideTypes::UNTIL_DATE,
291 'status_id' => 1,
292 ];
293 $files = [];
294 $membershipForm = new CRM_Member_Form_Membership();
295 $validationResponse = $membershipForm->formRule($params, $files, $membershipForm);
296 $this->assertType('array', $validationResponse);
297 $this->assertEquals('Please enter the Membership override end date.', $validationResponse['status_override_end_date']);
298 }
299
300 /**
301 * Test CRM_Member_Form_Membership::formRule() with a join date
302 * of one month from now and a rolling membership type
303 */
304 public function testFormRuleRollingJoin1MonthFromNow() {
305 $unixNow = time();
306 $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
307 $params = [
308 'join_date' => date('Y-m-d', $unix1MFmNow),
309 'start_date' => '',
310 'end_date' => '',
311 'membership_type_id' => ['23', '15'],
312 ];
313 $files = [];
314 $obj = new CRM_Member_Form_Membership();
315 $rc = $obj->formRule($params, $files, $obj);
316
317 // Should have found no valid membership status.
318 $this->assertType('array', $rc);
319 $this->assertTrue(array_key_exists('_qf_default', $rc));
320 }
321
322 /**
323 * Test CRM_Member_Form_Membership::formRule() with a join date of today and a rolling membership type.
324 */
325 public function testFormRuleRollingJoinToday() {
326 $params = [
327 'join_date' => date('Y-m-d'),
328 'start_date' => '',
329 'end_date' => '',
330 'membership_type_id' => ['23', '15'],
331 ];
332 $files = [];
333 $obj = new CRM_Member_Form_Membership();
334 $rc = $obj->formRule($params, $files, $obj);
335
336 // Should have found New membership status
337 $this->assertTrue($rc);
338 }
339
340 /**
341 * Test CRM_Member_Form_Membership::formRule() with a join date
342 * of one month ago and a rolling membership type
343 */
344 public function testFormRuleRollingJoin1MonthAgo() {
345 $unixNow = time();
346 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
347 $params = [
348 'join_date' => date('Y-m-d', $unix1MAgo),
349 'start_date' => '',
350 'end_date' => '',
351 'membership_type_id' => ['23', '15'],
352 ];
353 $files = [];
354 $obj = new CRM_Member_Form_Membership();
355 $rc = $obj->formRule($params, $files, $obj);
356
357 // Should have found New membership status.
358 $this->assertTrue($rc);
359 }
360
361 /**
362 * Test CRM_Member_Form_Membership::formRule() with a join date of six months ago and a rolling membership type.
363 */
364 public function testFormRuleRollingJoin6MonthsAgo() {
365 $unixNow = time();
366 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
367 $params = [
368 'join_date' => date('Y-m-d', $unix6MAgo),
369 'start_date' => '',
370 'end_date' => '',
371 'membership_type_id' => ['23', '15'],
372 ];
373 $files = [];
374 $obj = new CRM_Member_Form_Membership();
375 $rc = $obj->formRule($params, $files, $obj);
376
377 // Should have found Current membership status.
378 $this->assertTrue($rc);
379 }
380
381 /**
382 * Test CRM_Member_Form_Membership::formRule() with a join date
383 * of one year+ ago and a rolling membership type
384 */
385 public function testFormRuleRollingJoin1YearAgo() {
386 $unixNow = time();
387 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
388 $params = [
389 'join_date' => date('Y-m-d', $unix1YAgo),
390 'start_date' => '',
391 'end_date' => '',
392 'membership_type_id' => ['23', '15'],
393 ];
394 $files = [];
395 $obj = new CRM_Member_Form_Membership();
396 $rc = $obj->formRule($params, $files, $obj);
397
398 // Should have found Grace membership status
399 $this->assertTrue($rc);
400 }
401
402 /**
403 * Test CRM_Member_Form_Membership::formRule() with a join date
404 * of two years ago and a rolling membership type
405 */
406 public function testFormRuleRollingJoin2YearsAgo() {
407 $unixNow = time();
408 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
409 $params = [
410 'join_date' => date('Y-m-d', $unix2YAgo),
411 'start_date' => '',
412 'end_date' => '',
413 'membership_type_id' => ['23', '15'],
414 ];
415 $files = [];
416 $obj = new CRM_Member_Form_Membership();
417 $rc = $obj->formRule($params, $files, $obj);
418
419 // Should have found Expired membership status
420 $this->assertTrue($rc);
421 }
422
423 /**
424 * Test CRM_Member_Form_Membership::formRule() with a join date
425 * of six months ago and a fixed membership type
426 */
427 public function testFormRuleFixedJoin6MonthsAgo() {
428 $unixNow = time();
429 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
430 $params = [
431 'join_date' => date('Y-m-d', $unix6MAgo),
432 'start_date' => '',
433 'end_date' => '',
434 'membership_type_id' => ['23', '7'],
435 ];
436 $files = [];
437 $obj = new CRM_Member_Form_Membership();
438 $rc = $obj->formRule($params, $files, $obj);
439
440 // Should have found Current membership status
441 $this->assertTrue($rc);
442 }
443
444 /**
445 * Test the submit function of the membership form.
446 *
447 * @param string $thousandSeparator
448 *
449 * @dataProvider getThousandSeparators
450 */
451 public function testSubmit($thousandSeparator) {
452 CRM_Core_Session::singleton()->getStatus(TRUE);
453 $this->setCurrencySeparators($thousandSeparator);
454 $form = $this->getForm();
455 $form->preProcess();
456 $this->mut = new CiviMailUtils($this, TRUE);
457 $form->_mode = 'test';
458 $this->createLoggedInUser();
459 $params = [
460 'cid' => $this->_individualId,
461 'join_date' => date('2/d/Y', time()),
462 'start_date' => '',
463 'end_date' => '',
464 // This format reflects the 23 being the organisation & the 25 being the type.
465 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
466 'auto_renew' => '0',
467 'max_related' => '',
468 'num_terms' => '1',
469 'source' => '',
470 'total_amount' => $this->formatMoneyInput(1234.56),
471 //Member dues, see data.xml
472 'financial_type_id' => '2',
473 'soft_credit_type_id' => '',
474 'soft_credit_contact_id' => '',
475 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
476 'payment_processor_id' => $this->_paymentProcessorID,
477 'credit_card_number' => '4111111111111111',
478 'cvv2' => '123',
479 'credit_card_exp_date' => [
480 'M' => '9',
481 // TODO: Future proof
482 'Y' => '2024',
483 ],
484 'credit_card_type' => 'Visa',
485 'billing_first_name' => 'Test',
486 'billing_middlename' => 'Last',
487 'billing_street_address-5' => '10 Test St',
488 'billing_city-5' => 'Test',
489 'billing_state_province_id-5' => '1003',
490 'billing_postal_code-5' => '90210',
491 'billing_country_id-5' => '1228',
492 'send_receipt' => TRUE,
493 'receipt_text' => 'Receipt text',
494 ];
495 $form->_contactID = $this->_individualId;
496 $form->testSubmit($params);
497 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
498 $this->callAPISuccessGetCount('ContributionRecur', ['contact_id' => $this->_individualId], 0);
499 $contribution = $this->callAPISuccess('Contribution', 'get', [
500 'contact_id' => $this->_individualId,
501 'is_test' => TRUE,
502 ]);
503
504 //CRM-20264 : Check that CC type and number (last 4 digit) is stored during backoffice membership payment
505 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
506 $financialTrxn = $this->callAPISuccessGetSingle(
507 'FinancialTrxn',
508 [
509 'id' => $lastFinancialTrxnId['financialTrxnId'],
510 'return' => ['card_type_id', 'pan_truncation'],
511 ]
512 );
513 $this->assertEquals(1, $financialTrxn['card_type_id']);
514 $this->assertEquals(1111, $financialTrxn['pan_truncation']);
515
516 $this->callAPISuccessGetCount('LineItem', [
517 'entity_id' => $membership['id'],
518 'entity_table' => 'civicrm_membership',
519 'contribution_id' => $contribution['id'],
520 ], 1);
521
522 $this->_checkFinancialRecords([
523 'id' => $contribution['id'],
524 'total_amount' => 1234.56,
525 'financial_account_id' => 2,
526 'payment_instrument_id' => $this->callAPISuccessGetValue('PaymentProcessor', [
527 'id' => $this->_paymentProcessorID,
528 'return' => 'payment_instrument_id',
529 ]),
530 ], 'online');
531 $this->mut->checkMailLog([
532 CRM_Utils_Money::format('1234.56'),
533 'Receipt text',
534 ]);
535 $this->mut->stop();
536 $this->assertEquals([
537 [
538 'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is December 31st, ' . date('Y') . '. A membership confirmation and receipt has been sent to anthony_anderson@civicrm.org.',
539 'title' => 'Complete',
540 'type' => 'success',
541 'options' => NULL,
542 ],
543 ], CRM_Core_Session::singleton()->getStatus());
544 }
545
546 /**
547 * Test the submit function of the membership form on membership type change.
548 * Check if the related contribuion is also updated if the minimum_fee didn't match
549 */
550 public function testContributionUpdateOnMembershipTypeChange() {
551 // Step 1: Create a Membership via backoffice whose with 50.00 payment
552 $form = $this->getForm();
553 $form->preProcess();
554 $this->mut = new CiviMailUtils($this, TRUE);
555 $this->createLoggedInUser();
556 $priceSet = $this->callAPISuccess('PriceSet', 'Get', ["extends" => "CiviMember"]);
557 $form->set('priceSetId', $priceSet['id']);
558 CRM_Price_BAO_PriceSet::buildPriceSet($form);
559 $params = [
560 'cid' => $this->_individualId,
561 'join_date' => date('Y-m-d'),
562 'start_date' => '',
563 'end_date' => '',
564 // This format reflects the 23 being the organisation & the 25 being the type.
565 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
566 'record_contribution' => 1,
567 'total_amount' => 50,
568 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
569 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
570 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
571 //Member dues, see data.xml
572 'financial_type_id' => '2',
573 'payment_processor_id' => $this->_paymentProcessorID,
574 ];
575 $form->_contactID = $this->_individualId;
576 $form->testSubmit($params);
577 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
578 // check the membership status after partial payment, if its Pending
579 $this->assertEquals(array_search('New', CRM_Member_PseudoConstant::membershipStatus()), $membership['status_id']);
580 $contribution = $this->callAPISuccessGetSingle('Contribution', [
581 'contact_id' => $this->_individualId,
582 ]);
583 $this->assertEquals('Completed', $contribution['contribution_status']);
584 $this->assertEquals(50.00, $contribution['total_amount']);
585 $this->assertEquals(50.00, $contribution['net_amount']);
586
587 // Step 2: Change the membership type whose minimum free is less than earlier membership
588 $secondMembershipType = $this->callAPISuccess('membership_type', 'create', [
589 'domain_id' => 1,
590 'name' => "Second Test Membership",
591 'member_of_contact_id' => 23,
592 'duration_unit' => "month",
593 'minimum_fee' => 25,
594 'duration_interval' => 1,
595 'period_type' => "fixed",
596 'fixed_period_start_day' => "101",
597 'fixed_period_rollover_day' => "1231",
598 'relationship_type_id' => 20,
599 'financial_type_id' => 2,
600 ]);
601 Civi::settings()->set('update_contribution_on_membership_type_change', TRUE);
602 $form = $this->getForm();
603 $form->preProcess();
604 $form->_id = $membership['id'];
605 $form->set('priceSetId', $priceSet['id']);
606 CRM_Price_BAO_PriceSet::buildPriceSet($form);
607 $form->_action = CRM_Core_Action::UPDATE;
608 $params = [
609 'cid' => $this->_individualId,
610 'join_date' => date('Y-m-d'),
611 'start_date' => '',
612 'end_date' => '',
613 // This format reflects the 23 being the organisation & the 25 being the type.
614 'membership_type_id' => [23, $secondMembershipType['id']],
615 'record_contribution' => 1,
616 'status_id' => 1,
617 'total_amount' => 25,
618 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
619 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
620 //Member dues, see data.xml
621 'financial_type_id' => '2',
622 'payment_processor_id' => $this->_paymentProcessorID,
623 ];
624 $form->_contactID = $this->_individualId;
625 $form->testSubmit($params);
626 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
627 // check the membership status after partial payment, if its Pending
628 $contribution = $this->callAPISuccessGetSingle('Contribution', [
629 'contact_id' => $this->_individualId,
630 ]);
631 $payment = CRM_Contribute_BAO_Contribution::getPaymentInfo($membership['id'], 'membership', FALSE, TRUE);
632 // Check the contribution status on membership type change whose minimum fee was less than earlier memebership
633 $this->assertEquals('Pending refund', $contribution['contribution_status']);
634 // Earlier paid amount
635 $this->assertEquals(50, $payment['paid']);
636 // balance remaning
637 $this->assertEquals(-25, $payment['balance']);
638 }
639
640 /**
641 * Test the submit function of the membership form for partial payment.
642 *
643 * @param string $thousandSeparator
644 * punctuation used to refer to thousands.
645 *
646 * @dataProvider getThousandSeparators
647 */
648 public function testSubmitPartialPayment($thousandSeparator) {
649 $this->setCurrencySeparators($thousandSeparator);
650 // Step 1: submit a partial payment for a membership via backoffice
651 $form = $this->getForm();
652 $form->preProcess();
653 $this->mut = new CiviMailUtils($this, TRUE);
654 $this->createLoggedInUser();
655 $priceSet = $this->callAPISuccess('PriceSet', 'Get', ["extends" => "CiviMember"]);
656 $form->set('priceSetId', $priceSet['id']);
657 $partiallyPaidAmount = 25;
658 CRM_Price_BAO_PriceSet::buildPriceSet($form);
659 $params = [
660 'cid' => $this->_individualId,
661 'join_date' => date('Y-m-d'),
662 'start_date' => '',
663 'end_date' => '',
664 // This format reflects the 23 being the organisation & the 25 being the type.
665 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
666 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
667 'record_contribution' => 1,
668 'total_amount' => $this->formatMoneyInput($partiallyPaidAmount),
669 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
670 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Partially paid'),
671 //Member dues, see data.xml
672 'financial_type_id' => '2',
673 'payment_processor_id' => $this->_paymentProcessorID,
674 ];
675 $form->_contactID = $this->_individualId;
676 $form->testSubmit($params);
677 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
678 // check the membership status after partial payment, if its Pending
679 $this->assertEquals(array_search('Pending', CRM_Member_PseudoConstant::membershipStatus()), $membership['status_id']);
680 $contribution = $this->callAPISuccessGetSingle('Contribution', [
681 'contact_id' => $this->_individualId,
682 ]);
683 $this->assertEquals('Partially paid', $contribution['contribution_status']);
684 // $this->assertEquals(50.00, $contribution['total_amount']);
685 // $this->assertEquals(25.00, $contribution['net_amount']);
686
687 // Step 2: submit the other half of the partial payment
688 // via AdditionalPayment form to complete the related contribution
689 $form = new CRM_Contribute_Form_AdditionalPayment();
690 $submitParams = [
691 'contribution_id' => $contribution['contribution_id'],
692 'contact_id' => $this->_individualId,
693 'total_amount' => $this->formatMoneyInput($partiallyPaidAmount),
694 'currency' => 'USD',
695 'financial_type_id' => 2,
696 'receive_date' => '2015-04-21 23:27:00',
697 'trxn_date' => '2017-04-11 13:05:11',
698 'payment_processor_id' => 0,
699 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
700 'check_number' => 'check-12345',
701 ];
702 $form->cid = $this->_individualId;
703 $form->testSubmit($submitParams);
704 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
705 // check the membership status after additional payment, if its changed to 'New'
706 $this->assertEquals(array_search('New', CRM_Member_PseudoConstant::membershipStatus()), $membership['status_id']);
707
708 // check the contribution status and net amount after additional payment
709 $contribution = $this->callAPISuccessGetSingle('Contribution', [
710 'contact_id' => $this->_individualId,
711 ]);
712 $this->assertEquals('Completed', $contribution['contribution_status']);
713 // $this->assertEquals(50.00, $contribution['net_amount']);
714 }
715
716 /**
717 * Test the submit function of the membership form.
718 *
719 * @throws \CRM_Core_Exception
720 */
721 public function testSubmitRecur() {
722 CRM_Core_Session::singleton()->getStatus(TRUE);
723 $pendingVal = $this->callAPISuccessGetValue('OptionValue', [
724 'return' => "id",
725 'option_group_id' => "contribution_status",
726 'label' => "Pending Label**",
727 ]);
728 //Update label for Pending contribution status.
729 $this->callAPISuccess('OptionValue', 'create', [
730 'id' => $pendingVal,
731 'label' => "PendingEdited",
732 ]);
733
734 $form = $this->getForm();
735
736 $this->callAPISuccess('MembershipType', 'create', [
737 'id' => $this->membershipTypeAnnualFixedID,
738 'duration_unit' => 'month',
739 'duration_interval' => 1,
740 'auto_renew' => TRUE,
741 ]);
742 $form->preProcess();
743 $this->createLoggedInUser();
744 $params = $this->getBaseSubmitParams();
745 $form->_mode = 'test';
746 $form->_contactID = $this->_individualId;
747 $form->testSubmit($params);
748 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
749 $this->callAPISuccessGetCount('ContributionRecur', ['contact_id' => $this->_individualId], 1);
750
751 $contribution = $this->callAPISuccess('Contribution', 'get', [
752 'contact_id' => $this->_individualId,
753 'is_test' => TRUE,
754 ]);
755
756 //Check if Membership Payment is recorded.
757 $this->callAPISuccessGetCount('MembershipPayment', [
758 'membership_id' => $membership['id'],
759 'contribution_id' => $contribution['id'],
760 ], 1);
761
762 // CRM-16992.
763 $this->callAPISuccessGetCount('LineItem', [
764 'entity_id' => $membership['id'],
765 'entity_table' => 'civicrm_membership',
766 'contribution_id' => $contribution['id'],
767 ], 1);
768 $this->assertEquals([
769 [
770 'text' => 'AnnualFixed membership for Mr. Anthony Anderson II has been added. The new membership End Date is ' . date('F jS, Y', strtotime('last day of this month')) . ' 12:00 AM.',
771 'title' => 'Complete',
772 'type' => 'success',
773 'options' => NULL,
774 ],
775 ], CRM_Core_Session::singleton()->getStatus());
776 }
777
778 /**
779 * CRM-20946: Test the financial entires especially the reversed amount,
780 * after related Contribution is cancelled
781 */
782 public function testFinancialEntiriesOnCancelledContribution() {
783 // Create two memberships for individual $this->_individualId, via a price set in the back end.
784 $this->createTwoMembershipsViaPriceSetInBackEnd($this->_individualId);
785
786 // cancel the related contribution via API
787 $contribution = $this->callAPISuccessGetSingle('Contribution', [
788 'contact_id' => $this->_individualId,
789 'contribution_status_id' => 2,
790 ]);
791 $this->callAPISuccess('Contribution', 'create', [
792 'id' => $contribution['id'],
793 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Cancelled'),
794 ]);
795
796 // fetch financial_trxn ID of the related contribution
797 $sql = "SELECT financial_trxn_id
798 FROM civicrm_entity_financial_trxn
799 WHERE entity_id = %1 AND entity_table = 'civicrm_contribution'
800 ORDER BY id DESC
801 LIMIT 1
802 ";
803 $financialTrxnID = CRM_Core_DAO::singleValueQuery($sql, [1 => [$contribution['id'], 'Int']]);
804
805 // fetch entity_financial_trxn records and compare their cancelled records
806 $result = $this->callAPISuccess('EntityFinancialTrxn', 'Get', [
807 'financial_trxn_id' => $financialTrxnID,
808 'entity_table' => 'civicrm_financial_item',
809 ]);
810 // compare the reversed amounts of respective memberships after cancelling contribution
811 $cancelledMembershipAmounts = [
812 -20.00,
813 -10.00,
814 ];
815 $count = 0;
816 foreach ($result['values'] as $record) {
817 $this->assertEquals($cancelledMembershipAmounts[$count], $record['amount']);
818 $count++;
819 }
820 }
821
822 /**
823 * Test the submit function of the membership form.
824 */
825 public function testSubmitPayLaterWithBilling() {
826 $form = $this->getForm(NULL);
827 $form->preProcess();
828 $this->createLoggedInUser();
829 $params = [
830 'cid' => $this->_individualId,
831 'join_date' => date('Y-m-d'),
832 'start_date' => '',
833 'end_date' => '',
834 // This format reflects the 23 being the organisation & the 25 being the type.
835 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
836 'auto_renew' => '0',
837 'max_related' => '',
838 'num_terms' => '2',
839 'source' => '',
840 'total_amount' => '50.00',
841 //Member dues, see data.xml
842 'financial_type_id' => '2',
843 'soft_credit_type_id' => '',
844 'soft_credit_contact_id' => '',
845 'payment_instrument_id' => 4,
846 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
847 'receipt_text_signup' => 'Thank you text',
848 'payment_processor_id' => $this->_paymentProcessorID,
849 'record_contribution' => TRUE,
850 'trxn_id' => 777,
851 'contribution_status_id' => 2,
852 'billing_first_name' => 'Test',
853 'billing_middlename' => 'Last',
854 'billing_street_address-5' => '10 Test St',
855 'billing_city-5' => 'Test',
856 'billing_state_province_id-5' => '1003',
857 'billing_postal_code-5' => '90210',
858 'billing_country_id-5' => '1228',
859 ];
860 $form->_contactID = $this->_individualId;
861
862 $form->testSubmit($params);
863 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
864 $contribution = $this->callAPISuccessGetSingle('Contribution', [
865 'contact_id' => $this->_individualId,
866 'contribution_status_id' => 2,
867 ]);
868 $this->assertEquals($contribution['trxn_id'], 777);
869
870 $this->callAPISuccessGetCount('LineItem', [
871 'entity_id' => $membership['id'],
872 'entity_table' => 'civicrm_membership',
873 'contribution_id' => $contribution['id'],
874 ], 1);
875 $this->callAPISuccessGetSingle('address', [
876 'contact_id' => $this->_individualId,
877 'street_address' => '10 Test St',
878 'postal_code' => 90210,
879 ]);
880 }
881
882 /**
883 * Test if membership is updated to New after contribution
884 * is updated from Partially paid to Completed.
885 */
886 public function testSubmitUpdateMembershipFromPartiallyPaid() {
887 $memStatus = CRM_Member_BAO_Membership::buildOptions('status_id', 'validate');
888
889 //Perform a pay later membership contribution.
890 $this->testSubmitPayLaterWithBilling();
891 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
892 $this->assertEquals($membership['status_id'], array_search('Pending', $memStatus));
893 $contribution = $this->callAPISuccessGetSingle('MembershipPayment', [
894 'membership_id' => $membership['id'],
895 ]);
896
897 //Update contribution to Partially paid.
898 $prevContribution = $this->callAPISuccess('Contribution', 'create', [
899 'id' => $contribution['contribution_id'],
900 'contribution_status_id' => 'Partially paid',
901 ]);
902 $prevContribution = $prevContribution['values'][1];
903
904 //Complete the contribution from offline form.
905 $form = new CRM_Contribute_Form_Contribution();
906 $submitParams = [
907 'id' => $contribution['contribution_id'],
908 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
909 'price_set_id' => 0,
910 ];
911 $fields = ['total_amount', 'net_amount', 'financial_type_id', 'receive_date', 'contact_id', 'payment_instrument_id'];
912 foreach ($fields as $val) {
913 $submitParams[$val] = $prevContribution[$val];
914 }
915 $form->testSubmit($submitParams, CRM_Core_Action::UPDATE);
916
917 //Check if Membership is updated to New.
918 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
919 $this->assertEquals($membership['status_id'], array_search('New', $memStatus));
920 }
921
922 /**
923 * Test the submit function of the membership form.
924 */
925 public function testSubmitRecurCompleteInstant() {
926 $form = $this->getForm();
927 $mut = new CiviMailUtils($this, TRUE);
928 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID);
929 $processor->setDoDirectPaymentResult([
930 'payment_status_id' => 1,
931 'trxn_id' => 'kettles boil water',
932 'fee_amount' => .14,
933 ]);
934 $processorDetail = $processor->getPaymentProcessor();
935 $this->callAPISuccess('MembershipType', 'create', [
936 'id' => $this->membershipTypeAnnualFixedID,
937 'duration_unit' => 'month',
938 'duration_interval' => 1,
939 'auto_renew' => TRUE,
940 ]);
941 $form->preProcess();
942 $this->createLoggedInUser();
943 $params = $this->getBaseSubmitParams();
944 $form->_mode = 'test';
945 $form->_contactID = $this->_individualId;
946 $form->testSubmit($params);
947 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
948 $this->callAPISuccessGetCount('ContributionRecur', ['contact_id' => $this->_individualId], 1);
949
950 $contribution = $this->callAPISuccess('Contribution', 'getsingle', [
951 'contact_id' => $this->_individualId,
952 'is_test' => TRUE,
953 ]);
954
955 $this->assertEquals(.14, $contribution['fee_amount']);
956 $this->assertEquals('kettles boil water', $contribution['trxn_id']);
957 $this->assertEquals($processorDetail['payment_instrument_id'], $contribution['payment_instrument_id']);
958
959 $this->callAPISuccessGetCount('LineItem', [
960 'entity_id' => $membership['id'],
961 'entity_table' => 'civicrm_membership',
962 'contribution_id' => $contribution['id'],
963 ], 1);
964 $mut->checkMailLog([
965 '===========================================================
966 Billing Name and Address
967 ===========================================================
968 Test
969 10 Test St
970 Test, AR 90210
971 US',
972 '===========================================================
973 Membership Information
974 ===========================================================
975 Membership Type: AnnualFixed
976 Membership Start Date: ',
977 '===========================================================
978 Credit Card Information
979 ===========================================================
980 Visa
981 ************1111
982 Expires: ',
983 ]);
984 $mut->stop();
985
986 }
987
988 /**
989 * Test membership form with Failed Contribution.
990 */
991 public function testFormWithFailedContribution() {
992 $form = $this->getForm();
993 $form->preProcess();
994 $this->createLoggedInUser();
995 $params = $this->getBaseSubmitParams();
996 unset($params['price_set_id']);
997 unset($params['credit_card_number']);
998 unset($params['cvv2']);
999 unset($params['credit_card_exp_date']);
1000 unset($params['credit_card_type']);
1001 unset($params['send_receipt']);
1002 unset($params['is_recur']);
1003
1004 $params['record_contribution'] = TRUE;
1005 $params['contribution_status_id'] = array_search('Failed', CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'));
1006 $form->_mode = NULL;
1007 $form->_contactID = $this->_individualId;
1008
1009 $form->testSubmit($params);
1010 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
1011 $form->testSubmit($params);
1012 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
1013 $this->assertEquals($membership['status_id'], array_search('Pending', CRM_Member_PseudoConstant::membershipStatus()));
1014 }
1015
1016 /**
1017 * CRM-20955, CRM-20966:
1018 * Test creating two memberships with inheritance via price set in the back end,
1019 * checking that the correct primary & secondary memberships, contributions, line items
1020 * & membership_payment records are created.
1021 * Uses some data from tests/phpunit/CRM/Member/Form/dataset/data.xml .
1022 */
1023 public function testTwoInheritedMembershipsViaPriceSetInBackend() {
1024 // Create an organization and give it a "Member of" relationship to $this->_individualId.
1025 $orgID = $this->organizationCreate();
1026 $relationship = $this->callAPISuccess('relationship', 'create', [
1027 'contact_id_a' => $this->_individualId,
1028 'contact_id_b' => $orgID,
1029 'relationship_type_id' => 20,
1030 'is_active' => 1,
1031 ]);
1032
1033 // Create two memberships for the organization, via a price set in the back end.
1034 $this->createTwoMembershipsViaPriceSetInBackEnd($orgID);
1035
1036 // Check the primary memberships on the organization.
1037 $orgMembershipResult = $this->callAPISuccess('membership', 'get', [
1038 'contact_id' => $orgID,
1039 ]);
1040 $this->assertEquals(2, $orgMembershipResult['count'], "2 primary memberships should have been created on the organization.");
1041 $primaryMembershipIds = [];
1042 foreach ($orgMembershipResult['values'] as $membership) {
1043 $primaryMembershipIds[] = $membership['id'];
1044 $this->assertTrue(empty($membership['owner_membership_id']), "Membership on the organization has owner_membership_id so is inherited.");
1045 }
1046
1047 // CRM-20955: check that correct inherited memberships were created for the individual,
1048 // for both of the primary memberships.
1049 $individualMembershipResult = $this->callAPISuccess('membership', 'get', [
1050 'contact_id' => $this->_individualId,
1051 ]);
1052 $this->assertEquals(2, $individualMembershipResult['count'], "2 inherited memberships should have been created on the individual.");
1053 foreach ($individualMembershipResult['values'] as $membership) {
1054 $this->assertNotEmpty($membership['owner_membership_id'], "Membership on the individual lacks owner_membership_id so is not inherited.");
1055 $this->assertNotContains($membership['id'], $primaryMembershipIds, "Inherited membership id should not be the id of a primary membership.");
1056 $this->assertContains($membership['owner_membership_id'], $primaryMembershipIds, "Inherited membership owner_membership_id should be the id of a primary membership.");
1057 }
1058
1059 // CRM-20966: check that the correct membership contribution, line items
1060 // & membership_payment records were created for the organization.
1061 $contributionResult = $this->callAPISuccess('contribution', 'get', [
1062 'contact_id' => $orgID,
1063 'sequential' => 1,
1064 'api.line_item.get' => [],
1065 'api.membership_payment.get' => [],
1066 ]);
1067 $this->assertEquals(1, $contributionResult['count'], "One contribution should have been created for the organization's memberships.");
1068
1069 $this->assertEquals(2, $contributionResult['values'][0]['api.line_item.get']['count'], "2 line items should have been created for the organization's memberships.");
1070 foreach ($contributionResult['values'][0]['api.line_item.get']['values'] as $lineItem) {
1071 $this->assertEquals('civicrm_membership', $lineItem['entity_table'], "Membership line item's entity_table should be 'civicrm_membership'.");
1072 $this->assertContains($lineItem['entity_id'], $primaryMembershipIds, "Membership line item's entity_id should be the id of a primary membership.");
1073 }
1074
1075 $this->assertEquals(2, $contributionResult['values'][0]['api.membership_payment.get']['count'], "2 membership payment records should have been created for the organization's memberships.");
1076 foreach ($contributionResult['values'][0]['api.membership_payment.get']['values'] as $membershipPayment) {
1077 $this->assertEquals($contributionResult['values'][0]['id'], $membershipPayment['contribution_id'], "membership payment's contribution ID should be the ID of the organization's membership contribution.");
1078 $this->assertContains($membershipPayment['membership_id'], $primaryMembershipIds, "membership payment's membership ID should be the ID of a primary membership.");
1079 }
1080
1081 // CRM-20966: check that deleting relationship used for inheritance does not delete contribution.
1082 $this->callAPISuccess('relationship', 'delete', [
1083 'id' => $relationship['id'],
1084 ]);
1085
1086 $contributionResultAfterRelationshipDelete = $this->callAPISuccess('contribution', 'get', [
1087 'id' => $contributionResult['values'][0]['id'],
1088 'contact_id' => $orgID,
1089 ]);
1090 $this->assertEquals(1, $contributionResultAfterRelationshipDelete['count'], "Contribution has been wrongly deleted.");
1091 }
1092
1093 /**
1094 * dev/core/issues/860:
1095 * Test creating two memberships via price set in the back end with a discount,
1096 * checking that the line items have correct amounts.
1097 */
1098 public function testTwoMembershipsViaPriceSetInBackendWithDiscount() {
1099 // Register buildAmount hook to apply discount.
1100 $this->hookClass->setHook('civicrm_buildAmount', [$this, 'buildAmountMembershipDiscount']);
1101
1102 // Create two memberships for individual $this->_individualId, via a price set in the back end.
1103 $this->createTwoMembershipsViaPriceSetInBackEnd($this->_individualId);
1104 $contribution = $this->callAPISuccessGetSingle('Contribution', [
1105 'contact_id' => $this->_individualId,
1106 ]);
1107 // Note: we can't check for the contribution total being discounted, because the total is set
1108 // when the contribution is created via $form->testSubmit(), but buildAmount isn't called
1109 // until testSubmit() runs. Fixing that might involve making testSubmit() more sophisticated,
1110 // or just hacking total_amount for this case.
1111
1112 $lineItemResult = $this->callAPISuccess('LineItem', 'get', [
1113 'contribution_id' => $contribution['id'],
1114 ]);
1115 $this->assertEquals(2, $lineItemResult['count']);
1116 $discountedItems = 0;
1117 foreach ($lineItemResult['values'] as $lineItem) {
1118 if (CRM_Utils_String::startsWith($lineItem['label'], 'Long Haired Goat')) {
1119 $this->assertEquals(15.0, $lineItem['line_total']);
1120 $this->assertEquals('Long Haired Goat - one leg free!', $lineItem['label']);
1121 $discountedItems++;
1122 }
1123 }
1124 $this->assertEquals(1, $discountedItems);
1125 }
1126
1127 /**
1128 * Implements hook_civicrm_buildAmount() for testTwoMembershipsViaPriceSetInBackendWithDiscount().
1129 */
1130 public function buildAmountMembershipDiscount($pageType, &$form, &$amount) {
1131 foreach ($amount as $id => $priceField) {
1132 if (is_array($priceField['options'])) {
1133 foreach ($priceField['options'] as $optionId => $option) {
1134 if ($option['membership_type_id'] == 15) {
1135 // Long Haired Goat membership discount.
1136 $amount[$id]['options'][$optionId]['amount'] = $option['amount'] * 0.75;
1137 $amount[$id]['options'][$optionId]['label'] = $option['label'] . ' - one leg free!';
1138 }
1139 }
1140 }
1141 }
1142 }
1143
1144 /**
1145 * Get a membership form object.
1146 *
1147 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
1148 *
1149 * @return \CRM_Member_Form_Membership
1150 */
1151 protected function getForm() {
1152 if (isset($_REQUEST['cid'])) {
1153 unset($_REQUEST['cid']);
1154 }
1155 $form = new CRM_Member_Form_Membership();
1156 $_SERVER['REQUEST_METHOD'] = 'GET';
1157 $form->controller = new CRM_Core_Controller();
1158 return $form;
1159 }
1160
1161 /**
1162 * @return array
1163 */
1164 protected function getBaseSubmitParams() {
1165 $params = [
1166 'cid' => $this->_individualId,
1167 'price_set_id' => 0,
1168 'join_date' => date('Y-m-d'),
1169 'start_date' => '',
1170 'end_date' => '',
1171 'campaign_id' => '',
1172 // This format reflects the 23 being the organisation & the 25 being the type.
1173 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
1174 'auto_renew' => '1',
1175 'is_recur' => 1,
1176 'max_related' => 0,
1177 'num_terms' => '1',
1178 'source' => '',
1179 'total_amount' => '77.00',
1180 //Member dues, see data.xml
1181 'financial_type_id' => '2',
1182 'soft_credit_type_id' => 11,
1183 'soft_credit_contact_id' => '',
1184 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
1185 'receipt_text' => 'Thank you text',
1186 'payment_processor_id' => $this->_paymentProcessorID,
1187 'credit_card_number' => '4111111111111111',
1188 'cvv2' => '123',
1189 'credit_card_exp_date' => [
1190 'M' => '9',
1191 // TODO: Future proof
1192 'Y' => '2019',
1193 ],
1194 'credit_card_type' => 'Visa',
1195 'billing_first_name' => 'Test',
1196 'billing_middlename' => 'Last',
1197 'billing_street_address-5' => '10 Test St',
1198 'billing_city-5' => 'Test',
1199 'billing_state_province_id-5' => '1003',
1200 'billing_postal_code-5' => '90210',
1201 'billing_country_id-5' => '1228',
1202 'send_receipt' => 1,
1203 ];
1204 return $params;
1205 }
1206
1207 /**
1208 * Scenario builder:
1209 * create two memberships for the same individual, via a price set in the back end.
1210 *
1211 * @param int $contactId Id of contact on which the memberships will be created.
1212 */
1213 protected function createTwoMembershipsViaPriceSetInBackEnd($contactId) {
1214 $form = $this->getForm(NULL);
1215 $form->preProcess();
1216 $this->createLoggedInUser();
1217
1218 // create a price-set of price-field of type checkbox and each price-option corresponds to a membership type
1219 $priceSet = $this->callAPISuccess('price_set', 'create', [
1220 'is_quick_config' => 0,
1221 'extends' => 'CiviMember',
1222 'financial_type_id' => 1,
1223 'title' => 'my Page',
1224 ]);
1225 $priceSetID = $priceSet['id'];
1226 // create respective checkbox price-field
1227 $priceField = $this->callAPISuccess('price_field', 'create', [
1228 'price_set_id' => $priceSetID,
1229 'label' => 'Memberships',
1230 'html_type' => 'Checkbox',
1231 ]);
1232 $priceFieldID = $priceField['id'];
1233 // create two price options, each represent a membership type of amount 20 and 10 respectively
1234 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1235 'price_set_id' => $priceSetID,
1236 'price_field_id' => $priceField['id'],
1237 'label' => 'Long Haired Goat',
1238 'amount' => 20,
1239 'financial_type_id' => 'Donation',
1240 'membership_type_id' => 15,
1241 'membership_num_terms' => 1,
1242 ]);
1243 $pfvIDs = [$priceFieldValue['id'] => 1];
1244 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
1245 'price_set_id' => $priceSetID,
1246 'price_field_id' => $priceField['id'],
1247 'label' => 'Shoe-eating Goat',
1248 'amount' => 10,
1249 'financial_type_id' => 'Donation',
1250 'membership_type_id' => 35,
1251 'membership_num_terms' => 2,
1252 ]);
1253 $pfvIDs[$priceFieldValue['id']] = 1;
1254
1255 // register for both of these memberships via backoffice membership form submission
1256 $params = [
1257 'cid' => $contactId,
1258 'join_date' => date('Y-m-d'),
1259 'start_date' => '',
1260 'end_date' => '',
1261 // This format reflects the 23 being the organisation & the 25 being the type.
1262 "price_$priceFieldID" => $pfvIDs,
1263 "price_set_id" => $priceSetID,
1264 'membership_type_id' => [1 => 0],
1265 'auto_renew' => '0',
1266 'max_related' => '',
1267 'num_terms' => '2',
1268 'source' => '',
1269 'total_amount' => '30.00',
1270 //Member dues, see data.xml
1271 'financial_type_id' => '2',
1272 'soft_credit_type_id' => '',
1273 'soft_credit_contact_id' => '',
1274 'payment_instrument_id' => 4,
1275 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
1276 'receipt_text_signup' => 'Thank you text',
1277 'payment_processor_id' => $this->_paymentProcessorID,
1278 'record_contribution' => TRUE,
1279 'trxn_id' => 777,
1280 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_DAO_Contribution', 'contribution_status_id', 'Pending'),
1281 'billing_first_name' => 'Test',
1282 'billing_middlename' => 'Last',
1283 'billing_street_address-5' => '10 Test St',
1284 'billing_city-5' => 'Test',
1285 'billing_state_province_id-5' => '1003',
1286 'billing_postal_code-5' => '90210',
1287 'billing_country_id-5' => '1228',
1288 ];
1289 $form->testSubmit($params);
1290 }
1291
1292 /**
1293 * Test membership status overrides when contribution is cancelled.
1294 */
1295 public function testContributionFormStatusUpdate() {
1296 $form = new CRM_Contribute_Form_Contribution();
1297
1298 //Create a membership with status = 'New'.
1299 $this->_individualId = $this->createLoggedInUser();
1300 $memParams = [
1301 'contact_id' => $this->_individualId,
1302 'membership_type_id' => $this->membershipTypeAnnualFixedID,
1303 'status_id' => array_search('New', CRM_Member_PseudoConstant::membershipStatus()),
1304 ];
1305 $cancelledStatusId = $this->callAPISuccessGetValue('OptionValue', [
1306 'return' => "value",
1307 'option_group_id' => "contribution_status",
1308 'name' => "Cancelled",
1309 ]);
1310 $params = [
1311 'total_amount' => 50,
1312 'financial_type_id' => 2,
1313 'contact_id' => $this->_individualId,
1314 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
1315 'contribution_status_id' => $cancelledStatusId,
1316 ];
1317 $membershipId = $this->contactMembershipCreate($memParams);
1318
1319 $contriParams = [
1320 'membership_id' => $membershipId,
1321 'total_amount' => 50,
1322 'financial_type_id' => 2,
1323 'contact_id' => $this->_individualId,
1324 ];
1325 $contribution = CRM_Member_BAO_Membership::recordMembershipContribution($contriParams);
1326
1327 //Update Contribution to Cancelled.
1328 $form->_id = $params['id'] = $contribution->id;
1329 $form->_mode = NULL;
1330 $form->_contactID = $this->_individualId;
1331 $form->testSubmit($params, CRM_Core_Action::UPDATE);
1332 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
1333
1334 //Assert membership status overrides when the contribution cancelled.
1335 $this->assertEquals($membership['is_override'], TRUE);
1336 $this->assertEquals($membership['status_id'], $this->callAPISuccessGetValue('MembershipStatus', [
1337 'return' => "id",
1338 'name' => "Cancelled",
1339 ]));
1340 }
1341
1342 /**
1343 * CRM-21656: Test the submit function of the membership form if Sales Tax is enabled.
1344 * This test simulates what happens when one hits Edit on a Contribution that has both LineItems and Sales Tax components
1345 * Without making any Edits -> check that the LineItem data remain the same
1346 * In addition (a data-integrity check) -> check that the LineItem data add up to the data at the Contribution level
1347 */
1348 public function testLineItemAmountOnSalesTax() {
1349 $this->enableTaxAndInvoicing();
1350 $this->relationForFinancialTypeWithFinancialAccount(2);
1351 $form = $this->getForm();
1352 $form->preProcess();
1353 $this->mut = new CiviMailUtils($this, TRUE);
1354 $this->createLoggedInUser();
1355 $priceSet = $this->callAPISuccess('PriceSet', 'Get', ["extends" => "CiviMember"]);
1356 $form->set('priceSetId', $priceSet['id']);
1357 // we are simulating the creation of a Price Set in Administer -> CiviContribute -> Manage Price Sets so set is_quick_config = 0
1358 $this->callAPISuccess('PriceSet', 'Create', ["id" => $priceSet['id'], 'is_quick_config' => 0]);
1359 // clean the price options static variable to repopulate the options, in order to fetch tax information
1360 \Civi::$statics['CRM_Price_BAO_PriceField']['priceOptions'] = NULL;
1361 CRM_Price_BAO_PriceSet::buildPriceSet($form);
1362 // rebuild the price set form variable to include the tax information against each price options
1363 $form->_priceSet = current(CRM_Price_BAO_PriceSet::getSetDetail($priceSet['id']));
1364 $params = [
1365 'cid' => $this->_individualId,
1366 'join_date' => date('Y-m-d'),
1367 'start_date' => '',
1368 'end_date' => '',
1369 // This format reflects the 23 being the organisation & the 25 being the type.
1370 'membership_type_id' => [23, $this->membershipTypeAnnualFixedID],
1371 'record_contribution' => 1,
1372 'total_amount' => 55,
1373 'receive_date' => date('Y-m-d', time()) . ' 20:36:00',
1374 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
1375 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
1376 //Member dues, see data.xml
1377 'financial_type_id' => 2,
1378 'payment_processor_id' => $this->_paymentProcessorID,
1379 ];
1380 $form->_contactID = $this->_individualId;
1381 $form->testSubmit($params);
1382
1383 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
1384 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['entity_id' => $membership['id'], 'entity_table' => 'civicrm_membership']);
1385 $this->assertEquals(1, $lineItem['qty']);
1386 $this->assertEquals(50.00, $lineItem['unit_price']);
1387 $this->assertEquals(50.00, $lineItem['line_total']);
1388 $this->assertEquals(5.00, $lineItem['tax_amount']);
1389
1390 // Simply save the 'Edit Contribution' form
1391 $form = new CRM_Contribute_Form_Contribution();
1392 $form->_context = 'membership';
1393 $form->_values = $this->callAPISuccessGetSingle('Contribution', ['id' => $lineItem['contribution_id'], 'return' => ['total_amount', 'net_amount', 'fee_amount', 'tax_amount']]);
1394 $form->testSubmit([
1395 'contact_id' => $this->_individualId,
1396 'id' => $lineItem['contribution_id'],
1397 'financial_type_id' => 2,
1398 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
1399 ],
1400 CRM_Core_Action::UPDATE);
1401
1402 // ensure that the LineItem data remain the same
1403 $lineItem = $this->callAPISuccessGetSingle('LineItem', ['entity_id' => $membership['id'], 'entity_table' => 'civicrm_membership']);
1404 $this->assertEquals(1, $lineItem['qty']);
1405 $this->assertEquals(50.00, $lineItem['unit_price']);
1406 $this->assertEquals(50.00, $lineItem['line_total']);
1407 $this->assertEquals(5.00, $lineItem['tax_amount']);
1408
1409 // ensure that the LineItem data add up to the data at the Contribution level
1410 $contribution = $this->callAPISuccessGetSingle('Contribution',
1411 [
1412 'contribution_id' => 1,
1413 'return' => ['tax_amount', 'total_amount'],
1414 ]
1415 );
1416 $this->assertEquals($contribution['total_amount'], $lineItem['line_total'] + $lineItem['tax_amount']);
1417 $this->assertEquals($contribution['tax_amount'], $lineItem['tax_amount']);
1418
1419 $financialItems = $this->callAPISuccess('FinancialItem', 'get', []);
1420 $financialItems_sum = 0;
1421 foreach ($financialItems['values'] as $financialItem) {
1422 $financialItems_sum += $financialItem['amount'];
1423 }
1424 $this->assertEquals($contribution['total_amount'], $financialItems_sum);
1425
1426 // reset the price options static variable so not leave any dummy data, that might hamper other unit tests
1427 \Civi::$statics['CRM_Price_BAO_PriceField']['priceOptions'] = NULL;
1428 $this->disableTaxAndInvoicing();
1429 }
1430
1431 /**
1432 * Test that membership end_date is correct for multiple terms for pending contribution
1433 *
1434 * @throws CiviCRM_API3_Exception
1435 * @throws \CRM_Core_Exception
1436 */
1437 public function testCreatePendingWithMultipleTerms() {
1438 CRM_Core_Session::singleton()->getStatus(TRUE);
1439 $form = $this->getForm();
1440 $form->preProcess();
1441 $this->mut = new CiviMailUtils($this, TRUE);
1442 $this->createLoggedInUser();
1443 $membershipTypeAnnualRolling = $this->callAPISuccess('membership_type', 'create', [
1444 'domain_id' => 1,
1445 'name' => "AnnualRollingNew",
1446 'member_of_contact_id' => 23,
1447 'duration_unit' => "year",
1448 'minimum_fee' => 50,
1449 'duration_interval' => 1,
1450 'period_type' => "rolling",
1451 'relationship_type_id' => 20,
1452 'relationship_direction' => 'b_a',
1453 'financial_type_id' => 2,
1454 ]);
1455 $params = [
1456 'cid' => $this->_individualId,
1457 'join_date' => date('Y-m-d'),
1458 'start_date' => '',
1459 'end_date' => '',
1460 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Pending'),
1461 'membership_type_id' => [23, $membershipTypeAnnualRolling['id']],
1462 'max_related' => '',
1463 'num_terms' => '3',
1464 'record_contribution' => 1,
1465 'source' => '',
1466 'total_amount' => $this->formatMoneyInput(150.00),
1467 'financial_type_id' => '2',
1468 'soft_credit_type_id' => '11',
1469 'soft_credit_contact_id' => '',
1470 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
1471 'receipt_text' => '',
1472 ];
1473 $form->_contactID = $this->_individualId;
1474 $form->testSubmit($params);
1475 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
1476 $contribution = $this->callAPISuccess('Contribution', 'get', [
1477 'contact_id' => $this->_individualId,
1478 ]);
1479 $endDate = (new DateTime(date('Y-m-d')))->modify("+3 years")->modify("-1 day");
1480 $endDate = $endDate->format("Y-m-d");
1481
1482 $this->assertEquals($endDate, $membership['end_date'], 'Membership end date should be ' . $endDate);
1483 $this->assertEquals(1, count($contribution['values']), 'Pending contribution should be created.');
1484 $contribution = $contribution['values'][$contribution['id']];
1485 $additionalPaymentForm = new CRM_Contribute_Form_AdditionalPayment();
1486 $additionalPaymentForm->testSubmit([
1487 'total_amount' => 150.00,
1488 'trxn_date' => date("Y-m-d H:i:s"),
1489 'payment_instrument_id' => array_search('Check', $this->paymentInstruments),
1490 'check_number' => 'check-12345',
1491 'trxn_id' => '',
1492 'currency' => 'USD',
1493 'fee_amount' => '',
1494 'financial_type_id' => 1,
1495 'net_amount' => '',
1496 'payment_processor_id' => 0,
1497 'contact_id' => $this->_individualId,
1498 'contribution_id' => $contribution['id'],
1499 ]);
1500 $membership = $this->callAPISuccessGetSingle('Membership', ['contact_id' => $this->_individualId]);
1501 $contribution = $this->callAPISuccess('Contribution', 'get', [
1502 'contact_id' => $this->_individualId,
1503 'contribution_status_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed'),
1504 ]);
1505 $this->assertEquals($endDate, $membership['end_date'], 'Membership end date should be same (' . $endDate . ') after payment');
1506 $this->assertEquals(1, count($contribution['values']), 'Completed contribution should be fetched.');
1507 }
1508
1509 }