Ian province abbreviation patch - issue 724
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / MembershipRenewalTest.php
CommitLineData
4187b12f
EM
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
4187b12f
EM
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 */
27
28/**
29 * File for the MembershipTest class
30 *
31 * (PHP 5)
32 *
33 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
34 */
35
36/**
37 * Include class definitions
38 */
39require_once 'CiviTest/CiviUnitTestCase.php';
40
41require_once 'HTML/QuickForm/Page.php';
42
43/**
44 * Test CRM_Member_Form_Membership functions.
45 *
46 * @package CiviCRM
47 */
bce3b15b 48class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
4187b12f
EM
49
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 * Membership type ID for annual fixed membership.
64 *
65 * @var int
66 */
67 protected $membershipTypeAnnualFixedID;
68
69 /**
70 * Parameters to create payment processor.
71 *
72 * @var array
73 */
74 protected $_processorParams = array();
75
76 /**
77 * ID of created membership.
78 *
79 * @var int
80 */
81 protected $_membershipID;
82
83 /**
84 * Payment instrument mapping.
85 *
86 * @var array
87 */
88 protected $paymentInstruments = array();
89
90 /**
91 * Test setup for every test.
92 *
93 * Connect to the database, truncate the tables that will be used
94 * and redirect stdin to a temporary file.
95 */
96 public function setUp() {
97 $this->_apiversion = 3;
98 parent::setUp();
99
100 $this->_individualId = $this->individualCreate();
101 $processor = $this->processorCreate();
102 $this->_paymentProcessorID = $processor->id;
103 // Insert test data.
104 $op = new PHPUnit_Extensions_Database_Operation_Insert();
105 $op->execute($this->_dbconn,
106 $this->createFlatXMLDataSet(
107 dirname(__FILE__) . '/dataset/data.xml'
108 )
109 );
110 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array(
111 'domain_id' => 1,
112 'name' => "AnnualFixed",
113 'member_of_contact_id' => 23,
114 'duration_unit' => "year",
115 'duration_interval' => 1,
116 'period_type' => "fixed",
117 'fixed_period_start_day' => "101",
118 'fixed_period_rollover_day' => "1231",
119 'relationship_type_id' => 20,
120 'financial_type_id' => 2,
121 ));
122 $this->membershipTypeAnnualFixedID = $membershipTypeAnnualFixed['id'];
123 $membership = $this->callAPISuccess('Membership', 'create', array(
124 'contact_id' => $this->_individualId,
125 'membership_type_id' => $this->membershipTypeAnnualFixedID,
126 ));
127 $this->_membershipID = $membership['id'];
128
129 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
130 $this->paymentInstruments = $instruments['values'];
131 }
132
133 /**
134 * Clean up after each test.
135 */
136 public function tearDown() {
137 $this->quickCleanUpFinancialEntities();
138 $this->quickCleanup(
139 array(
140 'civicrm_relationship',
141 'civicrm_membership_type',
142 'civicrm_membership',
143 'civicrm_uf_match',
0816949d 144 'civicrm_address',
4187b12f
EM
145 )
146 );
147 $this->callAPISuccess('contact', 'delete', array('id' => 17, 'skip_undelete' => TRUE));
148 $this->callAPISuccess('contact', 'delete', array('id' => 23, 'skip_undelete' => TRUE));
149 $this->callAPISuccess('relationship_type', 'delete', array('id' => 20));
150 }
151
4187b12f
EM
152 /**
153 * Test the submit function of the membership form.
154 */
155 public function testSubmit() {
156 $form = $this->getForm();
157 $this->createLoggedInUser();
158 $params = array(
159 'cid' => $this->_individualId,
160 'join_date' => date('m/d/Y', time()),
161 'start_date' => '',
162 'end_date' => '',
163 // This format reflects the 23 being the organisation & the 25 being the type.
164 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
165 'auto_renew' => '0',
166 'max_related' => '',
167 'num_terms' => '1',
168 'source' => '',
169 'total_amount' => '50.00',
1a00ea3c
EM
170 //Member dues, see data.xml
171 'financial_type_id' => '2',
4187b12f
EM
172 'soft_credit_type_id' => '',
173 'soft_credit_contact_id' => '',
174 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
175 'receipt_text_signup' => 'Thank you text',
176 'payment_processor_id' => $this->_paymentProcessorID,
177 'credit_card_number' => '4111111111111111',
178 'cvv2' => '123',
179 'credit_card_exp_date' => array(
180 'M' => '9',
181 'Y' => '2024', // TODO: Future proof
182 ),
183 'credit_card_type' => 'Visa',
184 'billing_first_name' => 'Test',
185 'billing_middlename' => 'Last',
186 'billing_street_address-5' => '10 Test St',
187 'billing_city-5' => 'Test',
188 'billing_state_province_id-5' => '1003',
189 'billing_postal_code-5' => '90210',
190 'billing_country_id-5' => '1228',
191 );
192 $form->_contactID = $this->_individualId;
193
194 $form->testSubmit($params);
195 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
196 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 0);
197 $contribution = $this->callAPISuccess('Contribution', 'get', array(
198 'contact_id' => $this->_individualId,
199 'is_test' => TRUE,
200 ));
201
202 $this->callAPISuccessGetCount('LineItem', array(
203 'entity_id' => $membership['id'],
204 'entity_table' => 'civicrm_membership',
205 'contribution_id' => $contribution['id'],
206 ), 1);
207 }
208
209 /**
210 * Test the submit function of the membership form.
211 */
212 public function testSubmitRecur() {
213 $form = $this->getForm();
214
215 $this->callAPISuccess('MembershipType', 'create', array(
216 'id' => $this->membershipTypeAnnualFixedID,
217 'duration_unit' => 'month',
218 'duration_interval' => 1,
219 'auto_renew' => TRUE,
220 ));
221 $form->preProcess();
222 $this->createLoggedInUser();
223 $params = array(
224 'cid' => $this->_individualId,
225 'price_set_id' => 0,
226 'join_date' => date('m/d/Y', time()),
227 'start_date' => '',
228 'end_date' => '',
229 'campaign_id' => '',
230 // This format reflects the 23 being the organisation & the 25 being the type.
231 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
232 'auto_renew' => '1',
233 'is_recur' => 1,
234 'max_related' => 0,
235 'num_terms' => '1',
236 'source' => '',
237 'total_amount' => '77.00',
238 'financial_type_id' => '2', //Member dues, see data.xml
239 'soft_credit_type_id' => 11,
240 'soft_credit_contact_id' => '',
241 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
242 'receipt_text' => 'Thank you text',
243 'payment_processor_id' => $this->_paymentProcessorID,
244 'credit_card_number' => '4111111111111111',
245 'cvv2' => '123',
246 'credit_card_exp_date' => array(
247 'M' => '9',
248 'Y' => '2019', // TODO: Future proof
249 ),
250 'credit_card_type' => 'Visa',
251 'billing_first_name' => 'Test',
252 'billing_middlename' => 'Last',
253 'billing_street_address-5' => '10 Test St',
254 'billing_city-5' => 'Test',
255 'billing_state_province_id-5' => '1003',
256 'billing_postal_code-5' => '90210',
257 'billing_country_id-5' => '1228',
258 );
259 $form->_mode = 'test';
260 $form->_contactID = $this->_individualId;
261
262 $form->testSubmit($params);
263 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
1a00ea3c 264 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 1);
4187b12f
EM
265 $contribution = $this->callAPISuccess('Contribution', 'get', array(
266 'contact_id' => $this->_individualId,
267 'is_test' => TRUE,
268 ));
269
270 $this->callAPISuccessGetCount('LineItem', array(
271 'entity_id' => $membership['id'],
272 'entity_table' => 'civicrm_membership',
273 'contribution_id' => $contribution['id'],
274 ), 1);
275
0816949d
EM
276 $this->callAPISuccessGetSingle('address', array(
277 'contact_id' => $this->_individualId,
278 'street_address' => '10 Test St',
279 'postal_code' => 90210,
280 ));
281 }
282
b3f6108a 283 /**
284 * Test the submit function of the membership form.
285 */
286 public function testSubmitRecurCompleteInstant() {
287 $form = $this->getForm();
288
289 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessorID);
290 $processor->setDoDirectPaymentResult(array(
291 'payment_status_id' => 1,
292 'trxn_id' => 'kettles boil water',
293 'fee_amount' => .29,
294 ));
295
296 $this->callAPISuccess('MembershipType', 'create', array(
297 'id' => $this->membershipTypeAnnualFixedID,
298 'duration_unit' => 'month',
299 'duration_interval' => 1,
300 'auto_renew' => TRUE,
301 ));
302 $this->createLoggedInUser();
303 $form->preProcess();
304
305 $form->_contactID = $this->_individualId;
306 $params = $this->getBaseSubmitParams();
307 $form->_mode = 'test';
308
309 $form->testSubmit($params);
310 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
3a1f9fd3 311 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array('contact_id' => $this->_individualId));
312 $this->assertEquals($contributionRecur['id'], $membership['contribution_recur_id']);
b3f6108a 313
314 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array(
315 'contact_id' => $this->_individualId,
316 'is_test' => TRUE,
317 ));
318
319 $this->assertEquals('kettles boil water', $contribution['trxn_id']);
320 $this->assertEquals(.29, $contribution['fee_amount']);
321 $this->assertEquals(78, $contribution['total_amount']);
322 $this->assertEquals(77.71, $contribution['net_amount']);
323
324 $this->callAPISuccessGetCount('LineItem', array(
325 'entity_id' => $membership['id'],
326 'entity_table' => 'civicrm_membership',
327 'contribution_id' => $contribution['id'],
328 ), 1);
329
330 }
3a1f9fd3 331
0816949d
EM
332 /**
333 * Test the submit function of the membership form.
334 */
335 public function testSubmitPayLater() {
336 $form = $this->getForm(NULL);
337 $this->createLoggedInUser();
338 $originalMembership = $this->callAPISuccessGetSingle('membership', array());
339 $params = array(
340 'cid' => $this->_individualId,
341 'join_date' => date('m/d/Y', time()),
342 'start_date' => '',
343 'end_date' => '',
344 // This format reflects the 23 being the organisation & the 25 being the type.
345 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
346 'auto_renew' => '0',
347 'max_related' => '',
348 'num_terms' => '2',
349 'source' => '',
350 'total_amount' => '50.00',
351 //Member dues, see data.xml
352 'financial_type_id' => '2',
353 'soft_credit_type_id' => '',
354 'soft_credit_contact_id' => '',
355 'payment_instrument_id' => 4,
356 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
357 'receipt_text_signup' => 'Thank you text',
358 'payment_processor_id' => $this->_paymentProcessorID,
359 'record_contribution' => TRUE,
360 'trxn_id' => 777,
361 'contribution_status_id' => 2,
362 );
363 $form->_contactID = $this->_individualId;
364
365 $form->testSubmit($params);
366 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
367 $this->assertEquals(strtotime($membership['end_date']), strtotime($originalMembership['end_date']));
368 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
369 'contact_id' => $this->_individualId,
370 'contribution_status_id' => 2,
371 ));
372 $this->assertEquals($contribution['trxn_id'], 777);
373
374 $this->callAPISuccessGetCount('LineItem', array(
375 'entity_id' => $membership['id'],
376 'entity_table' => 'civicrm_membership',
377 'contribution_id' => $contribution['id'],
378 ), 1);
379 }
380
381 /**
382 * Test the submit function of the membership form.
383 */
384 public function testSubmitPayLaterWithBilling() {
385 $form = $this->getForm(NULL);
386 $this->createLoggedInUser();
387 $originalMembership = $this->callAPISuccessGetSingle('membership', array());
388 $params = array(
389 'cid' => $this->_individualId,
390 'join_date' => date('m/d/Y', time()),
391 'start_date' => '',
392 'end_date' => '',
393 // This format reflects the 23 being the organisation & the 25 being the type.
394 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
395 'auto_renew' => '0',
396 'max_related' => '',
397 'num_terms' => '2',
398 'source' => '',
399 'total_amount' => '50.00',
400 //Member dues, see data.xml
401 'financial_type_id' => '2',
402 'soft_credit_type_id' => '',
403 'soft_credit_contact_id' => '',
404 'payment_instrument_id' => 4,
405 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
406 'receipt_text_signup' => 'Thank you text',
407 'payment_processor_id' => $this->_paymentProcessorID,
408 'record_contribution' => TRUE,
409 'trxn_id' => 777,
410 'contribution_status_id' => 2,
411 'billing_first_name' => 'Test',
412 'billing_middlename' => 'Last',
413 'billing_street_address-5' => '10 Test St',
414 'billing_city-5' => 'Test',
415 'billing_state_province_id-5' => '1003',
416 'billing_postal_code-5' => '90210',
417 'billing_country_id-5' => '1228',
418 );
419 $form->_contactID = $this->_individualId;
420
421 $form->testSubmit($params);
422 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
423 $this->assertEquals(strtotime($membership['end_date']), strtotime($originalMembership['end_date']));
424 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
425 'contact_id' => $this->_individualId,
426 'contribution_status_id' => 2,
427 ));
428 $this->assertEquals($contribution['trxn_id'], 777);
429
430 $this->callAPISuccessGetCount('LineItem', array(
431 'entity_id' => $membership['id'],
432 'entity_table' => 'civicrm_membership',
433 'contribution_id' => $contribution['id'],
434 ), 1);
435 $this->callAPISuccessGetSingle('address', array(
436 'contact_id' => $this->_individualId,
437 'street_address' => '10 Test St',
438 'postal_code' => 90210,
439 ));
440 }
441
442 /**
443 * Test the submit function of the membership form.
444 */
445 public function testSubmitComplete() {
446 $form = $this->getForm(NULL);
447 $this->createLoggedInUser();
448 $originalMembership = $this->callAPISuccessGetSingle('membership', array());
449 $params = array(
450 'cid' => $this->_individualId,
451 'join_date' => date('m/d/Y', time()),
452 'start_date' => '',
453 'end_date' => '',
454 // This format reflects the 23 being the organisation & the 25 being the type.
455 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
456 'auto_renew' => '0',
457 'max_related' => '',
458 'num_terms' => '2',
459 'source' => '',
460 'total_amount' => '50.00',
461 //Member dues, see data.xml
462 'financial_type_id' => '2',
463 'soft_credit_type_id' => '',
464 'soft_credit_contact_id' => '',
465 'payment_instrument_id' => 4,
466 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
467 'receipt_text_signup' => 'Thank you text',
468 'payment_processor_id' => $this->_paymentProcessorID,
469 'record_contribution' => TRUE,
470 'trxn_id' => 777,
471 'contribution_status_id' => 1,
77623a96 472 'fee_amount' => .5,
0816949d
EM
473 );
474 $form->_contactID = $this->_individualId;
475
476 $form->testSubmit($params);
477 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
478 $this->assertEquals(strtotime($membership['end_date']), strtotime('+ 2 years',
479 strtotime($originalMembership['end_date'])));
480 $contribution = $this->callAPISuccessGetSingle('Contribution', array(
481 'contact_id' => $this->_individualId,
482 'contribution_status_id' => 1,
483 ));
484
485 $this->assertEquals($contribution['trxn_id'], 777);
77623a96 486 $this->assertEquals(.5, $contribution['fee_amount']);
0816949d
EM
487 $this->callAPISuccessGetCount('LineItem', array(
488 'entity_id' => $membership['id'],
489 'entity_table' => 'civicrm_membership',
490 'contribution_id' => $contribution['id'],
491 ), 1);
4187b12f
EM
492 }
493
494 /**
495 * Get a membership form object.
496 *
497 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
498 *
0816949d
EM
499 * @param string $mode
500 *
4187b12f
EM
501 * @return \CRM_Member_Form_MembershipRenewal
502 */
0816949d 503 protected function getForm($mode = 'test') {
4187b12f
EM
504 $form = new CRM_Member_Form_MembershipRenewal();
505 $_SERVER['REQUEST_METHOD'] = 'GET';
506 $form->controller = new CRM_Core_Controller();
507 $form->_bltID = 5;
0816949d 508 $form->_mode = $mode;
4187b12f
EM
509 $form->_id = $this->_membershipID;
510 $form->preProcess();
511 return $form;
512 }
513
b3f6108a 514 /**
515 * Get some re-usable parameters for the submit function.
516 *
517 * @return array
518 */
519 protected function getBaseSubmitParams() {
520 $params = array(
521 'cid' => $this->_individualId,
522 'price_set_id' => 0,
523 'join_date' => date('m/d/Y', time()),
524 'start_date' => '',
525 'end_date' => '',
526 'campaign_id' => '',
527 // This format reflects the 23 being the organisation & the 25 being the type.
528 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
529 'auto_renew' => '1',
530 'is_recur' => 1,
531 'max_related' => 0,
532 'num_terms' => '1',
533 'source' => '',
534 'total_amount' => '78.00',
535 'financial_type_id' => '2', //Member dues, see data.xml
536 'soft_credit_type_id' => 11,
537 'soft_credit_contact_id' => '',
538 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
539 'receipt_text' => 'Thank you text',
540 'payment_processor_id' => $this->_paymentProcessorID,
541 'credit_card_number' => '4111111111111111',
542 'cvv2' => '123',
543 'credit_card_exp_date' => array(
544 'M' => '9',
545 'Y' => '2019', // TODO: Future proof
546 ),
547 'credit_card_type' => 'Visa',
548 'billing_first_name' => 'Test',
549 'billing_middlename' => 'Last',
550 'billing_street_address-5' => '10 Test St',
551 'billing_city-5' => 'Test',
552 'billing_state_province_id-5' => '1003',
553 'billing_postal_code-5' => '90210',
554 'billing_country_id-5' => '1228',
555 );
556 return $params;
557 }
558
4187b12f 559}