Merge pull request #8420 from ineffyble/fix-membership-id-in-search-builder
[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
EM
149 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
150 $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
151 }
5896d037 152
870156d6 153 /**
154 * Test form submission with billing first & last name where the contact does NOT
155 * otherwise have one.
156 */
157 public function testSubmitNewBillingNameData() {
158 $this->setUpContributionPage();
159 $contact = $this->callAPISuccess('Contact', 'create', array('contact_type' => 'Individual', 'email' => 'wonderwoman@amazon.com'));
160 $priceFieldID = reset($this->_ids['price_field']);
161 $priceFieldValueID = reset($this->_ids['price_field_value']);
162 $submitParams = array(
163 'price_' . $priceFieldID => $priceFieldValueID,
164 'id' => (int) $this->_ids['contribution_page'],
165 'amount' => 10,
166 'billing_first_name' => 'Wonder',
167 'billing_last_name' => 'Woman',
168 'contactID' => $contact['id'],
169 'email' => 'wonderwoman@amazon.com',
170 );
171
172 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
173 $contact = $this->callAPISuccess('Contact', 'get', array(
174 'id' => $contact['id'],
175 'return' => array(
176 'first_name',
177 'last_name',
178 'sort_name',
179 'display_name',
180 ),
181 ));
182 $this->assertEquals(array(
183 'first_name' => 'Wonder',
184 'last_name' => 'Woman',
185 'display_name' => 'Wonder Woman',
186 'sort_name' => 'Woman, Wonder',
187 'id' => $contact['id'],
188 'contact_id' => $contact['id'],
189 ), $contact['values'][$contact['id']]);
190
191 }
192
193 /**
194 * Test form submission with billing first & last name where the contact does
195 * otherwise have one and should not be overwritten.
196 */
197 public function testSubmitNewBillingNameDoNotOverwrite() {
198 $this->setUpContributionPage();
199 $contact = $this->callAPISuccess('Contact', 'create', array(
200 'contact_type' => 'Individual',
201 'email' => 'wonderwoman@amazon.com',
202 'first_name' => 'Super',
203 'last_name' => 'Boy',
204 ));
205 $priceFieldID = reset($this->_ids['price_field']);
206 $priceFieldValueID = reset($this->_ids['price_field_value']);
207 $submitParams = array(
208 'price_' . $priceFieldID => $priceFieldValueID,
209 'id' => (int) $this->_ids['contribution_page'],
210 'amount' => 10,
211 'billing_first_name' => 'Wonder',
212 'billing_last_name' => 'Woman',
213 'contactID' => $contact['id'],
214 'email' => 'wonderwoman@amazon.com',
215 );
216
217 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
218 $contact = $this->callAPISuccess('Contact', 'get', array(
219 'id' => $contact['id'],
220 'return' => array(
221 'first_name',
222 'last_name',
223 'sort_name',
224 'display_name',
225 ),
226 ));
227 $this->assertEquals(array(
228 'first_name' => 'Super',
229 'last_name' => 'Boy',
230 'display_name' => 'Super Boy',
231 'sort_name' => 'Boy, Super',
232 'id' => $contact['id'],
233 'contact_id' => $contact['id'],
234 ), $contact['values'][$contact['id']]);
235
236 }
237
7bc789a5 238 /**
239 * Test process with instant payment when more than one configured for the page.
240 *
241 * CRM-16923
242 */
243 public function testSubmitRecurMultiProcessorInstantPayment() {
244 $this->setUpContributionPage();
245 $this->setupPaymentProcessor();
d27635dc 246 $paymentProcessor2ID = $this->paymentProcessorCreate(array(
7bc789a5 247 'payment_processor_type_id' => 'Dummy',
248 'name' => 'processor 2',
249 'class_name' => 'Payment_Dummy',
250 'billing_mode' => 1,
251 ));
252 $dummyPP = Civi\Payment\System::singleton()->getById($paymentProcessor2ID);
70cc8754 253 $dummyPP->setDoDirectPaymentResult(array(
254 'payment_status_id' => 1,
255 'trxn_id' => 'create_first_success',
256 'fee_amount' => .85,
257 ));
7bc789a5 258 $this->callAPISuccess('ContributionPage', 'create', array(
d27635dc 259 'id' => $this->_ids['contribution_page'],
260 'payment_processor' => array($paymentProcessor2ID, $this->_ids['payment_processor']),
7bc789a5 261 ));
262
263 $priceFieldID = reset($this->_ids['price_field']);
264 $priceFieldValueID = reset($this->_ids['price_field_value']);
265 $submitParams = array(
266 'price_' . $priceFieldID => $priceFieldValueID,
267 'id' => (int) $this->_ids['contribution_page'],
268 'amount' => 10,
269 'is_recur' => 1,
270 'frequency_interval' => 1,
271 'frequency_unit' => 'month',
272 'payment_processor_id' => $paymentProcessor2ID,
273 );
274
275 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
70cc8754 276 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
7bc789a5 277 'contribution_page_id' => $this->_ids['contribution_page'],
278 'contribution_status_id' => 1,
279 ));
70cc8754 280 $this->assertEquals('create_first_success', $contribution['trxn_id']);
281 $this->assertEquals(10, $contribution['total_amount']);
282 $this->assertEquals(.85, $contribution['fee_amount']);
283 $this->assertEquals(9.15, $contribution['net_amount']);
bf722049 284 $this->_checkFinancialRecords(array(
285 'id' => $contribution['id'],
286 'total_amount' => $contribution['total_amount'],
287 ), 'online');
7bc789a5 288 }
70cc8754 289
f64a217a 290 /**
eceb18cc 291 * Test submit with a membership block in place.
f64a217a 292 */
f9342903
EM
293 public function testSubmitMembershipBlockNotSeparatePayment() {
294 $this->setUpMembershipContributionPage();
f64a217a 295 $submitParams = array(
3e28c791 296 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903 297 'id' => (int) $this->_ids['contribution_page'],
f64a217a
EM
298 'amount' => 10,
299 'billing_first_name' => 'Billy',
300 'billing_middle_name' => 'Goat',
301 'billing_last_name' => 'Gruff',
302 'selectMembership' => $this->_ids['membership_type'],
303
304 );
305
a828d7b8 306 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
f9342903 307 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
f64a217a 308 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
f9342903
EM
309 }
310
2f59d010 311 /**
312 * Test submit with a membership block in place.
313 */
314 public function testSubmitMembershipBlockNotSeparatePaymentWithEmail() {
315 $mut = new CiviMailUtils($this, TRUE);
316 $this->setUpMembershipContributionPage();
317 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
318
319 $submitParams = array(
320 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
321 'id' => (int) $this->_ids['contribution_page'],
322 'amount' => 10,
323 'billing_first_name' => 'Billy',
324 'billing_middle_name' => 'Goat',
325 'billing_last_name' => 'Gruff',
326 'selectMembership' => $this->_ids['membership_type'],
327 'email-Primary' => 'billy-goat@the-bridge.net',
9daadfce 328 'payment_processor_id' => $this->_paymentProcessor['id'],
329 'credit_card_number' => '4111111111111111',
330 'credit_card_type' => 'Visa',
331 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
332 'cvv2' => 123,
2f59d010 333 );
334
335 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
336 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
337 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
338 $mut->checkMailLog(array(
339 'Membership Type: General',
340 ));
341 $mut->stop();
342 $mut->clearMessages();
343 }
344
345 /**
346 * Test submit with a membership block in place.
347 */
348 public function testSubmitMembershipBlockNotSeparatePaymentZeroDollarsWithEmail() {
349 $mut = new CiviMailUtils($this, TRUE);
350 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
351 $this->setUpMembershipContributionPage();
352 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
353
354 $submitParams = array(
355 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
356 'id' => (int) $this->_ids['contribution_page'],
357 'amount' => 0,
358 'billing_first_name' => 'Billy',
359 'billing_middle_name' => 'Goat',
360 'billing_last_name' => 'Gruffier',
361 'selectMembership' => $this->_ids['membership_type'],
362 'email-Primary' => 'billy-goat@the-new-bridge.net',
363 );
364
365 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
366 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
367 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
368 $mut->checkMailLog(array(
369 'Membership Type: General',
370 'Gruffier',
371 ),
372 array(
373 'Amount',
374 )
375 );
376 $mut->stop();
9daadfce 377 $mut->clearMessages(999);
2f59d010 378 }
379
f9342903 380 /**
eceb18cc 381 * Test submit with a membership block in place.
f9342903
EM
382 */
383 public function testSubmitMembershipBlockIsSeparatePayment() {
384 $this->setUpMembershipContributionPage(TRUE);
9daadfce 385 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 386 $submitParams = array(
3e28c791 387 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
f9342903
EM
388 'id' => (int) $this->_ids['contribution_page'],
389 'amount' => 10,
390 'billing_first_name' => 'Billy',
391 'billing_middle_name' => 'Goat',
392 'billing_last_name' => 'Gruff',
393 'selectMembership' => $this->_ids['membership_type'],
394 );
395
a828d7b8 396 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
f9342903
EM
397 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
398 $this->assertCount(2, $contributions['values']);
399 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
400 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
401 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
402 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
403 }
f64a217a 404
2f59d010 405 /**
406 * Test submit with a membership block in place.
407 */
408 public function testSubmitMembershipBlockIsSeparatePaymentWithEmail() {
409 $mut = new CiviMailUtils($this, TRUE);
410 $this->setUpMembershipContributionPage(TRUE);
411 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
412
413 $submitParams = array(
414 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
415 'id' => (int) $this->_ids['contribution_page'],
416 'amount' => 10,
417 'billing_first_name' => 'Billy',
418 'billing_middle_name' => 'Goat',
419 'billing_last_name' => 'Gruff',
420 'selectMembership' => $this->_ids['membership_type'],
421 'email-Primary' => 'billy-goat@the-bridge.net',
9daadfce 422 'payment_processor_id' => $this->_paymentProcessor['id'],
423 'credit_card_number' => '4111111111111111',
424 'credit_card_type' => 'Visa',
425 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
426 'cvv2' => 123,
2f59d010 427 );
428
429 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
430 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
431 $this->assertCount(2, $contributions['values']);
432 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
433 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
434 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
435 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
9daadfce 436 $mut->checkAllMailLog(array(
437 '$ 2.00',
2f59d010 438 'Membership Fee',
439 ));
440 $mut->stop();
9daadfce 441 $mut->clearMessages(999);
2f59d010 442 }
443
444 /**
445 * Test submit with a membership block in place.
446 */
447 public function testSubmitMembershipBlockIsSeparatePaymentZeroDollarsPayLaterWithEmail() {
448 $mut = new CiviMailUtils($this, TRUE);
449 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 0)));
450 $this->setUpMembershipContributionPage(TRUE);
451 $this->addProfile('supporter_profile', $this->_ids['contribution_page']);
452
453 $submitParams = array(
454 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
455 'id' => (int) $this->_ids['contribution_page'],
456 'amount' => 0,
457 'billing_first_name' => 'Billy',
458 'billing_middle_name' => 'Goat',
459 'billing_last_name' => 'Gruffalo',
460 'selectMembership' => $this->_ids['membership_type'],
461 'payment_processor_id' => 0,
462 'email-Primary' => 'gruffalo@the-bridge.net',
463 );
464
465 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
466 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
467 $this->assertCount(2, $contributions['values']);
468 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
469 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
470 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
471 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
472 $mut->checkMailLog(array(
473 'Gruffalo',
474 'General Membership: $ 0.00',
475 'Membership Fee',
476 ));
477 $mut->stop();
478 $mut->clearMessages();
479 }
480
797c4f88 481 /**
eceb18cc 482 * Test submit with a membership block in place.
797c4f88
EM
483 */
484 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
485 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
486 $this->_ids['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
487 $this->setUpMembershipContributionPage(TRUE);
488 $submitParams = array(
489 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][1],
490 'id' => (int) $this->_ids['contribution_page'],
491 'amount' => 10,
492 'billing_first_name' => 'Billy',
493 'billing_middle_name' => 'Goat',
494 'billing_last_name' => 'Gruff',
495 'selectMembership' => $this->_ids['membership_type'][1],
496 );
497
a828d7b8 498 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
6c6e6187 499 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
797c4f88
EM
500 $this->assertCount(2, $contributions['values']);
501 $ids = array_keys($contributions['values']);
502 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
503 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
504 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
505 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
506 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
507 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
508 }
509
8ca611b7 510 /**
eceb18cc 511 * Test submit with a membership block in place.
329f3f66
EM
512 *
513 * We are expecting a separate payment for the membership vs the contribution.
8ca611b7 514 */
329f3f66 515 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
8ca611b7 516 $this->setUpMembershipContributionPage(TRUE);
0193ebdb 517 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
518 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
8ca611b7
EM
519 $submitParams = array(
520 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
521 'id' => (int) $this->_ids['contribution_page'],
522 'amount' => 10,
523 'billing_first_name' => 'Billy',
524 'billing_middle_name' => 'Goat',
525 'billing_last_name' => 'Gruff',
526 'selectMembership' => $this->_ids['membership_type'],
7bc789a5 527 'payment_processor_id' => $this->_paymentProcessor['id'],
8ca611b7
EM
528 'credit_card_number' => '4111111111111111',
529 'credit_card_type' => 'Visa',
5896d037 530 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
8ca611b7
EM
531 'cvv2' => 123,
532 );
05990684 533
a828d7b8 534 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 535 $contributions = $this->callAPISuccess('contribution', 'get', array(
92915c55
TO
536 'contribution_page_id' => $this->_ids['contribution_page'],
537 'contribution_status_id' => 1,
538 ));
8ca611b7
EM
539 $this->assertCount(2, $contributions['values']);
540 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
541 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
542 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
543 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
0193ebdb 544 foreach ($contributions['values'] as $contribution) {
545 $this->assertEquals(.72, $contribution['fee_amount']);
546 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
547 }
8ca611b7 548 }
f8fe0df6 549
5961bd47
EM
550 /**
551 * Test that when a transaction fails the pending contribution remains.
552 *
553 * An activity should also be created. CRM-16417.
554 */
555 public function testSubmitPaymentProcessorFailure() {
556 $this->setUpContributionPage();
557 $this->setupPaymentProcessor();
558 $this->createLoggedInUser();
559 $priceFieldID = reset($this->_ids['price_field']);
560 $priceFieldValueID = reset($this->_ids['price_field_value']);
561 $submitParams = array(
562 'price_' . $priceFieldID => $priceFieldValueID,
563 'id' => (int) $this->_ids['contribution_page'],
564 'amount' => 10,
565 'payment_processor_id' => 1,
566 'credit_card_number' => '4111111111111111',
567 'credit_card_type' => 'Visa',
568 'credit_card_exp_date' => array('M' => 9, 'Y' => 2008),
569 'cvv2' => 123,
570 );
571
572 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
573 $contribution = $this->callAPISuccessGetSingle('contribution', array(
574 'contribution_page_id' => $this->_ids['contribution_page'],
575 'contribution_status_id' => 2,
576 ));
577
578 $this->callAPISuccessGetSingle('activity', array(
579 'source_record_id' => $contribution['id'],
580 'activity_type_id' => 'Failed Payment',
581 ));
582
5961bd47
EM
583 }
584
f8fe0df6 585 /**
dd1b539a 586 * Test submit recurring membership with immediate confirmation (IATS style).
587 *
e6fa4056 588 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
1fee3ad2 589 * processor (IATS style - denoted by returning trxn_id)
e6fa4056
EM
590 * - the first creates a new membership, completed contribution, in progress recurring. Check these
591 * - create another - end date should be extended
f8fe0df6 592 */
8bef2e38 593 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
f8fe0df6 594 $this->params['is_recur'] = 1;
f8fe0df6 595 $this->params['recur_frequency_unit'] = 'month';
596 $this->setUpMembershipContributionPage();
7c85dc65 597 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
8bef2e38 598 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
f8fe0df6 599
600 $submitParams = array(
601 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
602 'id' => (int) $this->_ids['contribution_page'],
603 'amount' => 10,
604 'billing_first_name' => 'Billy',
605 'billing_middle_name' => 'Goat',
606 'billing_last_name' => 'Gruff',
607 'email' => 'billy@goat.gruff',
608 'selectMembership' => $this->_ids['membership_type'],
579a1fb7 609 'payment_processor_id' => 1,
f8fe0df6 610 'credit_card_number' => '4111111111111111',
611 'credit_card_type' => 'Visa',
5896d037 612 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 613 'cvv2' => 123,
614 'is_recur' => 1,
615 'frequency_interval' => 1,
616 'frequency_unit' => 'month',
617 );
618
a828d7b8 619 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 620 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
621 'contribution_page_id' => $this->_ids['contribution_page'],
622 'contribution_status_id' => 1,
623 ));
0739d6cf 624
625 $this->assertEquals('create_first_success', $contribution['trxn_id']);
f8fe0df6 626 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
627 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
628 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
629 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
630 $this->assertEquals(1, $membership['status_id']);
631 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
e6fa4056
EM
632 //@todo - check with Joe about these not existing
633 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
f8fe0df6 634 //renew it with processor setting completed - should extend membership
635 $submitParams['contact_id'] = $contribution['contact_id'];
8bef2e38 636 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
f8fe0df6 637 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
5896d037 638 $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
639 'id' => array('NOT IN' => array($contribution['id'])),
640 'contribution_page_id' => $this->_ids['contribution_page'],
641 'contribution_status_id' => 1,
642 ));
f8fe0df6 643 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
644 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
645 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
91259407 646 $this->assertEquals(5, $recurringContribution['contribution_status_id']);
f8fe0df6 647 }
648
449f4c90 649 /**
650 * Test submit recurring membership with immediate confirmation (IATS style).
651 *
652 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
653 * processor (IATS style - denoted by returning trxn_id)
654 * - the first creates a new membership, completed contribution, in progress recurring. Check these
655 * - create another - end date should be extended
656 */
657 public function testSubmitMembershipPriceSetPaymentPaymentProcessorSeparatePaymentRecurInstantPayment() {
658
659 $this->setUpMembershipContributionPage(TRUE);
660 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
661 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
662
663 $submitParams = array(
664 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
665 'id' => (int) $this->_ids['contribution_page'],
666 'amount' => 10,
667 'billing_first_name' => 'Billy',
668 'billing_middle_name' => 'Goat',
669 'billing_last_name' => 'Gruff',
670 'email' => 'billy@goat.gruff',
671 'selectMembership' => $this->_ids['membership_type'],
672 'payment_processor_id' => 1,
673 'credit_card_number' => '4111111111111111',
674 'credit_card_type' => 'Visa',
675 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
676 'cvv2' => 123,
677 'is_recur' => 1,
678 'auto_renew' => TRUE,
679 'frequency_interval' => 1,
680 'frequency_unit' => 'month',
681 );
682
683 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
684 $contribution = $this->callAPISuccess('contribution', 'get', array(
685 'contribution_page_id' => $this->_ids['contribution_page'],
686 'contribution_status_id' => 1,
687 ));
688
689 $this->assertEquals(2, $contribution['count']);
690 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
691 $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
692 $this->assertNotEmpty($contribution['values'][$membershipPayment['contribution_id']]['contribution_recur_id']);
693 $this->callAPISuccess('contribution_recur', 'getsingle', array());
694 }
695
f8fe0df6 696 /**
e6fa4056
EM
697 * Test submit recurring membership with delayed confirmation (Authorize.net style)
698 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
699 * processor (Authorize.net style - denoted by NOT returning trxn_id)
700 * - the first creates a pending membership, pending contribution, penging recurring. Check these
701 * - complete the transaction
702 * - create another - end date should NOT be extended
f8fe0df6 703 */
704 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
705 $this->params['is_recur'] = 1;
f8fe0df6 706 $this->params['recur_frequency_unit'] = 'month';
707 $this->setUpMembershipContributionPage();
ab30e033 708 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
b5eacf76 709 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
f8fe0df6 710
711 $submitParams = array(
712 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
713 'id' => (int) $this->_ids['contribution_page'],
714 'amount' => 10,
715 'billing_first_name' => 'Billy',
716 'billing_middle_name' => 'Goat',
717 'billing_last_name' => 'Gruff',
718 'email' => 'billy@goat.gruff',
719 'selectMembership' => $this->_ids['membership_type'],
579a1fb7 720 'payment_processor_id' => 1,
f8fe0df6 721 'credit_card_number' => '4111111111111111',
722 'credit_card_type' => 'Visa',
5896d037 723 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
f8fe0df6 724 'cvv2' => 123,
e6fa4056
EM
725 'is_recur' => 1,
726 'frequency_interval' => 1,
727 'frequency_unit' => 'month',
f8fe0df6 728 );
729
a828d7b8 730 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
5896d037 731 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
92915c55
TO
732 'contribution_page_id' => $this->_ids['contribution_page'],
733 'contribution_status_id' => 2,
734 ));
f8fe0df6 735 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
736 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
737 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
738 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
e6fa4056
EM
739 $this->assertEquals(5, $membership['status_id']);
740 //@todo - check with Joe about these not existing
741 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
5896d037 742 $this->callAPISuccess('contribution', 'completetransaction', array(
92915c55
TO
743 'id' => $contribution['id'],
744 'trxn_id' => 'ipn_called',
b396c447 745 'payment_processor_id' => $this->_paymentProcessor['id'],
92915c55 746 ));
e6fa4056 747 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
f8fe0df6 748 //renew it with processor setting completed - should extend membership
749 $submitParams = array_merge($submitParams, array(
5896d037
TO
750 'contact_id' => $contribution['contact_id'],
751 'is_recur' => 1,
752 'frequency_interval' => 1,
753 'frequency_unit' => 'month',
754 )
f8fe0df6 755 );
b5eacf76 756 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
f8fe0df6 757 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
6c6e6187 758 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
5896d037 759 'id' => array(
21dfd5f5 760 'NOT IN' => array($contribution['id']),
5896d037
TO
761 ),
762 'contribution_page_id' => $this->_ids['contribution_page'],
21dfd5f5 763 'contribution_status_id' => 2,
5896d037 764 )
f8fe0df6 765 );
e6fa4056 766
f8fe0df6 767 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
768 //no renewal as the date hasn't changed
769 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
e6fa4056
EM
770 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
771 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
f8fe0df6 772 }
773
f9342903 774 /**
eceb18cc 775 * Set up membership contribution page.
f9342903
EM
776 * @param bool $isSeparatePayment
777 */
00be9182 778 public function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
f9342903 779 $this->setUpMembershipBlockPriceSet();
a380f4a0 780 $this->setupPaymentProcessor();
f9342903
EM
781 $this->setUpContributionPage();
782
783 $this->callAPISuccess('membership_block', 'create', array(
784 'entity_id' => $this->_ids['contribution_page'],
785 'entity_table' => 'civicrm_contribution_page',
786 'is_required' => TRUE,
787 'is_active' => TRUE,
788 'is_separate_payment' => $isSeparatePayment,
789 'membership_type_default' => $this->_ids['membership_type'],
790 ));
f64a217a
EM
791 }
792
f9342903 793 /**
dd1b539a 794 * The default data set does not include a complete default membership price set - not quite sure why.
795 *
f9342903
EM
796 * This function ensures it exists & populates $this->_ids with it's data
797 */
00be9182 798 public function setUpMembershipBlockPriceSet() {
5896d037 799 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
92915c55
TO
800 'name' => 'default_membership_type_amount',
801 'return' => 'id',
802 ));
f9342903 803 if (empty($this->_ids['membership_type'])) {
3e28c791 804 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
f9342903 805 }
3e28c791
EM
806 $priceField = $this->callAPISuccess('price_field', 'create', array(
807 'price_set_id' => reset($this->_ids['price_set']),
808 'name' => 'membership_amount',
809 'label' => 'Membership Amount',
810 'html_type' => 'Radio',
811 'sequential' => 1,
812 ));
813 $this->_ids['price_field'][] = $priceField['id'];
dd1b539a 814
3e28c791
EM
815 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
816 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
f9342903
EM
817 'name' => 'membership_amount',
818 'label' => 'Membership Amount',
3e28c791 819 'amount' => 1,
43dbd988 820 'financial_type_id' => 'Donation',
3e28c791
EM
821 'format.only_id' => TRUE,
822 'membership_type_id' => $membershipTypeID,
823 'price_field_id' => $priceField['id'],
f9342903 824 ));
3e28c791 825 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
f9342903
EM
826 }
827 }
3e28c791 828
2f59d010 829 /**
830 * Add text field other amount to the price set.
831 */
832 public function addOtherAmountFieldToMembershipPriceSet() {
833 $this->_ids['price_field']['other_amount'] = $this->callAPISuccess('price_field', 'create', array(
834 'price_set_id' => reset($this->_ids['price_set']),
835 'name' => 'other_amount',
836 'label' => 'Other Amount',
837 'html_type' => 'Text',
838 'format.only_id' => TRUE,
839 'sequential' => 1,
840 ));
841 $this->_ids['price_field_value']['other_amount'] = $this->callAPISuccess('price_field_value', 'create', array(
842 'financial_type_id' => 'Donation',
843 'format.only_id' => TRUE,
844 'label' => 'Other Amount',
845 'amount' => 1,
846 'price_field_id' => $this->_ids['price_field']['other_amount'],
847 ));
848 }
849
f64a217a 850 /**
eceb18cc 851 * Help function to set up contribution page with some defaults.
f64a217a 852 */
00be9182 853 public function setUpContributionPage() {
f64a217a
EM
854 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
855 if (empty($this->_ids['price_set'])) {
856 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
857 $this->_ids['price_set'][] = $priceSet['id'];
858 }
859 $priceSetID = reset($this->_ids['price_set']);
5896d037 860 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
f9342903
EM
861
862 if (empty($this->_ids['price_field'])) {
863 $priceField = $this->callAPISuccess('price_field', 'create', array(
864 'price_set_id' => $priceSetID,
865 'label' => 'Goat Breed',
866 'html_type' => 'Radio',
867 ));
868 $this->_ids['price_field'] = array($priceField['id']);
869 }
870 if (empty($this->_ids['price_field_value'])) {
871 $this->callAPISuccess('price_field_value', 'create', array(
872 'price_set_id' => $priceSetID,
873 'price_field_id' => $priceField['id'],
874 'label' => 'Long Haired Goat',
43dbd988 875 'financial_type_id' => 'Donation',
f9342903 876 'amount' => 20,
3300bf67 877 'financial_type_id' => 'Donation',
f9342903
EM
878 )
879 );
880 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
881 'price_set_id' => $priceSetID,
882 'price_field_id' => $priceField['id'],
883 'label' => 'Shoe-eating Goat',
43dbd988 884 'financial_type_id' => 'Donation',
f9342903 885 'amount' => 10,
3300bf67 886 'financial_type_id' => 'Donation',
f9342903
EM
887 )
888 );
889 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
890 }
f64a217a 891 $this->_ids['contribution_page'] = $contributionPageResult['id'];
be26f3e0
EM
892 }
893
6a488035 894 public static function setUpBeforeClass() {
6c6e6187 895 // put stuff here that should happen before all tests in this unit
6a488035
TO
896 }
897
5896d037 898 public static function tearDownAfterClass() {
6a488035
TO
899 $tablesToTruncate = array(
900 'civicrm_contact',
901 'civicrm_financial_type',
902 'civicrm_contribution',
903 'civicrm_contribution_page',
904 );
905 $unitTest = new CiviUnitTestCase();
906 $unitTest->quickCleanup($tablesToTruncate);
907 }
96025800 908
a380f4a0
EM
909 /**
910 * Create a payment processor instance.
911 */
912 protected function setupPaymentProcessor() {
913 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array(
914 'payment_processor_type_id' => 'Dummy',
915 'class_name' => 'Payment_Dummy',
916 'billing_mode' => 1,
917 ));
918 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
919 }
920
6a488035 921}