Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-21-08-12-12
[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 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(
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('join_date' => date('m/d/Y', $unixNow),
336 'start_date' => date('m/d/Y', $unixNow),
337 'end_date' => date('m/d/Y',
338 $unixYearFromNow
339 ),
340 'membership_type_id' => array('23', '13'),
341 );
342 $files = array();
343 $obj = new CRM_Member_Form_Membership;
344 $rc = $obj->formRule($params, $files, $obj);
345 $this->assertType('array', $rc,
346 'In line ' . __LINE__
347 );
348 $this->assertTrue(array_key_exists('end_date', $rc),
349 'In line ' . __LINE__
350 );
351 }
352
353 /**
354 * Test CRM_Member_Form_Membership::formRule() with a parameter
355 * that has an override and no status
356 */
357 function testFormRuleOverrideNoStatus() {
358 $unixNow = time();
359 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
360 $params = array('join_date' => date('m/d/Y', $unixNow),
361 'membership_type_id' => array('23', '13'),
362 'is_override' => TRUE,
363 );
364 $files = array();
365 $obj = new CRM_Member_Form_Membership;
366 $rc = $obj->formRule($params, $files, $obj);
367 $this->assertType('array', $rc,
368 'In line ' . __LINE__
369 );
370 $this->assertTrue(array_key_exists('status_id', $rc),
371 'In line ' . __LINE__
372 );
373 }
374
375 /**
376 * Test CRM_Member_Form_Membership::formRule() with a join date
377 * of one month from now and a rolling membership type
378 */
379 function testFormRuleRollingJoin1MonthFromNow() {
380 $unixNow = time();
381 $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
382 $params = array('join_date' => date('m/d/Y', $unix1MFmNow),
383 'start_date' => '',
384 'end_date' => '',
385 'membership_type_id' => array('23', '3'),
386 );
387 $files = array();
388 $obj = new CRM_Member_Form_Membership;
389 $rc = $obj->formRule($params, $files, $obj);
390
391 // Should have found no valid membership status
392 $this->assertType('array', $rc,
393 'In line ' . __LINE__
394 );
395 $this->assertTrue(array_key_exists('_qf_default', $rc),
396 'In line ' . __LINE__
397 );
398 }
399
400 /**
401 * Test CRM_Member_Form_Membership::formRule() with a join date
402 * of today and a rolling membership type
403 */
404 function testFormRuleRollingJoinToday() {
405 $unixNow = time();
406 $params = array('join_date' => date('m/d/Y', $unixNow),
407 'start_date' => '',
408 'end_date' => '',
409 'membership_type_id' => array('23', '3'),
410 );
411 $files = array();
412 $obj = new CRM_Member_Form_Membership;
413 $rc = $obj->formRule($params, $files, $obj);
414
415 // Should have found New membership status
416 $this->assertTrue($rc, 'In line ' . __LINE__);
417 }
418
419 /**
420 * Test CRM_Member_Form_Membership::formRule() with a join date
421 * of one month ago and a rolling membership type
422 */
423 function testFormRuleRollingJoin1MonthAgo() {
424 $unixNow = time();
425 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
426 $params = array('join_date' => date('m/d/Y', $unix1MAgo),
427 'start_date' => '',
428 'end_date' => '',
429 'membership_type_id' => array('23', '3'),
430 );
431 $files = array();
432 $obj = new CRM_Member_Form_Membership;
433 $rc = $obj->formRule($params, $files, $obj);
434
435 // Should have found New membership status
436 $this->assertTrue($rc, 'In line ' . __LINE__);
437 }
438
439 /**
440 * Test CRM_Member_Form_Membership::formRule() with a join date
441 * of six months ago and a rolling membership type
442 */
443 function testFormRuleRollingJoin6MonthsAgo() {
444 $unixNow = time();
445 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
446 $params = array('join_date' => date('m/d/Y', $unix6MAgo),
447 'start_date' => '',
448 'end_date' => '',
449 'membership_type_id' => array('23', '3'),
450 );
451 $files = array();
452 $obj = new CRM_Member_Form_Membership;
453 $rc = $obj->formRule($params, $files, $obj);
454
455 // Should have found Current membership status
456 $this->assertTrue($rc, 'In line ' . __LINE__);
457 }
458
459 /**
460 * Test CRM_Member_Form_Membership::formRule() with a join date
461 * of one year+ ago and a rolling membership type
462 */
463 function testFormRuleRollingJoin1YearAgo() {
464 $unixNow = time();
465 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
466 $params = array('join_date' => date('m/d/Y', $unix1YAgo),
467 'start_date' => '',
468 'end_date' => '',
469 'membership_type_id' => array('23', '3'),
470 );
471 $files = array();
472 $obj = new CRM_Member_Form_Membership;
473 $rc = $obj->formRule($params, $files, $obj);
474
475 // Should have found Grace membership status
476 $this->assertTrue($rc, 'In line ' . __LINE__);
477 }
478
479 /**
480 * Test CRM_Member_Form_Membership::formRule() with a join date
481 * of two years ago and a rolling membership type
482 */
483 function testFormRuleRollingJoin2YearsAgo() {
484 $unixNow = time();
485 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
486 $params = array('join_date' => date('m/d/Y', $unix2YAgo),
487 'start_date' => '',
488 'end_date' => '',
489 'membership_type_id' => array('23', '3'),
490 );
491 $files = array();
492 $obj = new CRM_Member_Form_Membership;
493 $rc = $obj->formRule($params, $files, $obj);
494
495 // Should have found Expired membership status
496 $this->assertTrue($rc, 'In line ' . __LINE__);
497 }
498
499 /**
500 * Test CRM_Member_Form_Membership::formRule() with a join date
501 * of six months ago and a fixed membership type
502 */
503 function testFormRuleFixedJoin6MonthsAgo() {
504 $unixNow = time();
505 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
506 $params = array('join_date' => date('m/d/Y', $unix6MAgo),
507 'start_date' => '',
508 'end_date' => '',
509 'membership_type_id' => array('23', '7'),
510 );
511 $files = array();
512 $obj = new CRM_Member_Form_Membership;
513 $rc = $obj->formRule($params, $files, $obj);
514
515 // Should have found Current membership status
516 $this->assertTrue($rc, 'In line ' . __LINE__);
517 }
518 }
519 // class CRM_Member_Form_MembershipTest
520