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