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