--CRM-17240, fixed tests
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionPageTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 class 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';
44 protected $contribution_result = NULL;
45 protected $_priceSetParams = array();
46 /**
47 * Payment processor details.
48 * @var array
49 */
50 protected $_paymentProcessor = array();
51
52 /**
53 * @var array
54 * - contribution_page
55 * - price_set
56 * - price_field
57 * - price_field_value
58 */
59 protected $_ids = array();
60
61
62 public $DBResetRequired = TRUE;
63
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 public 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 /**
153 * Test process with instant payment when more than one configured for the page.
154 *
155 * CRM-16923
156 */
157 public function testSubmitRecurMultiProcessorInstantPayment() {
158 $this->setUpContributionPage();
159 $this->setupPaymentProcessor();
160 $paymentProcessor2ID = $this->paymentProcessorCreate(array(
161 'payment_processor_type_id' => 'Dummy',
162 'name' => 'processor 2',
163 'class_name' => 'Payment_Dummy',
164 'billing_mode' => 1,
165 ));
166 $dummyPP = Civi\Payment\System::singleton()->getById($paymentProcessor2ID);
167 $dummyPP->setDoDirectPaymentResult(array(
168 'payment_status_id' => 1,
169 'trxn_id' => 'create_first_success',
170 'fee_amount' => .85,
171 ));
172 $this->callAPISuccess('ContributionPage', 'create', array(
173 'id' => $this->_ids['contribution_page'],
174 'payment_processor' => array($paymentProcessor2ID, $this->_ids['payment_processor']),
175 ));
176
177 $priceFieldID = reset($this->_ids['price_field']);
178 $priceFieldValueID = reset($this->_ids['price_field_value']);
179 $submitParams = array(
180 'price_' . $priceFieldID => $priceFieldValueID,
181 'id' => (int) $this->_ids['contribution_page'],
182 'amount' => 10,
183 'is_recur' => 1,
184 'frequency_interval' => 1,
185 'frequency_unit' => 'month',
186 'payment_processor_id' => $paymentProcessor2ID,
187 );
188
189 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
190 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
191 'contribution_page_id' => $this->_ids['contribution_page'],
192 'contribution_status_id' => 1,
193 ));
194 $this->assertEquals('create_first_success', $contribution['trxn_id']);
195 $this->assertEquals(10, $contribution['total_amount']);
196 $this->assertEquals(.85, $contribution['fee_amount']);
197 $this->assertEquals(9.15, $contribution['net_amount']);
198 }
199
200 /**
201 * Test submit with a membership block in place.
202 */
203 public function testSubmitMembershipBlockNotSeparatePayment() {
204 $this->setUpMembershipContributionPage();
205 $submitParams = array(
206 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
207 'id' => (int) $this->_ids['contribution_page'],
208 'amount' => 10,
209 'billing_first_name' => 'Billy',
210 'billing_middle_name' => 'Goat',
211 'billing_last_name' => 'Gruff',
212 'selectMembership' => $this->_ids['membership_type'],
213
214 );
215
216 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
217 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page']));
218 $this->callAPISuccess('membership_payment', 'getsingle', array('contribution_id' => $contribution['id']));
219 }
220
221 /**
222 * Test submit with a membership block in place.
223 */
224 public function testSubmitMembershipBlockIsSeparatePayment() {
225 $this->setUpMembershipContributionPage(TRUE);
226 $submitParams = array(
227 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
228 'id' => (int) $this->_ids['contribution_page'],
229 'amount' => 10,
230 'billing_first_name' => 'Billy',
231 'billing_middle_name' => 'Goat',
232 'billing_last_name' => 'Gruff',
233 'selectMembership' => $this->_ids['membership_type'],
234 );
235
236 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
237 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
238 $this->assertCount(2, $contributions['values']);
239 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
240 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
241 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
242 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
243 }
244
245 /**
246 * Test submit with a membership block in place.
247 */
248 public function testSubmitMembershipBlockTwoTypesIsSeparatePayment() {
249 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 6)));
250 $this->_ids['membership_type'][] = $this->membershipTypeCreate(array('name' => 'Student', 'minimum_fee' => 50));
251 $this->setUpMembershipContributionPage(TRUE);
252 $submitParams = array(
253 'price_' . $this->_ids['price_field'][0] => $this->_ids['price_field_value'][1],
254 'id' => (int) $this->_ids['contribution_page'],
255 'amount' => 10,
256 'billing_first_name' => 'Billy',
257 'billing_middle_name' => 'Goat',
258 'billing_last_name' => 'Gruff',
259 'selectMembership' => $this->_ids['membership_type'][1],
260 );
261
262 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
263 $contributions = $this->callAPISuccess('contribution', 'get', array('contribution_page_id' => $this->_ids['contribution_page']));
264 $this->assertCount(2, $contributions['values']);
265 $ids = array_keys($contributions['values']);
266 $this->assertEquals('10.00', $contributions['values'][$ids[0]]['total_amount']);
267 $this->assertEquals('50.00', $contributions['values'][$ids[1]]['total_amount']);
268 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
269 $this->assertArrayHasKey($membershipPayment['contribution_id'], $contributions['values']);
270 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
271 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
272 }
273
274 /**
275 * Test submit with a membership block in place.
276 *
277 * We are expecting a separate payment for the membership vs the contribution.
278 */
279 public function testSubmitMembershipBlockIsSeparatePaymentPaymentProcessorNow() {
280 $this->setUpMembershipContributionPage(TRUE);
281 $processor = Civi\Payment\System::singleton()->getById($this->_paymentProcessor['id']);
282 $processor->setDoDirectPaymentResult(array('fee_amount' => .72));
283 $submitParams = array(
284 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
285 'id' => (int) $this->_ids['contribution_page'],
286 'amount' => 10,
287 'billing_first_name' => 'Billy',
288 'billing_middle_name' => 'Goat',
289 'billing_last_name' => 'Gruff',
290 'selectMembership' => $this->_ids['membership_type'],
291 'payment_processor_id' => $this->_paymentProcessor['id'],
292 'credit_card_number' => '4111111111111111',
293 'credit_card_type' => 'Visa',
294 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
295 'cvv2' => 123,
296 );
297
298 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
299 $contributions = $this->callAPISuccess('contribution', 'get', array(
300 'contribution_page_id' => $this->_ids['contribution_page'],
301 'contribution_status_id' => 1,
302 ));
303 $this->assertCount(2, $contributions['values']);
304 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
305 $this->assertTrue(in_array($membershipPayment['contribution_id'], array_keys($contributions['values'])));
306 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
307 $this->assertEquals($membership['contact_id'], $contributions['values'][$membershipPayment['contribution_id']]['contact_id']);
308 foreach ($contributions['values'] as $contribution) {
309 $this->assertEquals(.72, $contribution['fee_amount']);
310 $this->assertEquals($contribution['total_amount'] - .72, $contribution['net_amount']);
311 }
312 }
313
314 /**
315 * Test that when a transaction fails the pending contribution remains.
316 *
317 * An activity should also be created. CRM-16417.
318 */
319 public function testSubmitPaymentProcessorFailure() {
320 $this->setUpContributionPage();
321 $this->setupPaymentProcessor();
322 $this->createLoggedInUser();
323 $priceFieldID = reset($this->_ids['price_field']);
324 $priceFieldValueID = reset($this->_ids['price_field_value']);
325 $submitParams = array(
326 'price_' . $priceFieldID => $priceFieldValueID,
327 'id' => (int) $this->_ids['contribution_page'],
328 'amount' => 10,
329 'payment_processor_id' => 1,
330 'credit_card_number' => '4111111111111111',
331 'credit_card_type' => 'Visa',
332 'credit_card_exp_date' => array('M' => 9, 'Y' => 2008),
333 'cvv2' => 123,
334 );
335
336 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
337 $contribution = $this->callAPISuccessGetSingle('contribution', array(
338 'contribution_page_id' => $this->_ids['contribution_page'],
339 'contribution_status_id' => 2,
340 ));
341
342 $this->callAPISuccessGetSingle('activity', array(
343 'source_record_id' => $contribution['id'],
344 'activity_type_id' => 'Failed Payment',
345 ));
346
347 }
348
349 /**
350 * Test submit recurring membership with immediate confirmation (IATS style)
351 * - we process 2 membership transactions against with a recurring contribution against a contribution page with an immediate
352 * processor (IATS style - denoted by returning trxn_id)
353 * - the first creates a new membership, completed contribution, in progress recurring. Check these
354 * - create another - end date should be extended
355 */
356 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurInstantPayment() {
357 $this->params['is_recur'] = 1;
358 $this->params['recur_frequency_unit'] = 'month';
359 $this->setUpMembershipContributionPage();
360 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
361 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_first_success'));
362
363 $submitParams = array(
364 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
365 'id' => (int) $this->_ids['contribution_page'],
366 'amount' => 10,
367 'billing_first_name' => 'Billy',
368 'billing_middle_name' => 'Goat',
369 'billing_last_name' => 'Gruff',
370 'email' => 'billy@goat.gruff',
371 'selectMembership' => $this->_ids['membership_type'],
372 'payment_processor_id' => 1,
373 'credit_card_number' => '4111111111111111',
374 'credit_card_type' => 'Visa',
375 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
376 'cvv2' => 123,
377 'is_recur' => 1,
378 'frequency_interval' => 1,
379 'frequency_unit' => 'month',
380 );
381
382 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
383 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
384 'contribution_page_id' => $this->_ids['contribution_page'],
385 'contribution_status_id' => 1,
386 ));
387
388 $this->assertEquals('create_first_success', $contribution['trxn_id']);
389 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
390 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
391 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
392 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
393 $this->assertEquals(1, $membership['status_id']);
394 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
395 //@todo - check with Joe about these not existing
396 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
397 //renew it with processor setting completed - should extend membership
398 $submitParams['contact_id'] = $contribution['contact_id'];
399 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 1, 'trxn_id' => 'create_second_success'));
400 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
401 $this->callAPISuccess('contribution', 'getsingle', array(
402 'id' => array('NOT IN' => array($contribution['id'])),
403 'contribution_page_id' => $this->_ids['contribution_page'],
404 'contribution_status_id' => 1,
405 ));
406 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
407 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
408 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
409 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
410 }
411
412 /**
413 * Test submit recurring membership with delayed confirmation (Authorize.net style)
414 * - we process 2 membership transactions against with a recurring contribution against a contribution page with a delayed
415 * processor (Authorize.net style - denoted by NOT returning trxn_id)
416 * - the first creates a pending membership, pending contribution, penging recurring. Check these
417 * - complete the transaction
418 * - create another - end date should NOT be extended
419 */
420 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
421 $this->params['is_recur'] = 1;
422 $this->params['recur_frequency_unit'] = 'month';
423 $this->setUpMembershipContributionPage();
424 $dummyPP = Civi\Payment\System::singleton()->getByProcessor($this->_paymentProcessor);
425 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
426
427 $submitParams = array(
428 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
429 'id' => (int) $this->_ids['contribution_page'],
430 'amount' => 10,
431 'billing_first_name' => 'Billy',
432 'billing_middle_name' => 'Goat',
433 'billing_last_name' => 'Gruff',
434 'email' => 'billy@goat.gruff',
435 'selectMembership' => $this->_ids['membership_type'],
436 'payment_processor_id' => 1,
437 'credit_card_number' => '4111111111111111',
438 'credit_card_type' => 'Visa',
439 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040),
440 'cvv2' => 123,
441 'is_recur' => 1,
442 'frequency_interval' => 1,
443 'frequency_unit' => 'month',
444 );
445
446 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL);
447 $contribution = $this->callAPISuccess('contribution', 'getsingle', array(
448 'contribution_page_id' => $this->_ids['contribution_page'],
449 'contribution_status_id' => 2,
450 ));
451 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
452 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
453 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
454 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
455 $this->assertEquals(5, $membership['status_id']);
456 //@todo - check with Joe about these not existing
457 //$this->callAPISuccess('line_item', 'getsingle', array('contribution_id' => $contribution['id'], 'entity_id' => $membership['id']));
458 $this->callAPISuccess('contribution', 'completetransaction', array(
459 'id' => $contribution['id'],
460 'trxn_id' => 'ipn_called',
461 'payment_processor_id' => $this->_paymentProcessor['id'],
462 ));
463 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
464 //renew it with processor setting completed - should extend membership
465 $submitParams = array_merge($submitParams, array(
466 'contact_id' => $contribution['contact_id'],
467 'is_recur' => 1,
468 'frequency_interval' => 1,
469 'frequency_unit' => 'month',
470 )
471 );
472 $dummyPP->setDoDirectPaymentResult(array('payment_status_id' => 2));
473 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
474 $newContribution = $this->callAPISuccess('contribution', 'getsingle', array(
475 'id' => array(
476 'NOT IN' => array($contribution['id']),
477 ),
478 'contribution_page_id' => $this->_ids['contribution_page'],
479 'contribution_status_id' => 2,
480 )
481 );
482
483 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
484 //no renewal as the date hasn't changed
485 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
486 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $newContribution['contribution_recur_id']));
487 $this->assertEquals(2, $recurringContribution['contribution_status_id']);
488 }
489
490 /**
491 * Set up membership contribution page.
492 * @param bool $isSeparatePayment
493 */
494 public function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
495 $this->setUpMembershipBlockPriceSet();
496 $this->setupPaymentProcessor();
497 $this->setUpContributionPage();
498
499 $this->callAPISuccess('membership_block', 'create', array(
500 'entity_id' => $this->_ids['contribution_page'],
501 'entity_table' => 'civicrm_contribution_page',
502 'is_required' => TRUE,
503 'is_active' => TRUE,
504 'is_separate_payment' => $isSeparatePayment,
505 'membership_type_default' => $this->_ids['membership_type'],
506 ));
507 }
508
509 /**
510 * The default data set does not include a complete default membership price set - not quite sure why
511 * This function ensures it exists & populates $this->_ids with it's data
512 */
513 public function setUpMembershipBlockPriceSet() {
514 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array(
515 'name' => 'default_membership_type_amount',
516 'return' => 'id',
517 ));
518 if (empty($this->_ids['membership_type'])) {
519 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
520 }
521 $priceField = $this->callAPISuccess('price_field', 'create', array(
522 'price_set_id' => reset($this->_ids['price_set']),
523 'name' => 'membership_amount',
524 'label' => 'Membership Amount',
525 'html_type' => 'Radio',
526 'sequential' => 1,
527 ));
528 $this->_ids['price_field'][] = $priceField['id'];
529 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
530 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
531 'name' => 'membership_amount',
532 'label' => 'Membership Amount',
533 'amount' => 1,
534 'financial_type_id' => 'Donation',
535 'format.only_id' => TRUE,
536 'membership_type_id' => $membershipTypeID,
537 'price_field_id' => $priceField['id'],
538 ));
539 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
540 }
541 }
542
543 /**
544 * Help function to set up contribution page with some defaults.
545 */
546 public function setUpContributionPage() {
547 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
548 if (empty($this->_ids['price_set'])) {
549 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
550 $this->_ids['price_set'][] = $priceSet['id'];
551 }
552 $priceSetID = reset($this->_ids['price_set']);
553 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
554
555 if (empty($this->_ids['price_field'])) {
556 $priceField = $this->callAPISuccess('price_field', 'create', array(
557 'price_set_id' => $priceSetID,
558 'label' => 'Goat Breed',
559 'html_type' => 'Radio',
560 ));
561 $this->_ids['price_field'] = array($priceField['id']);
562 }
563 if (empty($this->_ids['price_field_value'])) {
564 $this->callAPISuccess('price_field_value', 'create', array(
565 'price_set_id' => $priceSetID,
566 'price_field_id' => $priceField['id'],
567 'label' => 'Long Haired Goat',
568 'financial_type_id' => 'Donation',
569 'amount' => 20,
570 )
571 );
572 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
573 'price_set_id' => $priceSetID,
574 'price_field_id' => $priceField['id'],
575 'label' => 'Shoe-eating Goat',
576 'financial_type_id' => 'Donation',
577 'amount' => 10,
578 )
579 );
580 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
581 }
582 $this->_ids['contribution_page'] = $contributionPageResult['id'];
583 }
584
585 public static function setUpBeforeClass() {
586 // put stuff here that should happen before all tests in this unit
587 }
588
589 public static function tearDownAfterClass() {
590 $tablesToTruncate = array(
591 'civicrm_contact',
592 'civicrm_financial_type',
593 'civicrm_contribution',
594 'civicrm_contribution_page',
595 );
596 $unitTest = new CiviUnitTestCase();
597 $unitTest->quickCleanup($tablesToTruncate);
598 }
599
600 /**
601 * Create a payment processor instance.
602 */
603 protected function setupPaymentProcessor() {
604 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array(
605 'payment_processor_type_id' => 'Dummy',
606 'class_name' => 'Payment_Dummy',
607 'billing_mode' => 1,
608 ));
609 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
610 }
611
612 }