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