Merge pull request #7584 from jitendrapurohit/CRM-17809
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionPageTest.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
TO
27
28require_once 'CiviTest/CiviUnitTestCase.php';
dd1b539a 29require_once 'CiviTest/CiviMailUtils.php';
6a488035
TO
30
31/**
32 * Test APIv3 civicrm_contribute_recur* functions
33 *
6c6e6187
TO
34 * @package CiviCRM_APIv3
35 * @subpackage API_Contribution
6a488035 36 */
6a488035
TO
37class api_v3_ContributionPageTest extends CiviUnitTestCase {
38 protected $_apiversion = 3;
39 protected $testAmount = 34567;
40 protected $params;
41 protected $id = 0;
42 protected $contactIds = array();
43 protected $_entity = 'contribution_page';
6c6e6187 44 protected $contribution_result = NULL;
f64a217a 45 protected $_priceSetParams = array();
f8fe0df6 46 /**
eceb18cc 47 * Payment processor details.
f8fe0df6 48 * @var array
49 */
50 protected $_paymentProcessor = array();
f64a217a
EM
51
52 /**
53 * @var array
16b10e64
CW
54 * - contribution_page
55 * - price_set
56 * - price_field
57 * - price_field_value
f64a217a
EM
58 */
59 protected $_ids = array();
60
b7c9bc4c 61
6a488035 62 public $DBResetRequired = TRUE;
5896d037 63
6a488035
TO
64 public function setUp() {
65 parent::setUp();
66 $this->contactIds[] = $this->individualCreate();
67 $this->params = array(
6a488035
TO
68 'title' => "Test Contribution Page",
69 'financial_type_id' => 1,
70 'currency' => 'NZD',
71 'goal_amount' => $this->testAmount,
6cdac50b 72 'is_pay_later' => 1,
f64a217a 73 'is_monetary' => TRUE,
dd1b539a 74 'is_email_receipt' => TRUE,
75 'receipt_from_email' => 'yourconscience@donate.com',
76 'receipt_from_name' => 'Ego Freud',
f64a217a
EM
77 );
78
79 $this->_priceSetParams = array(
80 'is_quick_config' => 1,
81 'extends' => 'CiviContribute',
82 'financial_type_id' => 'Donation',
21dfd5f5 83 'title' => 'my Page',
6a488035
TO
84 );
85 }
86
00be9182 87 public function tearDown() {
6a488035 88 foreach ($this->contactIds as $id) {
fc928539 89 $this->callAPISuccess('contact', 'delete', array('id' => $id));
611c7ece 90 }
f9342903 91 $this->quickCleanUpFinancialEntities();
6a488035
TO
92 }
93
94 public function testCreateContributionPage() {
fc928539 95 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
96 $this->assertEquals(1, $result['count']);
97 $this->assertNotNull($result['values'][$result['id']]['id']);
6a488035
TO
98 $this->getAndCheck($this->params, $result['id'], $this->_entity);
99 }
100
101 public function testGetBasicContributionPage() {
fc928539 102 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 103 $this->id = $createResult['id'];
6a488035 104 $getParams = array(
6a488035
TO
105 'currency' => 'NZD',
106 'financial_type_id' => 1,
107 );
fc928539 108 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
109 $this->assertEquals(1, $getResult['count']);
6a488035
TO
110 }
111
112 public function testGetContributionPageByAmount() {
fc928539 113 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
6a488035 114 $this->id = $createResult['id'];
6a488035 115 $getParams = array(
5896d037 116 'amount' => '' . $this->testAmount, // 3456
6a488035
TO
117 'currency' => 'NZD',
118 'financial_type_id' => 1,
119 );
fc928539 120 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
121 $this->assertEquals(1, $getResult['count']);
6a488035
TO
122 }
123
124 public function testDeleteContributionPage() {
fc928539 125 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
126 $deleteParams = array('id' => $createResult['id']);
611c7ece 127 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
fc928539 128 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
129 $this->assertEquals(0, $checkDeleted['count']);
6a488035
TO
130 }
131
132 public function testGetFieldsContributionPage() {
fc928539 133 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
6a488035
TO
134 $this->assertEquals(12, $result['values']['start_date']['type']);
135 }
136
be26f3e0 137
d58b453e 138 /**
eceb18cc 139 * Test form submission with basic price set.
d58b453e 140 */
be26f3e0 141 public function testSubmit() {
f64a217a
EM
142 $this->setUpContributionPage();
143 $priceFieldID = reset($this->_ids['price_field']);
144 $priceFieldValueID = reset($this->_ids['price_field_value']);
145 $submitParams = array(
146 'price_' . $priceFieldID => $priceFieldValueID,
147 'id' => (int) $this->_ids['contribution_page'],
21dfd5f5 148 'amount' => 10,
be26f3e0
EM
149 );
150
f64a217a
EM
151 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
152 $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
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']);
7bc789a5 286 }
70cc8754 287
f64a217a 288 /**
eceb18cc 289 * Test submit with a membership block in place.
f64a217a 290 */
f9342903
EM
291 public function testSubmitMembershipBlockNotSeparatePayment() {
292 $this->setUpMembershipContributionPage();
f64a217a 293 $submitParams = array(
3e28c791 294 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903 295 'id' => (int) $this->_ids['contribution_page'],
f64a217a
EM
296 'amount' => 10,
297 'billing_first_name' => 'Billy',
298 'billing_middle_name' => 'Goat',
299 'billing_last_name' => 'Gruff',
300 'selectMembership' => $this->_ids['membership_type'],
301
302 );
303
a828d7b8 304 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
f9342903 305 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
f64a217a 306 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
f9342903
EM
307 }
308
2f59d010 309 /**
310 * Test submit with a membership block in place.
311 */
312 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail() {
313 $mut = new CiviMailUtils($this, TRUE);
314 $this->setUpMembershipContributionPage();
315 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
316
317 $submitParams = array(
318 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
319 'id' => (int) $this->_ids['contribution_page'],
320 'amount' => 10,
321 'billing_first_name' => 'Billy',
322 'billing_middle_name' => 'Goat',
323 'billing_last_name' => 'Gruff',
324 'selectMembership' => $this->_ids['membership_type'],
325 'email-Primary' => 'billy-goat@the-bridge.net',
9daadfce 326 'payment_processor_id' => $this->_paymentProcessor['id'],
327 'credit_card_number' => '4111111111111111',
328 'credit_card_type' => 'Visa',
329 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
330 'cvv2' => 123,
2f59d010 331 );
332
333 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
334 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
335 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
336 $mut->checkMailLog(array(
337 'Membership Type: General',
338 ));
339 $mut->stop();
340 $mut->clearMessages();
341 }
342
343 /**
344 * Test submit with a membership block in place.
345 */
346 public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail() {
347 $mut = new CiviMailUtils($this, TRUE);
348 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
349 $this->setUpMembershipContributionPage();
350 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
351
352 $submitParams = array(
353 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
354 'id' => (int) $this->_ids['contribution_page'],
355 'amount' => 0,
356 'billing_first_name' => 'Billy',
357 'billing_middle_name' => 'Goat',
358 'billing_last_name' => 'Gruffier',
359 'selectMembership' => $this->_ids['membership_type'],
360 'email-Primary' => 'billy-goat@the-new-bridge.net',
361 );
362
363 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
364 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
365 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
366 $mut->checkMailLog(array(
367 'Membership Type: General',
368 'Gruffier',
369 ),
370 array(
371 'Amount',
372 )
373 );
374 $mut->stop();
9daadfce 375 $mut->clearMessages(999);
2f59d010 376 }
377
f9342903 378 /**
eceb18cc 379 * Test submit with a membership block in place.
f9342903
EM
380 */
381 public function testSubmitMembershipBlockIsSeparatePayment() {
382 $this->setUpMembershipContributionPage(TRUE);
9daadfce 383 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 384 $submitParams = array(
3e28c791 385 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903
EM
386 'id' => (int) $this->_ids['contribution_page'],
387 'amount' => 10,
388 'billing_first_name' => 'Billy',
389 'billing_middle_name' => 'Goat',
390 'billing_last_name' => 'Gruff',
391 'selectMembership' => $this->_ids['membership_type'],
392 );
393
a828d7b8 394 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
f9342903
EM
395 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
396 $this->assertCount(2, $contributions['values']);
397 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
398 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
399 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
400 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
401 }
f64a217a 402
2f59d010 403 /**
404 * Test submit with a membership block in place.
405 */
406 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
407 $mut = new CiviMailUtils($this, TRUE);
408 $this->setUpMembershipContributionPage(TRUE);
409 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
410
411 $submitParams = array(
412 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
413 'id' => (int) $this->_ids['contribution_page'],
414 'amount' => 10,
415 'billing_first_name' => 'Billy',
416 'billing_middle_name' => 'Goat',
417 'billing_last_name' => 'Gruff',
418 'selectMembership' => $this->_ids['membership_type'],
419 'email-Primary' => 'billy-goat@the-bridge.net',
9daadfce 420 'payment_processor_id' => $this->_paymentProcessor['id'],
421 'credit_card_number' => '4111111111111111',
422 'credit_card_type' => 'Visa',
423 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
424 'cvv2' => 123,
2f59d010 425 );
426
427 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
428 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
429 $this->assertCount(2, $contributions['values']);
430 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
431 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
432 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
433 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
9daadfce 434 $mut->checkAllMailLog(array(
435 '$ 2.00',
2f59d010 436 'Membership Fee',
437 ));
438 $mut->stop();
9daadfce 439 $mut->clearMessages(999);
2f59d010 440 }
441
442 /**
443 * Test submit with a membership block in place.
444 */
445 public function testSubmitMembershipBlockIsSeparatePaymentZeroDollarsPayLaterWithEmail() {
446 $mut = new CiviMailUtils($this, TRUE);
447 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
448 $this->setUpMembershipContributionPage(TRUE);
449 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
450
451 $submitParams = array(
452 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
453 'id' => (int) $this->_ids['contribution_page'],
454 'amount' => 0,
455 'billing_first_name' => 'Billy',
456 'billing_middle_name' => 'Goat',
457 'billing_last_name' => 'Gruffalo',
458 'selectMembership' => $this->_ids['membership_type'],
459 'payment_processor_id' => 0,
460 'email-Primary' => 'gruffalo@the-bridge.net',
461 );
462
463 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
464 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
465 $this->assertCount(2, $contributions['values']);
466 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
467 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
468 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
469 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
470 $mut->checkMailLog(array(
471 'Gruffalo',
472 'General Membership: $ 0.00',
473 'Membership Fee',
474 ));
475 $mut->stop();
476 $mut->clearMessages();
477 }
478
797c4f88 479 /**
eceb18cc 480 * Test submit with a membership block in place.
797c4f88
EM
481 */
482 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
483 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
484 $this->_ids['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
485 $this->setUpMembershipContributionPage(TRUE);
486 $submitParams = array(
487 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][1],
488 'id' => (int) $this->_ids['contribution_page'],
489 'amount' => 10,
490 'billing_first_name' => 'Billy',
491 'billing_middle_name' => 'Goat',
492 'billing_last_name' => 'Gruff',
493 'selectMembership' => $this->_ids['membership_type'][1],
494 );
495
a828d7b8 496 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
6c6e6187 497 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
797c4f88
EM
498 $this->assertCount(2, $contributions['values']);
499 $ids = array_keys($contributions['values']);
500 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
501 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
502 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
503 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
504 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
505 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
506 }
507
8ca611b7 508 /**
eceb18cc 509 * Test submit with a membership block in place.
329f3f66
EM
510 *
511 * We are expecting a separate payment for the membership vs the contribution.
8ca611b7 512 */
329f3f66 513 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
8ca611b7 514 $this->setUpMembershipContributionPage(TRUE);
0193ebdb 515 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
516 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
8ca611b7
EM
517 $submitParams = array(
518 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
519 'id' => (int) $this->_ids['contribution_page'],
520 'amount' => 10,
521 'billing_first_name' => 'Billy',
522 'billing_middle_name' => 'Goat',
523 'billing_last_name' => 'Gruff',
524 'selectMembership' => $this->_ids['membership_type'],
7bc789a5 525 'payment_processor_id' => $this->_paymentProcessor['id'],
8ca611b7
EM
526 'credit_card_number' => '4111111111111111',
527 'credit_card_type' => 'Visa',
5896d037 528 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
8ca611b7
EM
529 'cvv2' => 123,
530 );
05990684 531
a828d7b8 532 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 533 $contributions = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
534 'contribution_page_id' => $this->_ids['contribution_page'],
535 'contribution_status_id' => 1,
536 ));
8ca611b7
EM
537 $this->assertCount(2, $contributions['values']);
538 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
539 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
540 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
541 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
0193ebdb 542 foreach ($contributions['values'] as $contribution) {
543 $this->assertEquals(.72, $contribution['fee_amount']);
544 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
545 }
8ca611b7 546 }
f8fe0df6 547
5961bd47
EM
548 /**
549 * Test that when a transaction fails the pending contribution remains.
550 *
551 * An activity should also be created. CRM-16417.
552 */
553 public function testSubmitPaymentProcessorFailure() {
554 $this->setUpContributionPage();
555 $this->setupPaymentProcessor();
556 $this->createLoggedInUser();
557 $priceFieldID = reset($this->_ids['price_field']);
558 $priceFieldValueID = reset($this->_ids['price_field_value']);
559 $submitParams = array(
560 'price_' . $priceFieldID => $priceFieldValueID,
561 'id' => (int) $this->_ids['contribution_page'],
562 'amount' => 10,
563 'payment_processor_id' => 1,
564 'credit_card_number' => '4111111111111111',
565 'credit_card_type' => 'Visa',
566 'credit_card_exp_date' => array('M' => 9, 'Y' => 2008),
567 'cvv2' => 123,
568 );
569
570 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
571 $contribution = $this->callAPISuccessGetSingle('contribution', array(
572 'contribution_page_id' => $this->_ids['contribution_page'],
573 'contribution_status_id' => 2,
574 ));
575
576 $this->callAPISuccessGetSingle('activity', array(
577 'source_record_id' => $contribution['id'],
578 'activity_type_id' => 'Failed Payment',
579 ));
580
5961bd47
EM
581 }
582
f8fe0df6 583 /**
dd1b539a 584 * Test submit recurring membership with immediate confirmation (IATS style).
585 *
e6fa4056 586 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1fee3ad2 587 * processor (IATS style - denoted by returning trxn_id)
e6fa4056
EM
588 * - the first creates a new membership, completed contribution, in progress recurring. Check these
589 * - create another - end date should be extended
f8fe0df6 590 */
8bef2e38 591 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
f8fe0df6 592 $this->params['is_recur'] = 1;
f8fe0df6 593 $this->params['recur_frequency_unit'] = 'month';
594 $this->setUpMembershipContributionPage();
7c85dc65 595 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
8bef2e38 596 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
f8fe0df6 597
598 $submitParams = array(
599 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
600 'id' => (int) $this->_ids['contribution_page'],
601 'amount' => 10,
602 'billing_first_name' => 'Billy',
603 'billing_middle_name' => 'Goat',
604 'billing_last_name' => 'Gruff',
605 'email' => 'billy@goat.gruff',
606 'selectMembership' => $this->_ids['membership_type'],
579a1fb7 607 'payment_processor_id' => 1,
f8fe0df6 608 'credit_card_number' => '4111111111111111',
609 'credit_card_type' => 'Visa',
5896d037 610 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 611 'cvv2' => 123,
612 'is_recur' => 1,
613 'frequency_interval' => 1,
614 'frequency_unit' => 'month',
615 );
616
a828d7b8 617 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 618 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
619 'contribution_page_id' => $this->_ids['contribution_page'],
620 'contribution_status_id' => 1,
621 ));
0739d6cf 622
623 $this->assertEquals('create_first_success', $contribution['trxn_id']);
f8fe0df6 624 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
625 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
626 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
627 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
628 $this->assertEquals(1, $membership['status_id']);
629 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
e6fa4056
EM
630 //@todo - check with Joe about these not existing
631 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
f8fe0df6 632 //renew it with processor setting completed - should extend membership
633 $submitParams['contact_id'] = $contribution['contact_id'];
8bef2e38 634 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
f8fe0df6 635 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
5896d037 636 $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
637 'id' => array('NOT IN' => array($contribution['id'])),
638 'contribution_page_id' => $this->_ids['contribution_page'],
639 'contribution_status_id' => 1,
640 ));
f8fe0df6 641 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
642 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
643 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
91259407 644 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
f8fe0df6 645 }
646
449f4c90 647 /**
648 * Test submit recurring membership with immediate confirmation (IATS style).
649 *
650 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
651 * processor (IATS style - denoted by returning trxn_id)
652 * - the first creates a new membership, completed contribution, in progress recurring. Check these
653 * - create another - end date should be extended
654 */
655 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
656
657 $this->setUpMembershipContributionPage(TRUE);
658 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
659 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
660
661 $submitParams = array(
662 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
663 'id' => (int) $this->_ids['contribution_page'],
664 'amount' => 10,
665 'billing_first_name' => 'Billy',
666 'billing_middle_name' => 'Goat',
667 'billing_last_name' => 'Gruff',
668 'email' => 'billy@goat.gruff',
669 'selectMembership' => $this->_ids['membership_type'],
670 'payment_processor_id' => 1,
671 'credit_card_number' => '4111111111111111',
672 'credit_card_type' => 'Visa',
673 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
674 'cvv2' => 123,
675 'is_recur' => 1,
676 'auto_renew' => TRUE,
677 'frequency_interval' => 1,
678 'frequency_unit' => 'month',
679 );
680
681 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
682 $contribution = $this->callAPISuccess('contribution', 'get', array(
683 'contribution_page_id' => $this->_ids['contribution_page'],
684 'contribution_status_id' => 1,
685 ));
686
687 $this->assertEquals(2, $contribution['count']);
688 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
689 $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
690 $this->assertNotEmpty($contribution['values'][$membershipPayment['contribution_id']]['contribution_recur_id']);
691 $this->callAPISuccess('contribution_recur', 'getsingle', array());
692 }
693
f8fe0df6 694 /**
e6fa4056
EM
695 * Test submit recurring membership with delayed confirmation (Authorize.net style)
696 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
697 * processor (Authorize.net style - denoted by NOT returning trxn_id)
698 * - the first creates a pending membership, pending contribution, penging recurring. Check these
699 * - complete the transaction
700 * - create another - end date should NOT be extended
f8fe0df6 701 */
702 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
703 $this->params['is_recur'] = 1;
f8fe0df6 704 $this->params['recur_frequency_unit'] = 'month';
705 $this->setUpMembershipContributionPage();
ab30e033 706 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
b5eacf76 707 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
f8fe0df6 708
709 $submitParams = array(
710 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
711 'id' => (int) $this->_ids['contribution_page'],
712 'amount' => 10,
713 'billing_first_name' => 'Billy',
714 'billing_middle_name' => 'Goat',
715 'billing_last_name' => 'Gruff',
716 'email' => 'billy@goat.gruff',
717 'selectMembership' => $this->_ids['membership_type'],
579a1fb7 718 'payment_processor_id' => 1,
f8fe0df6 719 'credit_card_number' => '4111111111111111',
720 'credit_card_type' => 'Visa',
5896d037 721 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 722 'cvv2' => 123,
e6fa4056
EM
723 'is_recur' => 1,
724 'frequency_interval' => 1,
725 'frequency_unit' => 'month',
f8fe0df6 726 );
727
a828d7b8 728 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 729 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
730 'contribution_page_id' => $this->_ids['contribution_page'],
731 'contribution_status_id' => 2,
732 ));
f8fe0df6 733 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
734 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
735 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
736 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
e6fa4056
EM
737 $this->assertEquals(5, $membership['status_id']);
738 //@todo - check with Joe about these not existing
739 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
5896d037 740 $this->callAPISuccess('contribution', 'completetransaction', array(
92915c55
TO
741 'id' => $contribution['id'],
742 'trxn_id' => 'ipn_called',
b396c447 743 'payment_processor_id' => $this->_paymentProcessor['id'],
92915c55 744 ));
e6fa4056 745 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
f8fe0df6 746 //renew it with processor setting completed - should extend membership
747 $submitParams = array_merge($submitParams, array(
5896d037
TO
748 'contact_id' => $contribution['contact_id'],
749 'is_recur' => 1,
750 'frequency_interval' => 1,
751 'frequency_unit' => 'month',
752 )
f8fe0df6 753 );
b5eacf76 754 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
f8fe0df6 755 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
6c6e6187 756 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
5896d037 757 'id' => array(
21dfd5f5 758 'NOT IN' => array($contribution['id']),
5896d037
TO
759 ),
760 'contribution_page_id' => $this->_ids['contribution_page'],
21dfd5f5 761 'contribution_status_id' => 2,
5896d037 762 )
f8fe0df6 763 );
e6fa4056 764
f8fe0df6 765 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
766 //no renewal as the date hasn't changed
767 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
e6fa4056
EM
768 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
769 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
f8fe0df6 770 }
771
f9342903 772 /**
eceb18cc 773 * Set up membership contribution page.
f9342903
EM
774 * @param bool $isSeparatePayment
775 */
00be9182 776 public function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
f9342903 777 $this->setUpMembershipBlockPriceSet();
a380f4a0 778 $this->setupPaymentProcessor();
f9342903
EM
779 $this->setUpContributionPage();
780
781 $this->callAPISuccess('membership_block', 'create', array(
782 'entity_id' => $this->_ids['contribution_page'],
783 'entity_table' => 'civicrm_contribution_page',
784 'is_required' => TRUE,
785 'is_active' => TRUE,
786 'is_separate_payment' => $isSeparatePayment,
787 'membership_type_default' => $this->_ids['membership_type'],
788 ));
f64a217a
EM
789 }
790
f9342903 791 /**
dd1b539a 792 * The default data set does not include a complete default membership price set - not quite sure why.
793 *
f9342903
EM
794 * This function ensures it exists & populates $this->_ids with it's data
795 */
00be9182 796 public function setUpMembershipBlockPriceSet() {
5896d037 797 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
92915c55
TO
798 'name' => 'default_membership_type_amount',
799 'return' => 'id',
800 ));
f9342903 801 if (empty($this->_ids['membership_type'])) {
3e28c791 802 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 803 }
3e28c791
EM
804 $priceField = $this->callAPISuccess('price_field', 'create', array(
805 'price_set_id' => reset($this->_ids['price_set']),
806 'name' => 'membership_amount',
807 'label' => 'Membership Amount',
808 'html_type' => 'Radio',
809 'sequential' => 1,
810 ));
811 $this->_ids['price_field'][] = $priceField['id'];
dd1b539a 812
3e28c791
EM
813 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
814 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
f9342903
EM
815 'name' => 'membership_amount',
816 'label' => 'Membership Amount',
3e28c791 817 'amount' => 1,
43dbd988 818 'financial_type_id' => 'Donation',
3e28c791
EM
819 'format.only_id' => TRUE,
820 'membership_type_id' => $membershipTypeID,
821 'price_field_id' => $priceField['id'],
f9342903 822 ));
3e28c791 823 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
f9342903
EM
824 }
825 }
3e28c791 826
2f59d010 827 /**
828 * Add text field other amount to the price set.
829 */
830 public function addOtherAmountFieldToMembershipPriceSet() {
831 $this->_ids['price_field']['other_amount'] = $this->callAPISuccess('price_field', 'create', array(
832 'price_set_id' => reset($this->_ids['price_set']),
833 'name' => 'other_amount',
834 'label' => 'Other Amount',
835 'html_type' => 'Text',
836 'format.only_id' => TRUE,
837 'sequential' => 1,
838 ));
839 $this->_ids['price_field_value']['other_amount'] = $this->callAPISuccess('price_field_value', 'create', array(
840 'financial_type_id' => 'Donation',
841 'format.only_id' => TRUE,
842 'label' => 'Other Amount',
843 'amount' => 1,
844 'price_field_id' => $this->_ids['price_field']['other_amount'],
845 ));
846 }
847
f64a217a 848 /**
eceb18cc 849 * Help function to set up contribution page with some defaults.
f64a217a 850 */
00be9182 851 public function setUpContributionPage() {
f64a217a
EM
852 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
853 if (empty($this->_ids['price_set'])) {
854 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
855 $this->_ids['price_set'][] = $priceSet['id'];
856 }
857 $priceSetID = reset($this->_ids['price_set']);
5896d037 858 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
f9342903
EM
859
860 if (empty($this->_ids['price_field'])) {
861 $priceField = $this->callAPISuccess('price_field', 'create', array(
862 'price_set_id' => $priceSetID,
863 'label' => 'Goat Breed',
864 'html_type' => 'Radio',
865 ));
866 $this->_ids['price_field'] = array($priceField['id']);
867 }
868 if (empty($this->_ids['price_field_value'])) {
869 $this->callAPISuccess('price_field_value', 'create', array(
870 'price_set_id' => $priceSetID,
871 'price_field_id' => $priceField['id'],
872 'label' => 'Long Haired Goat',
43dbd988 873 'financial_type_id' => 'Donation',
f9342903 874 'amount' => 20,
3300bf67 875 'financial_type_id' => 'Donation',
f9342903
EM
876 )
877 );
878 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
879 'price_set_id' => $priceSetID,
880 'price_field_id' => $priceField['id'],
881 'label' => 'Shoe-eating Goat',
43dbd988 882 'financial_type_id' => 'Donation',
f9342903 883 'amount' => 10,
3300bf67 884 'financial_type_id' => 'Donation',
f9342903
EM
885 )
886 );
887 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
888 }
f64a217a 889 $this->_ids['contribution_page'] = $contributionPageResult['id'];
be26f3e0
EM
890 }
891
6a488035 892 public static function setUpBeforeClass() {
6c6e6187 893 // put stuff here that should happen before all tests in this unit
6a488035
TO
894 }
895
5896d037 896 public static function tearDownAfterClass() {
6a488035
TO
897 $tablesToTruncate = array(
898 'civicrm_contact',
899 'civicrm_financial_type',
900 'civicrm_contribution',
901 'civicrm_contribution_page',
902 );
903 $unitTest = new CiviUnitTestCase();
904 $unitTest->quickCleanup($tablesToTruncate);
905 }
96025800 906
a380f4a0
EM
907 /**
908 * Create a payment processor instance.
909 */
910 protected function setupPaymentProcessor() {
911 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array(
912 'payment_processor_type_id' => 'Dummy',
913 'class_name' => 'Payment_Dummy',
914 'billing_mode' => 1,
915 ));
916 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
917 }
918
6a488035 919}