Merge pull request #19105 from colemanw/searchJoins
[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' => $this->getMembershipLineItem(),
64 ],
65 ],
66 ])['id'];
67
68 $this->ids['ContributionRecur'][0] = $contributionRecur['id'];
69 $this->ids['Contribution'][0] = $orderID;
70 }
71
72 /**
73 * Create an order with a contribution AND a membership line item.
74 *
75 * @throws \CRM_Core_Exception
76 */
77 protected function createContributionAndMembershipOrder(): void {
78 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
79 $orderID = $this->callAPISuccess('Order', 'create', [
80 'financial_type_id' => 'Donation',
81 'contact_id' => $this->_contactID,
82 'is_test' => 0,
83 'payment_instrument_id' => 'Check',
84 'receive_date' => date('Y-m-d'),
85 'line_items' => [
86 [
87 'params' => [
88 'contact_id' => $this->_contactID,
89 'source' => 'Payment',
90 ],
91 'line_item' => [
92 [
93 'label' => 'Contribution Amount',
94 'qty' => 1,
95 'unit_price' => 100,
96 'line_total' => 100,
97 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
98 'entity_table' => 'civicrm_contribution',
99 'price_field_id' => $this->callAPISuccessGetValue('price_field', [
100 'return' => 'id',
101 'label' => 'Contribution Amount',
102 'options' => ['limit' => 1, 'sort' => 'id DESC'],
103 ]),
104 'price_field_value_id' => NULL,
105 ],
106 ],
107 ],
108 [
109 'params' => [
110 'contact_id' => $this->_contactID,
111 'membership_type_id' => 'General',
112 'source' => 'Payment',
113 // This is necessary because Membership_BAO otherwise ignores the
114 // pending status. I do have a fix but it's held up behind other pending-review PRs
115 // so this should be temporary until we get the membership PRs flowing.
116 'skipStatusCal' => TRUE,
117 ],
118 'line_item' => $this->getMembershipLineItem(),
119 ],
120 ],
121 ])['id'];
122
123 $this->ids['Contribution'][0] = $orderID;
124 }
125
126 /**
127 * Create an order with more than one membership.
128 *
129 * @throws \CRM_Core_Exception
130 */
131 protected function createMultipleMembershipOrder() {
132 $this->createExtraneousContribution();
133 $this->ids['contact'][0] = $this->individualCreate();
134 $this->ids['contact'][1] = $this->individualCreate();
135 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
136 $this->ids['membership_type'][1] = $this->membershipTypeCreate(['name' => 'Type 2']);
137 $priceFieldID = $this->callAPISuccessGetValue('price_field', [
138 'return' => 'id',
139 'label' => 'Membership Amount',
140 'options' => ['limit' => 1, 'sort' => 'id DESC'],
141 ]);
142 $generalPriceFieldValueID = $this->callAPISuccessGetValue('price_field_value', [
143 'return' => 'id',
144 'label' => 'General',
145 'options' => ['limit' => 1, 'sort' => 'id DESC'],
146 ]);
147
148 $orderID = $this->callAPISuccess('Order', 'create', [
149 'total_amount' => 400,
150 'financial_type_id' => 'Member Dues',
151 'contact_id' => $this->_contactID,
152 'is_test' => 0,
153 'payment_instrument_id' => 'Check',
154 'receive_date' => '2019-07-25 07:34:23',
155 'line_items' => [
156 [
157 'params' => [
158 'contact_id' => $this->ids['contact'][0],
159 'membership_type_id' => $this->ids['membership_type'][0],
160 'source' => 'Payment',
161 ],
162 'line_item' => [
163 [
164 'label' => 'General',
165 'qty' => 1,
166 'unit_price' => 200,
167 'line_total' => 200,
168 'financial_type_id' => 1,
169 'entity_table' => 'civicrm_membership',
170 'price_field_id' => $priceFieldID,
171 'price_field_value_id' => $generalPriceFieldValueID,
172 ],
173 ],
174 ],
175 [
176 'params' => [
177 'contact_id' => $this->ids['contact'][1],
178 'membership_type_id' => $this->ids['membership_type'][0],
179 'source' => 'Payment',
180 ],
181 'line_item' => [
182 [
183 'label' => 'General',
184 'qty' => 1,
185 'unit_price' => 200,
186 'line_total' => 200,
187 'financial_type_id' => 1,
188 'entity_table' => 'civicrm_membership',
189 'price_field_id' => $priceFieldID,
190 'price_field_value_id' => $generalPriceFieldValueID,
191 ],
192 ],
193 ],
194 ],
195 ])['id'];
196
197 $this->ids['Contribution'][0] = $orderID;
198 }
199
200 /**
201 * Create an order for an event.
202 *
203 * @param array $orderParams
204 *
205 * @throws \CRM_Core_Exception
206 */
207 protected function createEventOrder($orderParams = []) {
208 $this->ids['Contribution'][0] = $this->callAPISuccess('Order', 'create', array_merge($this->getParticipantOrderParams(), $orderParams))['id'];
209 $this->ids['Participant'][0] = $this->callAPISuccessGetValue('ParticipantPayment', ['return' => 'participant_id', 'contribution_id' => $this->ids['Contribution'][0]]);
210 }
211
212 /**
213 * Create an extraneous contribution to throw off any 'number one bugs'.
214 *
215 * Ie this means our real data starts from 2 & we won't hit 'pretend passes'
216 * just because the number 1 is used for multiple entities.
217 */
218 protected function createExtraneousContribution() {
219 $this->contributionCreate([
220 'contact_id' => $this->individualCreate(),
221 'is_test' => 1,
222 'financial_type_id' => 1,
223 'invoice_id' => 'abcd',
224 'trxn_id' => 345,
225 'receive_date' => '2019-07-25 07:34:23',
226 ]);
227 }
228
229 /**
230 * @return array[]
231 * @throws \CRM_Core_Exception
232 */
233 protected function getMembershipLineItem(): array {
234 return [
235 [
236 'label' => 'General',
237 'qty' => 1,
238 'unit_price' => 200,
239 'line_total' => 200,
240 'financial_type_id' => 1,
241 'entity_table' => 'civicrm_membership',
242 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
243 'return' => 'id',
244 'label' => 'Membership Amount',
245 'options' => ['limit' => 1, 'sort' => 'id DESC'],
246 ]),
247 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
248 'return' => 'id',
249 'label' => 'General',
250 'options' => ['limit' => 1, 'sort' => 'id DESC'],
251 ]),
252 ],
253 ];
254 }
255
256 }