tests passing
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / MembershipTest.php
CommitLineData
6a488035 1<?php
7865d848
EM
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 */
6a488035
TO
27
28/**
29 * File for the MembershipTest class
30 *
31 * (PHP 5)
32 *
6c6e6187 33 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
6a488035 34 *
6a488035
TO
35 */
36
37/**
38 * Include class definitions
39 */
40require_once 'CiviTest/CiviUnitTestCase.php';
41
42require_once 'HTML/QuickForm/Page.php';
43
44/**
7865d848 45 * Test CRM_Member_Form_Membership functions.
6a488035 46 *
6c6e6187 47 * @package CiviCRM
6a488035
TO
48 */
49class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
50
7865d848
EM
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
6a488035 84 /**
eceb18cc 85 * Test setup for every test.
6a488035
TO
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() {
cb447e7a 91 $this->_apiversion = 3;
6a488035
TO
92 // Connect to the database
93 parent::setUp();
94
cb447e7a
EM
95 $this->_individualId = $this->individualCreate();
96 $this->_paymentProcessorID = $this->processorCreate();
6a488035
TO
97 // Insert test data
98 $op = new PHPUnit_Extensions_Database_Operation_Insert();
99 $op->execute($this->_dbconn,
bbfd46a5 100 $this->createFlatXMLDataSet(
6a488035
TO
101 dirname(__FILE__) . '/dataset/data.xml'
102 )
103 );
7865d848 104
7865d848
EM
105 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
106 $this->paymentInstruments = $instruments['values'];
107 }
108
cb447e7a
EM
109 /**
110 * Clean up after each test.
111 */
112 public function tearDown() {
113 $this->quickCleanUpFinancialEntities();
114 $this->quickCleanup(
115 array(
116 'civicrm_relationship',
117 'civicrm_membership_type',
118 'civicrm_membership',
119 )
120 );
121 $this->callAPISuccess('contact', 'delete', array('id' => 17, 'skip_undelete' => TRUE));
122 $this->callAPISuccess('contact', 'delete', array('id' => 23, 'skip_undelete' => TRUE));
123 $this->callAPISuccess('relationship_type', 'delete', array('id' => 20));
124 }
125
6a488035
TO
126 /**
127 * Test CRM_Member_Form_Membership::buildQuickForm()
128 */
129 //function testCRMMemberFormMembershipBuildQuickForm()
130 //{
131 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
132 //}
133
134 /**
135 * Test CRM_Member_Form_Membership::formRule() with a parameter
136 * that has an empty contact_select_id value
137 */
00be9182 138 public function testFormRuleEmptyContact() {
6a488035
TO
139 $params = array(
140 'contact_select_id' => 0,
141 'membership_type_id' => array(),
142 );
143 $files = array();
a130e045 144 $obj = new CRM_Member_Form_Membership();
92915c55 145 $rc = $obj->formRule($params, $files, $obj);
cb447e7a
EM
146 $this->assertType('array', $rc);
147 $this->assertTrue(array_key_exists('membership_type_id', $rc));
6a488035
TO
148
149 $params['membership_type_id'] = array(1 => 3);
150 $rc = $obj->formRule($params, $files, $obj);
cb447e7a
EM
151 $this->assertType('array', $rc);
152 $this->assertTrue(array_key_exists('join_date', $rc));
6a488035
TO
153 }
154
155 /**
cb447e7a
EM
156 * Test that form rule fails if start date is before join date.
157 *
158 * Test CRM_Member_Form_Membership::formRule() with a parameter
159 * that has an start date before the join date and a rolling
160 * membership type.
6a488035 161 */
00be9182 162 public function testFormRuleRollingEarlyStart() {
92915c55
TO
163 $unixNow = time();
164 $ymdNow = date('m/d/Y', $unixNow);
6a488035 165 $unixYesterday = $unixNow - (24 * 60 * 60);
92915c55
TO
166 $ymdYesterday = date('m/d/Y', $unixYesterday);
167 $params = array(
6a488035
TO
168 'join_date' => $ymdNow,
169 'start_date' => $ymdYesterday,
170 'end_date' => '',
cb447e7a 171 'membership_type_id' => array('23', '15'),
6a488035
TO
172 );
173 $files = array();
a130e045 174 $obj = new CRM_Member_Form_Membership();
92915c55 175 $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'),
6a488035
TO
176 $params, $files, $obj
177 );
cb447e7a
EM
178 $this->assertType('array', $rc);
179 $this->assertTrue(array_key_exists('start_date', $rc));
6a488035
TO
180 }
181
182 /**
183 * Test CRM_Member_Form_Membership::formRule() with a parameter
184 * that has an end date before the start date and a rolling
185 * membership type
186 */
00be9182 187 public function testFormRuleRollingEarlyEnd() {
92915c55
TO
188 $unixNow = time();
189 $ymdNow = date('m/d/Y', $unixNow);
6a488035 190 $unixYesterday = $unixNow - (24 * 60 * 60);
92915c55
TO
191 $ymdYesterday = date('m/d/Y', $unixYesterday);
192 $params = array(
6a488035
TO
193 'join_date' => $ymdNow,
194 'start_date' => $ymdNow,
195 'end_date' => $ymdYesterday,
cb447e7a 196 'membership_type_id' => array('23', '15'),
6a488035
TO
197 );
198 $files = array();
a130e045 199 $obj = new CRM_Member_Form_Membership();
92915c55 200 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
201 $this->assertType('array', $rc,
202 'In line ' . __LINE__
203 );
204 $this->assertTrue(array_key_exists('end_date', $rc),
205 'In line ' . __LINE__
206 );
207 }
208
209 /**
210 * Test CRM_Member_Form_Membership::formRule() with a parameter
211 * that has an end date but no start date and a rolling
212 * membership type
213 */
00be9182 214 public function testFormRuleRollingEndNoStart() {
92915c55
TO
215 $unixNow = time();
216 $ymdNow = date('m/d/Y', $unixNow);
6a488035 217 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
92915c55
TO
218 $ymdYearFromNow = date('m/d/Y', $unixYearFromNow);
219 $params = array(
6a488035
TO
220 'join_date' => $ymdNow,
221 'start_date' => '',
222 'end_date' => $ymdYearFromNow,
cb447e7a 223 'membership_type_id' => array('23', '15'),
6a488035
TO
224 );
225 $files = array();
a130e045 226 $obj = new CRM_Member_Form_Membership();
92915c55 227 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
228 $this->assertType('array', $rc,
229 'In line ' . __LINE__
230 );
231 $this->assertTrue(array_key_exists('start_date', $rc),
232 'In line ' . __LINE__
233 );
234 }
235
236 /**
237 * Test CRM_Member_Form_Membership::formRule() with a parameter
238 * that has an end date and a lifetime membership type
239 */
00be9182 240 public function testFormRuleRollingLifetimeEnd() {
92915c55 241 $unixNow = time();
6a488035 242 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
92915c55 243 $params = array(
fada2199 244 'join_date' => date('m/d/Y', $unixNow),
6a488035
TO
245 'start_date' => date('m/d/Y', $unixNow),
246 'end_date' => date('m/d/Y',
247 $unixYearFromNow
248 ),
cb447e7a 249 'membership_type_id' => array('23', '25'),
6a488035
TO
250 );
251 $files = array();
a130e045 252 $obj = new CRM_Member_Form_Membership();
92915c55 253 $rc = $obj->formRule($params, $files, $obj);
fada2199
EM
254 $this->assertType('array', $rc);
255 $this->assertTrue(array_key_exists('status_id', $rc));
6a488035
TO
256 }
257
258 /**
259 * Test CRM_Member_Form_Membership::formRule() with a parameter
260 * that has an override and no status
261 */
00be9182 262 public function testFormRuleOverrideNoStatus() {
92915c55 263 $unixNow = time();
6a488035 264 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
92915c55
TO
265 $params = array(
266 'join_date' => date('m/d/Y', $unixNow),
cb447e7a 267 'membership_type_id' => array('23', '25'),
6a488035
TO
268 'is_override' => TRUE,
269 );
270 $files = array();
a130e045 271 $obj = new CRM_Member_Form_Membership();
92915c55 272 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
273 $this->assertType('array', $rc,
274 'In line ' . __LINE__
275 );
276 $this->assertTrue(array_key_exists('status_id', $rc),
277 'In line ' . __LINE__
278 );
279 }
280
281 /**
282 * Test CRM_Member_Form_Membership::formRule() with a join date
283 * of one month from now and a rolling membership type
284 */
00be9182 285 public function testFormRuleRollingJoin1MonthFromNow() {
92915c55 286 $unixNow = time();
6a488035 287 $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
92915c55
TO
288 $params = array(
289 'join_date' => date('m/d/Y', $unix1MFmNow),
6a488035
TO
290 'start_date' => '',
291 'end_date' => '',
cb447e7a 292 'membership_type_id' => array('23', '15'),
6a488035
TO
293 );
294 $files = array();
a130e045 295 $obj = new CRM_Member_Form_Membership();
92915c55 296 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
297
298 // Should have found no valid membership status
299 $this->assertType('array', $rc,
300 'In line ' . __LINE__
301 );
302 $this->assertTrue(array_key_exists('_qf_default', $rc),
303 'In line ' . __LINE__
304 );
305 }
306
307 /**
308 * Test CRM_Member_Form_Membership::formRule() with a join date
309 * of today and a rolling membership type
310 */
00be9182 311 public function testFormRuleRollingJoinToday() {
6a488035 312 $unixNow = time();
6c6e6187 313 $params = array(
92915c55 314 'join_date' => date('m/d/Y', $unixNow),
6a488035
TO
315 'start_date' => '',
316 'end_date' => '',
cb447e7a 317 'membership_type_id' => array('23', '15'),
6a488035
TO
318 );
319 $files = array();
a130e045 320 $obj = new CRM_Member_Form_Membership();
92915c55 321 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
322
323 // Should have found New membership status
324 $this->assertTrue($rc, 'In line ' . __LINE__);
325 }
326
327 /**
328 * Test CRM_Member_Form_Membership::formRule() with a join date
329 * of one month ago and a rolling membership type
330 */
00be9182 331 public function testFormRuleRollingJoin1MonthAgo() {
92915c55 332 $unixNow = time();
6a488035 333 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
92915c55
TO
334 $params = array(
335 'join_date' => date('m/d/Y', $unix1MAgo),
6a488035
TO
336 'start_date' => '',
337 'end_date' => '',
cb447e7a 338 'membership_type_id' => array('23', '15'),
6a488035
TO
339 );
340 $files = array();
a130e045 341 $obj = new CRM_Member_Form_Membership();
92915c55 342 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
343
344 // Should have found New membership status
345 $this->assertTrue($rc, 'In line ' . __LINE__);
346 }
347
348 /**
349 * Test CRM_Member_Form_Membership::formRule() with a join date
350 * of six months ago and a rolling membership type
351 */
00be9182 352 public function testFormRuleRollingJoin6MonthsAgo() {
92915c55 353 $unixNow = time();
6a488035 354 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
92915c55
TO
355 $params = array(
356 'join_date' => date('m/d/Y', $unix6MAgo),
6a488035
TO
357 'start_date' => '',
358 'end_date' => '',
cb447e7a 359 'membership_type_id' => array('23', '15'),
6a488035
TO
360 );
361 $files = array();
a130e045 362 $obj = new CRM_Member_Form_Membership();
92915c55 363 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
364
365 // Should have found Current membership status
366 $this->assertTrue($rc, 'In line ' . __LINE__);
367 }
368
369 /**
370 * Test CRM_Member_Form_Membership::formRule() with a join date
371 * of one year+ ago and a rolling membership type
372 */
00be9182 373 public function testFormRuleRollingJoin1YearAgo() {
92915c55 374 $unixNow = time();
6a488035 375 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
92915c55
TO
376 $params = array(
377 'join_date' => date('m/d/Y', $unix1YAgo),
6a488035
TO
378 'start_date' => '',
379 'end_date' => '',
cb447e7a 380 'membership_type_id' => array('23', '15'),
6a488035
TO
381 );
382 $files = array();
a130e045 383 $obj = new CRM_Member_Form_Membership();
92915c55 384 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
385
386 // Should have found Grace membership status
387 $this->assertTrue($rc, 'In line ' . __LINE__);
388 }
389
390 /**
391 * Test CRM_Member_Form_Membership::formRule() with a join date
392 * of two years ago and a rolling membership type
393 */
00be9182 394 public function testFormRuleRollingJoin2YearsAgo() {
92915c55 395 $unixNow = time();
6a488035 396 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
92915c55
TO
397 $params = array(
398 'join_date' => date('m/d/Y', $unix2YAgo),
6a488035
TO
399 'start_date' => '',
400 'end_date' => '',
cb447e7a 401 'membership_type_id' => array('23', '15'),
6a488035
TO
402 );
403 $files = array();
a130e045 404 $obj = new CRM_Member_Form_Membership();
92915c55 405 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
406
407 // Should have found Expired membership status
408 $this->assertTrue($rc, 'In line ' . __LINE__);
409 }
410
411 /**
412 * Test CRM_Member_Form_Membership::formRule() with a join date
413 * of six months ago and a fixed membership type
414 */
00be9182 415 public function testFormRuleFixedJoin6MonthsAgo() {
92915c55 416 $unixNow = time();
6a488035 417 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
92915c55
TO
418 $params = array(
419 'join_date' => date('m/d/Y', $unix6MAgo),
6a488035
TO
420 'start_date' => '',
421 'end_date' => '',
422 'membership_type_id' => array('23', '7'),
423 );
424 $files = array();
a130e045 425 $obj = new CRM_Member_Form_Membership();
92915c55 426 $rc = $obj->formRule($params, $files, $obj);
6a488035
TO
427
428 // Should have found Current membership status
cb447e7a 429 $this->assertTrue($rc);
6a488035 430 }
96025800 431
7865d848
EM
432 /**
433 * Test the submit function of the membership form.
434 */
435 public function testSubmit() {
436 $form = new CRM_Member_Form_Membership();
c41bfe2b 437 $this->createLoggedInUser();
7865d848
EM
438 $params = array(
439 'cid' => $this->_individualId,
440 'join_date' => date('m/d/Y', time()),
441 'start_date' => '',
442 'end_date' => '',
059103da 443 'membership_type_id' => array('25'),
7865d848
EM
444 'auto_renew' => '0',
445 'max_related' => '',
446 'num_terms' => '1',
447 'source' => '',
448 'total_amount' => '50.00',
059103da 449 'financial_type_id' => '2', //Member dues, see data.xml
7865d848
EM
450 'soft_credit_type_id' => '',
451 'soft_credit_contact_id' => '',
452 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
453 'receipt_text_signup' => 'Thank you text',
454 'payment_processor_id' => $this->_paymentProcessorID,
455 'credit_card_number' => '4111111111111111',
456 'cvv2' => '123',
457 'credit_card_exp_date' => array(
458 'M' => '9',
459 'Y' => '2019', // TODO: Future proof
460 ),
461 'credit_card_type' => 'Visa',
462 'billing_first_name' => 'Test',
463 'billing_middlename' => 'Last',
464 'billing_street_address-5' => '10 Test St',
465 'billing_city-5' => 'Test',
466 'billing_state_province_id-5' => '1003',
467 'billing_postal_code-5' => '90210',
468 'billing_country_id-5' => '1228',
469 );
470 $form->submit($params);
471 // TODO: This will still fail right now.
472 //$this->callAPISuccessGetCount('Membership', array('contact_id' => $this->_individualId), 1);
473 }
474
6a488035
TO
475}
476// class CRM_Member_Form_MembershipTest