Merge pull request #10030 from eileenmcnaughton/test
[civicrm-core.git] / tests / phpunit / api / v3 / OrderTest.php
CommitLineData
73c0e107
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
15a4309a 6 | Copyright CiviCRM LLC (c) 2004-2017 |
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() {
e4649817 102 $this->addOrder(FALSE, 100);
c39e27f2
PN
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 117 /**
e4649817 118 * Function to assert db values.
73c0e107
PN
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 /**
e4649817 135 * Add order.
73c0e107
PN
136 *
137 * @param bool $isPriceSet
138 * @param float $amount
139 * @param array $extraParams
140 *
141 * @return array
142 */
e4649817 143 public function addOrder($isPriceSet, $amount = 300.00, $extraParams = array()) {
73c0e107
PN
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() {
e4649817 207 $membershipType = $this->membershipTypeCreate();
208 $membershipType1 = $this->membershipTypeCreate();
209 $membershipType = $membershipTypes = array($membershipType, $membershipType1);
b8644ae3
PN
210 $p = array(
211 'contact_id' => $this->_individualId,
212 'receive_date' => '2010-01-20',
213 'total_amount' => 200,
214 'financial_type_id' => $this->_financialTypeId,
215 'contribution_status_id' => 1,
216 );
217 $priceFields = $this->createPriceSet();
218 foreach ($priceFields['values'] as $key => $priceField) {
219 $lineItems[$key] = array(
220 'price_field_id' => $priceField['price_field_id'],
221 'price_field_value_id' => $priceField['id'],
222 'label' => $priceField['label'],
223 'field_title' => $priceField['label'],
224 'qty' => 1,
225 'unit_price' => $priceField['amount'],
226 'line_total' => $priceField['amount'],
227 'financial_type_id' => $priceField['financial_type_id'],
228 'entity_table' => 'civicrm_membership',
229 'membership_type_id' => array_pop($membershipType),
230 );
231 }
232 $p['line_items'][] = array(
233 'line_item' => array(array_pop($lineItems)),
234 'params' => array(
235 'contact_id' => $this->_individualId,
236 'membership_type_id' => array_pop($membershipTypes),
237 'join_date' => '2006-01-21',
238 'start_date' => '2006-01-21',
239 'end_date' => '2006-12-21',
240 'source' => 'Payment',
241 'is_override' => 1,
242 'status_id' => 1,
243 ),
244 );
54a54e15 245 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__, __FILE__);
b8644ae3
PN
246 $params = array(
247 'contribution_id' => $order['id'],
248 );
249 $order = $this->callAPISuccess('order', 'get', $params);
250 $expectedResult = array(
251 $order['id'] => array(
252 'total_amount' => 200,
253 'contribution_id' => $order['id'],
254 'contribution_status' => 'Completed',
255 'net_amount' => 200,
256 ),
257 );
258 $this->checkPaymentResult($order, $expectedResult);
259 $this->callAPISuccessGetCount('MembershipPayment', $params, 1);
260 $this->callAPISuccess('Contribution', 'Delete', array(
261 'id' => $order['id'],
262 ));
263 $p['line_items'][] = array(
264 'line_item' => array(array_pop($lineItems)),
265 'params' => array(
266 'contact_id' => $this->_individualId,
267 'membership_type_id' => array_pop($membershipTypes),
268 'join_date' => '2006-01-21',
269 'start_date' => '2006-01-21',
270 'end_date' => '2006-12-21',
271 'source' => 'Payment',
272 'is_override' => 1,
273 'status_id' => 1,
274 ),
275 );
276 $p['total_amount'] = 300;
277 $order = $this->callAPISuccess('order', 'create', $p);
278 $expectedResult = array(
279 $order['id'] => array(
280 'total_amount' => 300,
281 'contribution_status' => 'Completed',
282 'net_amount' => 300,
283 ),
284 );
285 $paymentMembership = array(
286 'contribution_id' => $order['id'],
287 );
288 $order = $this->callAPISuccess('order', 'get', $paymentMembership);
289 $this->checkPaymentResult($order, $expectedResult);
290 $this->callAPISuccessGetCount('MembershipPayment', $paymentMembership, 2);
291 $this->callAPISuccess('Contribution', 'Delete', array(
292 'id' => $order['id'],
293 ));
294 }
295
296 /**
297 * Test create order api for participant
298 */
e4649817 299 public function testAddOrderForParticipant() {
300 $event = $this->eventCreate();
301 $this->_eventId = $event['id'];
b8644ae3
PN
302 $p = array(
303 'contact_id' => $this->_individualId,
304 'receive_date' => '2010-01-20',
305 'total_amount' => 300,
306 'financial_type_id' => $this->_financialTypeId,
307 'contribution_status_id' => 1,
308 );
309 $priceFields = $this->createPriceSet();
310 foreach ($priceFields['values'] as $key => $priceField) {
311 $lineItems[$key] = array(
312 'price_field_id' => $priceField['price_field_id'],
313 'price_field_value_id' => $priceField['id'],
314 'label' => $priceField['label'],
315 'field_title' => $priceField['label'],
316 'qty' => 1,
317 'unit_price' => $priceField['amount'],
318 'line_total' => $priceField['amount'],
319 'financial_type_id' => $priceField['financial_type_id'],
320 'entity_table' => 'civicrm_participant',
321 );
322 }
323 $p['line_items'][] = array(
324 'line_item' => $lineItems,
325 'params' => array(
326 'contact_id' => $this->_individualId,
327 'event_id' => $this->_eventId,
328 'status_id' => 1,
329 'role_id' => 1,
330 'register_date' => '2007-07-21 00:00:00',
331 'source' => 'Online Event Registration: API Testing',
332 ),
333 );
54a54e15 334 $order = $this->callAPIAndDocument('order', 'create', $p, __FUNCTION__, __FILE__, 'Create order for participant', 'CreateOrderParticipant');
b8644ae3
PN
335 $params = array(
336 'contribution_id' => $order['id'],
337 );
338 $order = $this->callAPISuccess('order', 'get', $params);
339 $expectedResult = array(
340 $order['id'] => array(
341 'total_amount' => 300,
342 'contribution_id' => $order['id'],
343 'contribution_status' => 'Completed',
344 'net_amount' => 300,
345 ),
346 );
347 $this->checkPaymentResult($order, $expectedResult);
348 $this->callAPISuccessGetCount('ParticipantPayment', $params, 1);
349 $this->callAPISuccess('Contribution', 'Delete', array(
350 'id' => $order['id'],
351 ));
352 $p['line_items'][] = array(
353 'line_item' => $lineItems,
354 'params' => array(
355 'contact_id' => $this->individualCreate(),
356 'event_id' => $this->_eventId,
357 'status_id' => 1,
358 'role_id' => 1,
359 'register_date' => '2007-07-21 00:00:00',
360 'source' => 'Online Event Registration: API Testing',
361 ),
362 );
363 $p['total_amount'] = 600;
364 $order = $this->callAPISuccess('order', 'create', $p);
365 $expectedResult = array(
f30868a2 366 $order['id'] => array(
b8644ae3
PN
367 'total_amount' => 600,
368 'contribution_status' => 'Completed',
369 'net_amount' => 600,
370 ),
371 );
372 $paymentParticipant = array(
373 'contribution_id' => $order['id'],
374 );
375 $order = $this->callAPISuccess('order', 'get', $paymentParticipant);
376 $this->checkPaymentResult($order, $expectedResult);
377 $this->callAPISuccessGetCount('ParticipantPayment', $paymentParticipant, 2);
378 $this->callAPISuccess('Contribution', 'Delete', array(
379 'id' => $order['id'],
380 ));
381 }
382
383 /**
384 * Test create order api with line items
385 */
386 public function testAddOrderWithLineItems() {
387 $order = $this->addOrder(TRUE);
388 $params = array(
389 'contribution_id' => $order['id'],
390 );
391 $order = $this->callAPISuccess('order', 'get', $params);
392 $expectedResult = array(
393 $order['id'] => array(
394 'total_amount' => 300,
395 'contribution_id' => $order['id'],
396 'contribution_status' => 'Completed',
397 'net_amount' => 300,
398 ),
399 );
400 $items[] = array(
401 'entity_table' => 'civicrm_contribution',
402 'entity_id' => $order['id'],
403 'contribution_id' => $order['id'],
404 'unit_price' => 100,
405 'line_total' => 100,
406 );
407 $items[] = array(
408 'entity_table' => 'civicrm_contribution',
409 'entity_id' => $order['id'],
410 'contribution_id' => $order['id'],
411 'unit_price' => 200,
412 'line_total' => 200,
413 );
414 $this->checkPaymentResult($order, $expectedResult, $items);
415 $params = array(
416 'entity_table' => 'civicrm_contribution',
417 'entity_id' => $order['id'],
418 );
419 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
420 $this->assertEquals($eft['values'][$eft['id']]['amount'], 300);
421 $params = array(
422 'entity_table' => 'civicrm_financial_item',
423 'financial_trxn_id' => $eft['values'][$eft['id']]['financial_trxn_id'],
424 );
425 $eft = $this->callAPISuccess('EntityFinancialTrxn', 'get', $params);
426 $amounts = array(200, 100);
427 foreach ($eft['values'] as $value) {
428 $this->assertEquals($value['amount'], array_pop($amounts));
429 }
430 $this->callAPISuccess('Contribution', 'Delete', array(
431 'id' => $order['id'],
432 ));
433 }
434
5f44f698
PN
435 /**
436 * Test delete order api
437 */
438 public function testDeleteOrder() {
439 $order = $this->addOrder(FALSE, 100);
440 $params = array(
441 'contribution_id' => $order['id'],
442 );
443 try {
444 $this->callAPISuccess('order', 'delete', $params);
445 $this->fail("Missed expected exception");
446 }
447 catch (Exception $expected) {
95c4e89e 448 $this->callAPISuccess('Contribution', 'create', array(
5f44f698
PN
449 'contribution_id' => $order['id'],
450 'is_test' => TRUE,
451 ));
54a54e15 452 $this->callAPIAndDocument('order', 'delete', $params, __FUNCTION__, __FILE__);
5f44f698
PN
453 $order = $this->callAPISuccess('order', 'get', $params);
454 $this->assertEquals(0, $order['count']);
455 }
456 }
457
65f7f9f6
PN
458 /**
459 * Test cancel order api
460 */
461 public function testCancelOrder() {
462 $contribution = $this->addOrder(FALSE, 100);
463 $params = array(
464 'contribution_id' => $contribution['id'],
465 );
54a54e15 466 $this->callAPIAndDocument('order', 'cancel', $params, __FUNCTION__, __FILE__);
65f7f9f6
PN
467 $order = $this->callAPISuccess('Order', 'get', $params);
468 $expectedResult = array(
469 $contribution['id'] => array(
470 'total_amount' => 100,
471 'contribution_id' => $contribution['id'],
472 'contribution_status' => 'Cancelled',
473 'net_amount' => 100,
474 ),
475 );
476 $this->checkPaymentResult($order, $expectedResult);
477 $this->callAPISuccess('Contribution', 'Delete', array(
478 'id' => $contribution['id'],
479 ));
480 }
481
b63480e3
PN
482 /**
483 * Test cancel order api
484 */
485 public function testCancelWithParticipant() {
e4649817 486 $event = $this->eventCreate();
487 $this->_eventId = $event['id'];
b63480e3
PN
488 $eventParams = array(
489 'id' => $this->_eventId,
490 'financial_type_id' => 4,
491 'is_monetary' => 1,
492 );
493 $this->callAPISuccess('event', 'create', $eventParams);
494 $participantParams = array(
495 'financial_type_id' => 4,
496 'event_id' => $this->_eventId,
497 'role_id' => 1,
498 'status_id' => 1,
499 'fee_currency' => 'USD',
500 'contact_id' => $this->_individualId,
501 );
502 $participant = $this->callAPISuccess('Participant', 'create', $participantParams);
503 $extraParams = array(
504 'contribution_mode' => 'participant',
505 'participant_id' => $participant['id'],
506 );
507 $contribution = $this->addOrder(TRUE, 100, $extraParams);
508 $paymentParticipant = array(
509 'participant_id' => $participant['id'],
510 'contribution_id' => $contribution['id'],
511 );
512 $this->callAPISuccess('ParticipantPayment', 'create', $paymentParticipant);
513 $params = array(
514 'contribution_id' => $contribution['id'],
515 );
516 $this->callAPISuccess('order', 'cancel', $params);
517 $order = $this->callAPISuccess('Order', 'get', $params);
518 $expectedResult = array(
519 $contribution['id'] => array(
520 'total_amount' => 100,
521 'contribution_id' => $contribution['id'],
522 'contribution_status' => 'Cancelled',
523 'net_amount' => 100,
524 ),
525 );
dfd6297d 526 $this->checkPaymentResult($order, $expectedResult);
b63480e3
PN
527 $participantPayment = $this->callAPISuccess('ParticipantPayment', 'getsingle', $params);
528 $participant = $this->callAPISuccess('participant', 'get', array('id' => $participantPayment['participant_id']));
529 $this->assertEquals($participant['values'][$participant['id']]['participant_status'], 'Cancelled');
530 $this->callAPISuccess('Contribution', 'Delete', array(
531 'id' => $contribution['id'],
532 ));
533 }
534
73c0e107 535}