Merge pull request #7797 from JKingsnorth/CRM-17977
[civicrm-core.git] / tests / phpunit / api / v3 / OrderTest.php
CommitLineData
73c0e107
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
fa938177 6 | Copyright CiviCRM LLC (c) 2004-2016 |
73c0e107
PN
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
73c0e107
PN
28/**
29 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
acb109b7 33 * @group headless
73c0e107
PN
34 */
35class api_v3_OrderTest extends CiviUnitTestCase {
36
37 /**
38 * Assume empty database with just civicrm_data.
39 */
40 protected $_individualId;
41 protected $_financialTypeId = 1;
42 protected $_apiversion;
43 public $debug = 0;
44
45 /**
46 * Setup function.
47 */
48 public function setUp() {
49 parent::setUp();
50
51 $this->_apiversion = 3;
52 $this->_individualId = $this->individualCreate();
53 }
54
55 /**
56 * Clean up after each test.
57 */
58 public function tearDown() {
59 $this->quickCleanUpFinancialEntities();
60 $this->quickCleanup(array('civicrm_uf_match'));
61 }
62
63 /**
c39e27f2 64 * Test Get order api.
73c0e107
PN
65 */
66 public function testGetOrder() {
67 $contribution = $this->addOrder(FALSE, 100);
68
69 $params = array(
70 'contribution_id' => $contribution['id'],
71 );
72
54a54e15 73 $order = $this->callAPIAndDocument('Order', 'get', $params, __FUNCTION__, __FILE__);
73c0e107
PN
74
75 $this->assertEquals(1, $order['count']);
c5e2b8c1
PN
76 $expectedResult = array(
77 $contribution['id'] => array(
78 'total_amount' => 100,
79 'contribution_id' => $contribution['id'],
80 'contribution_status' => 'Completed',
81 'net_amount' => 100,
2e36bc66 82 ),
c5e2b8c1 83 );
73c0e107
PN
84 $lineItems[] = array(
85 'entity_table' => 'civicrm_contribution',
86 'entity_id' => $contribution['id'],
87 'contribution_id' => $contribution['id'],
88 'unit_price' => 100,
89 'line_total' => 100,
90 'financial_type_id' => 1,
91 );
92 $this->checkPaymentResult($order, $expectedResult, $lineItems);
93 $this->callAPISuccess('Contribution', 'Delete', array(
94 'id' => $contribution['id'],
95 ));
96 }
97
c39e27f2
PN
98 /**
99 * Test Get Order api for participant contribution.
100 */
101 public function testGetOrderParticipant() {
102 $contribution = $this->addOrder(FALSE, 100);
103 list($items, $contribution) = $this->createParticipantWithContribution();
104
105 $params = array(
106 'contribution_id' => $contribution['id'],
107 );
108
109 $order = $this->callAPISuccess('Order', 'get', $params);
110
111 $this->assertEquals(2, count($order['values'][$contribution['id']]['line_items']));
112 $this->callAPISuccess('Contribution', 'Delete', array(
113 'id' => $contribution['id'],
114 ));
115 }
116
73c0e107
PN
117 /**
118 * Function to assert db values
119 */
120 public function checkPaymentResult($results, $expectedResult, $lineItems = NULL) {
121 foreach ($expectedResult[$results['id']] as $key => $value) {
122 $this->assertEquals($results['values'][$results['id']][$key], $value);
123 }
124
125 if ($lineItems) {
126 foreach ($lineItems as $key => $items) {
127 foreach ($items as $k => $item) {
128 $this->assertEquals($results['values'][$results['id']]['line_items'][$key][$k], $item);
129 }
130 }
131 }
132 }
133
134 /**
135 * add order
136 *
137 * @param bool $isPriceSet
138 * @param float $amount
139 * @param array $extraParams
140 *
141 * @return array
142 */
143 public function addOrder($isPriceSet, $amount = 300, $extraParams = array()) {
144 $p = array(
145 'contact_id' => $this->_individualId,
146 'receive_date' => '2010-01-20',
147 'total_amount' => $amount,
148 'financial_type_id' => $this->_financialTypeId,
149 'contribution_status_id' => 1,
150 );
151
152 if ($isPriceSet) {
153 $priceFields = $this->createPriceSet();
154 foreach ($priceFields['values'] as $key => $priceField) {
155 $lineItems[1][$key] = array(
156 'price_field_id' => $priceField['price_field_id'],
157 'price_field_value_id' => $priceField['id'],
158 'label' => $priceField['label'],
159 'field_title' => $priceField['label'],
160 'qty' => 1,
161 'unit_price' => $priceField['amount'],
162 'line_total' => $priceField['amount'],
163 'financial_type_id' => $priceField['financial_type_id'],
164 );
165 }
166 $p['line_item'] = $lineItems;
167 }
168 $p = array_merge($extraParams, $p);
169 return $this->callAPISuccess('Contribution', 'create', $p);
170 }
171
b8644ae3
PN
172 /**
173 * Test create order api
174 */
175 public function testAddOrder() {
176 $order = $this->addOrder(FALSE, 100);
177 $params = array(
178 'contribution_id' => $order['id'],
179 );
180 $order = $this->callAPISuccess('order', 'get', $params);
181 $expectedResult = array(
182 $order['id'] => array(
183 'total_amount' => 100,
184 'contribution_id' => $order['id'],
185 'contribution_status' => 'Completed',
186 'net_amount' => 100,
187 ),
188 );
189 $lineItems[] = array(
190 'entity_table' => 'civicrm_contribution',
191 'entity_id' => $order['id'],
192 'contribution_id' => $order['id'],
193 'unit_price' => 100,
194 'line_total' => 100,
195 'financial_type_id' => 1,
196 );
197 $this->checkPaymentResult($order, $expectedResult, $lineItems);
198 $this->callAPISuccess('Contribution', 'Delete', array(
199 'id' => $order['id'],
200 ));
201 }
202
203 /**
204 * Test create order api for membership
205 */
206 public function testAddOrderForMembership() {
207 require_once 'CiviTest/Membership.php';
208 $membership = new Membership();
209 $membershipType = $membership->createMembershipType();
210 $membershipType1 = $membership->createMembershipType();
211 $membershipType = $membershipTypes = array($membershipType->id, $membershipType1->id);
212 $p = array(
213 'contact_id' => $this->_individualId,
214 'receive_date' => '2010-01-20',
215 'total_amount' => 200,
216 'financial_type_id' => $this->_financialTypeId,
217 'contribution_status_id' => 1,
218 );
219 $priceFields = $this->createPriceSet();
220 foreach ($priceFields['values'] as $key => $priceField) {
221 $lineItems[$key] = array(
222 'price_field_id' => $priceField['price_field_id'],
223 'price_field_value_id' => $priceField['id'],
224 'label' => $priceField['label'],
225 'field_title' => $priceField['label'],
226 'qty' => 1,
227 'unit_price' => $priceField['amount'],
228 'line_total' => $priceField['amount'],
229 'financial_type_id' => $priceField['financial_type_id'],
230 'entity_table' => 'civicrm_membership',
231 'membership_type_id' => array_pop($membershipType),
232 );
233 }
234 $p['line_items'][] = array(
235 'line_item' => array(array_pop($lineItems)),
236 'params' => array(
237 'contact_id' => $this->_individualId,
238 'membership_type_id' => array_pop($membershipTypes),
239 'join_date' => '2006-01-21',
240 'start_date' => '2006-01-21',
241 'end_date' => '2006-12-21',
242 'source' => 'Payment',
243 'is_override' => 1,
244 'status_id' => 1,
245 ),
246 );
54a54e15 247 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__, __FILE__);
b8644ae3
PN
248 $params = array(
249 'contribution_id' => $order['id'],
250 );
251 $order = $this->callAPISuccess('order', 'get', $params);
252 $expectedResult = array(
253 $order['id'] => array(
254 'total_amount' => 200,
255 'contribution_id' => $order['id'],
256 'contribution_status' => 'Completed',
257 'net_amount' => 200,
258 ),
259 );
260 $this->checkPaymentResult($order, $expectedResult);
261 $this->callAPISuccessGetCount('MembershipPayment', $params, 1);
262 $this->callAPISuccess('Contribution', 'Delete', array(
263 'id' => $order['id'],
264 ));
265 $p['line_items'][] = array(
266 'line_item' => array(array_pop($lineItems)),
267 'params' => array(
268 'contact_id' => $this->_individualId,
269 'membership_type_id' => array_pop($membershipTypes),
270 'join_date' => '2006-01-21',
271 'start_date' => '2006-01-21',
272 'end_date' => '2006-12-21',
273 'source' => 'Payment',
274 'is_override' => 1,
275 'status_id' => 1,
276 ),
277 );
278 $p['total_amount'] = 300;
279 $order = $this->callAPISuccess('order', 'create', $p);
280 $expectedResult = array(
281 $order['id'] => array(
282 'total_amount' => 300,
283 'contribution_status' => 'Completed',
284 'net_amount' => 300,
285 ),
286 );
287 $paymentMembership = array(
288 'contribution_id' => $order['id'],
289 );
290 $order = $this->callAPISuccess('order', 'get', $paymentMembership);
291 $this->checkPaymentResult($order, $expectedResult);
292 $this->callAPISuccessGetCount('MembershipPayment', $paymentMembership, 2);
293 $this->callAPISuccess('Contribution', 'Delete', array(
294 'id' => $order['id'],
295 ));
296 }
297
298 /**
299 * Test create order api for participant
300 */
301 public function testAddOrderForPariticipant() {
302 require_once 'CiviTest/Event.php';
303 $this->_eventId = Event::create($this->_individualId);
304 $p = array(
305 'contact_id' => $this->_individualId,
306 'receive_date' => '2010-01-20',
307 'total_amount' => 300,
308 'financial_type_id' => $this->_financialTypeId,
309 'contribution_status_id' => 1,
310 );
311 $priceFields = $this->createPriceSet();
312 foreach ($priceFields['values'] as $key => $priceField) {
313 $lineItems[$key] = array(
314 'price_field_id' => $priceField['price_field_id'],
315 'price_field_value_id' => $priceField['id'],
316 'label' => $priceField['label'],
317 'field_title' => $priceField['label'],
318 'qty' => 1,
319 'unit_price' => $priceField['amount'],
320 'line_total' => $priceField['amount'],
321 'financial_type_id' => $priceField['financial_type_id'],
322 'entity_table' => 'civicrm_participant',
323 );
324 }
325 $p['line_items'][] = array(
326 'line_item' => $lineItems,
327 'params' => array(
328 'contact_id' => $this->_individualId,
329 'event_id' => $this->_eventId,
330 'status_id' => 1,
331 'role_id' => 1,
332 'register_date' => '2007-07-21 00:00:00',
333 'source' => 'Online Event Registration: API Testing',
334 ),
335 );
54a54e15 336 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__, __FILE__, 'Create order for participant', 'CreateOrderParticipant');
b8644ae3
PN
337 $params = array(
338 'contribution_id' => $order['id'],
339 );
340 $order = $this->callAPISuccess('order', 'get', $params);
341 $expectedResult = array(
342 $order['id'] => array(
343 'total_amount' => 300,
344 'contribution_id' => $order['id'],
345 'contribution_status' => 'Completed',
346 'net_amount' => 300,
347 ),
348 );
349 $this->checkPaymentResult($order, $expectedResult);
350 $this->callAPISuccessGetCount('ParticipantPayment', $params, 1);
351 $this->callAPISuccess('Contribution', 'Delete', array(
352 'id' => $order['id'],
353 ));
354 $p['line_items'][] = array(
355 'line_item' => $lineItems,
356 'params' => array(
357 'contact_id' => $this->individualCreate(),
358 'event_id' => $this->_eventId,
359 'status_id' => 1,
360 'role_id' => 1,
361 'register_date' => '2007-07-21 00:00:00',
362 'source' => 'Online Event Registration: API Testing',
363 ),
364 );
365 $p['total_amount'] = 600;
366 $order = $this->callAPISuccess('order', 'create', $p);
367 $expectedResult = array(
f30868a2 368 $order['id'] => array(
b8644ae3
PN
369 'total_amount' => 600,
370 'contribution_status' => 'Completed',
371 'net_amount' => 600,
372 ),
373 );
374 $paymentParticipant = array(
375 'contribution_id' => $order['id'],
376 );
377 $order = $this->callAPISuccess('order', 'get', $paymentParticipant);
378 $this->checkPaymentResult($order, $expectedResult);
379 $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
380 $this->callAPISuccess('Contribution', 'Delete', array(
381 'id' => $order['id'],
382 ));
383 }
384
385 /**
386 * Test create order api with line items
387 */
388 public function testAddOrderWithLineItems() {
389 $order = $this->addOrder(TRUE);
390 $params = array(
391 'contribution_id' => $order['id'],
392 );
393 $order = $this->callAPISuccess('order', 'get', $params);
394 $expectedResult = array(
395 $order['id'] => array(
396 'total_amount' => 300,
397 'contribution_id' => $order['id'],
398 'contribution_status' => 'Completed',
399 'net_amount' => 300,
400 ),
401 );
402 $items[] = array(
403 'entity_table' => 'civicrm_contribution',
404 'entity_id' => $order['id'],
405 'contribution_id' => $order['id'],
406 'unit_price' => 100,
407 'line_total' => 100,
408 );
409 $items[] = array(
410 'entity_table' => 'civicrm_contribution',
411 'entity_id' => $order['id'],
412 'contribution_id' => $order['id'],
413 'unit_price' => 200,
414 'line_total' => 200,
415 );
416 $this->checkPaymentResult($order, $expectedResult, $items);
417 $params = array(
418 'entity_table' => 'civicrm_contribution',
419 'entity_id' => $order['id'],
420 );
421 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
422 $this->assertEquals($eft['values'][$eft['id']]['amount'], 300);
423 $params = array(
424 'entity_table' => 'civicrm_financial_item',
425 'financial_trxn_id' => $eft['values'][$eft['id']]['financial_trxn_id'],
426 );
427 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
428 $amounts = array(200, 100);
429 foreach ($eft['values'] as $value) {
430 $this->assertEquals($value['amount'], array_pop($amounts));
431 }
432 $this->callAPISuccess('Contribution', 'Delete', array(
433 'id' => $order['id'],
434 ));
435 }
436
5f44f698
PN
437 /**
438 * Test delete order api
439 */
440 public function testDeleteOrder() {
441 $order = $this->addOrder(FALSE, 100);
442 $params = array(
443 'contribution_id' => $order['id'],
444 );
445 try {
446 $this->callAPISuccess('order', 'delete', $params);
447 $this->fail("Missed expected exception");
448 }
449 catch (Exception $expected) {
95c4e89e 450 $this->callAPISuccess('Contribution', 'create', array(
5f44f698
PN
451 'contribution_id' => $order['id'],
452 'is_test' => TRUE,
453 ));
54a54e15 454 $this->callAPIAndDocument('order', 'delete', $params, __FUNCTION__, __FILE__);
5f44f698
PN
455 $order = $this->callAPISuccess('order', 'get', $params);
456 $this->assertEquals(0, $order['count']);
457 }
458 }
459
65f7f9f6
PN
460 /**
461 * Test cancel order api
462 */
463 public function testCancelOrder() {
464 $contribution = $this->addOrder(FALSE, 100);
465 $params = array(
466 'contribution_id' => $contribution['id'],
467 );
54a54e15 468 $this->callAPIAndDocument('order', 'cancel', $params, __FUNCTION__, __FILE__);
65f7f9f6
PN
469 $order = $this->callAPISuccess('Order', 'get', $params);
470 $expectedResult = array(
471 $contribution['id'] => array(
472 'total_amount' => 100,
473 'contribution_id' => $contribution['id'],
474 'contribution_status' => 'Cancelled',
475 'net_amount' => 100,
476 ),
477 );
478 $this->checkPaymentResult($order, $expectedResult);
479 $this->callAPISuccess('Contribution', 'Delete', array(
480 'id' => $contribution['id'],
481 ));
482 }
483
b63480e3
PN
484 /**
485 * Test cancel order api
486 */
487 public function testCancelWithParticipant() {
488 $this->_eventId = Event::create($this->_individualId);
489 $eventParams = array(
490 'id' => $this->_eventId,
491 'financial_type_id' => 4,
492 'is_monetary' => 1,
493 );
494 $this->callAPISuccess('event', 'create', $eventParams);
495 $participantParams = array(
496 'financial_type_id' => 4,
497 'event_id' => $this->_eventId,
498 'role_id' => 1,
499 'status_id' => 1,
500 'fee_currency' => 'USD',
501 'contact_id' => $this->_individualId,
502 );
503 $participant = $this->callAPISuccess('Participant', 'create', $participantParams);
504 $extraParams = array(
505 'contribution_mode' => 'participant',
506 'participant_id' => $participant['id'],
507 );
508 $contribution = $this->addOrder(TRUE, 100, $extraParams);
509 $paymentParticipant = array(
510 'participant_id' => $participant['id'],
511 'contribution_id' => $contribution['id'],
512 );
513 $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant);
514 $params = array(
515 'contribution_id' => $contribution['id'],
516 );
517 $this->callAPISuccess('order', 'cancel', $params);
518 $order = $this->callAPISuccess('Order', 'get', $params);
519 $expectedResult = array(
520 $contribution['id'] => array(
521 'total_amount' => 100,
522 'contribution_id' => $contribution['id'],
523 'contribution_status' => 'Cancelled',
524 'net_amount' => 100,
525 ),
526 );
dfd6297d 527 $this->checkPaymentResult($order, $expectedResult);
b63480e3
PN
528 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $params);
529 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
530 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Cancelled');
531 $this->callAPISuccess('Contribution', 'Delete', array(
532 'id' => $contribution['id'],
533 ));
534 }
535
73c0e107 536}