Merge pull request #18114 from eileenmcnaughton/phone
[civicrm-core.git] / tests / phpunit / CRMTraits / Financial / OrderTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Trait OrderTrait
14 *
15 * Trait for setting up orders for tests.
16 */
17 trait CRMTraits_Financial_OrderTrait {
18
19 use \Civi\Test\Api3TestTrait;
20
21 /**
22 * Create a pending membership from a recurring order.
23 *
24 * @throws \CRM_Core_Exception
25 */
26 public function createRepeatMembershipOrder() {
27 $this->createExtraneousContribution();
28 $this->ids['contact'][0] = $this->individualCreate();
29 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
30
31 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array_merge([
32 'contact_id' => $this->_contactID,
33 'amount' => 1000,
34 'sequential' => 1,
35 'installments' => 5,
36 'frequency_unit' => 'Month',
37 'frequency_interval' => 1,
38 'invoice_id' => $this->_invoiceID,
39 'contribution_status_id' => 2,
40 'payment_processor_id' => $this->_paymentProcessorID,
41 // processor provided ID - use contact ID as proxy.
42 'processor_id' => $this->_contactID,
43 ]));
44
45 $orderID = $this->callAPISuccess('Order', 'create', [
46 'total_amount' => '200',
47 'financial_type_id' => 'Donation',
48 'contact_id' => $this->_contactID,
49 'contribution_page_id' => $this->_contributionPageID,
50 'payment_processor_id' => $this->_paymentProcessorID,
51 'is_test' => 0,
52 'receive_date' => '2019-07-25 07:34:23',
53 'skipCleanMoney' => TRUE,
54 'contribution_recur_id' => $contributionRecur['id'],
55 'line_items' => [
56 [
57 'params' => [
58 'contact_id' => $this->ids['contact'][0],
59 'membership_type_id' => $this->ids['membership_type'][0],
60 'contribution_recur_id' => $contributionRecur['id'],
61 'source' => 'Payment',
62 ],
63 'line_item' => [
64 [
65 'label' => 'General',
66 'qty' => 1,
67 'unit_price' => 200,
68 'line_total' => 200,
69 'financial_type_id' => 1,
70 'entity_table' => 'civicrm_membership',
71 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
72 'return' => 'id',
73 'label' => 'Membership Amount',
74 'options' => ['limit' => 1, 'sort' => 'id DESC'],
75 ]),
76 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
77 'return' => 'id',
78 'label' => 'General',
79 'options' => ['limit' => 1, 'sort' => 'id DESC'],
80 ]),
81 ],
82 ],
83 ],
84 ],
85 ])['id'];
86
87 $this->ids['ContributionRecur'][0] = $contributionRecur['id'];
88 $this->ids['Contribution'][0] = $orderID;
89 }
90
91 /**
92 * Create an order with more than one membership.
93 *
94 * @throws \CRM_Core_Exception
95 */
96 protected function createMultipleMembershipOrder() {
97 $this->createExtraneousContribution();
98 $this->ids['contact'][0] = $this->individualCreate();
99 $this->ids['contact'][1] = $this->individualCreate();
100 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
101 $this->ids['membership_type'][1] = $this->membershipTypeCreate(['name' => 'Type 2']);
102 $priceFieldID = $this->callAPISuccessGetValue('price_field', [
103 'return' => 'id',
104 'label' => 'Membership Amount',
105 'options' => ['limit' => 1, 'sort' => 'id DESC'],
106 ]);
107 $generalPriceFieldValueID = $this->callAPISuccessGetValue('price_field_value', [
108 'return' => 'id',
109 'label' => 'General',
110 'options' => ['limit' => 1, 'sort' => 'id DESC'],
111 ]);
112
113 $orderID = $this->callAPISuccess('Order', 'create', [
114 'total_amount' => 400,
115 'financial_type_id' => 'Member Dues',
116 'contact_id' => $this->_contactID,
117 'is_test' => 0,
118 'payment_instrument_id' => 'Check',
119 'receive_date' => '2019-07-25 07:34:23',
120 'line_items' => [
121 [
122 'params' => [
123 'contact_id' => $this->ids['contact'][0],
124 'membership_type_id' => $this->ids['membership_type'][0],
125 'source' => 'Payment',
126 ],
127 'line_item' => [
128 [
129 'label' => 'General',
130 'qty' => 1,
131 'unit_price' => 200,
132 'line_total' => 200,
133 'financial_type_id' => 1,
134 'entity_table' => 'civicrm_membership',
135 'price_field_id' => $priceFieldID,
136 'price_field_value_id' => $generalPriceFieldValueID,
137 ],
138 ],
139 ],
140 [
141 'params' => [
142 'contact_id' => $this->ids['contact'][1],
143 'membership_type_id' => $this->ids['membership_type'][0],
144 'source' => 'Payment',
145 ],
146 'line_item' => [
147 [
148 'label' => 'General',
149 'qty' => 1,
150 'unit_price' => 200,
151 'line_total' => 200,
152 'financial_type_id' => 1,
153 'entity_table' => 'civicrm_membership',
154 'price_field_id' => $priceFieldID,
155 'price_field_value_id' => $generalPriceFieldValueID,
156 ],
157 ],
158 ],
159 ],
160 ])['id'];
161
162 $this->ids['Contribution'][0] = $orderID;
163 }
164
165 /**
166 * Create an extraneous contribution to throw off any 'number one bugs'.
167 *
168 * Ie this means our real data starts from 2 & we won't hit 'pretend passes'
169 * just because the number 1 is used for multiple entities.
170 */
171 protected function createExtraneousContribution() {
172 $this->contributionCreate([
173 'contact_id' => $this->individualCreate(),
174 'is_test' => 1,
175 'financial_type_id' => 1,
176 'invoice_id' => 'abcd',
177 'trxn_id' => 345,
178 'receive_date' => '2019-07-25 07:34:23',
179 ]);
180 }
181
182 }