Merge pull request #1013 from pratik-joshi/CRM-12791-minor-fixes
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionSoftTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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 //FIXME:This existed in ContributionTest, don't think it's necessary
30 //require_once 'CiviTest/CiviMailUtils.php';
31
32
33 /**
34 * Test APIv3 civicrm_contribute_* functions
35 *
36 * @package CiviCRM_APIv3
37 * @subpackage API_ContributionSoft
38 */
39
40 class api_v3_ContributionSoftTest extends CiviUnitTestCase {
41
42 /**
43 * Assume empty database with just civicrm_data
44 */
45 protected $_individualId; //the hard credit contact
46 protected $_softIndividual1Id; //the first soft credit contact
47 protected $_softIndividual2Id; //the second soft credit contact
48 protected $_contributionId;
49 protected $_contributionTypeId = 1;
50 protected $_apiversion;
51 protected $_entity = 'Contribution';
52 public $debug = 0;
53 protected $_params;
54 public $_eNoticeCompliant = TRUE;
55
56 function setUp() {
57 parent::setUp();
58
59 $this->_apiversion = 3;
60 $this->_individualId = $this->individualCreate();
61 $this->_softIndividual1Id = $this->individualCreate();
62 $this->_softIndividual2Id = $this->individualCreate();
63 $this->_contributionId = $this->contributionCreate($this->_individualId);
64
65 $paymentProcessor = $this->processorCreate();
66 $this->_params = array(
67 'contact_id' => $this->_individualId,
68 'receive_date' => '20120511',
69 'total_amount' => 100.00,
70 'financial_type_id' => $this->_contributionTypeId,
71 'non_deductible_amount' => 10.00,
72 'fee_amount' => 5.00,
73 'net_amount' => 95.00,
74 'source' => 'SSF',
75 'contribution_status_id' => 1,
76 'version' => $this->_apiversion,
77 );
78 $this->_processorParams = array(
79 'domain_id' => 1,
80 'name' => 'Dummy',
81 'payment_processor_type_id' => 10,
82 'financial_account_id' => 12,
83 'is_active' => 1,
84 'user_name' => '',
85 'url_site' => 'http://dummy.com',
86 'url_recur' => 'http://dummy.com',
87 'billing_mode' => 1,
88 );
89 // $this->_pageParams = array(
90 // 'version' => 3,
91 // 'title' => 'Test Contribution Page',
92 // 'financial_type_id' => 1,
93 // 'currency' => 'USD',
94 // 'financial_account_id' => 1,
95 // 'payment_processor' => $paymentProcessor->id,
96 // 'is_active' => 1,
97 // 'is_allow_other_amount' => 1,
98 // 'min_amount' => 10,
99 // 'max_amount' => 1000,
100 // );
101 }
102
103 function tearDown() {
104
105 $this->contributionTypeDelete();
106 $this->quickCleanup(array(
107 'civicrm_contribution',
108 'civicrm_event',
109 'civicrm_contribution_page',
110 'civicrm_participant',
111 'civicrm_participant_payment',
112 'civicrm_line_item',
113 'civicrm_financial_trxn',
114 'civicrm_financial_item',
115 'civicrm_entity_financial_trxn',
116 'civicrm_contact',
117 'civicrm_contribution_soft'
118 ));
119 }
120
121 function testGetParamsNotArrayContributionSoft() {
122 $params = 'contact_id= 1';
123 $contribution = $this->callAPIFailure('contribution', 'get', $params);
124 $this->assertEquals($contribution['error_message'], 'Input variable `params` is not an array');
125 }
126
127 function testGetContributionSoft() {
128 //We don't test for PCP fields because there's no PCP API, so we can't create campaigns.
129 $p = array(
130 'contribution_id' => $this->_contributionId,
131 'contact_id' => $this->_softIndividual1Id,
132 'amount' => 10.00,
133 'currency' => 'USD',
134 'version' => $this->_apiversion,
135 );
136
137 $this->_softcontribution = civicrm_api('contribution_soft', 'create', $p);
138 $this->assertEquals($this->_softcontribution['is_error'], 0, 'In line ' . __LINE__);
139 $params = array(
140 'id' => $this->_softcontribution['id'],
141 'version' => $this->_apiversion,
142 );
143 $softcontribution = civicrm_api('contribution_soft', 'get', $params);
144 $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__);
145 $this->assertEquals(1,$softcontribution['count']);
146
147 $this->documentMe($params, $softcontribution, __FUNCTION__, __FILE__);
148 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
149 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
150 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__);
151 $this->assertEquals($softcontribution['values'][$this->_softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__);
152
153 //create a second soft contribution on the same hard contribution - we are testing that 'id' gets the right soft contribution id (not the contribution id)
154 $p['contact_id'] = $this->_softIndividual2Id;
155 $this->_softcontribution2 = civicrm_api('contribution_soft', 'create', $p);
156 $this->assertAPISuccess($this->_softcontribution2, 'In line ' . __LINE__);
157
158 $params = array(
159 'version' => $this->_apiversion,
160 );
161 // now we have 2 - test getcount
162 $softcontribution = civicrm_api('contribution_soft', 'getcount', array(
163 'version' => $this->_apiversion,
164 ));
165
166 $this->assertEquals(2, $softcontribution);
167 //test id only format
168 $softcontribution = civicrm_api('contribution_soft', 'get', array
169 ('version' => $this->_apiversion,
170 'id' => $this->_softcontribution['id'],
171 'format.only_id' => 1,
172 )
173 );
174 $this->assertEquals($this->_softcontribution['id'], $softcontribution, print_r($softcontribution,true) . " in line " . __LINE__);
175 //test id only format - second soft credit
176 $softcontribution = civicrm_api('contribution_soft', 'get', array
177 ('version' => $this->_apiversion,
178 'id' => $this->_softcontribution2['id'],
179 'format.only_id' => 1,
180 )
181 );
182 $this->assertEquals($this->_softcontribution2['id'], $softcontribution);
183 $softcontribution = civicrm_api('contribution_soft', 'get', array(
184 'version' => $this->_apiversion,
185 'id' => $this->_softcontribution['id'],
186 ));
187 //test id as field
188 $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__);
189 $this->assertEquals(1, $softcontribution['count'], 'In line ' . __LINE__);
190 $this->assertEquals($this->_softcontribution['id'], $softcontribution['id'] ) ;
191 //test get by contact id works
192 $softcontribution = civicrm_api('contribution_soft', 'get', array('version' => $this->_apiversion, 'contact_id' => $this->_softIndividual2Id));
193 $this->assertAPISuccess($softcontribution, 'In line ' . __LINE__ . "get with contact_id" . print_r(array('version' => $this->_apiversion, 'contact_id' => $this->_softIndividual2Id), TRUE));
194
195 $this->assertEquals(1, $softcontribution['count'], 'In line ' . __LINE__);
196 civicrm_api('contribution_soft', 'Delete', array(
197 'id' => $this->_softcontribution['id'],
198 'version' => $this->_apiversion,
199 ));
200 civicrm_api('Contribution', 'Delete', array(
201 'id' => $this->_softcontribution2['id'],
202 'version' => $this->_apiversion,
203 ));
204 }
205
206
207 ///////////////// civicrm_contribution_soft
208 function testCreateEmptyParamsContributionSoft() {
209
210
211 $params = array('version' => $this->_apiversion);
212 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params);
213 $this->assertEquals($softcontribution['error_message'], 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id', 'In line ' . __LINE__);
214 }
215
216 function testCreateParamsNotArrayContributionSoft() {
217
218 $params = 'contact_id= 1';
219 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params);
220 $this->assertEquals($softcontribution['error_message'], 'Input variable `params` is not an array');
221 }
222
223 function testCreateParamsWithoutRequiredKeysContributionSoft() {
224 $params = array('version' => 3);
225 $softcontribution = $this->callAPIFailure('contribution_soft', 'create', $params);
226 $this->assertEquals($softcontribution['error_message'], 'Mandatory key(s) missing from params array: contribution_id, amount, contact_id');
227 }
228
229 function testCreateContributionSoftInvalidContact() {
230
231 $params = array(
232 'contact_id' => 999,
233 'contribution_id' => $this->_contributionId,
234 'amount' => 10.00,
235 'currency' => 'USD',
236 'version' => $this->_apiversion,
237 );
238
239 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
240 $this->assertEquals($softcontribution['error_message'], 'contact_id is not valid : 999', 'In line ' . __LINE__);
241 }
242
243 function testCreateContributionSoftInvalidContributionId() {
244
245 $params = array(
246 'contribution_id' => 999999,
247 'contact_id' => $this->_softIndividual1Id,
248 'amount' => 10.00,
249 'currency' => 'USD',
250 'version' => $this->_apiversion,
251 );
252
253 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
254 $this->assertEquals($softcontribution['error_message'], 'contribution_id is not valid : 999999', 'In line ' . __LINE__);
255 }
256
257 /*
258 * Function tests that additional financial records are created when fee amount is recorded
259 */
260 function testCreateContributionSoft() {
261 $params = array(
262 'contribution_id' => $this->_contributionId,
263 'contact_id' => $this->_softIndividual1Id,
264 'amount' => 10.00,
265 'currency' => 'USD',
266 'version' => $this->_apiversion,
267 );
268
269 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
270 $this->documentMe($params, $softcontribution, __FUNCTION__, __FILE__);
271 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
272 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
273 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['amount'], '10.00', 'In line ' . __LINE__);
274 $this->assertEquals($softcontribution['values'][$softcontribution['id']]['currency'], 'USD', 'In line ' . __LINE__);
275 }
276
277 /**
278 * Test using example code
279 */
280 // function testContributionSoftCreateExample() {
281 // //make sure at least one page exists since there is a truncate in tear down
282 // $page = civicrm_api('contribution_page', 'create', $this->_pageParams);
283 // $this->assertAPISuccess($page);
284 // //FIXME: Can't written until ContributionSoftDelete is written
285 // require_once 'api/v3/examples/ContributionSoftCreate.php';
286 // $result = contribution_soft_create_example();
287 // $this->assertAPISuccess($result);
288 // $contributionId = $result['id'];
289 // $expectedResult = contribution_soft_create_expectedresult();
290 // $this->checkArrayEquals($result, $expectedResult);
291 // $this->contributionDelete($contributionId);
292 // }
293 //
294 //To Update Soft Contribution
295 function testCreateUpdateContributionSoft() {
296 //create a soft credit
297 $params = array(
298 'contribution_id' => $this->_contributionId,
299 'contact_id' => $this->_softIndividual1Id,
300 'amount' => 10.00,
301 'currency' => 'USD',
302 'version' => $this->_apiversion,
303 );
304
305 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
306 $softcontributionID = $softcontribution['id'];
307
308 $old_params = array(
309 'contribution_soft_id' => $softcontributionID,
310 'version' => $this->_apiversion,
311 );
312 $original = civicrm_api('contribution_soft', 'get', $old_params);
313 //Make sure it came back
314 $this->assertTrue(empty($original['is_error']), 'In line ' . __LINE__);
315 $this->assertEquals($original['id'], $softcontributionID, 'In line ' . __LINE__);
316 //set up list of old params, verify
317 $old_contribution_id = $original['values'][$softcontributionID]['contribution_id'];
318 $old_contact_id = $original['values'][$softcontributionID]['contact_id'];
319 $old_amount = $original['values'][$softcontributionID]['amount'];
320 $old_currency = $original['values'][$softcontributionID]['currency'];
321
322 //check against original values
323 $this->assertEquals($old_contribution_id, $this->_contributionId, 'In line ' . __LINE__);
324 $this->assertEquals($old_contact_id, $this->_softIndividual1Id, 'In line ' . __LINE__);
325 $this->assertEquals($old_amount, 10.00, 'In line ' . __LINE__);
326 $this->assertEquals($old_currency, 'USD', 'In line ' . __LINE__);
327 $params = array(
328 'id' => $softcontributionID,
329 'contribution_id' => $this->_contributionId,
330 'contact_id' => $this->_softIndividual1Id,
331 'amount' => 7.00,
332 'currency' => 'CAD',
333 'version' => $this->_apiversion,
334 );
335
336 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
337
338 $new_params = array(
339 'id' => $softcontribution['id'],
340 'version' => $this->_apiversion,
341 );
342 $softcontribution = civicrm_api('contribution_soft', 'get', $new_params);
343 //check against original values
344 $this->assertEquals($softcontribution['values'][$softcontributionID]['contribution_id'], $this->_contributionId, 'In line ' . __LINE__);
345 $this->assertEquals($softcontribution['values'][$softcontributionID]['contact_id'], $this->_softIndividual1Id, 'In line ' . __LINE__);
346 $this->assertEquals($softcontribution['values'][$softcontributionID]['amount'], 7.00, 'In line ' . __LINE__);
347 $this->assertEquals($softcontribution['values'][$softcontributionID]['currency'], 'CAD', 'In line ' . __LINE__);
348
349 $params = array(
350 'id' => $softcontributionID,
351 'version' => $this->_apiversion,
352 );
353 $result = civicrm_api('contribution_soft', 'delete', $params);
354 $this->assertAPISuccess($result, 'in line' . __LINE__);
355 }
356
357 ///////////////// civicrm_contribution_soft_delete methods
358 function testDeleteEmptyParamsContributionSoft() {
359 $params = array('version' => $this->_apiversion);
360 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
361 }
362
363 function testDeleteParamsNotArrayContributionSoft() {
364 $params = 'id= 1';
365 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
366 $this->assertEquals($softcontribution['error_message'], 'Input variable `params` is not an array');
367 }
368
369 function testDeleteWrongParamContributionSoft() {
370 $params = array(
371 'contribution_source' => 'SSF',
372 'version' => $this->_apiversion,
373 );
374 $softcontribution = $this->callAPIFailure('contribution_soft', 'delete', $params);
375 }
376
377 function testDeleteContributionSoft() {
378 //create a soft credit
379 $params = array(
380 'contribution_id' => $this->_contributionId,
381 'contact_id' => $this->_softIndividual1Id,
382 'amount' => 10.00,
383 'currency' => 'USD',
384 'version' => $this->_apiversion,
385 );
386
387 $softcontribution = civicrm_api('contribution_soft', 'create', $params);
388 $softcontributionID = $softcontribution['id'];
389 $params = array(
390 'id' => $softcontributionID,
391 'version' => $this->_apiversion,
392 );
393 $result = civicrm_api('contribution_soft', 'delete', $params);
394 $this->documentMe($params, $result, __FUNCTION__, __FILE__);
395 $this->assertAPISuccess($result, 'In line ' . __LINE__);
396 }
397
398 ///////////////// civicrm_contribution_search methods
399
400 /**
401 * Test civicrm_contribution_soft_search with wrong params type
402 */
403 function testSearchWrongParamsType() {
404 $params = 'a string';
405 $result = $this->callAPIFailure('contribution_soft', 'get', $params);
406 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
407 }
408
409 /**
410 * Test civicrm_contribution_search with empty params.
411 * All available contributions expected.
412 */
413 function testSearchEmptyParams() {
414 $params = array('version' => $this->_apiversion);
415
416 $p = array(
417 'contribution_id' => $this->_contributionId,
418 'contact_id' => $this->_softIndividual1Id,
419 'amount' => 10.00,
420 'currency' => 'USD',
421 'version' => $this->_apiversion,
422 );
423 $softcontribution = civicrm_api('contribution_soft', 'create', $p);
424
425 $result = civicrm_api('contribution_soft', 'get', $params);
426 // We're taking the first element.
427 $res = $result['values'][$softcontribution['id']];
428
429 $this->assertEquals($p['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__);
430 $this->assertEquals($p['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
431 $this->assertEquals($p['amount'], $res['amount'], 'In line ' . __LINE__);
432 $this->assertEquals($p['currency'], $res['currency'], 'In line ' . __LINE__);
433 }
434
435 /**
436 * Test civicrm_contribution_soft_search. Success expected.
437 */
438 function testSearch() {
439 $p1 = array(
440 'contribution_id' => $this->_contributionId,
441 'contact_id' => $this->_softIndividual1Id,
442 'amount' => 10.00,
443 'currency' => 'USD',
444 'version' => $this->_apiversion,
445 );
446 $softcontribution1 = civicrm_api('contribution_soft', 'create', $p1);
447
448 $p2 = array(
449 'contribution_id' => $this->_contributionId,
450 'contact_id' => $this->_softIndividual2Id,
451 'amount' => 25.00,
452 'currency' => 'CAD',
453 'version' => $this->_apiversion,
454 );
455 $softcontribution2 = civicrm_api('contribution_soft', 'create', $p2);
456
457 $params = array(
458 'id' => $softcontribution2['id'],
459 'version' => $this->_apiversion,
460 );
461 $result = civicrm_api('contribution_soft', 'get', $params);
462 $res = $result['values'][$softcontribution2['id']];
463
464 $this->assertEquals($p2['contribution_id'], $res['contribution_id'], 'In line ' . __LINE__);
465 $this->assertEquals($p2['contact_id'], $res['contact_id'], 'In line ' . __LINE__);
466 $this->assertEquals($p2['amount'], $res['amount'], 'In line ' . __LINE__);
467 $this->assertEquals($p2['currency'], $res['currency'], 'In line ' . __LINE__ );
468 }
469 }
470