3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * File for the MembershipTest class
33 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
37 * Include class definitions
39 require_once 'CiviTest/CiviUnitTestCase.php';
41 require_once 'HTML/QuickForm/Page.php';
44 * Test CRM_Member_Form_Membership functions.
48 class CRM_Member_Form_MembershipTest
extends CiviUnitTestCase
{
51 * Assume empty database with just civicrm_data.
53 protected $_individualId;
54 protected $_contribution;
55 protected $_financialTypeId = 1;
56 protected $_apiversion;
57 protected $_entity = 'Membership';
59 protected $_ids = array();
60 protected $_paymentProcessorID;
63 * Membership type ID for annual fixed membership.
67 protected $membershipTypeAnnualFixedID;
70 * Parameters to create payment processor.
74 protected $_processorParams = array();
77 * ID of created membership.
81 protected $_membershipID;
84 * Payment instrument mapping.
88 protected $paymentInstruments = array();
91 * Test setup for every test.
93 * Connect to the database, truncate the tables that will be used
94 * and redirect stdin to a temporary file.
96 public function setUp() {
97 $this->_apiversion
= 3;
100 $this->_individualId
= $this->individualCreate();
101 $processor = $this->processorCreate();
102 $this->_paymentProcessorID
= $processor->id
;
104 $op = new PHPUnit_Extensions_Database_Operation_Insert();
105 $op->execute($this->_dbconn
,
106 $this->createFlatXMLDataSet(
107 dirname(__FILE__
) . '/dataset/data.xml'
110 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array(
112 'name' => "AnnualFixed",
113 'member_of_contact_id' => 23,
114 'duration_unit' => "year",
115 'duration_interval' => 1,
116 'period_type' => "fixed",
117 'fixed_period_start_day' => "101",
118 'fixed_period_rollover_day' => "1231",
119 'relationship_type_id' => 20,
120 'financial_type_id' => 2,
122 $this->membershipTypeAnnualFixedID
= $membershipTypeAnnualFixed['id'];
124 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
125 $this->paymentInstruments
= $instruments['values'];
129 * Clean up after each test.
131 public function tearDown() {
132 $this->quickCleanUpFinancialEntities();
135 'civicrm_relationship',
136 'civicrm_membership_type',
137 'civicrm_membership',
141 $this->callAPISuccess('contact', 'delete', array('id' => 17, 'skip_undelete' => TRUE));
142 $this->callAPISuccess('contact', 'delete', array('id' => 23, 'skip_undelete' => TRUE));
143 $this->callAPISuccess('relationship_type', 'delete', array('id' => 20));
147 * Test CRM_Member_Form_Membership::buildQuickForm()
149 //function testCRMMemberFormMembershipBuildQuickForm()
151 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
155 * Test CRM_Member_Form_Membership::formRule() with a parameter
156 * that has an empty contact_select_id value
158 public function testFormRuleEmptyContact() {
160 'contact_select_id' => 0,
161 'membership_type_id' => array(1 => NULL),
164 $obj = new CRM_Member_Form_Membership();
165 $rc = $obj->formRule($params, $files, $obj);
166 $this->assertType('array', $rc);
167 $this->assertTrue(array_key_exists('membership_type_id', $rc));
169 $params['membership_type_id'] = array(1 => 3);
170 $rc = $obj->formRule($params, $files, $obj);
171 $this->assertType('array', $rc);
172 $this->assertTrue(array_key_exists('join_date', $rc));
176 * Test that form rule fails if start date is before join date.
178 * Test CRM_Member_Form_Membership::formRule() with a parameter
179 * that has an start date before the join date and a rolling
182 public function testFormRuleRollingEarlyStart() {
184 $ymdNow = date('m/d/Y', $unixNow);
185 $unixYesterday = $unixNow - (24 * 60 * 60);
186 $ymdYesterday = date('m/d/Y', $unixYesterday);
188 'join_date' => $ymdNow,
189 'start_date' => $ymdYesterday,
191 'membership_type_id' => array('23', '15'),
194 $obj = new CRM_Member_Form_Membership();
195 $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'),
196 $params, $files, $obj
198 $this->assertType('array', $rc);
199 $this->assertTrue(array_key_exists('start_date', $rc));
203 * Test CRM_Member_Form_Membership::formRule() with a parameter
204 * that has an end date before the start date and a rolling
207 public function testFormRuleRollingEarlyEnd() {
209 $ymdNow = date('m/d/Y', $unixNow);
210 $unixYesterday = $unixNow - (24 * 60 * 60);
211 $ymdYesterday = date('m/d/Y', $unixYesterday);
213 'join_date' => $ymdNow,
214 'start_date' => $ymdNow,
215 'end_date' => $ymdYesterday,
216 'membership_type_id' => array('23', '15'),
219 $obj = new CRM_Member_Form_Membership();
220 $rc = $obj->formRule($params, $files, $obj);
221 $this->assertType('array', $rc);
222 $this->assertTrue(array_key_exists('end_date', $rc));
226 * Test CRM_Member_Form_Membership::formRule() with end date but no start date and a rolling membership type.
228 public function testFormRuleRollingEndNoStart() {
230 $ymdNow = date('m/d/Y', $unixNow);
231 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
232 $ymdYearFromNow = date('m/d/Y', $unixYearFromNow);
234 'join_date' => $ymdNow,
236 'end_date' => $ymdYearFromNow,
237 'membership_type_id' => array('23', '15'),
240 $obj = new CRM_Member_Form_Membership();
241 $rc = $obj->formRule($params, $files, $obj);
242 $this->assertType('array', $rc);
243 $this->assertTrue(array_key_exists('start_date', $rc));
247 * Test CRM_Member_Form_Membership::formRule() with a parameter
248 * that has an end date and a lifetime membership type
250 public function testFormRuleRollingLifetimeEnd() {
252 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
254 'join_date' => date('m/d/Y', $unixNow),
255 'start_date' => date('m/d/Y', $unixNow),
256 'end_date' => date('m/d/Y',
259 'membership_type_id' => array('23', '25'),
262 $obj = new CRM_Member_Form_Membership();
263 $rc = $obj->formRule($params, $files, $obj);
264 $this->assertType('array', $rc);
265 $this->assertTrue(array_key_exists('status_id', $rc));
269 * Test CRM_Member_Form_Membership::formRule() with a parameter
270 * that has an override and no status
272 public function testFormRuleOverrideNoStatus() {
275 'join_date' => date('m/d/Y', $unixNow),
276 'membership_type_id' => array('23', '25'),
277 'is_override' => TRUE,
280 $obj = new CRM_Member_Form_Membership();
281 $rc = $obj->formRule($params, $files, $obj);
282 $this->assertType('array', $rc);
283 $this->assertTrue(array_key_exists('status_id', $rc));
287 * Test CRM_Member_Form_Membership::formRule() with a join date
288 * of one month from now and a rolling membership type
290 public function testFormRuleRollingJoin1MonthFromNow() {
292 $unix1MFmNow = $unixNow +
(31 * 24 * 60 * 60);
294 'join_date' => date('m/d/Y', $unix1MFmNow),
297 'membership_type_id' => array('23', '15'),
300 $obj = new CRM_Member_Form_Membership();
301 $rc = $obj->formRule($params, $files, $obj);
303 // Should have found no valid membership status.
304 $this->assertType('array', $rc);
305 $this->assertTrue(array_key_exists('_qf_default', $rc));
309 * Test CRM_Member_Form_Membership::formRule() with a join date of today and a rolling membership type.
311 public function testFormRuleRollingJoinToday() {
314 'join_date' => date('m/d/Y', $unixNow),
317 'membership_type_id' => array('23', '15'),
320 $obj = new CRM_Member_Form_Membership();
321 $rc = $obj->formRule($params, $files, $obj);
323 // Should have found New membership status
324 $this->assertTrue($rc);
328 * Test CRM_Member_Form_Membership::formRule() with a join date
329 * of one month ago and a rolling membership type
331 public function testFormRuleRollingJoin1MonthAgo() {
333 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
335 'join_date' => date('m/d/Y', $unix1MAgo),
338 'membership_type_id' => array('23', '15'),
341 $obj = new CRM_Member_Form_Membership();
342 $rc = $obj->formRule($params, $files, $obj);
344 // Should have found New membership status.
345 $this->assertTrue($rc);
349 * Test CRM_Member_Form_Membership::formRule() with a join date of six months ago and a rolling membership type.
351 public function testFormRuleRollingJoin6MonthsAgo() {
353 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
355 'join_date' => date('m/d/Y', $unix6MAgo),
358 'membership_type_id' => array('23', '15'),
361 $obj = new CRM_Member_Form_Membership();
362 $rc = $obj->formRule($params, $files, $obj);
364 // Should have found Current membership status.
365 $this->assertTrue($rc);
369 * Test CRM_Member_Form_Membership::formRule() with a join date
370 * of one year+ ago and a rolling membership type
372 public function testFormRuleRollingJoin1YearAgo() {
374 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
376 'join_date' => date('m/d/Y', $unix1YAgo),
379 'membership_type_id' => array('23', '15'),
382 $obj = new CRM_Member_Form_Membership();
383 $rc = $obj->formRule($params, $files, $obj);
385 // Should have found Grace membership status
386 $this->assertTrue($rc);
390 * Test CRM_Member_Form_Membership::formRule() with a join date
391 * of two years ago and a rolling membership type
393 public function testFormRuleRollingJoin2YearsAgo() {
395 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
397 'join_date' => date('m/d/Y', $unix2YAgo),
400 'membership_type_id' => array('23', '15'),
403 $obj = new CRM_Member_Form_Membership();
404 $rc = $obj->formRule($params, $files, $obj);
406 // Should have found Expired membership status
407 $this->assertTrue($rc);
411 * Test CRM_Member_Form_Membership::formRule() with a join date
412 * of six months ago and a fixed membership type
414 public function testFormRuleFixedJoin6MonthsAgo() {
416 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
418 'join_date' => date('m/d/Y', $unix6MAgo),
421 'membership_type_id' => array('23', '7'),
424 $obj = new CRM_Member_Form_Membership();
425 $rc = $obj->formRule($params, $files, $obj);
427 // Should have found Current membership status
428 $this->assertTrue($rc);
432 * Test the submit function of the membership form.
434 public function testSubmit() {
435 $form = $this->getForm();
436 $form->_mode
= 'test';
437 $this->createLoggedInUser();
439 'cid' => $this->_individualId
,
440 'join_date' => date('m/d/Y', time()),
443 // This format reflects the 23 being the organisation & the 25 being the type.
444 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
449 'total_amount' => '50.00',
450 'financial_type_id' => '2', //Member dues, see data.xml
451 'soft_credit_type_id' => '',
452 'soft_credit_contact_id' => '',
453 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
454 'receipt_text_signup' => 'Thank you text',
455 'payment_processor_id' => $this->_paymentProcessorID
,
456 'credit_card_number' => '4111111111111111',
458 'credit_card_exp_date' => array(
460 'Y' => '2024', // TODO: Future proof
462 'credit_card_type' => 'Visa',
463 'billing_first_name' => 'Test',
464 'billing_middlename' => 'Last',
465 'billing_street_address-5' => '10 Test St',
466 'billing_city-5' => 'Test',
467 'billing_state_province_id-5' => '1003',
468 'billing_postal_code-5' => '90210',
469 'billing_country_id-5' => '1228',
471 $form->submit($params);
472 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
473 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId
), 0);
474 $contribution = $this->callAPISuccess('Contribution', 'get', array(
475 'contact_id' => $this->_individualId
,
479 $this->callAPISuccessGetCount('LineItem', array(
480 'entity_id' => $membership['id'],
481 'entity_table' => 'civicrm_membership',
482 'contribution_id' => $contribution['id'],
487 * Test the submit function of the membership form.
489 public function testSubmitRecur() {
490 $form = $this->getForm();
492 $this->callAPISuccess('MembershipType', 'create', array(
493 'id' => $this->membershipTypeAnnualFixedID
,
494 'duration_unit' => 'month',
495 'duration_interval' => 1,
496 'auto_renew' => TRUE,
499 $this->createLoggedInUser();
500 $params = $this->getBaseSubmitParams();
501 $form->_mode
= 'test';
503 $form->submit($params);
504 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
505 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId
), 1);
507 $contribution = $this->callAPISuccess('Contribution', 'get', array(
508 'contact_id' => $this->_individualId
,
513 $this->callAPISuccessGetCount('LineItem', array(
514 'entity_id' => $membership['id'],
515 'entity_table' => 'civicrm_membership',
516 'contribution_id' => $contribution['id'],
521 * Test the submit function of the membership form.
523 public function testSubmitRecurCompleteInstant() {
524 $form = $this->getForm();
526 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessorID
);
527 $processor->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'kettles boil water'));
528 $this->callAPISuccess('MembershipType', 'create', array(
529 'id' => $this->membershipTypeAnnualFixedID
,
530 'duration_unit' => 'month',
531 'duration_interval' => 1,
532 'auto_renew' => TRUE,
535 $this->createLoggedInUser();
536 $params = $this->getBaseSubmitParams();
537 $form->_mode
= 'test';
539 $form->submit($params);
540 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
541 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId
), 1);
543 $contribution = $this->callAPISuccess('Contribution', 'get', array(
544 'contact_id' => $this->_individualId
,
548 $this->callAPISuccessGetCount('LineItem', array(
549 'entity_id' => $membership['id'],
550 'entity_table' => 'civicrm_membership',
551 'contribution_id' => $contribution['id'],
557 * Get a membership form object.
559 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
561 * @return \CRM_Member_Form_Membership
563 protected function getForm() {
564 $form = new CRM_Member_Form_Membership();
565 $_SERVER['REQUEST_METHOD'] = 'GET';
566 $form->controller
= new CRM_Core_Controller();
574 protected function getBaseSubmitParams() {
576 'cid' => $this->_individualId
,
578 'join_date' => date('m/d/Y', time()),
582 // This format reflects the 23 being the organisation & the 25 being the type.
583 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
589 'total_amount' => '77.00',
590 'financial_type_id' => '2', //Member dues, see data.xml
591 'soft_credit_type_id' => 11,
592 'soft_credit_contact_id' => '',
593 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
594 'receipt_text' => 'Thank you text',
595 'payment_processor_id' => $this->_paymentProcessorID
,
596 'credit_card_number' => '4111111111111111',
598 'credit_card_exp_date' => array(
600 'Y' => '2019', // TODO: Future proof
602 'credit_card_type' => 'Visa',
603 'billing_first_name' => 'Test',
604 'billing_middlename' => 'Last',
605 'billing_street_address-5' => '10 Test St',
606 'billing_city-5' => 'Test',
607 'billing_state_province_id-5' => '1003',
608 'billing_postal_code-5' => '90210',
609 'billing_country_id-5' => '1228',