3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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 * Test APIv3 civicrm_contribute_recur* functions
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
35 class api_v3_ContributionPageTest
extends CiviUnitTestCase
{
36 protected $_apiversion = 3;
37 protected $testAmount = 34567;
40 protected $contactIds = array();
41 protected $_entity = 'contribution_page';
42 protected $contribution_result = NULL;
43 protected $_priceSetParams = array();
45 * Payment processor details.
48 protected $_paymentProcessor = array();
57 protected $_ids = array();
60 public $DBResetRequired = TRUE;
62 public function setUp() {
64 $this->contactIds
[] = $this->individualCreate();
65 $this->params
= array(
66 'title' => "Test Contribution Page",
67 'financial_type_id' => 1,
69 'goal_amount' => $this->testAmount
,
71 'is_monetary' => TRUE,
72 'is_email_receipt' => TRUE,
73 'receipt_from_email' => 'yourconscience@donate.com',
74 'receipt_from_name' => 'Ego Freud',
77 $this->_priceSetParams
= array(
78 'is_quick_config' => 1,
79 'extends' => 'CiviContribute',
80 'financial_type_id' => 'Donation',
85 public function tearDown() {
86 foreach ($this->contactIds
as $id) {
87 $this->callAPISuccess('contact', 'delete', array('id' => $id));
89 $this->quickCleanUpFinancialEntities();
92 public function testCreateContributionPage() {
93 $result = $this->callAPIAndDocument($this->_entity
, 'create', $this->params
, __FUNCTION__
, __FILE__
);
94 $this->assertEquals(1, $result['count']);
95 $this->assertNotNull($result['values'][$result['id']]['id']);
96 $this->getAndCheck($this->params
, $result['id'], $this->_entity
);
99 public function testGetBasicContributionPage() {
100 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
101 $this->id
= $createResult['id'];
104 'financial_type_id' => 1,
106 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
107 $this->assertEquals(1, $getResult['count']);
110 public function testGetContributionPageByAmount() {
111 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
112 $this->id
= $createResult['id'];
114 'amount' => '' . $this->testAmount
, // 3456
116 'financial_type_id' => 1,
118 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
119 $this->assertEquals(1, $getResult['count']);
122 public function testDeleteContributionPage() {
123 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
124 $deleteParams = array('id' => $createResult['id']);
125 $this->callAPIAndDocument($this->_entity
, 'delete', $deleteParams, __FUNCTION__
, __FILE__
);
126 $checkDeleted = $this->callAPISuccess($this->_entity
, 'get', array());
127 $this->assertEquals(0, $checkDeleted['count']);
130 public function testGetFieldsContributionPage() {
131 $result = $this->callAPISuccess($this->_entity
, 'getfields', array('action' => 'create'));
132 $this->assertEquals(12, $result['values']['start_date']['type']);
137 * Test form submission with basic price set.
139 public function testSubmit() {
140 $this->setUpContributionPage();
141 $priceFieldID = reset($this->_ids
['price_field']);
142 $priceFieldValueID = reset($this->_ids
['price_field_value']);
143 $submitParams = array(
144 'price_' . $priceFieldID => $priceFieldValueID,
145 'id' => (int) $this->_ids
['contribution_page'],
149 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
150 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
151 //assert non-deductible amount
152 $this->assertEquals(5.00, $contribution['non_deductible_amount']);
156 * Test form submission with billing first & last name where the contact does NOT
157 * otherwise have one.
159 public function testSubmitNewBillingNameData() {
160 $this->setUpContributionPage();
161 $contact = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => 'wonderwoman@amazon.com'));
162 $priceFieldID = reset($this->_ids
['price_field']);
163 $priceFieldValueID = reset($this->_ids
['price_field_value']);
164 $submitParams = array(
165 'price_' . $priceFieldID => $priceFieldValueID,
166 'id' => (int) $this->_ids
['contribution_page'],
168 'billing_first_name' => 'Wonder',
169 'billing_last_name' => 'Woman',
170 'contactID' => $contact['id'],
171 'email' => 'wonderwoman@amazon.com',
174 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
175 $contact = $this->callAPISuccess('Contact', 'get', array(
176 'id' => $contact['id'],
184 $this->assertEquals(array(
185 'first_name' => 'Wonder',
186 'last_name' => 'Woman',
187 'display_name' => 'Wonder Woman',
188 'sort_name' => 'Woman, Wonder',
189 'id' => $contact['id'],
190 'contact_id' => $contact['id'],
191 ), $contact['values'][$contact['id']]);
196 * Test form submission with billing first & last name where the contact does
197 * otherwise have one and should not be overwritten.
199 public function testSubmitNewBillingNameDoNotOverwrite() {
200 $this->setUpContributionPage();
201 $contact = $this->callAPISuccess('Contact', 'create', array(
202 'contact_type' => 'Individual',
203 'email' => 'wonderwoman@amazon.com',
204 'first_name' => 'Super',
205 'last_name' => 'Boy',
207 $priceFieldID = reset($this->_ids
['price_field']);
208 $priceFieldValueID = reset($this->_ids
['price_field_value']);
209 $submitParams = array(
210 'price_' . $priceFieldID => $priceFieldValueID,
211 'id' => (int) $this->_ids
['contribution_page'],
213 'billing_first_name' => 'Wonder',
214 'billing_last_name' => 'Woman',
215 'contactID' => $contact['id'],
216 'email' => 'wonderwoman@amazon.com',
219 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
220 $contact = $this->callAPISuccess('Contact', 'get', array(
221 'id' => $contact['id'],
229 $this->assertEquals(array(
230 'first_name' => 'Super',
231 'last_name' => 'Boy',
232 'display_name' => 'Super Boy',
233 'sort_name' => 'Boy, Super',
234 'id' => $contact['id'],
235 'contact_id' => $contact['id'],
236 ), $contact['values'][$contact['id']]);
241 * Test process with instant payment when more than one configured for the page.
245 public function testSubmitRecurMultiProcessorInstantPayment() {
246 $this->setUpContributionPage();
247 $this->setupPaymentProcessor();
248 $paymentProcessor2ID = $this->paymentProcessorCreate(array(
249 'payment_processor_type_id' => 'Dummy',
250 'name' => 'processor 2',
251 'class_name' => 'Payment_Dummy',
254 $dummyPP = Civi\Payment\System
::singleton()->getById($paymentProcessor2ID);
255 $dummyPP->setDoDirectPaymentResult(array(
256 'payment_status_id' => 1,
257 'trxn_id' => 'create_first_success',
260 $this->callAPISuccess('ContributionPage', 'create', array(
261 'id' => $this->_ids
['contribution_page'],
262 'payment_processor' => array($paymentProcessor2ID, $this->_ids
['payment_processor']),
265 $priceFieldID = reset($this->_ids
['price_field']);
266 $priceFieldValueID = reset($this->_ids
['price_field_value']);
267 $submitParams = array(
268 'price_' . $priceFieldID => $priceFieldValueID,
269 'id' => (int) $this->_ids
['contribution_page'],
272 'frequency_interval' => 1,
273 'frequency_unit' => 'month',
274 'payment_processor_id' => $paymentProcessor2ID,
277 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
278 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
279 'contribution_page_id' => $this->_ids
['contribution_page'],
280 'contribution_status_id' => 1,
282 $this->assertEquals('create_first_success', $contribution['trxn_id']);
283 $this->assertEquals(10, $contribution['total_amount']);
284 $this->assertEquals(.85, $contribution['fee_amount']);
285 $this->assertEquals(9.15, $contribution['net_amount']);
286 $this->_checkFinancialRecords(array(
287 'id' => $contribution['id'],
288 'total_amount' => $contribution['total_amount'],
293 * Test submit with a membership block in place.
295 public function testSubmitMembershipBlockNotSeparatePayment() {
296 $this->setUpMembershipContributionPage();
297 $submitParams = array(
298 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
299 'id' => (int) $this->_ids
['contribution_page'],
301 'billing_first_name' => 'Billy',
302 'billing_middle_name' => 'Goat',
303 'billing_last_name' => 'Gruff',
304 'selectMembership' => $this->_ids
['membership_type'],
308 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
309 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
310 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
311 $this->callAPISuccessGetSingle('LineItem', array('contribution_id' => $contribution['id'], 'entity_id' => $membershipPayment['id']));
315 * Test submit with a membership block in place.
317 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail() {
318 $mut = new CiviMailUtils($this, TRUE);
319 $this->setUpMembershipContributionPage();
320 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
322 $submitParams = array(
323 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
324 'id' => (int) $this->_ids
['contribution_page'],
326 'billing_first_name' => 'Billy',
327 'billing_middle_name' => 'Goat',
328 'billing_last_name' => 'Gruff',
329 'selectMembership' => $this->_ids
['membership_type'],
330 'email-Primary' => 'billy-goat@the-bridge.net',
331 'payment_processor_id' => $this->_paymentProcessor
['id'],
332 'credit_card_number' => '4111111111111111',
333 'credit_card_type' => 'Visa',
334 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
338 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
339 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
340 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
341 $mut->checkMailLog(array(
342 'Membership Type: General',
345 $mut->clearMessages();
349 * Test submit with a membership block in place.
351 public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail() {
352 $mut = new CiviMailUtils($this, TRUE);
353 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
354 $this->setUpMembershipContributionPage();
355 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
357 $submitParams = array(
358 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
359 'id' => (int) $this->_ids
['contribution_page'],
361 'billing_first_name' => 'Billy',
362 'billing_middle_name' => 'Goat',
363 'billing_last_name' => 'Gruffier',
364 'selectMembership' => $this->_ids
['membership_type'],
365 'email-Primary' => 'billy-goat@the-new-bridge.net',
368 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
369 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
370 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
371 $mut->checkMailLog(array(
372 'Membership Type: General',
380 $mut->clearMessages(999);
384 * Test submit with a membership block in place.
386 public function testSubmitMembershipBlockIsSeparatePayment() {
387 $this->setUpMembershipContributionPage(TRUE);
388 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
389 $submitParams = array(
390 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
391 'id' => (int) $this->_ids
['contribution_page'],
393 'billing_first_name' => 'Billy',
394 'billing_middle_name' => 'Goat',
395 'billing_last_name' => 'Gruff',
396 'selectMembership' => $this->_ids
['membership_type'],
399 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
400 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
401 $this->assertCount(2, $contributions['values']);
402 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
403 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
404 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
405 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
409 * Test submit with a membership block in place.
411 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
412 $mut = new CiviMailUtils($this, TRUE);
413 $this->setUpMembershipContributionPage(TRUE);
414 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
416 $submitParams = array(
417 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
418 'id' => (int) $this->_ids
['contribution_page'],
420 'billing_first_name' => 'Billy',
421 'billing_middle_name' => 'Goat',
422 'billing_last_name' => 'Gruff',
423 'selectMembership' => $this->_ids
['membership_type'],
424 'email-Primary' => 'billy-goat@the-bridge.net',
425 'payment_processor_id' => $this->_paymentProcessor
['id'],
426 'credit_card_number' => '4111111111111111',
427 'credit_card_type' => 'Visa',
428 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
432 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
433 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
434 $this->assertCount(2, $contributions['values']);
435 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
436 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
437 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
438 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
439 $mut->checkAllMailLog(array(
444 $mut->clearMessages(999);
448 * Test submit with a membership block in place.
450 public function testSubmitMembershipBlockIsSeparatePaymentZeroDollarsPayLaterWithEmail() {
451 $mut = new CiviMailUtils($this, TRUE);
452 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
453 $this->setUpMembershipContributionPage(TRUE);
454 $this->addProfile('supporter_profile', $this->_ids
['contribution_page']);
456 $submitParams = array(
457 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
458 'id' => (int) $this->_ids
['contribution_page'],
460 'billing_first_name' => 'Billy',
461 'billing_middle_name' => 'Goat',
462 'billing_last_name' => 'Gruffalo',
463 'selectMembership' => $this->_ids
['membership_type'],
464 'payment_processor_id' => 0,
465 'email-Primary' => 'gruffalo@the-bridge.net',
468 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
469 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
470 $this->assertCount(2, $contributions['values']);
471 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
472 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
473 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
474 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
475 $mut->checkMailLog(array(
477 'General Membership: $ 0.00',
481 $mut->clearMessages();
485 * Test submit with a membership block in place.
487 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
488 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
489 $this->_ids
['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
490 $this->setUpMembershipContributionPage(TRUE);
491 $submitParams = array(
492 'price_' . $this->_ids
['price_field'][0] => $this->_ids
['price_field_value'][1],
493 'id' => (int) $this->_ids
['contribution_page'],
495 'billing_first_name' => 'Billy',
496 'billing_middle_name' => 'Goat',
497 'billing_last_name' => 'Gruff',
498 'selectMembership' => $this->_ids
['membership_type'][1],
501 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
502 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids
['contribution_page']));
503 $this->assertCount(2, $contributions['values']);
504 $ids = array_keys($contributions['values']);
505 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
506 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
507 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
508 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
509 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
510 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
514 * Test submit with a membership block in place.
516 * We are expecting a separate payment for the membership vs the contribution.
518 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
519 $this->setUpMembershipContributionPage(TRUE);
520 $processor = Civi\Payment\System
::singleton()->getById($this->_paymentProcessor
['id']);
521 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
522 $submitParams = array(
523 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
524 'id' => (int) $this->_ids
['contribution_page'],
526 'billing_first_name' => 'Billy',
527 'billing_middle_name' => 'Goat',
528 'billing_last_name' => 'Gruff',
529 'selectMembership' => $this->_ids
['membership_type'],
530 'payment_processor_id' => $this->_paymentProcessor
['id'],
531 'credit_card_number' => '4111111111111111',
532 'credit_card_type' => 'Visa',
533 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
537 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
538 $contributions = $this->callAPISuccess('contribution', 'get', array(
539 'contribution_page_id' => $this->_ids
['contribution_page'],
540 'contribution_status_id' => 1,
542 $this->assertCount(2, $contributions['values']);
543 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
544 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
545 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
546 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
547 foreach ($contributions['values'] as $contribution) {
548 $this->assertEquals(.72, $contribution['fee_amount']);
549 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
554 * Test that when a transaction fails the pending contribution remains.
556 * An activity should also be created. CRM-16417.
558 public function testSubmitPaymentProcessorFailure() {
559 $this->setUpContributionPage();
560 $this->setupPaymentProcessor();
561 $this->createLoggedInUser();
562 $priceFieldID = reset($this->_ids
['price_field']);
563 $priceFieldValueID = reset($this->_ids
['price_field_value']);
564 $submitParams = array(
565 'price_' . $priceFieldID => $priceFieldValueID,
566 'id' => (int) $this->_ids
['contribution_page'],
568 'payment_processor_id' => 1,
569 'credit_card_number' => '4111111111111111',
570 'credit_card_type' => 'Visa',
571 'credit_card_exp_date' => array('M' => 9, 'Y' => 2008),
575 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
576 $contribution = $this->callAPISuccessGetSingle('contribution', array(
577 'contribution_page_id' => $this->_ids
['contribution_page'],
578 'contribution_status_id' => 2,
581 $this->callAPISuccessGetSingle('activity', array(
582 'source_record_id' => $contribution['id'],
583 'activity_type_id' => 'Failed Payment',
589 * Test submit recurring membership with immediate confirmation (IATS style).
591 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
592 * processor (IATS style - denoted by returning trxn_id)
593 * - the first creates a new membership, completed contribution, in progress recurring. Check these
594 * - create another - end date should be extended
596 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
597 $this->params
['is_recur'] = 1;
598 $this->params
['recur_frequency_unit'] = 'month';
599 $this->setUpMembershipContributionPage();
600 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
601 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
603 $submitParams = array(
604 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
605 'id' => (int) $this->_ids
['contribution_page'],
607 'billing_first_name' => 'Billy',
608 'billing_middle_name' => 'Goat',
609 'billing_last_name' => 'Gruff',
610 'email' => 'billy@goat.gruff',
611 'selectMembership' => $this->_ids
['membership_type'],
612 'payment_processor_id' => 1,
613 'credit_card_number' => '4111111111111111',
614 'credit_card_type' => 'Visa',
615 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
618 'frequency_interval' => 1,
619 'frequency_unit' => 'month',
622 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
623 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
624 'contribution_page_id' => $this->_ids
['contribution_page'],
625 'contribution_status_id' => 1,
628 $this->assertEquals('create_first_success', $contribution['trxn_id']);
629 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
630 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
631 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
632 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
633 $this->assertEquals(1, $membership['status_id']);
634 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
635 //@todo - check with Joe about these not existing
636 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
637 //renew it with processor setting completed - should extend membership
638 $submitParams['contact_id'] = $contribution['contact_id'];
639 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
640 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
641 $this->callAPISuccess('contribution', 'getsingle', array(
642 'id' => array('NOT IN' => array($contribution['id'])),
643 'contribution_page_id' => $this->_ids
['contribution_page'],
644 'contribution_status_id' => 1,
646 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
647 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
648 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
649 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
653 * Test submit recurring membership with immediate confirmation (IATS style).
655 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
656 * processor (IATS style - denoted by returning trxn_id)
657 * - the first creates a new membership, completed contribution, in progress recurring. Check these
658 * - create another - end date should be extended
660 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
662 $this->setUpMembershipContributionPage(TRUE);
663 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
664 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
666 $submitParams = array(
667 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
668 'id' => (int) $this->_ids
['contribution_page'],
670 'billing_first_name' => 'Billy',
671 'billing_middle_name' => 'Goat',
672 'billing_last_name' => 'Gruff',
673 'email' => 'billy@goat.gruff',
674 'selectMembership' => $this->_ids
['membership_type'],
675 'payment_processor_id' => 1,
676 'credit_card_number' => '4111111111111111',
677 'credit_card_type' => 'Visa',
678 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
681 'auto_renew' => TRUE,
682 'frequency_interval' => 1,
683 'frequency_unit' => 'month',
686 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
687 $contribution = $this->callAPISuccess('contribution', 'get', array(
688 'contribution_page_id' => $this->_ids
['contribution_page'],
689 'contribution_status_id' => 1,
692 $this->assertEquals(2, $contribution['count']);
693 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
694 $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
695 $this->assertNotEmpty($contribution['values'][$membershipPayment['contribution_id']]['contribution_recur_id']);
696 $this->callAPISuccess('contribution_recur', 'getsingle', array());
700 * Test submit recurring membership with delayed confirmation (Authorize.net style)
701 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
702 * processor (Authorize.net style - denoted by NOT returning trxn_id)
703 * - the first creates a pending membership, pending contribution, penging recurring. Check these
704 * - complete the transaction
705 * - create another - end date should NOT be extended
707 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
708 $this->params
['is_recur'] = 1;
709 $this->params
['recur_frequency_unit'] = 'month';
710 $this->setUpMembershipContributionPage();
711 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
712 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
714 $submitParams = array(
715 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
716 'id' => (int) $this->_ids
['contribution_page'],
718 'billing_first_name' => 'Billy',
719 'billing_middle_name' => 'Goat',
720 'billing_last_name' => 'Gruff',
721 'email' => 'billy@goat.gruff',
722 'selectMembership' => $this->_ids
['membership_type'],
723 'payment_processor_id' => 1,
724 'credit_card_number' => '4111111111111111',
725 'credit_card_type' => 'Visa',
726 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
729 'frequency_interval' => 1,
730 'frequency_unit' => 'month',
733 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
734 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
735 'contribution_page_id' => $this->_ids
['contribution_page'],
736 'contribution_status_id' => 2,
738 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
739 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
740 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
741 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
742 $this->assertEquals(5, $membership['status_id']);
743 //@todo - check with Joe about these not existing
744 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
745 $this->callAPISuccess('contribution', 'completetransaction', array(
746 'id' => $contribution['id'],
747 'trxn_id' => 'ipn_called',
748 'payment_processor_id' => $this->_paymentProcessor
['id'],
750 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
751 //renew it with processor setting completed - should extend membership
752 $submitParams = array_merge($submitParams, array(
753 'contact_id' => $contribution['contact_id'],
755 'frequency_interval' => 1,
756 'frequency_unit' => 'month',
759 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
760 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
761 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
763 'NOT IN' => array($contribution['id']),
765 'contribution_page_id' => $this->_ids
['contribution_page'],
766 'contribution_status_id' => 2,
770 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
771 //no renewal as the date hasn't changed
772 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
773 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
774 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
778 * Set up membership contribution page.
779 * @param bool $isSeparatePayment
781 public function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
782 $this->setUpMembershipBlockPriceSet();
783 $this->setupPaymentProcessor();
784 $this->setUpContributionPage();
786 $this->callAPISuccess('membership_block', 'create', array(
787 'entity_id' => $this->_ids
['contribution_page'],
788 'entity_table' => 'civicrm_contribution_page',
789 'is_required' => TRUE,
791 'is_separate_payment' => $isSeparatePayment,
792 'membership_type_default' => $this->_ids
['membership_type'],
797 * Set up pledge block.
799 public function setUpPledgeBlock() {
801 'entity_table' => 'civicrm_contribution_page',
802 'entity_id' => $this->_ids
['contribution_page'],
803 'pledge_frequency_unit' => 'week',
804 'is_pledge_interval' => 0,
805 'pledge_start_date' => json_encode(array('calendar_date' => date('Ymd', strtotime("+1 month")))),
807 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock
::create($params);
808 $this->_ids
['pledge_block_id'] = $pledgeBlock->id
;
812 * The default data set does not include a complete default membership price set - not quite sure why.
814 * This function ensures it exists & populates $this->_ids with it's data
816 public function setUpMembershipBlockPriceSet() {
817 $this->_ids
['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
818 'name' => 'default_membership_type_amount',
821 if (empty($this->_ids
['membership_type'])) {
822 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
824 $priceField = $this->callAPISuccess('price_field', 'create', array(
825 'price_set_id' => reset($this->_ids
['price_set']),
826 'name' => 'membership_amount',
827 'label' => 'Membership Amount',
828 'html_type' => 'Radio',
831 $this->_ids
['price_field'][] = $priceField['id'];
833 foreach ($this->_ids
['membership_type'] as $membershipTypeID) {
834 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
835 'name' => 'membership_amount',
836 'label' => 'Membership Amount',
838 'financial_type_id' => 'Donation',
839 'format.only_id' => TRUE,
840 'membership_type_id' => $membershipTypeID,
841 'price_field_id' => $priceField['id'],
843 $this->_ids
['price_field_value'][] = $priceFieldValue;
848 * Add text field other amount to the price set.
850 public function addOtherAmountFieldToMembershipPriceSet() {
851 $this->_ids
['price_field']['other_amount'] = $this->callAPISuccess('price_field', 'create', array(
852 'price_set_id' => reset($this->_ids
['price_set']),
853 'name' => 'other_amount',
854 'label' => 'Other Amount',
855 'html_type' => 'Text',
856 'format.only_id' => TRUE,
859 $this->_ids
['price_field_value']['other_amount'] = $this->callAPISuccess('price_field_value', 'create', array(
860 'financial_type_id' => 'Donation',
861 'format.only_id' => TRUE,
862 'label' => 'Other Amount',
864 'price_field_id' => $this->_ids
['price_field']['other_amount'],
869 * Help function to set up contribution page with some defaults.
871 public function setUpContributionPage() {
872 $contributionPageResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
873 if (empty($this->_ids
['price_set'])) {
874 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams
);
875 $this->_ids
['price_set'][] = $priceSet['id'];
877 $priceSetID = reset($this->_ids
['price_set']);
878 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
880 if (empty($this->_ids
['price_field'])) {
881 $priceField = $this->callAPISuccess('price_field', 'create', array(
882 'price_set_id' => $priceSetID,
883 'label' => 'Goat Breed',
884 'html_type' => 'Radio',
886 $this->_ids
['price_field'] = array($priceField['id']);
888 if (empty($this->_ids
['price_field_value'])) {
889 $this->callAPISuccess('price_field_value', 'create', array(
890 'price_set_id' => $priceSetID,
891 'price_field_id' => $priceField['id'],
892 'label' => 'Long Haired Goat',
893 'financial_type_id' => 'Donation',
895 'financial_type_id' => 'Donation',
896 'non_deductible_amount' => 15,
899 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
900 'price_set_id' => $priceSetID,
901 'price_field_id' => $priceField['id'],
902 'label' => 'Shoe-eating Goat',
903 'financial_type_id' => 'Donation',
905 'financial_type_id' => 'Donation',
906 'non_deductible_amount' => 5,
909 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
911 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
914 public static function setUpBeforeClass() {
915 // put stuff here that should happen before all tests in this unit
918 public static function tearDownAfterClass() {
919 $tablesToTruncate = array(
921 'civicrm_financial_type',
922 'civicrm_contribution',
923 'civicrm_contribution_page',
925 $unitTest = new CiviUnitTestCase();
926 $unitTest->quickCleanup($tablesToTruncate);
930 * Create a payment processor instance.
932 protected function setupPaymentProcessor() {
933 $this->params
['payment_processor_id'] = $this->_ids
['payment_processor'] = $this->paymentProcessorCreate(array(
934 'payment_processor_type_id' => 'Dummy',
935 'class_name' => 'Payment_Dummy',
938 $this->_paymentProcessor
= $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params
['payment_processor_id']));
942 * Test submit recurring pledge.
944 * - we process 1 pledge with a future start date. A recur contribution and the pledge should be created with first payment date in the future.
946 public function testSubmitPledgePaymentPaymentProcessorRecurFuturePayment() {
947 $this->params
['adjust_recur_start_date'] = TRUE;
948 $this->params
['is_pay_later'] = FALSE;
949 $this->setUpContributionPage();
950 $this->setUpPledgeBlock();
951 $this->setupPaymentProcessor();
952 $dummyPP = Civi\Payment\System
::singleton()->getByProcessor($this->_paymentProcessor
);
953 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
955 $submitParams = array(
956 'id' => (int) $this->_ids
['contribution_page'],
958 'billing_first_name' => 'Billy',
959 'billing_middle_name' => 'Goat',
960 'billing_last_name' => 'Gruff',
961 'email' => 'billy@goat.gruff',
962 'payment_processor_id' => 1,
963 'credit_card_number' => '4111111111111111',
964 'credit_card_type' => 'Visa',
965 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
967 'pledge_frequency_interval' => 1,
968 'pledge_frequency_unit' => 'week',
969 'pledge_installments' => 3,
971 'pledge_block_id' => (int) $this->_ids
['pledge_block_id'],
974 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL);
976 // Check if contribution created.
977 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
978 'contribution_page_id' => $this->_ids
['contribution_page'],
979 'contribution_status_id' => 'Completed', // Will be pending when actual payment processor is used (dummy processor does not support future payments).
982 $this->assertEquals('create_first_success', $contribution['trxn_id']);
984 // Check if pledge created.
985 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
986 $this->assertEquals(date('Ymd', strtotime($pledge['pledge_start_date'])), date('Ymd', strtotime("+1 month")));
987 $this->assertEquals($pledge['pledge_amount'], 300.00);
989 // Check if pledge payments created.
991 'pledge_id' => $pledge['id'],
993 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
994 $this->assertEquals($pledgePayment['count'], 3);
995 $this->assertEquals(date('Ymd', strtotime($pledgePayment['values'][1]['scheduled_date'])), date('Ymd', strtotime("+1 month")));
996 $this->assertEquals($pledgePayment['values'][1]['scheduled_amount'], 100.00);
997 $this->assertEquals($pledgePayment['values'][1]['status_id'], 1); // Will be pending when actual payment processor is used (dummy processor does not support future payments).
999 // Check contribution recur record.
1000 $recur = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
1001 $this->assertEquals(date('Ymd', strtotime($recur['start_date'])), date('Ymd', strtotime("+1 month")));
1002 $this->assertEquals($recur['amount'], 100.00);
1003 $this->assertEquals($recur['contribution_status_id'], 5); // In progress status.