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