commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / 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(
357 '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 public function testFormRuleRollingJoin1MonthFromNow() {
377 $unixNow = time();
378 $unix1MFmNow = $unixNow + (31 * 24 * 60 * 60);
379 $params = array(
380 'join_date' => date('m/d/Y', $unix1MFmNow),
381 'start_date' => '',
382 'end_date' => '',
383 'membership_type_id' => array('23', '3'),
384 );
385 $files = array();
386 $obj = new CRM_Member_Form_Membership();
387 $rc = $obj->formRule($params, $files, $obj);
388
389 // Should have found no valid membership status
390 $this->assertType('array', $rc,
391 'In line ' . __LINE__
392 );
393 $this->assertTrue(array_key_exists('_qf_default', $rc),
394 'In line ' . __LINE__
395 );
396 }
397
398 /**
399 * Test CRM_Member_Form_Membership::formRule() with a join date
400 * of today and a rolling membership type
401 */
402 public function testFormRuleRollingJoinToday() {
403 $unixNow = time();
404 $params = array(
405 'join_date' => date('m/d/Y', $unixNow),
406 'start_date' => '',
407 'end_date' => '',
408 'membership_type_id' => array('23', '3'),
409 );
410 $files = array();
411 $obj = new CRM_Member_Form_Membership();
412 $rc = $obj->formRule($params, $files, $obj);
413
414 // Should have found New membership status
415 $this->assertTrue($rc, 'In line ' . __LINE__);
416 }
417
418 /**
419 * Test CRM_Member_Form_Membership::formRule() with a join date
420 * of one month ago and a rolling membership type
421 */
422 public function testFormRuleRollingJoin1MonthAgo() {
423 $unixNow = time();
424 $unix1MAgo = $unixNow - (31 * 24 * 60 * 60);
425 $params = array(
426 '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 public function testFormRuleRollingJoin6MonthsAgo() {
444 $unixNow = time();
445 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
446 $params = array(
447 'join_date' => date('m/d/Y', $unix6MAgo),
448 'start_date' => '',
449 'end_date' => '',
450 'membership_type_id' => array('23', '3'),
451 );
452 $files = array();
453 $obj = new CRM_Member_Form_Membership();
454 $rc = $obj->formRule($params, $files, $obj);
455
456 // Should have found Current membership status
457 $this->assertTrue($rc, 'In line ' . __LINE__);
458 }
459
460 /**
461 * Test CRM_Member_Form_Membership::formRule() with a join date
462 * of one year+ ago and a rolling membership type
463 */
464 public function testFormRuleRollingJoin1YearAgo() {
465 $unixNow = time();
466 $unix1YAgo = $unixNow - (370 * 24 * 60 * 60);
467 $params = array(
468 'join_date' => date('m/d/Y', $unix1YAgo),
469 'start_date' => '',
470 'end_date' => '',
471 'membership_type_id' => array('23', '3'),
472 );
473 $files = array();
474 $obj = new CRM_Member_Form_Membership();
475 $rc = $obj->formRule($params, $files, $obj);
476
477 // Should have found Grace membership status
478 $this->assertTrue($rc, 'In line ' . __LINE__);
479 }
480
481 /**
482 * Test CRM_Member_Form_Membership::formRule() with a join date
483 * of two years ago and a rolling membership type
484 */
485 public function testFormRuleRollingJoin2YearsAgo() {
486 $unixNow = time();
487 $unix2YAgo = $unixNow - (2 * 365 * 24 * 60 * 60);
488 $params = array(
489 'join_date' => date('m/d/Y', $unix2YAgo),
490 'start_date' => '',
491 'end_date' => '',
492 'membership_type_id' => array('23', '3'),
493 );
494 $files = array();
495 $obj = new CRM_Member_Form_Membership();
496 $rc = $obj->formRule($params, $files, $obj);
497
498 // Should have found Expired membership status
499 $this->assertTrue($rc, 'In line ' . __LINE__);
500 }
501
502 /**
503 * Test CRM_Member_Form_Membership::formRule() with a join date
504 * of six months ago and a fixed membership type
505 */
506 public function testFormRuleFixedJoin6MonthsAgo() {
507 $unixNow = time();
508 $unix6MAgo = $unixNow - (180 * 24 * 60 * 60);
509 $params = array(
510 'join_date' => date('m/d/Y', $unix6MAgo),
511 'start_date' => '',
512 'end_date' => '',
513 'membership_type_id' => array('23', '7'),
514 );
515 $files = array();
516 $obj = new CRM_Member_Form_Membership();
517 $rc = $obj->formRule($params, $files, $obj);
518
519 // Should have found Current membership status
520 $this->assertTrue($rc, 'In line ' . __LINE__);
521 }
522
523 }
524 // class CRM_Member_Form_MembershipTest