fb57392d60251e3076686a4a199764ec5fefd926
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / MembershipTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 /**
38 * Include class definitions
39 */
40 require_once 'CiviTest/CiviUnitTestCase.php';
41
42 require_once 'HTML/QuickForm/Page.php';
43
44 /**
45 * Test CRM_Member_Form_Membership functions.
46 *
47 * @package CiviCRM
48 */
49 class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
50
51 /**
52 * Assume empty database with just civicrm_data.
53 */
54 protected $_individualId;
55 protected $_contribution;
56 protected $_financialTypeId = 1;
57 protected $_apiversion;
58 protected $_entity = 'Membership';
59 protected $_params;
60 protected $_ids = array();
61 protected $_paymentProcessorID;
62
63 /**
64 * Parameters to create payment processor.
65 *
66 * @var array
67 */
68 protected $_processorParams = array();
69
70 /**
71 * ID of created membership.
72 *
73 * @var int
74 */
75 protected $_membershipID;
76
77 /**
78 * Payment instrument mapping.
79 *
80 * @var array
81 */
82 protected $paymentInstruments = array();
83
84 /**
85 * Test setup for every test.
86 *
87 * Connect to the database, truncate the tables that will be used
88 * and redirect stdin to a temporary file
89 */
90 public function setUp() {
91 $this->_apiversion = 3;
92 // Connect to the database
93 parent::setUp();
94
95 $this->_individualId = $this->individualCreate();
96 $this->_paymentProcessorID = $this->processorCreate();
97 // Insert test data
98 $op = new PHPUnit_Extensions_Database_Operation_Insert();
99 $op->execute($this->_dbconn,
100 $this->createFlatXMLDataSet(
101 dirname(__FILE__) . '/dataset/data.xml'
102 )
103 );
104
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,
116 'source' => 'SSF',
117 'contribution_status_id' => 1,
118 );
119 $this->_processorParams = array(
120 'domain_id' => 1,
121 'name' => 'Dummy',
122 'payment_processor_type_id' => 10,
123 'financial_account_id' => 12,
124 'is_active' => 1,
125 'user_name' => '',
126 'url_site' => 'http://dummy.com',
127 'url_recur' => 'http://dummy.com',
128 'billing_mode' => 1,
129 );
130 $this->_pageParams = array(
131 'title' => 'Test Contribution Page',
132 'financial_type_id' => 1,
133 'currency' => 'USD',
134 'financial_account_id' => 1,
135 'payment_processor' => $paymentProcessor->id,
136 'is_active' => 1,
137 'is_allow_other_amount' => 1,
138 'min_amount' => 10,
139 'max_amount' => 1000,
140 );
141 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
142 $this->paymentInstruments = $instruments['values'];
143 }
144
145 /**
146 * Clean up after each test.
147 */
148 public function tearDown() {
149 $this->quickCleanUpFinancialEntities();
150 }
151
152 /**
153 * Clean up after each test.
154 */
155 public function tearDown() {
156 $this->quickCleanUpFinancialEntities();
157 $this->quickCleanup(
158 array(
159 'civicrm_relationship',
160 'civicrm_membership_type',
161 'civicrm_membership',
162 )
163 );
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));
167 }
168
169 /**
170 * Test CRM_Member_Form_Membership::buildQuickForm()
171 */
172 //function testCRMMemberFormMembershipBuildQuickForm()
173 //{
174 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
175 //}
176
177 /**
178 * Test CRM_Member_Form_Membership::formRule() with a parameter
179 * that has an empty contact_select_id value
180 */
181 public function testFormRuleEmptyContact() {
182 $params = array(
183 'contact_select_id' => 0,
184 'membership_type_id' => array(),
185 );
186 $files = 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));
191
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));
196 }
197
198 /**
199 * Test that form rule fails if start date is before join date.
200 *
201 * Test CRM_Member_Form_Membership::formRule() with a parameter
202 * that has an start date before the join date and a rolling
203 * membership type.
204 */
205 public function testFormRuleRollingEarlyStart() {
206 $unixNow = time();
207 $ymdNow = date('m/d/Y', $unixNow);
208 $unixYesterday = $unixNow - (24 * 60 * 60);
209 $ymdYesterday = date('m/d/Y', $unixYesterday);
210 $params = array(
211 'join_date' => $ymdNow,
212 'start_date' => $ymdYesterday,
213 'end_date' => '',
214 'membership_type_id' => array('23', '15'),
215 );
216 $files = array();
217 $obj = new CRM_Member_Form_Membership();
218 $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'),
219 $params, $files, $obj
220 );
221 $this->assertType('array', $rc);
222 $this->assertTrue(array_key_exists('start_date', $rc));
223 }
224
225 /**
226 * Test CRM_Member_Form_Membership::formRule() with a parameter
227 * that has an end date before the start date and a rolling
228 * membership type
229 */
230 public function testFormRuleRollingEarlyEnd() {
231 $unixNow = time();
232 $ymdNow = date('m/d/Y', $unixNow);
233 $unixYesterday = $unixNow - (24 * 60 * 60);
234 $ymdYesterday = date('m/d/Y', $unixYesterday);
235 $params = array(
236 'join_date' => $ymdNow,
237 'start_date' => $ymdNow,
238 'end_date' => $ymdYesterday,
239 'membership_type_id' => array('23', '15'),
240 );
241 $files = array();
242 $obj = new CRM_Member_Form_Membership();
243 $rc = $obj->formRule($params, $files, $obj);
244 $this->assertType('array', $rc,
245 'In line ' . __LINE__
246 );
247 $this->assertTrue(array_key_exists('end_date', $rc),
248 'In line ' . __LINE__
249 );
250 }
251
252 /**
253 * Test CRM_Member_Form_Membership::formRule() with a parameter
254 * that has an end date but no start date and a rolling
255 * membership type
256 */
257 public function testFormRuleRollingEndNoStart() {
258 $unixNow = time();
259 $ymdNow = date('m/d/Y', $unixNow);
260 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
261 $ymdYearFromNow = date('m/d/Y', $unixYearFromNow);
262 $params = array(
263 'join_date' => $ymdNow,
264 'start_date' => '',
265 'end_date' => $ymdYearFromNow,
266 'membership_type_id' => array('23', '15'),
267 );
268 $files = array();
269 $obj = new CRM_Member_Form_Membership();
270 $rc = $obj->formRule($params, $files, $obj);
271 $this->assertType('array', $rc,
272 'In line ' . __LINE__
273 );
274 $this->assertTrue(array_key_exists('start_date', $rc),
275 'In line ' . __LINE__
276 );
277 }
278
279 /**
280 * Test CRM_Member_Form_Membership::formRule() with a parameter
281 * that has an end date and a lifetime membership type
282 */
283 public function testFormRuleRollingLifetimeEnd() {
284 $unixNow = time();
285 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
286 $params = array(
287 'join_date' => date('m/d/Y', $unixNow),
288 'start_date' => date('m/d/Y', $unixNow),
289 'end_date' => date('m/d/Y',
290 $unixYearFromNow
291 ),
292 'membership_type_id' => array('23', '25'),
293 );
294 $files = array();
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));
299 }
300
301 /**
302 * Test CRM_Member_Form_Membership::formRule() with a parameter
303 * that has an override and no status
304 */
305 public function testFormRuleOverrideNoStatus() {
306 $unixNow = time();
307 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
308 $params = array(
309 'join_date' => date('m/d/Y', $unixNow),
310 'membership_type_id' => array('23', '25'),
311 'is_override' => TRUE,
312 );
313 $files = array();
314 $obj = new CRM_Member_Form_Membership();
315 $rc = $obj->formRule($params, $files, $obj);
316 $this->assertType('array', $rc,
317 'In line ' . __LINE__
318 );
319 $this->assertTrue(array_key_exists('status_id', $rc),
320 'In line ' . __LINE__
321 );
322 }
323
324 /**
325 * Test CRM_Member_Form_Membership::formRule() with a join date
326 * of one month from now and a rolling membership type
327 */
328 public function testFormRuleRollingJoin1MonthFromNow() {
329 $unixNow = time();
330 $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
331 $params = array(
332 'join_date' => date('m/d/Y', $unix1MFmNow),
333 'start_date' => '',
334 'end_date' => '',
335 'membership_type_id' => array('23', '15'),
336 );
337 $files = array();
338 $obj = new CRM_Member_Form_Membership();
339 $rc = $obj->formRule($params, $files, $obj);
340
341 // Should have found no valid membership status
342 $this->assertType('array', $rc,
343 'In line ' . __LINE__
344 );
345 $this->assertTrue(array_key_exists('_qf_default', $rc),
346 'In line ' . __LINE__
347 );
348 }
349
350 /**
351 * Test CRM_Member_Form_Membership::formRule() with a join date
352 * of today and a rolling membership type
353 */
354 public function testFormRuleRollingJoinToday() {
355 $unixNow = time();
356 $params = array(
357 'join_date' => date('m/d/Y', $unixNow),
358 'start_date' => '',
359 'end_date' => '',
360 'membership_type_id' => array('23', '15'),
361 );
362 $files = array();
363 $obj = new CRM_Member_Form_Membership();
364 $rc = $obj->formRule($params, $files, $obj);
365
366 // Should have found New membership status
367 $this->assertTrue($rc, 'In line ' . __LINE__);
368 }
369
370 /**
371 * Test CRM_Member_Form_Membership::formRule() with a join date
372 * of one month ago and a rolling membership type
373 */
374 public function testFormRuleRollingJoin1MonthAgo() {
375 $unixNow = time();
376 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
377 $params = array(
378 'join_date' => date('m/d/Y', $unix1MAgo),
379 'start_date' => '',
380 'end_date' => '',
381 'membership_type_id' => array('23', '15'),
382 );
383 $files = array();
384 $obj = new CRM_Member_Form_Membership();
385 $rc = $obj->formRule($params, $files, $obj);
386
387 // Should have found New membership status
388 $this->assertTrue($rc, 'In line ' . __LINE__);
389 }
390
391 /**
392 * Test CRM_Member_Form_Membership::formRule() with a join date
393 * of six months ago and a rolling membership type
394 */
395 public function testFormRuleRollingJoin6MonthsAgo() {
396 $unixNow = time();
397 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
398 $params = array(
399 'join_date' => date('m/d/Y', $unix6MAgo),
400 'start_date' => '',
401 'end_date' => '',
402 'membership_type_id' => array('23', '15'),
403 );
404 $files = array();
405 $obj = new CRM_Member_Form_Membership();
406 $rc = $obj->formRule($params, $files, $obj);
407
408 // Should have found Current membership status
409 $this->assertTrue($rc, 'In line ' . __LINE__);
410 }
411
412 /**
413 * Test CRM_Member_Form_Membership::formRule() with a join date
414 * of one year+ ago and a rolling membership type
415 */
416 public function testFormRuleRollingJoin1YearAgo() {
417 $unixNow = time();
418 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
419 $params = array(
420 'join_date' => date('m/d/Y', $unix1YAgo),
421 'start_date' => '',
422 'end_date' => '',
423 'membership_type_id' => array('23', '15'),
424 );
425 $files = array();
426 $obj = new CRM_Member_Form_Membership();
427 $rc = $obj->formRule($params, $files, $obj);
428
429 // Should have found Grace membership status
430 $this->assertTrue($rc, 'In line ' . __LINE__);
431 }
432
433 /**
434 * Test CRM_Member_Form_Membership::formRule() with a join date
435 * of two years ago and a rolling membership type
436 */
437 public function testFormRuleRollingJoin2YearsAgo() {
438 $unixNow = time();
439 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
440 $params = array(
441 'join_date' => date('m/d/Y', $unix2YAgo),
442 'start_date' => '',
443 'end_date' => '',
444 'membership_type_id' => array('23', '15'),
445 );
446 $files = array();
447 $obj = new CRM_Member_Form_Membership();
448 $rc = $obj->formRule($params, $files, $obj);
449
450 // Should have found Expired membership status
451 $this->assertTrue($rc, 'In line ' . __LINE__);
452 }
453
454 /**
455 * Test CRM_Member_Form_Membership::formRule() with a join date
456 * of six months ago and a fixed membership type
457 */
458 public function testFormRuleFixedJoin6MonthsAgo() {
459 $unixNow = time();
460 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
461 $params = array(
462 'join_date' => date('m/d/Y', $unix6MAgo),
463 'start_date' => '',
464 'end_date' => '',
465 'membership_type_id' => array('23', '7'),
466 );
467 $files = array();
468 $obj = new CRM_Member_Form_Membership();
469 $rc = $obj->formRule($params, $files, $obj);
470
471 // Should have found Current membership status
472 $this->assertTrue($rc);
473 }
474
475 /**
476 * Test the submit function of the membership form.
477 */
478 public function testSubmit() {
479 $form = new CRM_Member_Form_Membership();
480 $params = array(
481 'cid' => $this->_individualId,
482 'join_date' => date('m/d/Y', time()),
483 'start_date' => '',
484 'end_date' => '',
485 'membership_type_id' => array('13'),
486 'auto_renew' => '0',
487 'max_related' => '',
488 'num_terms' => '1',
489 'source' => '',
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',
498 'cvv2' => '123',
499 'credit_card_exp_date' => array(
500 'M' => '9',
501 'Y' => '2019', // TODO: Future proof
502 ),
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',
511 );
512 $form->submit($params);
513 // TODO: This will still fail right now.
514 //$this->callAPISuccessGetCount('Membership', array('contact_id' => $this->_individualId), 1);
515 }
516
517 }
518 // class CRM_Member_Form_MembershipTest