3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
28 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Test APIv3 civicrm_contribute_recur* functions
34 * @package CiviCRM_APIv3
35 * @subpackage API_Contribution
38 class api_v3_ContributionPageTest
extends CiviUnitTestCase
{
39 protected $_apiversion = 3;
40 protected $testAmount = 34567;
43 protected $contactIds = array();
44 protected $_entity = 'contribution_page';
45 protected $contribution_result = null;
46 protected $_priceSetParams = array();
48 * Payment processor details
51 protected $_paymentProcessor = array();
60 protected $_ids = array();
63 public $DBResetRequired = TRUE;
64 public function setUp() {
66 $this->contactIds
[] = $this->individualCreate();
67 $this->params
= array(
68 'title' => "Test Contribution Page",
69 'financial_type_id' => 1,
71 'goal_amount' => $this->testAmount
,
73 'is_monetary' => TRUE,
76 $this->_priceSetParams
= array(
77 'is_quick_config' => 1,
78 'extends' => 'CiviContribute',
79 'financial_type_id' => 'Donation',
85 foreach ($this->contactIds
as $id) {
86 $this->callAPISuccess('contact', 'delete', array('id' => $id));
88 $this->quickCleanUpFinancialEntities();
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
);
98 public function testGetBasicContributionPage() {
99 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
100 $this->id
= $createResult['id'];
103 'financial_type_id' => 1,
105 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
106 $this->assertEquals(1, $getResult['count']);
109 public function testGetContributionPageByAmount() {
110 $createResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
111 $this->id
= $createResult['id'];
113 'amount' => ''. $this->testAmount
, // 3456
115 'financial_type_id' => 1,
117 $getResult = $this->callAPIAndDocument($this->_entity
, 'get', $getParams, __FUNCTION__
, __FILE__
);
118 $this->assertEquals(1, $getResult['count']);
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']);
129 public function testGetFieldsContributionPage() {
130 $result = $this->callAPISuccess($this->_entity
, 'getfields', array('action' => 'create'));
131 $this->assertEquals(12, $result['values']['start_date']['type']);
136 * Test form submission with basic price set
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'],
148 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
149 $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page']));
152 * Test submit with a membership block in place
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'],
160 'billing_first_name' => 'Billy',
161 'billing_middle_name' => 'Goat',
162 'billing_last_name' => 'Gruff',
163 'selectMembership' => $this->_ids
['membership_type'],
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']));
173 * Test submit with a membership block in place
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'],
181 'billing_first_name' => 'Billy',
182 'billing_middle_name' => 'Goat',
183 'billing_last_name' => 'Gruff',
184 'selectMembership' => $this->_ids
['membership_type'],
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']);
197 * Test submit with a membership block in place
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'],
207 'billing_first_name' => 'Billy',
208 'billing_middle_name' => 'Goat',
209 'billing_last_name' => 'Gruff',
210 'selectMembership' => $this->_ids
['membership_type'][1],
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']);
226 * Test submit with a membership block in place
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'],
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 ),
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']);
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
261 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecur() {
262 $this->params
['is_recur'] = 1;
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',));
269 $submitParams = array(
270 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
271 'id' => (int) $this->_ids
['contribution_page'],
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 ),
284 'frequency_interval' => 1,
285 'frequency_unit' => 'month',
288 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL, 'Submit');
289 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page'], 'contribution_status_id' => 1));
290 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
291 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
292 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
293 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
294 $this->assertEquals(1, $membership['status_id']);
295 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
296 //@todo - check with Joe about these not existing
297 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
298 //renew it with processor setting completed - should extend membership
299 $submitParams['contact_id'] = $contribution['contact_id'];
300 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 'create_second_success',));
301 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
302 $this->callAPISuccess('contribution', 'getsingle', array('id' => array('NOT IN' => array($contribution['id'])), 'contribution_page_id' => $this->_ids
['contribution_page'], 'contribution_status_id' => 1));
303 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
304 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
305 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
306 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
310 * Test submit recurring membership with delayed confirmation (Authorize.net style)
311 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
312 * processor (Authorize.net style - denoted by NOT returning trxn_id)
313 * - the first creates a pending membership, pending contribution, penging recurring. Check these
314 * - complete the transaction
315 * - create another - end date should NOT be extended
317 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
318 $this->params
['is_recur'] = 1;
319 $this->params
['recur_frequency_unit'] = 'month';
320 $this->setUpMembershipContributionPage();
321 $dummyPP = CRM_Core_Payment
::singleton('live', $this->_paymentProcessor
);
322 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 2,));
324 $submitParams = array(
325 'price_' . $this->_ids
['price_field'][0] => reset($this->_ids
['price_field_value']),
326 'id' => (int) $this->_ids
['contribution_page'],
328 'billing_first_name' => 'Billy',
329 'billing_middle_name' => 'Goat',
330 'billing_last_name' => 'Gruff',
331 'email' => 'billy@goat.gruff',
332 'selectMembership' => $this->_ids
['membership_type'],
333 'payment_processor' => 1,
334 'credit_card_number' => '4111111111111111',
335 'credit_card_type' => 'Visa',
336 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040 ),
339 'frequency_interval' => 1,
340 'frequency_unit' => 'month',
343 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__
, __FILE__
, 'submit contribution page', NULL, 'Submit');
344 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids
['contribution_page'], 'contribution_status_id' => 2));
345 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
346 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
347 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
348 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
349 $this->assertEquals(5, $membership['status_id']);
350 //@todo - check with Joe about these not existing
351 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
352 $this->callAPISuccess('contribution', 'completetransaction', array('id' => $contribution['id'], 'trxn_id' => 'ipn_called'));
353 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
354 //renew it with processor setting completed - should extend membership
355 $submitParams = array_merge($submitParams, array(
356 'contact_id' => $contribution['contact_id'],
358 'frequency_interval' => 1,
359 'frequency_unit' => 'month',)
361 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 2,));
362 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
363 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => array(
364 'NOT IN' => array($contribution['id'])), 'contribution_page_id' => $this->_ids
['contribution_page'], 'contribution_status_id' => 2)
367 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
368 //no renewal as the date hasn't changed
369 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
370 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
371 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
375 * Set up membership contribution page
376 * @param bool $isSeparatePayment
378 function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
379 $this->setUpMembershipBlockPriceSet();
380 $this->params
['payment_processor_id'] = $this->_ids
['payment_processor'] = $this->paymentProcessorCreate(array(
381 'payment_processor_type_id' => 'Dummy',
382 'class_name' => 'Payment_Dummy',
385 $this->_paymentProcessor
= $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params
['payment_processor_id']));
386 $this->setUpContributionPage();
388 $this->callAPISuccess('membership_block', 'create', array(
389 'entity_id' => $this->_ids
['contribution_page'],
390 'entity_table' => 'civicrm_contribution_page',
391 'is_required' => TRUE,
393 'is_separate_payment' => $isSeparatePayment,
394 'membership_type_default' => $this->_ids
['membership_type'],
399 * The default data set does not include a complete default membership price set - not quite sure why
400 * This function ensures it exists & populates $this->_ids with it's data
402 function setUpMembershipBlockPriceSet() {
403 $this->_ids
['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array('name' => 'default_membership_type_amount', 'return' => 'id'));
404 if (empty($this->_ids
['membership_type'])) {
405 $this->_ids
['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
407 $priceField = $this->callAPISuccess('price_field', 'create', array(
408 'price_set_id' => reset($this->_ids
['price_set']),
409 'name' => 'membership_amount',
410 'label' => 'Membership Amount',
411 'html_type' => 'Radio',
414 $this->_ids
['price_field'][] = $priceField['id'];
415 foreach ($this->_ids
['membership_type'] as $membershipTypeID) {
416 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
417 'name' => 'membership_amount',
418 'label' => 'Membership Amount',
420 'financial_type_id' => 1,
421 'format.only_id' => TRUE,
422 'membership_type_id' => $membershipTypeID,
423 'price_field_id' => $priceField['id'],
425 $this->_ids
['price_field_value'][] = $priceFieldValue['id'];
430 * Help function to set up contribution page with some defaults
432 function setUpContributionPage() {
433 $contributionPageResult = $this->callAPISuccess($this->_entity
, 'create', $this->params
);
434 if (empty($this->_ids
['price_set'])) {
435 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams
);
436 $this->_ids
['price_set'][] = $priceSet['id'];
438 $priceSetID = reset($this->_ids
['price_set']);
439 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
441 if (empty($this->_ids
['price_field'])) {
442 $priceField = $this->callAPISuccess('price_field', 'create', array(
443 'price_set_id' => $priceSetID,
444 'label' => 'Goat Breed',
445 'html_type' => 'Radio',
447 $this->_ids
['price_field'] = array($priceField['id']);
449 if (empty($this->_ids
['price_field_value'])) {
450 $this->callAPISuccess('price_field_value', 'create', array(
451 'price_set_id' => $priceSetID,
452 'price_field_id' => $priceField['id'],
453 'label' => 'Long Haired Goat',
457 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
458 'price_set_id' => $priceSetID,
459 'price_field_id' => $priceField['id'],
460 'label' => 'Shoe-eating Goat',
464 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
466 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
469 public static function setUpBeforeClass() {
470 // put stuff here that should happen before all tests in this unit
473 public static function tearDownAfterClass(){
474 $tablesToTruncate = array(
476 'civicrm_financial_type',
477 'civicrm_contribution',
478 'civicrm_contribution_page',
480 $unitTest = new CiviUnitTestCase();
481 $unitTest->quickCleanup($tablesToTruncate);