Merge pull request #67 from dpradeep/merge-20140827
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / MembershipTest.php
1 <?php
2
3 /**
4 * File for the MembershipTest class
5 *
6 * (PHP 5)
7 *
8 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
9 * @copyright Copyright CiviCRM LLC (C) 2009
10 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
11 * GNU Affero General Public License version 3
12 * @package CiviCRM
13 *
14 * This file is part of CiviCRM
15 *
16 * CiviCRM is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU Affero General Public License
18 * as published by the Free Software Foundation; either version 3 of
19 * the License, or (at your option) any later version.
20 *
21 * CiviCRM is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU Affero General Public License for more details.
25 *
26 * You should have received a copy of the GNU Affero General Public
27 * License along with this program. If not, see
28 * <http://www.gnu.org/licenses/>.
29 */
30
31 /**
32 * Include class definitions
33 */
34 require_once 'CiviTest/CiviUnitTestCase.php';
35
36 require_once 'HTML/QuickForm/Page.php';
37
38 /**
39 * Test APIv2 civicrm_activity_* functions
40 *
41 * @package CiviCRM
42 */
43 class CRM_Member_Form_MembershipTest extends CiviUnitTestCase {
44
45 /**
46 * Test setup for every test
47 *
48 * Connect to the database, truncate the tables that will be used
49 * and redirect stdin to a temporary file
50 */
51 public function setUp() {
52 // Connect to the database
53 parent::setUp();
54
55 $this->quickCleanup(
56 array(
57 'civicrm_address_format',
58 'civicrm_currency',
59 'civicrm_domain',
60 'civicrm_file',
61 'civicrm_financial_account',
62 'civicrm_financial_trxn',
63 'civicrm_job',
64 'civicrm_job_log',
65 'civicrm_location_type',
66 'civicrm_mail_settings',
67 'civicrm_mapping',
68 'civicrm_navigation',
69 'civicrm_option_group',
70 'civicrm_payment_processor',
71 'civicrm_payment_processor_type',
72 'civicrm_preferences_date',
73 'civicrm_worldregion',
74 'civicrm_component',
75 'civicrm_persistent',
76 'civicrm_prevnext_cache',
77 'civicrm_action_mapping',
78 'civicrm_acl',
79 'civicrm_acl_entity_role',
80 'civicrm_contact',
81 'civicrm_acl_contact_cache',
82 'civicrm_relationship_type',
83 'civicrm_saved_search',
84 'civicrm_contact_type',
85 'civicrm_mailing_component',
86 'civicrm_mailing_bounce_type',
87 'civicrm_mailing_bounce_pattern',
88 'civicrm_financial_type',
89 'civicrm_premiums',
90 'civicrm_product',
91 'civicrm_premiums_product',
92 'civicrm_sms_provider',
93 'civicrm_membership_status',
94 'civicrm_campaign',
95 'civicrm_campaign_group',
96 'civicrm_survey',
97 'civicrm_participant_status_type',
98 'civicrm_event_carts',
99 'civicrm_dedupe_rule_group',
100 'civicrm_dedupe_rule',
101 'civicrm_dedupe_exception',
102 'civicrm_case',
103 'civicrm_case_contact',
104 'civicrm_grant',
105 'civicrm_tell_friend',
106 'civicrm_pledge_block',
107 'civicrm_queue_item',
108 'civicrm_report_instance',
109 'civicrm_price_set',
110 'civicrm_price_set_entity',
111 'civicrm_pcp',
112 'civicrm_batch',
113 'civicrm_cache',
114 'civicrm_country',
115 'civicrm_custom_group',
116 'civicrm_custom_field',
117 'civicrm_dashboard',
118 'civicrm_email',
119 'civicrm_entity_batch',
120 'civicrm_entity_file',
121 'civicrm_entity_financial_trxn',
122 'civicrm_im',
123 'civicrm_log',
124 'civicrm_mapping_field',
125 'civicrm_menu',
126 'civicrm_note',
127 'civicrm_option_value',
128 'civicrm_phone',
129 'civicrm_state_province',
130 'civicrm_tag',
131 'civicrm_uf_match',
132 'civicrm_timezone',
133 'civicrm_openid',
134 'civicrm_discount',
135 'civicrm_website',
136 'civicrm_setting',
137 'civicrm_acl_cache',
138 'civicrm_dashboard_contact',
139 'civicrm_group',
140 'civicrm_subscription_history',
141 'civicrm_group_contact_cache',
142 'civicrm_group_nesting',
143 'civicrm_group_organization',
144 'civicrm_relationship',
145 'civicrm_mailing_event_subscribe',
146 'civicrm_mailing_event_confirm',
147 'civicrm_contribution_recur',
148 'civicrm_contribution_page',
149 'civicrm_contribution_widget',
150 'civicrm_activity',
151 'civicrm_activity_contact',
152 'civicrm_case_activity',
153 'civicrm_pledge',
154 'civicrm_price_field',
155 'civicrm_county',
156 'civicrm_entity_tag',
157 'civicrm_msg_template',
158 'civicrm_uf_group',
159 'civicrm_uf_field',
160 'civicrm_uf_join',
161 'civicrm_action_schedule',
162 'civicrm_action_log',
163 'civicrm_mailing',
164 'civicrm_mailing_group',
165 'civicrm_mailing_trackable_url',
166 'civicrm_mailing_job',
167 'civicrm_mailing_recipients',
168 'civicrm_mailing_spool',
169 'civicrm_mailing_event_queue',
170 'civicrm_mailing_event_bounce',
171 'civicrm_mailing_event_delivered',
172 'civicrm_mailing_event_forward',
173 'civicrm_mailing_event_opened',
174 'civicrm_mailing_event_reply',
175 'civicrm_mailing_event_trackable_url_open',
176 'civicrm_mailing_event_unsubscribe',
177 'civicrm_membership_type',
178 'civicrm_membership',
179 'civicrm_membership_block',
180 'civicrm_membership_log',
181 'civicrm_price_field_value',
182 'civicrm_line_item',
183 'civicrm_pcp_block',
184 'civicrm_address',
185 'civicrm_loc_block',
186 'civicrm_group_contact',
187 'civicrm_contribution',
188 'civicrm_contribution_product',
189 'civicrm_contribution_soft',
190 'civicrm_membership_payment',
191 'civicrm_event',
192 'civicrm_participant',
193 'civicrm_participant_payment',
194 'civicrm_events_in_carts',
195 'civicrm_pledge_payment',
196 )
197 );
198
199 // Insert test data
200 $op = new PHPUnit_Extensions_Database_Operation_Insert();
201 $op->execute($this->_dbconn,
202 $this->createFlatXMLDataSet(
203 dirname(__FILE__) . '/dataset/data.xml'
204 )
205 );
206 }
207
208 /**
209 * Test CRM_Member_Form_Membership::buildQuickForm()
210 */
211 //function testCRMMemberFormMembershipBuildQuickForm()
212 //{
213 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
214 //}
215
216 /**
217 * Test CRM_Member_Form_Membership::formRule() with a parameter
218 * that has an empty contact_select_id value
219 */
220 function testFormRuleEmptyContact() {
221 $params = array(
222 'contact_select_id' => 0,
223 'membership_type_id' => array(),
224 );
225 $files = array();
226 $obj = new CRM_Member_Form_Membership;
227 $rc = $obj->formRule($params, $files, $obj);
228 $this->assertType('array', $rc,
229 'In line ' . __LINE__
230 );
231 $this->assertTrue(array_key_exists('membership_type_id', $rc),
232 'In line ' . __LINE__
233 );
234
235 $params['membership_type_id'] = array(1 => 3);
236 $rc = $obj->formRule($params, $files, $obj);
237 $this->assertType('array', $rc,
238 'In line ' . __LINE__
239 );
240 $this->assertTrue(array_key_exists('join_date', $rc),
241 'In line ' . __LINE__
242 );
243 }
244
245 /**
246 * Test CRM_Member_Form_Membership::formRule() with a parameter
247 * that has an start date before the join date and a rolling
248 * membership type
249 */
250 function testFormRuleRollingEarlyStart() {
251 $unixNow = time();
252 $ymdNow = date('m/d/Y', $unixNow);
253 $unixYesterday = $unixNow - (24 * 60 * 60);
254 $ymdYesterday = date('m/d/Y', $unixYesterday);
255 $params = array(
256 'join_date' => $ymdNow,
257 'start_date' => $ymdYesterday,
258 'end_date' => '',
259 'membership_type_id' => array('23', '3'),
260 );
261 $files = array();
262 $obj = new CRM_Member_Form_Membership;
263 $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'),
264 $params, $files, $obj
265 );
266 $this->assertType('array', $rc,
267 'In line ' . __LINE__
268 );
269 $this->assertTrue(array_key_exists('start_date', $rc),
270 'In line ' . __LINE__
271 );
272 }
273
274 /**
275 * Test CRM_Member_Form_Membership::formRule() with a parameter
276 * that has an end date before the start date and a rolling
277 * membership type
278 */
279 function testFormRuleRollingEarlyEnd() {
280 $unixNow = time();
281 $ymdNow = date('m/d/Y', $unixNow);
282 $unixYesterday = $unixNow - (24 * 60 * 60);
283 $ymdYesterday = date('m/d/Y', $unixYesterday);
284 $params = array(
285 'join_date' => $ymdNow,
286 'start_date' => $ymdNow,
287 'end_date' => $ymdYesterday,
288 'membership_type_id' => array('23', '3'),
289 );
290 $files = array();
291 $obj = new CRM_Member_Form_Membership;
292 $rc = $obj->formRule($params, $files, $obj);
293 $this->assertType('array', $rc,
294 'In line ' . __LINE__
295 );
296 $this->assertTrue(array_key_exists('end_date', $rc),
297 'In line ' . __LINE__
298 );
299 }
300
301 /**
302 * Test CRM_Member_Form_Membership::formRule() with a parameter
303 * that has an end date but no start date and a rolling
304 * membership type
305 */
306 function testFormRuleRollingEndNoStart() {
307 $unixNow = time();
308 $ymdNow = date('m/d/Y', $unixNow);
309 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
310 $ymdYearFromNow = date('m/d/Y', $unixYearFromNow);
311 $params = array(
312 'join_date' => $ymdNow,
313 'start_date' => '',
314 'end_date' => $ymdYearFromNow,
315 'membership_type_id' => array('23', '3'),
316 );
317 $files = array();
318 $obj = new CRM_Member_Form_Membership;
319 $rc = $obj->formRule($params, $files, $obj);
320 $this->assertType('array', $rc,
321 'In line ' . __LINE__
322 );
323 $this->assertTrue(array_key_exists('start_date', $rc),
324 'In line ' . __LINE__
325 );
326 }
327
328 /**
329 * Test CRM_Member_Form_Membership::formRule() with a parameter
330 * that has an end date and a lifetime membership type
331 */
332 function testFormRuleRollingLifetimeEnd() {
333 $unixNow = time();
334 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
335 $params = array(
336 'join_date' => date('m/d/Y', $unixNow),
337 'start_date' => date('m/d/Y', $unixNow),
338 'end_date' => date('m/d/Y',
339 $unixYearFromNow
340 ),
341 'membership_type_id' => array('23', '13'),
342 );
343 $files = array();
344 $obj = new CRM_Member_Form_Membership;
345 $rc = $obj->formRule($params, $files, $obj);
346 $this->assertType('array', $rc);
347 $this->assertTrue(array_key_exists('status_id', $rc));
348 }
349
350 /**
351 * Test CRM_Member_Form_Membership::formRule() with a parameter
352 * that has an override and no status
353 */
354 function testFormRuleOverrideNoStatus() {
355 $unixNow = time();
356 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
357 $params = array('join_date' => date('m/d/Y', $unixNow),
358 'membership_type_id' => array('23', '13'),
359 'is_override' => TRUE,
360 );
361 $files = array();
362 $obj = new CRM_Member_Form_Membership;
363 $rc = $obj->formRule($params, $files, $obj);
364 $this->assertType('array', $rc,
365 'In line ' . __LINE__
366 );
367 $this->assertTrue(array_key_exists('status_id', $rc),
368 'In line ' . __LINE__
369 );
370 }
371
372 /**
373 * Test CRM_Member_Form_Membership::formRule() with a join date
374 * of one month from now and a rolling membership type
375 */
376 function testFormRuleRollingJoin1MonthFromNow() {
377 $unixNow = time();
378 $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
379 $params = array('join_date' => date('m/d/Y', $unix1MFmNow),
380 'start_date' => '',
381 'end_date' => '',
382 'membership_type_id' => array('23', '3'),
383 );
384 $files = array();
385 $obj = new CRM_Member_Form_Membership;
386 $rc = $obj->formRule($params, $files, $obj);
387
388 // Should have found no valid membership status
389 $this->assertType('array', $rc,
390 'In line ' . __LINE__
391 );
392 $this->assertTrue(array_key_exists('_qf_default', $rc),
393 'In line ' . __LINE__
394 );
395 }
396
397 /**
398 * Test CRM_Member_Form_Membership::formRule() with a join date
399 * of today and a rolling membership type
400 */
401 function testFormRuleRollingJoinToday() {
402 $unixNow = time();
403 $params = array('join_date' => date('m/d/Y', $unixNow),
404 'start_date' => '',
405 'end_date' => '',
406 'membership_type_id' => array('23', '3'),
407 );
408 $files = array();
409 $obj = new CRM_Member_Form_Membership;
410 $rc = $obj->formRule($params, $files, $obj);
411
412 // Should have found New membership status
413 $this->assertTrue($rc, 'In line ' . __LINE__);
414 }
415
416 /**
417 * Test CRM_Member_Form_Membership::formRule() with a join date
418 * of one month ago and a rolling membership type
419 */
420 function testFormRuleRollingJoin1MonthAgo() {
421 $unixNow = time();
422 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
423 $params = array('join_date' => date('m/d/Y', $unix1MAgo),
424 'start_date' => '',
425 'end_date' => '',
426 'membership_type_id' => array('23', '3'),
427 );
428 $files = array();
429 $obj = new CRM_Member_Form_Membership;
430 $rc = $obj->formRule($params, $files, $obj);
431
432 // Should have found New membership status
433 $this->assertTrue($rc, 'In line ' . __LINE__);
434 }
435
436 /**
437 * Test CRM_Member_Form_Membership::formRule() with a join date
438 * of six months ago and a rolling membership type
439 */
440 function testFormRuleRollingJoin6MonthsAgo() {
441 $unixNow = time();
442 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
443 $params = array('join_date' => date('m/d/Y', $unix6MAgo),
444 'start_date' => '',
445 'end_date' => '',
446 'membership_type_id' => array('23', '3'),
447 );
448 $files = array();
449 $obj = new CRM_Member_Form_Membership;
450 $rc = $obj->formRule($params, $files, $obj);
451
452 // Should have found Current membership status
453 $this->assertTrue($rc, 'In line ' . __LINE__);
454 }
455
456 /**
457 * Test CRM_Member_Form_Membership::formRule() with a join date
458 * of one year+ ago and a rolling membership type
459 */
460 function testFormRuleRollingJoin1YearAgo() {
461 $unixNow = time();
462 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
463 $params = array('join_date' => date('m/d/Y', $unix1YAgo),
464 'start_date' => '',
465 'end_date' => '',
466 'membership_type_id' => array('23', '3'),
467 );
468 $files = array();
469 $obj = new CRM_Member_Form_Membership;
470 $rc = $obj->formRule($params, $files, $obj);
471
472 // Should have found Grace membership status
473 $this->assertTrue($rc, 'In line ' . __LINE__);
474 }
475
476 /**
477 * Test CRM_Member_Form_Membership::formRule() with a join date
478 * of two years ago and a rolling membership type
479 */
480 function testFormRuleRollingJoin2YearsAgo() {
481 $unixNow = time();
482 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
483 $params = array('join_date' => date('m/d/Y', $unix2YAgo),
484 'start_date' => '',
485 'end_date' => '',
486 'membership_type_id' => array('23', '3'),
487 );
488 $files = array();
489 $obj = new CRM_Member_Form_Membership;
490 $rc = $obj->formRule($params, $files, $obj);
491
492 // Should have found Expired membership status
493 $this->assertTrue($rc, 'In line ' . __LINE__);
494 }
495
496 /**
497 * Test CRM_Member_Form_Membership::formRule() with a join date
498 * of six months ago and a fixed membership type
499 */
500 function testFormRuleFixedJoin6MonthsAgo() {
501 $unixNow = time();
502 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
503 $params = array('join_date' => date('m/d/Y', $unix6MAgo),
504 'start_date' => '',
505 'end_date' => '',
506 'membership_type_id' => array('23', '7'),
507 );
508 $files = array();
509 $obj = new CRM_Member_Form_Membership;
510 $rc = $obj->formRule($params, $files, $obj);
511
512 // Should have found Current membership status
513 $this->assertTrue($rc, 'In line ' . __LINE__);
514 }
515 }
516 // class CRM_Member_Form_MembershipTest
517