Merge pull request #9386 from eileenmcnaughton/bits
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionPageTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
6a488035
TO
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 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035 27
6a488035
TO
28/**
29 * Test APIv3 civicrm_contribute_recur* functions
30 *
6c6e6187
TO
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
acb109b7 33 * @group headless
6a488035 34 */
6a488035
TO
35class api_v3_ContributionPageTest extends CiviUnitTestCase {
36 protected $_apiversion = 3;
37 protected $testAmount = 34567;
38 protected $params;
39 protected $id = 0;
40 protected $contactIds = array();
41 protected $_entity = 'contribution_page';
6c6e6187 42 protected $contribution_result = NULL;
f64a217a 43 protected $_priceSetParams = array();
f8fe0df6 44 /**
eceb18cc 45 * Payment processor details.
f8fe0df6 46 * @var array
47 */
48 protected $_paymentProcessor = array();
f64a217a
EM
49
50 /**
51 * @var array
16b10e64
CW
52 * - contribution_page
53 * - price_set
54 * - price_field
55 * - price_field_value
f64a217a
EM
56 */
57 protected $_ids = array();
58
b7c9bc4c 59
6a488035 60 public $DBResetRequired = TRUE;
5896d037 61
6a488035
TO
62 public function setUp() {
63 parent::setUp();
64 $this->contactIds[] = $this->individualCreate();
65 $this->params = array(
6a488035
TO
66 'title' => "Test Contribution Page",
67 'financial_type_id' => 1,
68 'currency' => 'NZD',
69 'goal_amount' => $this->testAmount,
6cdac50b 70 'is_pay_later' => 1,
f64a217a 71 'is_monetary' => TRUE,
dd1b539a 72 'is_email_receipt' => TRUE,
73 'receipt_from_email' => 'yourconscience@donate.com',
74 'receipt_from_name' => 'Ego Freud',
f64a217a
EM
75 );
76
77 $this->_priceSetParams = array(
78 'is_quick_config' => 1,
79 'extends' => 'CiviContribute',
80 'financial_type_id' => 'Donation',
21dfd5f5 81 'title' => 'my Page',
6a488035
TO
82 );
83 }
84
00be9182 85 public function tearDown() {
6a488035 86 foreach ($this->contactIds as $id) {
fc928539 87 $this->callAPISuccess('contact', 'delete', array('id' => $id));
611c7ece 88 }
f9342903 89 $this->quickCleanUpFinancialEntities();
6a488035
TO
90 }
91
92 public function testCreateContributionPage() {
fc928539 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']);
6a488035
TO
96 $this->getAndCheck($this->params, $result['id'], $this->_entity);
97 }
98
99 public function testGetBasicContributionPage() {
fc928539 100 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 101 $this->id = $createResult['id'];
6a488035 102 $getParams = array(
6a488035
TO
103 'currency' => 'NZD',
104 'financial_type_id' => 1,
105 );
fc928539 106 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
107 $this->assertEquals(1, $getResult['count']);
6a488035
TO
108 }
109
110 public function testGetContributionPageByAmount() {
fc928539 111 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 112 $this->id = $createResult['id'];
6a488035 113 $getParams = array(
5896d037 114 'amount' => '' . $this->testAmount, // 3456
6a488035
TO
115 'currency' => 'NZD',
116 'financial_type_id' => 1,
117 );
fc928539 118 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
119 $this->assertEquals(1, $getResult['count']);
6a488035
TO
120 }
121
122 public function testDeleteContributionPage() {
fc928539 123 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
124 $deleteParams = array('id' => $createResult['id']);
611c7ece 125 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
fc928539 126 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
127 $this->assertEquals(0, $checkDeleted['count']);
6a488035
TO
128 }
129
130 public function testGetFieldsContributionPage() {
fc928539 131 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
6a488035
TO
132 $this->assertEquals(12, $result['values']['start_date']['type']);
133 }
134
be26f3e0 135
d58b453e 136 /**
eceb18cc 137 * Test form submission with basic price set.
d58b453e 138 */
be26f3e0 139 public function testSubmit() {
f64a217a
EM
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'],
21dfd5f5 146 'amount' => 10,
be26f3e0
EM
147 );
148
f64a217a 149 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
05465712 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']);
f64a217a 153 }
5896d037 154
870156d6 155 /**
156 * Test form submission with billing first & last name where the contact does NOT
157 * otherwise have one.
158 */
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'],
167 'amount' => 10,
168 'billing_first_name' => 'Wonder',
169 'billing_last_name' => 'Woman',
170 'contactID' => $contact['id'],
171 'email' => 'wonderwoman@amazon.com',
172 );
173
174 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
175 $contact = $this->callAPISuccess('Contact', 'get', array(
176 'id' => $contact['id'],
177 'return' => array(
178 'first_name',
179 'last_name',
180 'sort_name',
181 'display_name',
182 ),
183 ));
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']]);
192
193 }
194
195 /**
196 * Test form submission with billing first & last name where the contact does
197 * otherwise have one and should not be overwritten.
198 */
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',
206 ));
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'],
212 'amount' => 10,
213 'billing_first_name' => 'Wonder',
214 'billing_last_name' => 'Woman',
215 'contactID' => $contact['id'],
216 'email' => 'wonderwoman@amazon.com',
217 );
218
219 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
220 $contact = $this->callAPISuccess('Contact', 'get', array(
221 'id' => $contact['id'],
222 'return' => array(
223 'first_name',
224 'last_name',
225 'sort_name',
226 'display_name',
227 ),
228 ));
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']]);
237
238 }
239
7bc789a5 240 /**
241 * Test process with instant payment when more than one configured for the page.
242 *
243 * CRM-16923
244 */
245 public function testSubmitRecurMultiProcessorInstantPayment() {
246 $this->setUpContributionPage();
247 $this->setupPaymentProcessor();
d27635dc 248 $paymentProcessor2ID = $this->paymentProcessorCreate(array(
7bc789a5 249 'payment_processor_type_id' => 'Dummy',
250 'name' => 'processor 2',
251 'class_name' => 'Payment_Dummy',
252 'billing_mode' => 1,
253 ));
254 $dummyPP = Civi\Payment\System::singleton()->getById($paymentProcessor2ID);
70cc8754 255 $dummyPP->setDoDirectPaymentResult(array(
256 'payment_status_id' => 1,
257 'trxn_id' => 'create_first_success',
258 'fee_amount' => .85,
259 ));
7bc789a5 260 $this->callAPISuccess('ContributionPage', 'create', array(
d27635dc 261 'id' => $this->_ids['contribution_page'],
262 'payment_processor' => array($paymentProcessor2ID, $this->_ids['payment_processor']),
7bc789a5 263 ));
264
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'],
270 'amount' => 10,
271 'is_recur' => 1,
272 'frequency_interval' => 1,
273 'frequency_unit' => 'month',
274 'payment_processor_id' => $paymentProcessor2ID,
275 );
276
277 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
70cc8754 278 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
7bc789a5 279 'contribution_page_id' => $this->_ids['contribution_page'],
280 'contribution_status_id' => 1,
281 ));
70cc8754 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']);
bf722049 286 $this->_checkFinancialRecords(array(
287 'id' => $contribution['id'],
288 'total_amount' => $contribution['total_amount'],
289 ), 'online');
7bc789a5 290 }
70cc8754 291
f64a217a 292 /**
eceb18cc 293 * Test submit with a membership block in place.
f64a217a 294 */
f9342903
EM
295 public function testSubmitMembershipBlockNotSeparatePayment() {
296 $this->setUpMembershipContributionPage();
f64a217a 297 $submitParams = array(
3e28c791 298 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903 299 'id' => (int) $this->_ids['contribution_page'],
f64a217a
EM
300 'amount' => 10,
301 'billing_first_name' => 'Billy',
302 'billing_middle_name' => 'Goat',
303 'billing_last_name' => 'Gruff',
304 'selectMembership' => $this->_ids['membership_type'],
305
306 );
307
a828d7b8 308 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
f9342903 309 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
840e3907 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']));
f9342903
EM
312 }
313
2f59d010 314 /**
315 * Test submit with a membership block in place.
316 */
317 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail() {
318 $mut = new CiviMailUtils($this, TRUE);
319 $this->setUpMembershipContributionPage();
320 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
321
322 $submitParams = array(
323 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
324 'id' => (int) $this->_ids['contribution_page'],
325 'amount' => 10,
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',
9daadfce 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),
335 'cvv2' => 123,
2f59d010 336 );
337
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',
343 ));
344 $mut->stop();
345 $mut->clearMessages();
346 }
347
348 /**
349 * Test submit with a membership block in place.
350 */
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']);
356
357 $submitParams = array(
358 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
359 'id' => (int) $this->_ids['contribution_page'],
360 'amount' => 0,
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',
366 );
367
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',
373 'Gruffier',
374 ),
375 array(
376 'Amount',
377 )
378 );
379 $mut->stop();
9daadfce 380 $mut->clearMessages(999);
2f59d010 381 }
382
f9342903 383 /**
eceb18cc 384 * Test submit with a membership block in place.
f9342903
EM
385 */
386 public function testSubmitMembershipBlockIsSeparatePayment() {
387 $this->setUpMembershipContributionPage(TRUE);
9daadfce 388 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 389 $submitParams = array(
3e28c791 390 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903
EM
391 'id' => (int) $this->_ids['contribution_page'],
392 'amount' => 10,
393 'billing_first_name' => 'Billy',
394 'billing_middle_name' => 'Goat',
395 'billing_last_name' => 'Gruff',
396 'selectMembership' => $this->_ids['membership_type'],
397 );
398
a828d7b8 399 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
f9342903
EM
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']);
406 }
f64a217a 407
2f59d010 408 /**
409 * Test submit with a membership block in place.
410 */
411 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
412 $mut = new CiviMailUtils($this, TRUE);
413 $this->setUpMembershipContributionPage(TRUE);
414 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
415
416 $submitParams = array(
417 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
418 'id' => (int) $this->_ids['contribution_page'],
419 'amount' => 10,
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',
9daadfce 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),
429 'cvv2' => 123,
2f59d010 430 );
431
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']);
9daadfce 439 $mut->checkAllMailLog(array(
440 '$ 2.00',
2f59d010 441 'Membership Fee',
442 ));
443 $mut->stop();
9daadfce 444 $mut->clearMessages(999);
2f59d010 445 }
446
447 /**
448 * Test submit with a membership block in place.
449 */
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']);
455
456 $submitParams = array(
457 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
458 'id' => (int) $this->_ids['contribution_page'],
459 'amount' => 0,
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',
466 );
467
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(
476 'Gruffalo',
477 'General Membership: $ 0.00',
478 'Membership Fee',
479 ));
480 $mut->stop();
481 $mut->clearMessages();
482 }
483
797c4f88 484 /**
eceb18cc 485 * Test submit with a membership block in place.
797c4f88
EM
486 */
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'],
494 'amount' => 10,
495 'billing_first_name' => 'Billy',
496 'billing_middle_name' => 'Goat',
497 'billing_last_name' => 'Gruff',
498 'selectMembership' => $this->_ids['membership_type'][1],
499 );
500
a828d7b8 501 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
6c6e6187 502 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
797c4f88
EM
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']);
511 }
512
8ca611b7 513 /**
eceb18cc 514 * Test submit with a membership block in place.
329f3f66
EM
515 *
516 * We are expecting a separate payment for the membership vs the contribution.
8ca611b7 517 */
329f3f66 518 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
8ca611b7 519 $this->setUpMembershipContributionPage(TRUE);
0193ebdb 520 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
521 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
8ca611b7
EM
522 $submitParams = array(
523 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
524 'id' => (int) $this->_ids['contribution_page'],
525 'amount' => 10,
526 'billing_first_name' => 'Billy',
527 'billing_middle_name' => 'Goat',
528 'billing_last_name' => 'Gruff',
529 'selectMembership' => $this->_ids['membership_type'],
7bc789a5 530 'payment_processor_id' => $this->_paymentProcessor['id'],
8ca611b7
EM
531 'credit_card_number' => '4111111111111111',
532 'credit_card_type' => 'Visa',
5896d037 533 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
8ca611b7
EM
534 'cvv2' => 123,
535 );
05990684 536
a828d7b8 537 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 538 $contributions = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
539 'contribution_page_id' => $this->_ids['contribution_page'],
540 'contribution_status_id' => 1,
541 ));
8ca611b7
EM
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']);
0193ebdb 547 foreach ($contributions['values'] as $contribution) {
548 $this->assertEquals(.72, $contribution['fee_amount']);
549 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
550 }
8ca611b7 551 }
f8fe0df6 552
5961bd47
EM
553 /**
554 * Test that when a transaction fails the pending contribution remains.
555 *
556 * An activity should also be created. CRM-16417.
557 */
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'],
567 'amount' => 10,
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),
572 'cvv2' => 123,
573 );
574
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,
579 ));
580
581 $this->callAPISuccessGetSingle('activity', array(
582 'source_record_id' => $contribution['id'],
583 'activity_type_id' => 'Failed Payment',
584 ));
585
5961bd47
EM
586 }
587
f8fe0df6 588 /**
dd1b539a 589 * Test submit recurring membership with immediate confirmation (IATS style).
590 *
e6fa4056 591 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1fee3ad2 592 * processor (IATS style - denoted by returning trxn_id)
e6fa4056
EM
593 * - the first creates a new membership, completed contribution, in progress recurring. Check these
594 * - create another - end date should be extended
f8fe0df6 595 */
8bef2e38 596 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
f8fe0df6 597 $this->params['is_recur'] = 1;
f8fe0df6 598 $this->params['recur_frequency_unit'] = 'month';
599 $this->setUpMembershipContributionPage();
7c85dc65 600 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
8bef2e38 601 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
f8fe0df6 602
603 $submitParams = array(
604 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
605 'id' => (int) $this->_ids['contribution_page'],
606 'amount' => 10,
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'],
579a1fb7 612 'payment_processor_id' => 1,
f8fe0df6 613 'credit_card_number' => '4111111111111111',
614 'credit_card_type' => 'Visa',
5896d037 615 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 616 'cvv2' => 123,
617 'is_recur' => 1,
618 'frequency_interval' => 1,
619 'frequency_unit' => 'month',
620 );
621
a828d7b8 622 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 623 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
624 'contribution_page_id' => $this->_ids['contribution_page'],
625 'contribution_status_id' => 1,
626 ));
0739d6cf 627
628 $this->assertEquals('create_first_success', $contribution['trxn_id']);
f8fe0df6 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']));
e6fa4056
EM
635 //@todo - check with Joe about these not existing
636 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
f8fe0df6 637 //renew it with processor setting completed - should extend membership
638 $submitParams['contact_id'] = $contribution['contact_id'];
8bef2e38 639 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
f8fe0df6 640 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
5896d037 641 $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
642 'id' => array('NOT IN' => array($contribution['id'])),
643 'contribution_page_id' => $this->_ids['contribution_page'],
644 'contribution_status_id' => 1,
645 ));
f8fe0df6 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']));
91259407 649 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
f8fe0df6 650 }
651
449f4c90 652 /**
653 * Test submit recurring membership with immediate confirmation (IATS style).
654 *
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
659 */
660 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
661
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'));
665
666 $submitParams = array(
667 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
668 'id' => (int) $this->_ids['contribution_page'],
669 'amount' => 10,
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),
679 'cvv2' => 123,
680 'is_recur' => 1,
681 'auto_renew' => TRUE,
682 'frequency_interval' => 1,
683 'frequency_unit' => 'month',
684 );
685
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,
690 ));
691
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());
697 }
698
f8fe0df6 699 /**
e6fa4056
EM
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
f8fe0df6 706 */
707 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
708 $this->params['is_recur'] = 1;
f8fe0df6 709 $this->params['recur_frequency_unit'] = 'month';
710 $this->setUpMembershipContributionPage();
ab30e033 711 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
b5eacf76 712 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
f8fe0df6 713
714 $submitParams = array(
715 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
716 'id' => (int) $this->_ids['contribution_page'],
717 'amount' => 10,
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'],
579a1fb7 723 'payment_processor_id' => 1,
f8fe0df6 724 'credit_card_number' => '4111111111111111',
725 'credit_card_type' => 'Visa',
5896d037 726 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 727 'cvv2' => 123,
e6fa4056
EM
728 'is_recur' => 1,
729 'frequency_interval' => 1,
730 'frequency_unit' => 'month',
f8fe0df6 731 );
732
a828d7b8 733 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 734 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
735 'contribution_page_id' => $this->_ids['contribution_page'],
736 'contribution_status_id' => 2,
737 ));
f8fe0df6 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']);
e6fa4056
EM
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']));
5896d037 745 $this->callAPISuccess('contribution', 'completetransaction', array(
92915c55
TO
746 'id' => $contribution['id'],
747 'trxn_id' => 'ipn_called',
b396c447 748 'payment_processor_id' => $this->_paymentProcessor['id'],
92915c55 749 ));
e6fa4056 750 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
f8fe0df6 751 //renew it with processor setting completed - should extend membership
752 $submitParams = array_merge($submitParams, array(
5896d037
TO
753 'contact_id' => $contribution['contact_id'],
754 'is_recur' => 1,
755 'frequency_interval' => 1,
756 'frequency_unit' => 'month',
757 )
f8fe0df6 758 );
b5eacf76 759 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
f8fe0df6 760 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
6c6e6187 761 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
5896d037 762 'id' => array(
21dfd5f5 763 'NOT IN' => array($contribution['id']),
5896d037
TO
764 ),
765 'contribution_page_id' => $this->_ids['contribution_page'],
21dfd5f5 766 'contribution_status_id' => 2,
5896d037 767 )
f8fe0df6 768 );
e6fa4056 769
f8fe0df6 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']);
e6fa4056
EM
773 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
774 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
f8fe0df6 775 }
776
f9342903 777 /**
eceb18cc 778 * Set up membership contribution page.
f9342903
EM
779 * @param bool $isSeparatePayment
780 */
00be9182 781 public function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
f9342903 782 $this->setUpMembershipBlockPriceSet();
a380f4a0 783 $this->setupPaymentProcessor();
f9342903
EM
784 $this->setUpContributionPage();
785
786 $this->callAPISuccess('membership_block', 'create', array(
787 'entity_id' => $this->_ids['contribution_page'],
788 'entity_table' => 'civicrm_contribution_page',
789 'is_required' => TRUE,
790 'is_active' => TRUE,
791 'is_separate_payment' => $isSeparatePayment,
792 'membership_type_default' => $this->_ids['membership_type'],
793 ));
f64a217a
EM
794 }
795
dccd9f4f
ERL
796 /**
797 * Set up pledge block.
798 */
799 public function setUpPledgeBlock() {
800 $params = array(
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")))),
806 );
807 $pledgeBlock = CRM_Pledge_BAO_PledgeBlock::create($params);
808 $this->_ids['pledge_block_id'] = $pledgeBlock->id;
809 }
810
f9342903 811 /**
dd1b539a 812 * The default data set does not include a complete default membership price set - not quite sure why.
813 *
f9342903
EM
814 * This function ensures it exists & populates $this->_ids with it's data
815 */
00be9182 816 public function setUpMembershipBlockPriceSet() {
5896d037 817 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
92915c55
TO
818 'name' => 'default_membership_type_amount',
819 'return' => 'id',
820 ));
f9342903 821 if (empty($this->_ids['membership_type'])) {
3e28c791 822 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 823 }
3e28c791
EM
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',
829 'sequential' => 1,
830 ));
831 $this->_ids['price_field'][] = $priceField['id'];
dd1b539a 832
3e28c791
EM
833 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
834 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
f9342903
EM
835 'name' => 'membership_amount',
836 'label' => 'Membership Amount',
3e28c791 837 'amount' => 1,
43dbd988 838 'financial_type_id' => 'Donation',
3e28c791
EM
839 'format.only_id' => TRUE,
840 'membership_type_id' => $membershipTypeID,
841 'price_field_id' => $priceField['id'],
f9342903 842 ));
840e3907 843 $this->_ids['price_field_value'][] = $priceFieldValue;
f9342903
EM
844 }
845 }
3e28c791 846
2f59d010 847 /**
848 * Add text field other amount to the price set.
849 */
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,
857 'sequential' => 1,
858 ));
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',
863 'amount' => 1,
864 'price_field_id' => $this->_ids['price_field']['other_amount'],
865 ));
866 }
867
f64a217a 868 /**
eceb18cc 869 * Help function to set up contribution page with some defaults.
f64a217a 870 */
00be9182 871 public function setUpContributionPage() {
f64a217a
EM
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'];
876 }
877 $priceSetID = reset($this->_ids['price_set']);
5896d037 878 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
f9342903
EM
879
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',
885 ));
886 $this->_ids['price_field'] = array($priceField['id']);
887 }
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',
43dbd988 893 'financial_type_id' => 'Donation',
f9342903 894 'amount' => 20,
3300bf67 895 'financial_type_id' => 'Donation',
05465712 896 'non_deductible_amount' => 15,
f9342903
EM
897 )
898 );
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',
43dbd988 903 'financial_type_id' => 'Donation',
f9342903 904 'amount' => 10,
3300bf67 905 'financial_type_id' => 'Donation',
05465712 906 'non_deductible_amount' => 5,
f9342903
EM
907 )
908 );
909 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
910 }
f64a217a 911 $this->_ids['contribution_page'] = $contributionPageResult['id'];
be26f3e0
EM
912 }
913
6a488035 914 public static function setUpBeforeClass() {
6c6e6187 915 // put stuff here that should happen before all tests in this unit
6a488035
TO
916 }
917
5896d037 918 public static function tearDownAfterClass() {
6a488035
TO
919 $tablesToTruncate = array(
920 'civicrm_contact',
921 'civicrm_financial_type',
922 'civicrm_contribution',
923 'civicrm_contribution_page',
924 );
925 $unitTest = new CiviUnitTestCase();
926 $unitTest->quickCleanup($tablesToTruncate);
927 }
96025800 928
a380f4a0
EM
929 /**
930 * Create a payment processor instance.
931 */
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',
936 'billing_mode' => 1,
937 ));
938 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
939 }
940
dccd9f4f
ERL
941 /**
942 * Test submit recurring pledge.
943 *
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.
945 */
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'));
954
955 $submitParams = array(
956 'id' => (int) $this->_ids['contribution_page'],
957 'amount' => 100,
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),
966 'cvv2' => 123,
967 'pledge_frequency_interval' => 1,
968 'pledge_frequency_unit' => 'week',
969 'pledge_installments' => 3,
970 'is_pledge' => TRUE,
971 'pledge_block_id' => (int) $this->_ids['pledge_block_id'],
972 );
973
974 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
975
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).
980 ));
981
982 $this->assertEquals('create_first_success', $contribution['trxn_id']);
983
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);
988
989 // Check if pledge payments created.
990 $params = array(
991 'pledge_id' => $pledge['id'],
992 );
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).
998
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.
1004 }
1005
603577b2
E
1006 /**
1007 * Test submit pledge payment.
1008 *
1009 * - test submitting a pledge payment using contribution form.
1010 */
1011 public function testSubmitPledgePayment() {
1012 $this->testSubmitPledgePaymentPaymentProcessorRecurFuturePayment();
1013 $pledge = $this->callAPISuccess('pledge', 'getsingle', array());
1014 $params = array(
1015 'pledge_id' => $pledge['id'],
1016 );
1017 $submitParams = array(
1018 'id' => (int) $pledge['pledge_contribution_page_id'],
1019 'pledge_amount' => array(2 => 1),
1020 'billing_first_name' => 'Billy',
1021 'billing_middle_name' => 'Goat',
1022 'billing_last_name' => 'Gruff',
1023 'email' => 'billy@goat.gruff',
1024 'payment_processor_id' => 1,
1025 'credit_card_number' => '4111111111111111',
1026 'credit_card_type' => 'Visa',
1027 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
1028 'cvv2' => 123,
1029 'pledge_id' => $pledge['id'],
1030 'cid' => $pledge['contact_id'],
1031 'contact_id' => $pledge['contact_id'],
1032 'amount' => 100.00,
1033 'is_pledge' => TRUE,
1034 'pledge_block_id' => $this->_ids['pledge_block_id'],
1035 );
1036 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1037 $this->assertEquals($pledgePayment['values'][2]['status_id'], 2);
1038
1039 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
1040
1041 // Check if contribution created.
1042 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
1043 'contribution_page_id' => $pledge['pledge_contribution_page_id'],
1044 'contribution_status_id' => 'Completed',
1045 'contact_id' => $pledge['contact_id'],
1046 'contribution_recur_id' => array('IS NULL' => 1),
1047 ));
1048
1049 $this->assertEquals(100.00, $contribution['total_amount']);
1050 $pledgePayment = $this->callAPISuccess('pledge_payment', 'get', $params);
1051 $this->assertEquals($pledgePayment['values'][2]['status_id'], 1, "This pledge payment should have been completed");
1052 $this->assertEquals($pledgePayment['values'][2]['contribution_id'], $contribution['id']);
1053 }
1054
6a488035 1055}