Merge remote-tracking branch 'upstream/4.5' into 4.5-master-2014-09-29-13-19-52
[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 with a membership block in place
256 */
257 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecur() {
258 $this->params['is_recur'] = 1;
259 $var = array();
260 $this->params['recur_frequency_unit'] = 'month';
261 $this->setUpMembershipContributionPage();
262 $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor);
263 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 1233,));
264
265 $submitParams = array(
266 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
267 'id' => (int) $this->_ids['contribution_page'],
268 'amount' => 10,
269 'billing_first_name' => 'Billy',
270 'billing_middle_name' => 'Goat',
271 'billing_last_name' => 'Gruff',
272 'email' => 'billy@goat.gruff',
273 'selectMembership' => $this->_ids['membership_type'],
274 'payment_processor' => 1,
275 'credit_card_number' => '4111111111111111',
276 'credit_card_type' => 'Visa',
277 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040 ),
278 'cvv2' => 123,
279 'is_recur' => 1,
280 'frequency_interval' => 1,
281 'frequency_unit' => 'month',
282 );
283
284 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
285 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 1));
286 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
287 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
288 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
289 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
290 $this->assertEquals(1, $membership['status_id']);
291 $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
292
293 //renew it with processor setting completed - should extend membership
294 $submitParams['contact_id'] = $contribution['contact_id'];
295 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 1234,));
296 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
297 $this->callAPISuccess('contribution', 'getsingle', array('id' => array('NOT IN' => array($contribution['id'])), 'contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 1));
298 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
299 $this->assertEquals(date('Y-m-d', strtotime('+ 1 year', strtotime($membership['end_date']))), $renewedMembership['end_date']);
300 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
301 //@todo this assertion should pass
302 //$this->assertEquals(5, $recurringContribution['contribution_status_id']);
303 }
304
305 /**
306 * Test submit with a membership block in place
307 */
308 public function testSubmitMembershipPriceSetPaymentPaymentProcessorRecurDelayed() {
309 $this->params['is_recur'] = 1;
310 $var = array();
311 $this->params['recur_frequency_unit'] = 'month';
312 $this->setUpMembershipContributionPage();
313 $dummyPP = CRM_Core_Payment::singleton('live', $this->_paymentProcessor);
314 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 1, 'trxn_id' => 1233,));
315
316 $submitParams = array(
317 'price_' . $this->_ids['price_field'][0] => reset($this->_ids['price_field_value']),
318 'id' => (int) $this->_ids['contribution_page'],
319 'amount' => 10,
320 'billing_first_name' => 'Billy',
321 'billing_middle_name' => 'Goat',
322 'billing_last_name' => 'Gruff',
323 'email' => 'billy@goat.gruff',
324 'selectMembership' => $this->_ids['membership_type'],
325 'payment_processor' => 1,
326 'credit_card_number' => '4111111111111111',
327 'credit_card_type' => 'Visa',
328 'credit_card_exp_date' => array('M' => 9, 'Y' => 2040 ),
329 'cvv2' => 123,
330 );
331
332 $this->callAPIAndDocument('contribution_page', 'submit', $submitParams, __FUNCTION__, __FILE__, 'submit contribution page', NULL, 'Submit');
333 $contribution = $this->callAPISuccess('contribution', 'getsingle', array('contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 1));
334 $membershipPayment = $this->callAPISuccess('membership_payment', 'getsingle', array());
335 $this->assertEquals($membershipPayment['contribution_id'], $contribution['id']);
336 $membership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
337 $this->assertEquals($membership['contact_id'], $contribution['contact_id']);
338 $this->assertEquals(1, $membership['status_id']);
339
340 //renew it with processor setting completed - should extend membership
341 $submitParams = array_merge($submitParams, array(
342 'contact_id' => $contribution['contact_id'],
343 'is_recur' => 1,
344 'frequency_interval' => 1,
345 'frequency_unit' => 'month',)
346 );
347 $dummyPP->setDoDirectPaymentResult(array('contribution_status_id' => 2,));
348 $this->markTestIncomplete('currently failing - this needs fixing per CRM-15296 as it shows that renewals are being actioned even when payment is not');
349 $this->callAPISuccess('contribution_page', 'submit', $submitParams);
350 $this->callAPISuccess('contribution', 'getsingle', array('id' => array(
351 'NOT IN' => array($contribution['id'])), 'contribution_page_id' => $this->_ids['contribution_page'], 'contribution_status_id' => 2)
352 );
353 $renewedMembership = $this->callAPISuccessGetSingle('membership', array('id' => $membershipPayment['membership_id']));
354 //no renewal as the date hasn't changed
355 $this->assertEquals($membership['end_date'], $renewedMembership['end_date']);
356 $recurringContribution = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $contribution['contribution_recur_id']));
357 $this->assertEquals(5, $recurringContribution['status_id']);
358 }
359
360 /**
361 * set up membership contribution page
362 * @param bool $isSeparatePayment
363 */
364 function setUpMembershipContributionPage($isSeparatePayment = FALSE) {
365 $this->setUpMembershipBlockPriceSet();
366 $this->params['payment_processor_id'] = $this->_ids['payment_processor'] = $this->paymentProcessorCreate(array(
367 'payment_processor_type_id' => 'Dummy',
368 'class_name' => 'Payment_Dummy',
369 'billing_mode' => 1,
370 ));
371 $this->_paymentProcessor = $this->callAPISuccess('payment_processor', 'getsingle', array('id' => $this->params['payment_processor_id']));
372 $this->setUpContributionPage();
373
374 $this->callAPISuccess('membership_block', 'create', array(
375 'entity_id' => $this->_ids['contribution_page'],
376 'entity_table' => 'civicrm_contribution_page',
377 'is_required' => TRUE,
378 'is_active' => TRUE,
379 'is_separate_payment' => $isSeparatePayment,
380 'membership_type_default' => $this->_ids['membership_type'],
381 ));
382 }
383
384 /**
385 * The default data set does not include a complete default membership price set - not quite sure why
386 * This function ensures it exists & populates $this->_ids with it's data
387 */
388 function setUpMembershipBlockPriceSet() {
389 $this->_ids['price_set'][] = $this->callAPISuccess('price_set', 'getvalue', array('name' => 'default_membership_type_amount', 'return' => 'id'));
390 if (empty($this->_ids['membership_type'])) {
391 $this->_ids['membership_type'] = array($this->membershipTypeCreate(array('minimum_fee' => 2)));
392 }
393 $priceField = $this->callAPISuccess('price_field', 'create', array(
394 'price_set_id' => reset($this->_ids['price_set']),
395 'name' => 'membership_amount',
396 'label' => 'Membership Amount',
397 'html_type' => 'Radio',
398 'sequential' => 1,
399 ));
400 $this->_ids['price_field'][] = $priceField['id'];
401 foreach ($this->_ids['membership_type'] as $membershipTypeID) {
402 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
403 'name' => 'membership_amount',
404 'label' => 'Membership Amount',
405 'amount' => 1,
406 'financial_type_id' => 1,
407 'format.only_id' => TRUE,
408 'membership_type_id' => $membershipTypeID,
409 'price_field_id' => $priceField['id'],
410 ));
411 $this->_ids['price_field_value'][] = $priceFieldValue['id'];
412 }
413 }
414
415 /**
416 * help function to set up contribution page with some defaults
417 */
418 function setUpContributionPage() {
419 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
420 if (empty($this->_ids['price_set'])) {
421 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
422 $this->_ids['price_set'][] = $priceSet['id'];
423 }
424 $priceSetID = reset($this->_ids['price_set']);
425 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID );
426
427 if (empty($this->_ids['price_field'])) {
428 $priceField = $this->callAPISuccess('price_field', 'create', array(
429 'price_set_id' => $priceSetID,
430 'label' => 'Goat Breed',
431 'html_type' => 'Radio',
432 ));
433 $this->_ids['price_field'] = array($priceField['id']);
434 }
435 if (empty($this->_ids['price_field_value'])) {
436 $this->callAPISuccess('price_field_value', 'create', array(
437 'price_set_id' => $priceSetID,
438 'price_field_id' => $priceField['id'],
439 'label' => 'Long Haired Goat',
440 'amount' => 20,
441 )
442 );
443 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
444 'price_set_id' => $priceSetID,
445 'price_field_id' => $priceField['id'],
446 'label' => 'Shoe-eating Goat',
447 'amount' => 10,
448 )
449 );
450 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
451 }
452 $this->_ids['contribution_page'] = $contributionPageResult['id'];
453 }
454
455 public static function setUpBeforeClass() {
456 // put stuff here that should happen before all tests in this unit
457 }
458
459 public static function tearDownAfterClass(){
460 $tablesToTruncate = array(
461 'civicrm_contact',
462 'civicrm_financial_type',
463 'civicrm_contribution',
464 'civicrm_contribution_page',
465 );
466 $unitTest = new CiviUnitTestCase();
467 $unitTest->quickCleanup($tablesToTruncate);
468 }
469 }
470