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