47d1ffb1c425f439364b019b317628c1eb4d3d66
[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_mailing_component',
85 'civicrm_mailing_bounce_type',
86 'civicrm_mailing_bounce_pattern',
87 'civicrm_financial_type',
88 'civicrm_premiums',
89 'civicrm_product',
90 'civicrm_premiums_product',
91 'civicrm_sms_provider',
92 'civicrm_membership_status',
93 'civicrm_campaign',
94 'civicrm_campaign_group',
95 'civicrm_survey',
96 'civicrm_participant_status_type',
97 'civicrm_event_carts',
98 'civicrm_dedupe_rule_group',
99 'civicrm_dedupe_rule',
100 'civicrm_dedupe_exception',
101 'civicrm_case',
102 'civicrm_case_contact',
103 'civicrm_grant',
104 'civicrm_tell_friend',
105 'civicrm_pledge_block',
106 'civicrm_queue_item',
107 'civicrm_report_instance',
108 'civicrm_price_set',
109 'civicrm_price_set_entity',
110 'civicrm_pcp',
111 'civicrm_batch',
112 'civicrm_cache',
113 'civicrm_country',
114 'civicrm_custom_group',
115 'civicrm_custom_field',
116 'civicrm_dashboard',
117 'civicrm_email',
118 'civicrm_entity_batch',
119 'civicrm_entity_file',
120 'civicrm_entity_financial_trxn',
121 'civicrm_im',
122 'civicrm_log',
123 'civicrm_mapping_field',
124 'civicrm_menu',
125 'civicrm_note',
126 'civicrm_option_value',
127 'civicrm_phone',
128 'civicrm_state_province',
129 'civicrm_tag',
130 'civicrm_uf_match',
131 'civicrm_timezone',
132 'civicrm_openid',
133 'civicrm_discount',
134 'civicrm_website',
135 'civicrm_setting',
136 'civicrm_acl_cache',
137 'civicrm_dashboard_contact',
138 'civicrm_group',
139 'civicrm_subscription_history',
140 'civicrm_group_contact_cache',
141 'civicrm_group_nesting',
142 'civicrm_group_organization',
143 'civicrm_relationship',
144 'civicrm_mailing_event_subscribe',
145 'civicrm_mailing_event_confirm',
146 'civicrm_contribution_recur',
147 'civicrm_contribution_page',
148 'civicrm_contribution_widget',
149 'civicrm_activity',
150 'civicrm_activity_contact',
151 'civicrm_case_activity',
152 'civicrm_pledge',
153 'civicrm_price_field',
154 'civicrm_county',
155 'civicrm_entity_tag',
156 'civicrm_msg_template',
157 'civicrm_uf_group',
158 'civicrm_uf_field',
159 'civicrm_uf_join',
160 'civicrm_action_schedule',
161 'civicrm_action_log',
162 'civicrm_mailing',
163 'civicrm_mailing_group',
164 'civicrm_mailing_trackable_url',
165 'civicrm_mailing_job',
166 'civicrm_mailing_recipients',
167 'civicrm_mailing_spool',
168 'civicrm_mailing_event_queue',
169 'civicrm_mailing_event_bounce',
170 'civicrm_mailing_event_delivered',
171 'civicrm_mailing_event_forward',
172 'civicrm_mailing_event_opened',
173 'civicrm_mailing_event_reply',
174 'civicrm_mailing_event_trackable_url_open',
175 'civicrm_mailing_event_unsubscribe',
176 'civicrm_membership_type',
177 'civicrm_membership',
178 'civicrm_membership_block',
179 'civicrm_membership_log',
180 'civicrm_price_field_value',
181 'civicrm_line_item',
182 'civicrm_pcp_block',
183 'civicrm_address',
184 'civicrm_loc_block',
185 'civicrm_group_contact',
186 'civicrm_contribution',
187 'civicrm_contribution_product',
188 'civicrm_contribution_soft',
189 'civicrm_membership_payment',
190 'civicrm_event',
191 'civicrm_participant',
192 'civicrm_participant_payment',
193 'civicrm_events_in_carts',
194 'civicrm_pledge_payment',
195 )
196 );
197
198 // Insert test data
199 $op = new PHPUnit_Extensions_Database_Operation_Insert();
200 $op->execute($this->_dbconn,
201 $this->createFlatXMLDataSet(
202 dirname(__FILE__) . '/dataset/data.xml'
203 )
204 );
205 }
206
207 /**
208 * Test CRM_Member_Form_Membership::buildQuickForm()
209 */
210 //function testCRMMemberFormMembershipBuildQuickForm()
211 //{
212 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
213 //}
214
215 /**
216 * Test CRM_Member_Form_Membership::formRule() with a parameter
217 * that has an empty contact_select_id value
218 */
219 public function testFormRuleEmptyContact() {
220 $params = array(
221 'contact_select_id' => 0,
222 'membership_type_id' => array(),
223 );
224 $files = array();
225 $obj = new CRM_Member_Form_Membership;
226 $rc = $obj->formRule($params, $files, $obj);
227 $this->assertType('array', $rc,
228 'In line ' . __LINE__
229 );
230 $this->assertTrue(array_key_exists('membership_type_id', $rc),
231 'In line ' . __LINE__
232 );
233
234 $params['membership_type_id'] = array(1 => 3);
235 $rc = $obj->formRule($params, $files, $obj);
236 $this->assertType('array', $rc,
237 'In line ' . __LINE__
238 );
239 $this->assertTrue(array_key_exists('join_date', $rc),
240 'In line ' . __LINE__
241 );
242 }
243
244 /**
245 * Test CRM_Member_Form_Membership::formRule() with a parameter
246 * that has an start date before the join date and a rolling
247 * membership type
248 */
249 public function testFormRuleRollingEarlyStart() {
250 $unixNow = time();
251 $ymdNow = date('m/d/Y', $unixNow);
252 $unixYesterday = $unixNow - (24 * 60 * 60);
253 $ymdYesterday = date('m/d/Y', $unixYesterday);
254 $params = array(
255 'join_date' => $ymdNow,
256 'start_date' => $ymdYesterday,
257 'end_date' => '',
258 'membership_type_id' => array('23', '3'),
259 );
260 $files = array();
261 $obj = new CRM_Member_Form_Membership;
262 $rc = call_user_func(array('CRM_Member_Form_Membership', 'formRule'),
263 $params, $files, $obj
264 );
265 $this->assertType('array', $rc,
266 'In line ' . __LINE__
267 );
268 $this->assertTrue(array_key_exists('start_date', $rc),
269 'In line ' . __LINE__
270 );
271 }
272
273 /**
274 * Test CRM_Member_Form_Membership::formRule() with a parameter
275 * that has an end date before the start date and a rolling
276 * membership type
277 */
278 public function testFormRuleRollingEarlyEnd() {
279 $unixNow = time();
280 $ymdNow = date('m/d/Y', $unixNow);
281 $unixYesterday = $unixNow - (24 * 60 * 60);
282 $ymdYesterday = date('m/d/Y', $unixYesterday);
283 $params = array(
284 'join_date' => $ymdNow,
285 'start_date' => $ymdNow,
286 'end_date' => $ymdYesterday,
287 'membership_type_id' => array('23', '3'),
288 );
289 $files = array();
290 $obj = new CRM_Member_Form_Membership;
291 $rc = $obj->formRule($params, $files, $obj);
292 $this->assertType('array', $rc,
293 'In line ' . __LINE__
294 );
295 $this->assertTrue(array_key_exists('end_date', $rc),
296 'In line ' . __LINE__
297 );
298 }
299
300 /**
301 * Test CRM_Member_Form_Membership::formRule() with a parameter
302 * that has an end date but no start date and a rolling
303 * membership type
304 */
305 public function testFormRuleRollingEndNoStart() {
306 $unixNow = time();
307 $ymdNow = date('m/d/Y', $unixNow);
308 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
309 $ymdYearFromNow = date('m/d/Y', $unixYearFromNow);
310 $params = array(
311 'join_date' => $ymdNow,
312 'start_date' => '',
313 'end_date' => $ymdYearFromNow,
314 'membership_type_id' => array('23', '3'),
315 );
316 $files = array();
317 $obj = new CRM_Member_Form_Membership;
318 $rc = $obj->formRule($params, $files, $obj);
319 $this->assertType('array', $rc,
320 'In line ' . __LINE__
321 );
322 $this->assertTrue(array_key_exists('start_date', $rc),
323 'In line ' . __LINE__
324 );
325 }
326
327 /**
328 * Test CRM_Member_Form_Membership::formRule() with a parameter
329 * that has an end date and a lifetime membership type
330 */
331 public function testFormRuleRollingLifetimeEnd() {
332 $unixNow = time();
333 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
334 $params = array(
335 '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 $this->assertTrue(array_key_exists('status_id', $rc));
347 }
348
349 /**
350 * Test CRM_Member_Form_Membership::formRule() with a parameter
351 * that has an override and no status
352 */
353 public function testFormRuleOverrideNoStatus() {
354 $unixNow = time();
355 $unixYearFromNow = $unixNow + (365 * 24 * 60 * 60);
356 $params = array('join_date' => date('m/d/Y', $unixNow),
357 'membership_type_id' => array('23', '13'),
358 'is_override' => TRUE,
359 );
360 $files = array();
361 $obj = new CRM_Member_Form_Membership;
362 $rc = $obj->formRule($params, $files, $obj);
363 $this->assertType('array', $rc,
364 'In line ' . __LINE__
365 );
366 $this->assertTrue(array_key_exists('status_id', $rc),
367 'In line ' . __LINE__
368 );
369 }
370
371 /**
372 * Test CRM_Member_Form_Membership::formRule() with a join date
373 * of one month from now and a rolling membership type
374 */
375 public function testFormRuleRollingJoin1MonthFromNow() {
376 $unixNow = time();
377 $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
378 $params = array('join_date' => date('m/d/Y', $unix1MFmNow),
379 'start_date' => '',
380 'end_date' => '',
381 'membership_type_id' => array('23', '3'),
382 );
383 $files = array();
384 $obj = new CRM_Member_Form_Membership;
385 $rc = $obj->formRule($params, $files, $obj);
386
387 // Should have found no valid membership status
388 $this->assertType('array', $rc,
389 'In line ' . __LINE__
390 );
391 $this->assertTrue(array_key_exists('_qf_default', $rc),
392 'In line ' . __LINE__
393 );
394 }
395
396 /**
397 * Test CRM_Member_Form_Membership::formRule() with a join date
398 * of today and a rolling membership type
399 */
400 public function testFormRuleRollingJoinToday() {
401 $unixNow = time();
402 $params = array('join_date' => date('m/d/Y', $unixNow),
403 'start_date' => '',
404 'end_date' => '',
405 'membership_type_id' => array('23', '3'),
406 );
407 $files = array();
408 $obj = new CRM_Member_Form_Membership;
409 $rc = $obj->formRule($params, $files, $obj);
410
411 // Should have found New membership status
412 $this->assertTrue($rc, 'In line ' . __LINE__);
413 }
414
415 /**
416 * Test CRM_Member_Form_Membership::formRule() with a join date
417 * of one month ago and a rolling membership type
418 */
419 public function testFormRuleRollingJoin1MonthAgo() {
420 $unixNow = time();
421 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
422 $params = array('join_date' => date('m/d/Y', $unix1MAgo),
423 'start_date' => '',
424 'end_date' => '',
425 'membership_type_id' => array('23', '3'),
426 );
427 $files = array();
428 $obj = new CRM_Member_Form_Membership;
429 $rc = $obj->formRule($params, $files, $obj);
430
431 // Should have found New membership status
432 $this->assertTrue($rc, 'In line ' . __LINE__);
433 }
434
435 /**
436 * Test CRM_Member_Form_Membership::formRule() with a join date
437 * of six months ago and a rolling membership type
438 */
439 public function testFormRuleRollingJoin6MonthsAgo() {
440 $unixNow = time();
441 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
442 $params = array('join_date' => date('m/d/Y', $unix6MAgo),
443 'start_date' => '',
444 'end_date' => '',
445 'membership_type_id' => array('23', '3'),
446 );
447 $files = array();
448 $obj = new CRM_Member_Form_Membership;
449 $rc = $obj->formRule($params, $files, $obj);
450
451 // Should have found Current membership status
452 $this->assertTrue($rc, 'In line ' . __LINE__);
453 }
454
455 /**
456 * Test CRM_Member_Form_Membership::formRule() with a join date
457 * of one year+ ago and a rolling membership type
458 */
459 public function testFormRuleRollingJoin1YearAgo() {
460 $unixNow = time();
461 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
462 $params = array('join_date' => date('m/d/Y', $unix1YAgo),
463 'start_date' => '',
464 'end_date' => '',
465 'membership_type_id' => array('23', '3'),
466 );
467 $files = array();
468 $obj = new CRM_Member_Form_Membership;
469 $rc = $obj->formRule($params, $files, $obj);
470
471 // Should have found Grace membership status
472 $this->assertTrue($rc, 'In line ' . __LINE__);
473 }
474
475 /**
476 * Test CRM_Member_Form_Membership::formRule() with a join date
477 * of two years ago and a rolling membership type
478 */
479 public function testFormRuleRollingJoin2YearsAgo() {
480 $unixNow = time();
481 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
482 $params = array('join_date' => date('m/d/Y', $unix2YAgo),
483 'start_date' => '',
484 'end_date' => '',
485 'membership_type_id' => array('23', '3'),
486 );
487 $files = array();
488 $obj = new CRM_Member_Form_Membership;
489 $rc = $obj->formRule($params, $files, $obj);
490
491 // Should have found Expired membership status
492 $this->assertTrue($rc, 'In line ' . __LINE__);
493 }
494
495 /**
496 * Test CRM_Member_Form_Membership::formRule() with a join date
497 * of six months ago and a fixed membership type
498 */
499 public function testFormRuleFixedJoin6MonthsAgo() {
500 $unixNow = time();
501 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
502 $params = array('join_date' => date('m/d/Y', $unix6MAgo),
503 'start_date' => '',
504 'end_date' => '',
505 'membership_type_id' => array('23', '7'),
506 );
507 $files = array();
508 $obj = new CRM_Member_Form_Membership;
509 $rc = $obj->formRule($params, $files, $obj);
510
511 // Should have found Current membership status
512 $this->assertTrue($rc, 'In line ' . __LINE__);
513 }
514 }
515 // class CRM_Member_Form_MembershipTest