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
38 * Include class definitions
40 require_once 'CiviTest/CiviUnitTestCase.php';
42 require_once 'HTML/QuickForm/Page.php';
45 * Test CRM_Member_Form_Membership functions.
49 class CRM_Member_Form_MembershipTest
extends CiviUnitTestCase
{
52 * Assume empty database with just civicrm_data.
54 protected $_individualId;
55 protected $_contribution;
56 protected $_financialTypeId = 1;
57 protected $_apiversion;
58 protected $_entity = 'Membership';
60 protected $_ids = array();
61 protected $_paymentProcessorID;
64 * Parameters to create payment processor.
68 protected $_processorParams = array();
71 * ID of created membership.
75 protected $_membershipID;
78 * Payment instrument mapping.
82 protected $paymentInstruments = array();
85 * Test setup for every test.
87 * Connect to the database, truncate the tables that will be used
88 * and redirect stdin to a temporary file
90 public function setUp() {
91 $this->_apiversion
= 3;
92 // Connect to the database
95 $this->_individualId
= $this->individualCreate();
96 $this->_paymentProcessorID
= $this->processorCreate();
98 $op = new PHPUnit_Extensions_Database_Operation_Insert();
99 $op->execute($this->_dbconn
,
100 $this->createFlatXMLDataSet(
101 dirname(__FILE__
) . '/dataset/data.xml'
105 $this->_apiversion
= 3;
106 $this->_individualId
= $this->individualCreate();
107 $this->_paymentProcessorID
= $this->processorCreate();
108 $this->_params
= array(
109 'contact_id' => $this->_individualId
,
110 'receive_date' => '20120511',
111 'total_amount' => 100.00,
112 'financial_type_id' => $this->_financialTypeId
,
113 'non_deductible_amount' => 10.00,
114 'fee_amount' => 5.00,
115 'net_amount' => 95.00,
117 'contribution_status_id' => 1,
119 $this->_processorParams
= array(
122 'payment_processor_type_id' => 10,
123 'financial_account_id' => 12,
126 'url_site' => 'http://dummy.com',
127 'url_recur' => 'http://dummy.com',
130 $this->_pageParams
= array(
131 'title' => 'Test Contribution Page',
132 'financial_type_id' => 1,
134 'financial_account_id' => 1,
135 'payment_processor' => $paymentProcessor->id
,
137 'is_allow_other_amount' => 1,
139 'max_amount' => 1000,
141 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
142 $this->paymentInstruments
= $instruments['values'];
146 * Clean up after each test.
148 public function tearDown() {
149 $this->quickCleanUpFinancialEntities();
153 * Clean up after each test.
155 public function tearDown() {
156 $this->quickCleanUpFinancialEntities();
159 'civicrm_relationship',
160 'civicrm_membership_type',
161 'civicrm_membership',
164 $this->callAPISuccess('contact', 'delete', array('id' => 17, 'skip_undelete' => TRUE));
165 $this->callAPISuccess('contact', 'delete', array('id' => 23, 'skip_undelete' => TRUE));
166 $this->callAPISuccess('relationship_type', 'delete', array('id' => 20));
170 * Test CRM_Member_Form_Membership::buildQuickForm()
172 //function testCRMMemberFormMembershipBuildQuickForm()
174 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
178 * Test CRM_Member_Form_Membership::formRule() with a parameter
179 * that has an empty contact_select_id value
181 public function testFormRuleEmptyContact() {
183 'contact_select_id' => 0,
184 'membership_type_id' => array(),
187 $obj = new CRM_Member_Form_Membership();
188 $rc = $obj->formRule($params, $files, $obj);
189 $this->assertType('array', $rc);
190 $this->assertTrue(array_key_exists('membership_type_id', $rc));
192 $params['membership_type_id'] = array(1 => 3);
193 $rc = $obj->formRule($params, $files, $obj);
194 $this->assertType('array', $rc);
195 $this->assertTrue(array_key_exists('join_date', $rc));
199 * Test that form rule fails if start date is before join date.
201 * Test CRM_Member_Form_Membership::formRule() with a parameter
202 * that has an start date before the join date and a rolling
205 public function testFormRuleRollingEarlyStart() {
207 $ymdNow = date('m/d/Y', $unixNow);
208 $unixYesterday = $unixNow - (24 * 60 * 60);
209 $ymdYesterday = date('m/d/Y', $unixYesterday);
211 'join_date' => $ymdNow,
212 'start_date' => $ymdYesterday,
214 'membership_type_id' => array('23', '15'),
217 $obj = new CRM_Member_Form_Membership();
218 $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'),
219 $params, $files, $obj
221 $this->assertType('array', $rc);
222 $this->assertTrue(array_key_exists('start_date', $rc));
226 * Test CRM_Member_Form_Membership::formRule() with a parameter
227 * that has an end date before the start date and a rolling
230 public function testFormRuleRollingEarlyEnd() {
232 $ymdNow = date('m/d/Y', $unixNow);
233 $unixYesterday = $unixNow - (24 * 60 * 60);
234 $ymdYesterday = date('m/d/Y', $unixYesterday);
236 'join_date' => $ymdNow,
237 'start_date' => $ymdNow,
238 'end_date' => $ymdYesterday,
239 'membership_type_id' => array('23', '15'),
242 $obj = new CRM_Member_Form_Membership();
243 $rc = $obj->formRule($params, $files, $obj);
244 $this->assertType('array', $rc,
245 'In line ' . __LINE__
247 $this->assertTrue(array_key_exists('end_date', $rc),
248 'In line ' . __LINE__
253 * Test CRM_Member_Form_Membership::formRule() with a parameter
254 * that has an end date but no start date and a rolling
257 public function testFormRuleRollingEndNoStart() {
259 $ymdNow = date('m/d/Y', $unixNow);
260 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
261 $ymdYearFromNow = date('m/d/Y', $unixYearFromNow);
263 'join_date' => $ymdNow,
265 'end_date' => $ymdYearFromNow,
266 'membership_type_id' => array('23', '15'),
269 $obj = new CRM_Member_Form_Membership();
270 $rc = $obj->formRule($params, $files, $obj);
271 $this->assertType('array', $rc,
272 'In line ' . __LINE__
274 $this->assertTrue(array_key_exists('start_date', $rc),
275 'In line ' . __LINE__
280 * Test CRM_Member_Form_Membership::formRule() with a parameter
281 * that has an end date and a lifetime membership type
283 public function testFormRuleRollingLifetimeEnd() {
285 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
287 'join_date' => date('m/d/Y', $unixNow),
288 'start_date' => date('m/d/Y', $unixNow),
289 'end_date' => date('m/d/Y',
292 'membership_type_id' => array('23', '25'),
295 $obj = new CRM_Member_Form_Membership();
296 $rc = $obj->formRule($params, $files, $obj);
297 $this->assertType('array', $rc);
298 $this->assertTrue(array_key_exists('status_id', $rc));
302 * Test CRM_Member_Form_Membership::formRule() with a parameter
303 * that has an override and no status
305 public function testFormRuleOverrideNoStatus() {
307 $unixYearFromNow = $unixNow +
(365 * 24 * 60 * 60);
309 'join_date' => date('m/d/Y', $unixNow),
310 'membership_type_id' => array('23', '25'),
311 'is_override' => TRUE,
314 $obj = new CRM_Member_Form_Membership();
315 $rc = $obj->formRule($params, $files, $obj);
316 $this->assertType('array', $rc,
317 'In line ' . __LINE__
319 $this->assertTrue(array_key_exists('status_id', $rc),
320 'In line ' . __LINE__
325 * Test CRM_Member_Form_Membership::formRule() with a join date
326 * of one month from now and a rolling membership type
328 public function testFormRuleRollingJoin1MonthFromNow() {
330 $unix1MFmNow = $unixNow +
(31 * 24 * 60 * 60);
332 'join_date' => date('m/d/Y', $unix1MFmNow),
335 'membership_type_id' => array('23', '15'),
338 $obj = new CRM_Member_Form_Membership();
339 $rc = $obj->formRule($params, $files, $obj);
341 // Should have found no valid membership status
342 $this->assertType('array', $rc,
343 'In line ' . __LINE__
345 $this->assertTrue(array_key_exists('_qf_default', $rc),
346 'In line ' . __LINE__
351 * Test CRM_Member_Form_Membership::formRule() with a join date
352 * of today and a rolling membership type
354 public function testFormRuleRollingJoinToday() {
357 'join_date' => date('m/d/Y', $unixNow),
360 'membership_type_id' => array('23', '15'),
363 $obj = new CRM_Member_Form_Membership();
364 $rc = $obj->formRule($params, $files, $obj);
366 // Should have found New membership status
367 $this->assertTrue($rc, 'In line ' . __LINE__
);
371 * Test CRM_Member_Form_Membership::formRule() with a join date
372 * of one month ago and a rolling membership type
374 public function testFormRuleRollingJoin1MonthAgo() {
376 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
378 'join_date' => date('m/d/Y', $unix1MAgo),
381 'membership_type_id' => array('23', '15'),
384 $obj = new CRM_Member_Form_Membership();
385 $rc = $obj->formRule($params, $files, $obj);
387 // Should have found New membership status
388 $this->assertTrue($rc, 'In line ' . __LINE__
);
392 * Test CRM_Member_Form_Membership::formRule() with a join date
393 * of six months ago and a rolling membership type
395 public function testFormRuleRollingJoin6MonthsAgo() {
397 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
399 'join_date' => date('m/d/Y', $unix6MAgo),
402 'membership_type_id' => array('23', '15'),
405 $obj = new CRM_Member_Form_Membership();
406 $rc = $obj->formRule($params, $files, $obj);
408 // Should have found Current membership status
409 $this->assertTrue($rc, 'In line ' . __LINE__
);
413 * Test CRM_Member_Form_Membership::formRule() with a join date
414 * of one year+ ago and a rolling membership type
416 public function testFormRuleRollingJoin1YearAgo() {
418 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
420 'join_date' => date('m/d/Y', $unix1YAgo),
423 'membership_type_id' => array('23', '15'),
426 $obj = new CRM_Member_Form_Membership();
427 $rc = $obj->formRule($params, $files, $obj);
429 // Should have found Grace membership status
430 $this->assertTrue($rc, 'In line ' . __LINE__
);
434 * Test CRM_Member_Form_Membership::formRule() with a join date
435 * of two years ago and a rolling membership type
437 public function testFormRuleRollingJoin2YearsAgo() {
439 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
441 'join_date' => date('m/d/Y', $unix2YAgo),
444 'membership_type_id' => array('23', '15'),
447 $obj = new CRM_Member_Form_Membership();
448 $rc = $obj->formRule($params, $files, $obj);
450 // Should have found Expired membership status
451 $this->assertTrue($rc, 'In line ' . __LINE__
);
455 * Test CRM_Member_Form_Membership::formRule() with a join date
456 * of six months ago and a fixed membership type
458 public function testFormRuleFixedJoin6MonthsAgo() {
460 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
462 'join_date' => date('m/d/Y', $unix6MAgo),
465 'membership_type_id' => array('23', '7'),
468 $obj = new CRM_Member_Form_Membership();
469 $rc = $obj->formRule($params, $files, $obj);
471 // Should have found Current membership status
472 $this->assertTrue($rc);
476 * Test the submit function of the membership form.
478 public function testSubmit() {
479 $form = new CRM_Member_Form_Membership();
481 'cid' => $this->_individualId
,
482 'join_date' => date('m/d/Y', time()),
485 'membership_type_id' => array('13'),
490 'total_amount' => '50.00',
491 'financial_type_id' => '5', //Member dues, see data.xml
492 'soft_credit_type_id' => '',
493 'soft_credit_contact_id' => '',
494 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
495 'receipt_text_signup' => 'Thank you text',
496 'payment_processor_id' => $this->_paymentProcessorID
,
497 'credit_card_number' => '4111111111111111',
499 'credit_card_exp_date' => array(
501 'Y' => '2019', // TODO: Future proof
503 'credit_card_type' => 'Visa',
504 'billing_first_name' => 'Test',
505 'billing_middlename' => 'Last',
506 'billing_street_address-5' => '10 Test St',
507 'billing_city-5' => 'Test',
508 'billing_state_province_id-5' => '1003',
509 'billing_postal_code-5' => '90210',
510 'billing_country_id-5' => '1228',
512 $form->submit($params);
513 // TODO: This will still fail right now.
514 //$this->callAPISuccessGetCount('Membership', array('contact_id' => $this->_individualId), 1);
518 // class CRM_Member_Form_MembershipTest