Preliminary cleanup - use CRM_Core_Exception, fix comments
[civicrm-core.git] / tests / phpunit / CRM / Event / Form / Registration / ConfirmTest.php
1 <?php
2
3 /**
4 * Test CRM_Event_Form_Registration functions.
5 *
6 * @package CiviCRM
7 * @group headless
8 */
9 class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase {
10
11 public function setUp() {
12 $this->useTransaction(TRUE);
13 parent::setUp();
14 }
15
16 /**
17 * Initial test of submit function.
18 *
19 * @throws \Exception
20 */
21 public function testSubmit() {
22 $event = $this->eventCreate();
23 CRM_Event_Form_Registration_Confirm::testSubmit([
24 'id' => $event['id'],
25 'contributeMode' => 'direct',
26 'registerByID' => $this->createLoggedInUser(),
27 'params' => [
28 [
29 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
30 'entryURL' => 'http://dmaster.local/civicrm/event/register?reset=1&amp;id=3',
31 'first_name' => 'k',
32 'last_name' => 'p',
33 'email-Primary' => 'demo@example.com',
34 'hidden_processor' => '1',
35 'credit_card_number' => '4111111111111111',
36 'cvv2' => '123',
37 'credit_card_exp_date' => [
38 'M' => '1',
39 'Y' => '2019',
40 ],
41 'credit_card_type' => 'Visa',
42 'billing_first_name' => 'p',
43 'billing_middle_name' => '',
44 'billing_last_name' => 'p',
45 'billing_street_address-5' => 'p',
46 'billing_city-5' => 'p',
47 'billing_state_province_id-5' => '1061',
48 'billing_postal_code-5' => '7',
49 'billing_country_id-5' => '1228',
50 'scriptFee' => '',
51 'scriptArray' => '',
52 'priceSetId' => '6',
53 'price_7' => [
54 13 => 1,
55 ],
56 'payment_processor_id' => '1',
57 'bypass_payment' => '',
58 'MAX_FILE_SIZE' => '33554432',
59 'is_primary' => 1,
60 'is_pay_later' => 0,
61 'campaign_id' => NULL,
62 'defaultRole' => 1,
63 'participant_role_id' => '1',
64 'currencyID' => 'USD',
65 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
66 'amount' => '800.00',
67 'tax_amount' => NULL,
68 'year' => '2019',
69 'month' => '1',
70 'ip_address' => '127.0.0.1',
71 'invoiceID' => '57adc34957a29171948e8643ce906332',
72 'button' => '_qf_Register_upload',
73 'billing_state_province-5' => 'AP',
74 'billing_country-5' => 'US',
75 ],
76 ],
77 ]);
78 $this->callAPISuccessGetSingle('Participant', []);
79 }
80
81 /**
82 * Initial test of submit function for paid event.
83 *
84 * @param string $thousandSeparator
85 *
86 * @dataProvider getThousandSeparators
87 *
88 * @throws \Exception
89 */
90 public function testPaidSubmit($thousandSeparator) {
91 $this->setCurrencySeparators($thousandSeparator);
92 $paymentProcessorID = $this->processorCreate();
93 /* @var \CRM_Core_Payment_Dummy $processor */
94 $processor = Civi\Payment\System::singleton()->getById($paymentProcessorID);
95 $processor->setDoDirectPaymentResult(['fee_amount' => 1.67]);
96 $params = ['is_monetary' => 1, 'financial_type_id' => 1];
97 $event = $this->eventCreate($params);
98 $individualID = $this->individualCreate();
99 CRM_Event_Form_Registration_Confirm::testSubmit([
100 'id' => $event['id'],
101 'contributeMode' => 'direct',
102 'registerByID' => $individualID,
103 'paymentProcessorObj' => CRM_Financial_BAO_PaymentProcessor::getPayment($paymentProcessorID),
104 'totalAmount' => $this->formatMoneyInput(8000.67),
105 'params' => [
106 [
107 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
108 'entryURL' => 'http://dmaster.local/civicrm/event/register?reset=1&amp;id=3',
109 'first_name' => 'k',
110 'last_name' => 'p',
111 'email-Primary' => 'demo@example.com',
112 'hidden_processor' => '1',
113 'credit_card_number' => '4111111111111111',
114 'cvv2' => '123',
115 'credit_card_exp_date' => [
116 'M' => '1',
117 'Y' => '2019',
118 ],
119 'credit_card_type' => 'Visa',
120 'billing_first_name' => 'p',
121 'billing_middle_name' => '',
122 'billing_last_name' => 'p',
123 'billing_street_address-5' => 'p',
124 'billing_city-5' => 'p',
125 'billing_state_province_id-5' => '1061',
126 'billing_postal_code-5' => '7',
127 'billing_country_id-5' => '1228',
128 'scriptFee' => '',
129 'scriptArray' => '',
130 'priceSetId' => '6',
131 'price_7' => [
132 13 => 1,
133 ],
134 'payment_processor_id' => $paymentProcessorID,
135 'bypass_payment' => '',
136 'MAX_FILE_SIZE' => '33554432',
137 'is_primary' => 1,
138 'is_pay_later' => 0,
139 'campaign_id' => NULL,
140 'defaultRole' => 1,
141 'participant_role_id' => '1',
142 'currencyID' => 'USD',
143 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
144 'amount' => $this->formatMoneyInput(8000.67),
145 'tax_amount' => NULL,
146 'year' => '2019',
147 'month' => '1',
148 'ip_address' => '127.0.0.1',
149 'invoiceID' => '57adc34957a29171948e8643ce906332',
150 'button' => '_qf_Register_upload',
151 'billing_state_province-5' => 'AP',
152 'billing_country-5' => 'US',
153 ],
154 ],
155 ]);
156 $this->callAPISuccessGetCount('Participant', [], 1);
157 $contribution = $this->callAPISuccessGetSingle('Contribution', []);
158 $this->assertEquals(8000.67, $contribution['total_amount']);
159 $this->assertEquals(1.67, $contribution['fee_amount']);
160 $this->assertEquals(7999, $contribution['net_amount']);
161 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC');
162 $financialTrxn = $this->callAPISuccessGetSingle(
163 'FinancialTrxn',
164 [
165 'id' => $lastFinancialTrxnId['financialTrxnId'],
166 'return' => ['payment_processor_id', 'card_type_id.label', 'pan_truncation'],
167 ]
168 );
169 $this->assertEquals(CRM_Utils_Array::value('payment_processor_id', $financialTrxn), $paymentProcessorID);
170 $this->assertEquals(CRM_Utils_Array::value('card_type_id.label', $financialTrxn), 'Visa');
171 $this->assertEquals(CRM_Utils_Array::value('pan_truncation', $financialTrxn), 1111);
172
173 // This looks like it's missing an item for the main contribution - but just locking in current behaviour.
174 $financialItems = $this->callAPISuccess('FinancialItem', 'get', [
175 'return' => ['description', 'financial_account_id', 'amount', 'contact_id', 'currency', 'status_id', 'entity_table', 'entity_id'],
176 'sequential' => 1,
177 ])['values'];
178
179 $entityFinancialTrxns = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['sequential' => 1])['values'];
180
181 $this->assertAPIArrayComparison([
182 'entity_table' => 'civicrm_contribution',
183 'entity_id' => $contribution['id'],
184 'financial_trxn_id' => $financialTrxn['id'],
185 'amount' => '8000.67',
186 ], $entityFinancialTrxns[0], ['id']);
187
188 $this->assertAPIArrayComparison([
189 'entity_table' => 'civicrm_contribution',
190 'entity_id' => $contribution['id'],
191 'financial_trxn_id' => $financialTrxn['id'] + 1,
192 'amount' => '1.67',
193 ], $entityFinancialTrxns[1], ['id']);
194
195 $this->assertAPIArrayComparison([
196 'entity_table' => 'civicrm_financial_item',
197 'entity_id' => $financialItems[0]['id'],
198 'financial_trxn_id' => $financialTrxn['id'] + 1,
199 'amount' => '1.67',
200 ], $entityFinancialTrxns[2], ['id', 'entity_id']);
201 }
202
203 /**
204 * Test for Tax amount for multiple participant.
205 *
206 * @throws \Exception
207 */
208 public function testTaxMultipleParticipant() {
209 $params = ['is_monetary' => 1, 'financial_type_id' => 1];
210 $event = $this->eventCreate($params);
211 CRM_Event_Form_Registration_Confirm::testSubmit([
212 'id' => $event['id'],
213 'contributeMode' => 'direct',
214 'registerByID' => $this->createLoggedInUser(),
215 'totalAmount' => 440,
216 'event' => reset($event['values']),
217 'params' => [
218 [
219 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
220 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&amp;id={$event['id']}",
221 'first_name' => 'Participant1',
222 'last_name' => 'LastName',
223 'email-Primary' => 'participant1@example.com',
224 'scriptFee' => '',
225 'scriptArray' => '',
226 'additional_participants' => 2,
227 'payment_processor_id' => 0,
228 'bypass_payment' => '',
229 'MAX_FILE_SIZE' => '33554432',
230 'is_primary' => 1,
231 'is_pay_later' => 1,
232 'campaign_id' => NULL,
233 'defaultRole' => 1,
234 'participant_role_id' => '1',
235 'currencyID' => 'USD',
236 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
237 'amount' => '100.00',
238 'tax_amount' => 10,
239 'ip_address' => '127.0.0.1',
240 'invoiceID' => '57adc34957a29171948e8643ce906332',
241 'trxn_id' => '123456789',
242 'button' => '_qf_Register_upload',
243 ],
244 [
245 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
246 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&amp;id={$event['id']}",
247 'first_name' => 'Participant2',
248 'last_name' => 'LastName',
249 'email-Primary' => 'participant2@example.com',
250 'scriptFee' => '',
251 'scriptArray' => '',
252 'campaign_id' => NULL,
253 'is_pay_later' => 1,
254 'participant_role_id' => '1',
255 'currencyID' => 'USD',
256 'amount_level' => '\ 1Tiny-tots (ages 9-18) - 1\ 1',
257 'amount' => '200.00',
258 'tax_amount' => 20,
259 ],
260 [
261 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
262 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&amp;id={$event['id']}",
263 'first_name' => 'Participant3',
264 'last_name' => 'LastName',
265 'email-Primary' => 'participant3@example.com',
266 'scriptFee' => '',
267 'scriptArray' => '',
268 'campaign_id' => NULL,
269 'is_pay_later' => 1,
270 'participant_role_id' => '1',
271 'currencyID' => 'USD',
272 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
273 'amount' => '100.00',
274 'tax_amount' => 10,
275 ],
276 ],
277 ]);
278 $this->callAPISuccessGetCount('Participant', [], 3);
279 $contribution = $this->callAPISuccessGetSingle(
280 'Contribution',
281 [
282 'return' => ['tax_amount', 'total_amount'],
283 ]
284 );
285 $this->assertEquals($contribution['tax_amount'], 40, 'Invalid Tax amount.');
286 $this->assertEquals($contribution['total_amount'], 440, 'Invalid Tax amount.');
287 }
288
289 /**
290 * Test online registration for event with no price options selected as per CRM-19964.
291 */
292 public function testOnlineRegNoPrice() {
293 $paymentProcessorID = $this->processorCreate(['is_default' => TRUE, 'user_name' => 'Test', 'is_test' => FALSE]);
294 $paymentProcessorID = $this->processorCreate(['is_default' => TRUE, 'user_name' => 'Test', 'is_test' => TRUE]);
295 $params = [
296 'start_date' => date('YmdHis', strtotime('+ 1 week')),
297 'end_date' => date('YmdHis', strtotime('+ 1 year')),
298 'registration_start_date' => date('YmdHis', strtotime('- 1 day')),
299 'registration_end_date' => date('YmdHis', strtotime('+ 1 year')),
300 'payment_processor_id' => $paymentProcessorID,
301 'is_monetary' => TRUE,
302 'financial_type_id' => 'Event Fee',
303 ];
304 $event = $this->eventCreate($params);
305 $priceFieldOptions = [
306 'option_label' => 'Price Field',
307 'option_value' => 100,
308 'is_required' => FALSE,
309 'html_type' => 'Text',
310 ];
311 $this->createPriceSet('event', $event['id'], $priceFieldOptions);
312
313 $priceField = $this->callAPISuccess('PriceField', 'get',
314 [
315 'label' => 'Price Field',
316 ]
317 );
318 // Create online event registration.
319 CRM_Event_Form_Registration_Confirm::testSubmit([
320 'id' => $event['id'],
321 'contributeMode' => 'direct',
322 'registerByID' => $this->createLoggedInUser(),
323 'params' => [
324 [
325 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
326 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&amp;id={$event['id']}",
327 'first_name' => 'Bruce',
328 'last_name' => 'Wayne',
329 'email-Primary' => 'bruce@gotham.com',
330 'price_' . $priceField['id'] => '',
331 'priceSetId' => $priceField['values'][$priceField['id']]['price_set_id'],
332 'payment_processor_id' => $paymentProcessorID,
333 'amount' => 0,
334 'bypass_payment' => '',
335 'MAX_FILE_SIZE' => '33554432',
336 'is_primary' => 1,
337 'is_pay_later' => 0,
338 'campaign_id' => NULL,
339 'defaultRole' => 1,
340 'participant_role_id' => '1',
341 'tax_amount' => NULL,
342 'ip_address' => '127.0.0.1',
343 'invoiceID' => '57adc34957a29171948e8643ce906332',
344 'button' => '_qf_Register_upload',
345 'scriptFee' => '',
346 'scriptArray' => '',
347 ],
348 ],
349 ]);
350 $contribution = $this->callAPISuccess('Contribution', 'get', ['invoice_id' => '57adc34957a29171948e8643ce906332']);
351 $this->assertEquals($contribution['count'], '0', "Contribution should not be created for zero fee event registration when no price field selected.");
352 }
353
354 }