Merge branch 'master' of github.com:webaccess/civicrm-core into sales-tax
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionPageTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 require_once 'CiviTest/CiviUnitTestCase.php';
29
30
31 /**
32 * Test APIv3 civicrm_contribute_recur* functions
33 *
34 * @package CiviCRM_APIv3
35 * @subpackage API_Contribution
36 */
37
38 class api_v3_ContributionPageTest extends CiviUnitTestCase {
39 protected $_apiversion = 3;
40 protected $testAmount = 34567;
41 protected $params;
42 protected $id = 0;
43 protected $contactIds = array();
44 protected $_entity = 'contribution_page';
45 protected $contribution_result = null;
46 protected $_priceSetParams = array();
47 /**
48 * Payment processor details
49 * @var array
50 */
51 protected $_paymentProcessor = array();
52
53 /**
54 * @var array
55 * - contribution_page
56 * - price_set
57 * - price_field
58 * - price_field_value
59 */
60 protected $_ids = array();
61
62
63 public $DBResetRequired = TRUE;
64 public function setUp() {
65 parent::setUp();
66 $this->contactIds[] = $this->individualCreate();
67 $this->params = array(
68 'title' => "Test Contribution Page",
69 'financial_type_id' => 1,
70 'currency' => 'NZD',
71 'goal_amount' => $this->testAmount,
72 'is_pay_later' => 1,
73 'is_monetary' => TRUE,
74 );
75
76 $this->_priceSetParams = array(
77 'is_quick_config' => 1,
78 'extends' => 'CiviContribute',
79 'financial_type_id' => 'Donation',
80 'title' => 'my Page'
81 );
82 }
83
84 function tearDown() {
85 foreach ($this->contactIds as $id) {
86 $this->callAPISuccess('contact', 'delete', array('id' => $id));
87 }
88 $this->quickCleanUpFinancialEntities();
89 }
90
91 public function testCreateContributionPage() {
92 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->params, __FUNCTION__, __FILE__);
93 $this->assertEquals(1, $result['count']);
94 $this->assertNotNull($result['values'][$result['id']]['id']);
95 $this->getAndCheck($this->params, $result['id'], $this->_entity);
96 }
97
98 public function testGetBasicContributionPage() {
99 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
100 $this->id = $createResult['id'];
101 $getParams = array(
102 'currency' => 'NZD',
103 'financial_type_id' => 1,
104 );
105 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
106 $this->assertEquals(1, $getResult['count']);
107 }
108
109 public function testGetContributionPageByAmount() {
110 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
111 $this->id = $createResult['id'];
112 $getParams = array(
113 'amount' => ''. $this->testAmount, // 3456
114 'currency' => 'NZD',
115 'financial_type_id' => 1,
116 );
117 $getResult = $this->callAPIAndDocument($this->_entity, 'get', $getParams, __FUNCTION__, __FILE__);
118 $this->assertEquals(1, $getResult['count']);
119 }
120
121 public function testDeleteContributionPage() {
122 $createResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
123 $deleteParams = array('id' => $createResult['id']);
124 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
125 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', array());
126 $this->assertEquals(0, $checkDeleted['count']);
127 }
128
129 public function testGetFieldsContributionPage() {
130 $result = $this->callAPISuccess($this->_entity, 'getfields', array('action' => 'create'));
131 $this->assertEquals(12, $result['values']['start_date']['type']);
132 }
133
134
135 /**
136 * Test form submission with basic price set
137 */
138 public function testSubmit() {
139 $this->setUpContributionPage();
140 $priceFieldID = reset($this->_ids['price_field']);
141 $priceFieldValueID = reset($this->_ids['price_field_value']);
142 $submitParams = array(
143 'price_' . $priceFieldID => $priceFieldValueID,
144 'id' => (int) $this->_ids['contribution_page'],
145 'amount' => 10
146 );
147
148 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
149 $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
150 }
151 /**
152 * Test submit with a membership block in place
153 */
154 public function testSubmitMembershipBlockNotSeparatePayment() {
155 $this->setUpMembershipContributionPage();
156 $submitParams = array(
157 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
158 'id' => (int) $this->_ids['contribution_page'],
159 'amount' => 10,
160 'billing_first_name' => 'Billy',
161 'billing_middle_name' => 'Goat',
162 'billing_last_name' => 'Gruff',
163 'selectMembership' => $this->_ids['membership_type'],
164
165 );
166
167 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
168 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
169 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
170 }
171
172 /**
173 * Test submit with a membership block in place
174 */
175 public function testSubmitMembershipBlockIsSeparatePayment() {
176 $this->setUpMembershipContributionPage(TRUE);
177 $submitParams = array(
178 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
179 'id' => (int) $this->_ids['contribution_page'],
180 'amount' => 10,
181 'billing_first_name' => 'Billy',
182 'billing_middle_name' => 'Goat',
183 'billing_last_name' => 'Gruff',
184 'selectMembership' => $this->_ids['membership_type'],
185 );
186
187 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
188 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
189 $this->assertCount(2, $contributions['values']);
190 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
191 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
192 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
193 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
194 }
195
196 /**
197 * Test submit with a membership block in place
198 */
199 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
200 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
201 $this->_ids['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
202 $this->setUpMembershipContributionPage(TRUE);
203 $submitParams = array(
204 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][1],
205 'id' => (int) $this->_ids['contribution_page'],
206 'amount' => 10,
207 'billing_first_name' => 'Billy',
208 'billing_middle_name' => 'Goat',
209 'billing_last_name' => 'Gruff',
210 'selectMembership' => $this->_ids['membership_type'][1],
211 );
212
213 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
214 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page'],));
215 $this->assertCount(2, $contributions['values']);
216 $ids = array_keys($contributions['values']);
217 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
218 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
219 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
220 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
221 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
222 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
223 }
224
225 /**
226 * Test submit with a membership block in place
227 */
228 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessor() {
229 $this->setUpMembershipContributionPage(TRUE);
230 $submitParams = array(
231 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
232 'id' => (int) $this->_ids['contribution_page'],
233 'amount' => 10,
234 'billing_first_name' => 'Billy',
235 'billing_middle_name' => 'Goat',
236 'billing_last_name' => 'Gruff',
237 'selectMembership' => $this->_ids['membership_type'],
238 'payment_processor' => 1,
239 'credit_card_number' => '4111111111111111',
240 'credit_card_type' => 'Visa',
241 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040 ),
242 'cvv2' => 123,
243 );
244
245 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
246 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 1));
247 $this->assertCount(2, $contributions['values']);
248 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
249 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
250 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
251 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
252 }
253
254 /**
255 * Test submit recurring membership with immediate confirmation (IATS style)
256 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
257 * processor (IASTS style - denoted by returning trxn_id)
258 * - the first creates a new membership, completed contribution, in progress recurring. Check these
259 * - create another - end date should be extended
260 */
261 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecur() {
262 $this->params['is_recur'] = 1;
263 $var = array();
264 $this->params['recur_frequency_unit'] = 'month';
265 $this->setUpMembershipContributionPage();
266 $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor);
267 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 'create_first_success',));
268
269 $submitParams = array(
270 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
271 'id' => (int) $this->_ids['contribution_page'],
272 'amount' => 10,
273 'billing_first_name' => 'Billy',
274 'billing_middle_name' => 'Goat',
275 'billing_last_name' => 'Gruff',
276 'email' => 'billy@goat.gruff',
277 'selectMembership' => $this->_ids['membership_type'],
278 'payment_processor' => 1,
279 'credit_card_number' => '4111111111111111',
280 'credit_card_type' => 'Visa',
281 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040 ),
282 'cvv2' => 123,
283 'is_recur' => 1,
284 'frequency_interval' => 1,
285 'frequency_unit' => 'month',
286 );
287
288 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
289 $this->markTestIncomplete('CRM-15296 fix not yet applied');
290 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 1));
291 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
292 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
293 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
294 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
295 $this->assertEquals(1, $membership['status_id']);
296 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
297 //@todo - check with Joe about these not existing
298 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
299 //renew it with processor setting completed - should extend membership
300 $submitParams['contact_id'] = $contribution['contact_id'];
301 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 'create_second_success',));
302 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
303 $this->callAPISuccess('contribution', 'getsingle', array('id' => array('NOT IN' => array($contribution['id'])), 'contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 1));
304 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
305 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
306 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
307 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
308 }
309
310 /**
311 * Test submit recurring membership with delayed confirmation (Authorize.net style)
312 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
313 * processor (Authorize.net style - denoted by NOT returning trxn_id)
314 * - the first creates a pending membership, pending contribution, penging recurring. Check these
315 * - complete the transaction
316 * - create another - end date should NOT be extended
317 */
318 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
319 $this->params['is_recur'] = 1;
320 $this->params['recur_frequency_unit'] = 'month';
321 $this->setUpMembershipContributionPage();
322 $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor);
323 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 2,));
324
325 $submitParams = array(
326 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
327 'id' => (int) $this->_ids['contribution_page'],
328 'amount' => 10,
329 'billing_first_name' => 'Billy',
330 'billing_middle_name' => 'Goat',
331 'billing_last_name' => 'Gruff',
332 'email' => 'billy@goat.gruff',
333 'selectMembership' => $this->_ids['membership_type'],
334 'payment_processor' => 1,
335 'credit_card_number' => '4111111111111111',
336 'credit_card_type' => 'Visa',
337 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040 ),
338 'cvv2' => 123,
339 'is_recur' => 1,
340 'frequency_interval' => 1,
341 'frequency_unit' => 'month',
342 );
343
344 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
345 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 2));
346 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
347 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
348 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
349 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
350 $this->assertEquals(5, $membership['status_id']);
351 //@todo - check with Joe about these not existing
352 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
353 $this->markTestIncomplete('e-Notice - seemingly due to missing line items');
354 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_id' => 'ipn_called'));
355 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
356 //renew it with processor setting completed - should extend membership
357 $submitParams = array_merge($submitParams, array(
358 'contact_id' => $contribution['contact_id'],
359 'is_recur' => 1,
360 'frequency_interval' => 1,
361 'frequency_unit' => 'month',)
362 );
363 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 2,));
364 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
365 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => array(
366 'NOT IN' => array($contribution['id'])), 'contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 2)
367 );
368
369 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
370 //no renewal as the date hasn't changed
371 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
372 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
373 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
374 }
375
376 /**
377 * set up membership contribution page
378 * @param bool $isSeparatePayment
379 */
380 function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
381 $this->setUpMembershipBlockPriceSet();
382 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array(
383 'payment_processor_type_id' => 'Dummy',
384 'class_name' => 'Payment_Dummy',
385 'billing_mode' => 1,
386 ));
387 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
388 $this->setUpContributionPage();
389
390 $this->callAPISuccess('membership_block', 'create', array(
391 'entity_id' => $this->_ids['contribution_page'],
392 'entity_table' => 'civicrm_contribution_page',
393 'is_required' => TRUE,
394 'is_active' => TRUE,
395 'is_separate_payment' => $isSeparatePayment,
396 'membership_type_default' => $this->_ids['membership_type'],
397 ));
398 }
399
400 /**
401 * The default data set does not include a complete default membership price set - not quite sure why
402 * This function ensures it exists & populates $this->_ids with it's data
403 */
404 function setUpMembershipBlockPriceSet() {
405 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array('name' => 'default_membership_type_amount', 'return' => 'id'));
406 if (empty($this->_ids['membership_type'])) {
407 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
408 }
409 $priceField = $this->callAPISuccess('price_field', 'create', array(
410 'price_set_id' => reset($this->_ids['price_set']),
411 'name' => 'membership_amount',
412 'label' => 'Membership Amount',
413 'html_type' => 'Radio',
414 'sequential' => 1,
415 ));
416 $this->_ids['price_field'][] = $priceField['id'];
417 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
418 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
419 'name' => 'membership_amount',
420 'label' => 'Membership Amount',
421 'amount' => 1,
422 'financial_type_id' => 1,
423 'format.only_id' => TRUE,
424 'membership_type_id' => $membershipTypeID,
425 'price_field_id' => $priceField['id'],
426 ));
427 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
428 }
429 }
430
431 /**
432 * help function to set up contribution page with some defaults
433 */
434 function setUpContributionPage() {
435 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
436 if (empty($this->_ids['price_set'])) {
437 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
438 $this->_ids['price_set'][] = $priceSet['id'];
439 }
440 $priceSetID = reset($this->_ids['price_set']);
441 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
442
443 if (empty($this->_ids['price_field'])) {
444 $priceField = $this->callAPISuccess('price_field', 'create', array(
445 'price_set_id' => $priceSetID,
446 'label' => 'Goat Breed',
447 'html_type' => 'Radio',
448 ));
449 $this->_ids['price_field'] = array($priceField['id']);
450 }
451 if (empty($this->_ids['price_field_value'])) {
452 $this->callAPISuccess('price_field_value', 'create', array(
453 'price_set_id' => $priceSetID,
454 'price_field_id' => $priceField['id'],
455 'label' => 'Long Haired Goat',
456 'amount' => 20,
457 )
458 );
459 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
460 'price_set_id' => $priceSetID,
461 'price_field_id' => $priceField['id'],
462 'label' => 'Shoe-eating Goat',
463 'amount' => 10,
464 )
465 );
466 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
467 }
468 $this->_ids['contribution_page'] = $contributionPageResult['id'];
469 }
470
471 public static function setUpBeforeClass() {
472 // put stuff here that should happen before all tests in this unit
473 }
474
475 public static function tearDownAfterClass(){
476 $tablesToTruncate = array(
477 'civicrm_contact',
478 'civicrm_financial_type',
479 'civicrm_contribution',
480 'civicrm_contribution_page',
481 );
482 $unitTest = new CiviUnitTestCase();
483 $unitTest->quickCleanup($tablesToTruncate);
484 }
485 }
486