3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * File for the MembershipTest class
33 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
37 * Include class definitions
39 require_once 'CiviTest/CiviUnitTestCase.php';
41 require_once 'HTML/QuickForm/Page.php';
44 * Test CRM_Member_Form_Membership functions.
48 class CRM_Member_Form_MembershipRenewalTest
extends CiviUnitTestCase
{
51 * Assume empty database with just civicrm_data.
53 protected $_individualId;
54 protected $_contribution;
55 protected $_financialTypeId = 1;
56 protected $_apiversion;
57 protected $_entity = 'Membership';
59 protected $_ids = array();
60 protected $_paymentProcessorID;
63 * Membership type ID for annual fixed membership.
67 protected $membershipTypeAnnualFixedID;
70 * Parameters to create payment processor.
74 protected $_processorParams = array();
77 * ID of created membership.
81 protected $_membershipID;
84 * Payment instrument mapping.
88 protected $paymentInstruments = array();
91 * Test setup for every test.
93 * Connect to the database, truncate the tables that will be used
94 * and redirect stdin to a temporary file.
96 public function setUp() {
97 $this->_apiversion
= 3;
100 $this->_individualId
= $this->individualCreate();
101 $processor = $this->processorCreate();
102 $this->_paymentProcessorID
= $processor->id
;
104 $op = new PHPUnit_Extensions_Database_Operation_Insert();
105 $op->execute($this->_dbconn
,
106 $this->createFlatXMLDataSet(
107 dirname(__FILE__
) . '/dataset/data.xml'
110 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array(
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,
122 $this->membershipTypeAnnualFixedID
= $membershipTypeAnnualFixed['id'];
123 $membership = $this->callAPISuccess('Membership', 'create', array(
124 'contact_id' => $this->_individualId
,
125 'membership_type_id' => $this->membershipTypeAnnualFixedID
,
127 $this->_membershipID
= $membership['id'];
129 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
130 $this->paymentInstruments
= $instruments['values'];
134 * Clean up after each test.
136 public function tearDown() {
137 $this->quickCleanUpFinancialEntities();
140 'civicrm_relationship',
141 'civicrm_membership_type',
142 'civicrm_membership',
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));
153 * Test the submit function of the membership form.
155 public function testSubmit() {
156 $form = $this->getForm();
157 $this->createLoggedInUser();
159 'cid' => $this->_individualId
,
160 'join_date' => date('m/d/Y', time()),
163 // This format reflects the 23 being the organisation & the 25 being the type.
164 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
169 'total_amount' => '50.00',
170 //Member dues, see data.xml
171 'financial_type_id' => '2',
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',
179 'credit_card_exp_date' => array(
181 'Y' => '2024', // TODO: Future proof
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',
192 $form->_contactID
= $this->_individualId
;
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
,
202 $this->callAPISuccessGetCount('LineItem', array(
203 'entity_id' => $membership['id'],
204 'entity_table' => 'civicrm_membership',
205 'contribution_id' => $contribution['id'],
210 * Test the submit function of the membership form.
212 public function testSubmitRecur() {
213 $form = $this->getForm();
215 $this->callAPISuccess('MembershipType', 'create', array(
216 'id' => $this->membershipTypeAnnualFixedID
,
217 'duration_unit' => 'month',
218 'duration_interval' => 1,
219 'auto_renew' => TRUE,
222 $this->createLoggedInUser();
224 'cid' => $this->_individualId
,
226 'join_date' => date('m/d/Y', time()),
230 // This format reflects the 23 being the organisation & the 25 being the type.
231 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
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',
246 'credit_card_exp_date' => array(
248 'Y' => '2019', // TODO: Future proof
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',
259 $form->_mode
= 'test';
260 $form->_contactID
= $this->_individualId
;
262 $form->testSubmit($params);
263 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
264 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId
), 1);
265 $contribution = $this->callAPISuccess('Contribution', 'get', array(
266 'contact_id' => $this->_individualId
,
270 $this->callAPISuccessGetCount('LineItem', array(
271 'entity_id' => $membership['id'],
272 'entity_table' => 'civicrm_membership',
273 'contribution_id' => $contribution['id'],
276 $this->callAPISuccessGetSingle('address', array(
277 'contact_id' => $this->_individualId
,
278 'street_address' => '10 Test St',
279 'postal_code' => 90210,
284 * Test the submit function of the membership form.
286 public function testSubmitRecurCompleteInstant() {
287 $form = $this->getForm();
289 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessorID
);
290 $processor->setDoDirectPaymentResult(array(
291 'payment_status_id' => 1,
292 'trxn_id' => 'kettles boil water',
296 $this->callAPISuccess('MembershipType', 'create', array(
297 'id' => $this->membershipTypeAnnualFixedID
,
298 'duration_unit' => 'month',
299 'duration_interval' => 1,
300 'auto_renew' => TRUE,
302 $this->createLoggedInUser();
305 $form->_contactID
= $this->_individualId
;
306 $params = $this->getBaseSubmitParams();
307 $form->_mode
= 'test';
309 $form->testSubmit($params);
310 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId
));
311 $contributionRecur = $this->callAPISuccessGetSingle('ContributionRecur', array('contact_id' => $this->_individualId
));
312 $this->assertEquals($contributionRecur['id'], $membership['contribution_recur_id']);
314 $contribution = $this->callAPISuccess('Contribution', 'getsingle', array(
315 'contact_id' => $this->_individualId
,
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']);
324 $this->callAPISuccessGetCount('LineItem', array(
325 'entity_id' => $membership['id'],
326 'entity_table' => 'civicrm_membership',
327 'contribution_id' => $contribution['id'],
333 * Test the submit function of the membership form.
335 public function testSubmitPayLater() {
336 $form = $this->getForm(NULL);
337 $this->createLoggedInUser();
338 $originalMembership = $this->callAPISuccessGetSingle('membership', array());
340 'cid' => $this->_individualId
,
341 'join_date' => date('m/d/Y', time()),
344 // This format reflects the 23 being the organisation & the 25 being the type.
345 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
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,
361 'contribution_status_id' => 2,
363 $form->_contactID
= $this->_individualId
;
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,
372 $this->assertEquals($contribution['trxn_id'], 777);
374 $this->callAPISuccessGetCount('LineItem', array(
375 'entity_id' => $membership['id'],
376 'entity_table' => 'civicrm_membership',
377 'contribution_id' => $contribution['id'],
382 * Test the submit function of the membership form.
384 public function testSubmitPayLaterWithBilling() {
385 $form = $this->getForm(NULL);
386 $this->createLoggedInUser();
387 $originalMembership = $this->callAPISuccessGetSingle('membership', array());
389 'cid' => $this->_individualId
,
390 'join_date' => date('m/d/Y', time()),
393 // This format reflects the 23 being the organisation & the 25 being the type.
394 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
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,
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',
419 $form->_contactID
= $this->_individualId
;
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,
428 $this->assertEquals($contribution['trxn_id'], 777);
430 $this->callAPISuccessGetCount('LineItem', array(
431 'entity_id' => $membership['id'],
432 'entity_table' => 'civicrm_membership',
433 'contribution_id' => $contribution['id'],
435 $this->callAPISuccessGetSingle('address', array(
436 'contact_id' => $this->_individualId
,
437 'street_address' => '10 Test St',
438 'postal_code' => 90210,
443 * Test the submit function of the membership form.
445 public function testSubmitComplete() {
446 $form = $this->getForm(NULL);
447 $this->createLoggedInUser();
448 $originalMembership = $this->callAPISuccessGetSingle('membership', array());
450 'cid' => $this->_individualId
,
451 'join_date' => date('m/d/Y', time()),
454 // This format reflects the 23 being the organisation & the 25 being the type.
455 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
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,
471 'contribution_status_id' => 1,
474 $form->_contactID
= $this->_individualId
;
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,
485 $this->assertEquals($contribution['trxn_id'], 777);
486 $this->assertEquals(.5, $contribution['fee_amount']);
487 $this->callAPISuccessGetCount('LineItem', array(
488 'entity_id' => $membership['id'],
489 'entity_table' => 'civicrm_membership',
490 'contribution_id' => $contribution['id'],
495 * Get a membership form object.
497 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
499 * @param string $mode
501 * @return \CRM_Member_Form_MembershipRenewal
503 protected function getForm($mode = 'test') {
504 $form = new CRM_Member_Form_MembershipRenewal();
505 $_SERVER['REQUEST_METHOD'] = 'GET';
506 $form->controller
= new CRM_Core_Controller();
508 $form->_mode
= $mode;
509 $form->_id
= $this->_membershipID
;
515 * Get some re-usable parameters for the submit function.
519 protected function getBaseSubmitParams() {
521 'cid' => $this->_individualId
,
523 'join_date' => date('m/d/Y', time()),
527 // This format reflects the 23 being the organisation & the 25 being the type.
528 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID
),
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',
543 'credit_card_exp_date' => array(
545 'Y' => '2019', // TODO: Future proof
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',