CRM-16188, added order api test for get action
[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 /**
66 * Test Get Payment api.
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']);
78 $expectedResult = array($contribution['id'] => array(
79 'total_amount' => 100,
80 'contribution_id' => $contribution['id'],
81 'contribution_status' => 'Completed',
82 'net_amount' => 100,
83 ));
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
98 /**
99 * Function to assert db values
100 */
101 public function checkPaymentResult($results, $expectedResult, $lineItems = NULL) {
102 foreach ($expectedResult[$results['id']] as $key => $value) {
103 $this->assertEquals($results['values'][$results['id']][$key], $value);
104 }
105
106 if ($lineItems) {
107 foreach ($lineItems as $key => $items) {
108 foreach ($items as $k => $item) {
109 $this->assertEquals($results['values'][$results['id']]['line_items'][$key][$k], $item);
110 }
111 }
112 }
113 }
114
115 /**
116 * add order
117 *
118 * @param bool $isPriceSet
119 * @param float $amount
120 * @param array $extraParams
121 *
122 * @return array
123 */
124 public function addOrder($isPriceSet, $amount = 300, $extraParams = array()) {
125 $p = array(
126 'contact_id' => $this->_individualId,
127 'receive_date' => '2010-01-20',
128 'total_amount' => $amount,
129 'financial_type_id' => $this->_financialTypeId,
130 'contribution_status_id' => 1,
131 );
132
133 if ($isPriceSet) {
134 $priceFields = $this->createPriceSet();
135 foreach ($priceFields['values'] as $key => $priceField) {
136 $lineItems[1][$key] = array(
137 'price_field_id' => $priceField['price_field_id'],
138 'price_field_value_id' => $priceField['id'],
139 'label' => $priceField['label'],
140 'field_title' => $priceField['label'],
141 'qty' => 1,
142 'unit_price' => $priceField['amount'],
143 'line_total' => $priceField['amount'],
144 'financial_type_id' => $priceField['financial_type_id'],
145 );
146 }
147 $p['line_item'] = $lineItems;
148 }
149 $p = array_merge($extraParams, $p);
150 return $this->callAPISuccess('Contribution', 'create', $p);
151 }
152
153}