4 * Test CRM_Event_Form_Registration functions.
9 class CRM_Event_Form_Registration_ConfirmTest
extends CiviUnitTestCase
{
11 use CRMTraits_Profile_ProfileTrait
;
13 public function setUp() {
14 $this->useTransaction(TRUE);
19 * Initial test of submit function.
23 public function testSubmit() {
24 $event = $this->eventCreate();
25 $mut = new CiviMailUtils($this, TRUE);
26 CRM_Event_Form_Registration_Confirm
::testSubmit([
28 'contributeMode' => 'direct',
29 'registerByID' => $this->createLoggedInUser(),
32 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
33 'entryURL' => 'http://dmaster.local/civicrm/event/register?reset=1&id=3',
36 'email-Primary' => 'demo@example.com',
37 'hidden_processor' => '1',
38 'credit_card_number' => '4111111111111111',
40 'credit_card_exp_date' => [
44 'credit_card_type' => 'Visa',
45 'billing_first_name' => 'p',
46 'billing_middle_name' => '',
47 'billing_last_name' => 'p',
48 'billing_street_address-5' => 'p',
49 'billing_city-5' => 'p',
50 'billing_state_province_id-5' => '1061',
51 'billing_postal_code-5' => '7',
52 'billing_country_id-5' => '1228',
59 'payment_processor_id' => '1',
60 'bypass_payment' => '',
61 'MAX_FILE_SIZE' => '33554432',
64 'campaign_id' => NULL,
66 'participant_role_id' => '1',
67 'currencyID' => 'USD',
68 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
73 'ip_address' => '127.0.0.1',
74 'invoiceID' => '57adc34957a29171948e8643ce906332',
75 'button' => '_qf_Register_upload',
76 'billing_state_province-5' => 'AP',
77 'billing_country-5' => 'US',
81 $participant = $this->callAPISuccessGetSingle('Participant', []);
83 'Dear Logged In, Thank you for your participation. This letter is a confirmation that your registration has been received and your status has been updated to Registered.',
86 $mut->clearMessages();
87 $tplVars = CRM_Core_Smarty
::singleton()->get_template_vars();
88 $this->assertEquals($participant['id'], $tplVars['participantID']);
93 * Initial test of submit function for paid event.
95 * @param string $thousandSeparator
97 * @dataProvider getThousandSeparators
101 public function testPaidSubmit($thousandSeparator) {
102 $this->setCurrencySeparators($thousandSeparator);
103 $mut = new CiviMailUtils($this);
104 $paymentProcessorID = $this->processorCreate();
105 /* @var \CRM_Core_Payment_Dummy $processor */
106 $processor = Civi\Payment\System
::singleton()->getById($paymentProcessorID);
107 $processor->setDoDirectPaymentResult(['fee_amount' => 1.67]);
108 $params = ['is_monetary' => 1, 'financial_type_id' => 1];
109 $event = $this->eventCreate($params);
110 $individualID = $this->individualCreate();
111 CRM_Event_Form_Registration_Confirm
::testSubmit([
112 'id' => $event['id'],
113 'contributeMode' => 'direct',
114 'registerByID' => $individualID,
115 'paymentProcessorObj' => CRM_Financial_BAO_PaymentProcessor
::getPayment($paymentProcessorID),
116 'totalAmount' => $this->formatMoneyInput(8000.67),
119 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
120 'entryURL' => 'http://dmaster.local/civicrm/event/register?reset=1&id=3',
123 'email-Primary' => 'demo@example.com',
124 'hidden_processor' => '1',
125 'credit_card_number' => '4111111111111111',
127 'credit_card_exp_date' => [
131 'credit_card_type' => 'Visa',
132 'billing_first_name' => 'p',
133 'billing_middle_name' => '',
134 'billing_last_name' => 'p',
135 'billing_street_address-5' => 'p',
136 'billing_city-5' => 'p',
137 'billing_state_province_id-5' => '1061',
138 'billing_postal_code-5' => '7',
139 'billing_country_id-5' => '1228',
144 'payment_processor_id' => $paymentProcessorID,
145 'bypass_payment' => '',
148 'campaign_id' => NULL,
150 'participant_role_id' => '1',
151 'currencyID' => 'USD',
152 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
153 'amount' => $this->formatMoneyInput(8000.67),
154 'tax_amount' => NULL,
157 'ip_address' => '127.0.0.1',
158 'invoiceID' => '57adc34957a29171948e8643ce906332',
159 'button' => '_qf_Register_upload',
160 'billing_state_province-5' => 'AP',
161 'billing_country-5' => 'US',
165 $this->callAPISuccessGetCount('Participant', [], 1);
166 $contribution = $this->callAPISuccessGetSingle('Contribution', []);
167 $this->assertEquals(8000.67, $contribution['total_amount']);
168 $this->assertEquals(1.67, $contribution['fee_amount']);
169 $this->assertEquals(7999, $contribution['net_amount']);
170 $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn
::getFinancialTrxnId($contribution['id'], 'DESC');
171 $financialTrxn = $this->callAPISuccessGetSingle(
174 'id' => $lastFinancialTrxnId['financialTrxnId'],
175 'return' => ['payment_processor_id', 'card_type_id.label', 'pan_truncation'],
178 $this->assertEquals(CRM_Utils_Array
::value('payment_processor_id', $financialTrxn), $paymentProcessorID);
179 $this->assertEquals(CRM_Utils_Array
::value('card_type_id.label', $financialTrxn), 'Visa');
180 $this->assertEquals(CRM_Utils_Array
::value('pan_truncation', $financialTrxn), 1111);
182 // This looks like it's missing an item for the main contribution - but just locking in current behaviour.
183 $financialItems = $this->callAPISuccess('FinancialItem', 'get', [
184 'return' => ['description', 'financial_account_id', 'amount', 'contact_id', 'currency', 'status_id', 'entity_table', 'entity_id'],
188 $entityFinancialTrxns = $this->callAPISuccess('EntityFinancialTrxn', 'get', ['sequential' => 1])['values'];
190 $this->assertAPIArrayComparison([
191 'entity_table' => 'civicrm_contribution',
192 'entity_id' => $contribution['id'],
193 'financial_trxn_id' => $financialTrxn['id'],
194 'amount' => '8000.67',
195 ], $entityFinancialTrxns[0], ['id']);
197 $this->assertAPIArrayComparison([
198 'entity_table' => 'civicrm_contribution',
199 'entity_id' => $contribution['id'],
200 'financial_trxn_id' => $financialTrxn['id'] +
1,
202 ], $entityFinancialTrxns[1], ['id']);
204 $this->assertAPIArrayComparison([
205 'entity_table' => 'civicrm_financial_item',
206 'entity_id' => $financialItems[0]['id'],
207 'financial_trxn_id' => $financialTrxn['id'] +
1,
209 ], $entityFinancialTrxns[2], ['id', 'entity_id']);
211 'Event Information and Location', 'Registration Confirmation - Annual CiviCRM meet',
212 'This letter is a confirmation that your registration has been received and your status has been updated to <strong> Registered</strong>',
214 $mut->clearMessages();
218 * Test for Tax amount for multiple participant.
220 * @throws \CRM_Core_Exception
222 public function testTaxMultipleParticipant() {
223 $mut = new CiviMailUtils($this);
224 $params = ['is_monetary' => 1, 'financial_type_id' => 1];
225 $event = $this->eventCreate($params);
226 CRM_Event_Form_Registration_Confirm
::testSubmit([
227 'id' => $event['id'],
228 'contributeMode' => 'direct',
229 'registerByID' => $this->createLoggedInUser(),
230 'totalAmount' => 440,
231 'event' => reset($event['values']),
234 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
235 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&id={$event['id']}",
236 'first_name' => 'Participant1',
237 'last_name' => 'LastName',
238 'email-Primary' => 'participant1@example.com',
239 'additional_participants' => 2,
240 'payment_processor_id' => 0,
241 'bypass_payment' => '',
242 'MAX_FILE_SIZE' => '33554432',
245 'campaign_id' => NULL,
247 'participant_role_id' => '1',
248 'currencyID' => 'USD',
249 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
250 'amount' => '100.00',
252 'ip_address' => '127.0.0.1',
253 'invoiceID' => '57adc34957a29171948e8643ce906332',
254 'trxn_id' => '123456789',
255 'button' => '_qf_Register_upload',
258 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
259 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&id={$event['id']}",
260 'first_name' => 'Participant2',
261 'last_name' => 'LastName',
262 'email-Primary' => 'participant2@example.com',
265 'campaign_id' => NULL,
267 'participant_role_id' => '1',
268 'currencyID' => 'USD',
269 'amount_level' => '\ 1Tiny-tots (ages 9-18) - 1\ 1',
270 'amount' => '200.00',
274 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
275 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&id={$event['id']}",
276 'first_name' => 'Participant3',
277 'last_name' => 'LastName',
278 'email-Primary' => 'participant3@example.com',
281 'campaign_id' => NULL,
283 'participant_role_id' => '1',
284 'currencyID' => 'USD',
285 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
286 'amount' => '100.00',
291 $participants = $this->callAPISuccess('Participant', 'get', [])['values'];
292 $this->assertCount(3, $participants);
293 $contribution = $this->callAPISuccessGetSingle(
296 'return' => ['tax_amount', 'total_amount'],
299 $this->assertEquals($contribution['tax_amount'], 40, 'Invalid Tax amount.');
300 $this->assertEquals($contribution['total_amount'], 440, 'Invalid Tax amount.');
301 $mailSent = $mut->getAllMessages();
302 $this->assertCount(3, $mailSent, 'Three mails should have been sent to the 3 participants.');
306 * Test online registration for event with no price options selected as per CRM-19964.
308 public function testOnlineRegNoPrice() {
309 $paymentProcessorID = $this->processorCreate(['is_default' => TRUE, 'user_name' => 'Test', 'is_test' => FALSE]);
310 $paymentProcessorID = $this->processorCreate(['is_default' => TRUE, 'user_name' => 'Test', 'is_test' => TRUE]);
312 'start_date' => date('YmdHis', strtotime('+ 1 week')),
313 'end_date' => date('YmdHis', strtotime('+ 1 year')),
314 'registration_start_date' => date('YmdHis', strtotime('- 1 day')),
315 'registration_end_date' => date('YmdHis', strtotime('+ 1 year')),
316 'payment_processor_id' => $paymentProcessorID,
317 'is_monetary' => TRUE,
318 'financial_type_id' => 'Event Fee',
320 $event = $this->eventCreate($params);
321 $priceFieldOptions = [
322 'option_label' => 'Price Field',
323 'option_value' => 100,
324 'is_required' => FALSE,
325 'html_type' => 'Text',
327 $this->createPriceSet('event', $event['id'], $priceFieldOptions);
329 $priceField = $this->callAPISuccess('PriceField', 'get',
331 'label' => 'Price Field',
334 // Create online event registration.
335 CRM_Event_Form_Registration_Confirm
::testSubmit([
336 'id' => $event['id'],
337 'contributeMode' => 'direct',
338 'registerByID' => $this->createLoggedInUser(),
341 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
342 'entryURL' => "http://dmaster.local/civicrm/event/register?reset=1&id={$event['id']}",
343 'first_name' => 'Bruce',
344 'last_name' => 'Wayne',
345 'email-Primary' => 'bruce@gotham.com',
346 'price_' . $priceField['id'] => '',
347 'priceSetId' => $priceField['values'][$priceField['id']]['price_set_id'],
348 'payment_processor_id' => $paymentProcessorID,
350 'bypass_payment' => '',
351 'MAX_FILE_SIZE' => '33554432',
354 'campaign_id' => NULL,
356 'participant_role_id' => '1',
357 'tax_amount' => NULL,
358 'ip_address' => '127.0.0.1',
359 'invoiceID' => '57adc34957a29171948e8643ce906332',
360 'button' => '_qf_Register_upload',
366 $contribution = $this->callAPISuccess('Contribution', 'get', ['invoice_id' => '57adc34957a29171948e8643ce906332']);
367 $this->assertEquals($contribution['count'], '0', "Contribution should not be created for zero fee event registration when no price field selected.");
371 * Test form profile assignment.
373 * @throws \CRM_Core_Exception
376 public function testAssignProfiles() {
377 $event = $this->eventCreate();
378 $this->createJoinedProfile(['entity_table' => 'civicrm_event', 'entity_id' => $event['id']]);
380 /* @var \CRM_Event_Form_Registration_Confirm $form */
381 $form = $this->getFormObject('CRM_Event_Form_Registration_Confirm');
382 $form->set('params', [[]]);
383 $form->set('id', $event['id']);
384 $form->set('values', [
385 'event' => $event['values'][$event['id']],
387 'custom_pre_id' => $this->ids
['UFGroup']['our profile'],
391 CRM_Event_Form_Registration_Confirm
::assignProfiles($form);
393 $smarty = CRM_Core_Smarty
::singleton();
394 $tplVar = $smarty->get_template_vars();
395 $this->assertEquals([
396 'CustomPre' => ['First Name' => NULL],
397 'CustomPreGroupTitle' => 'Public title',
398 ], $tplVar['primaryParticipantProfile']);
402 * Submit event registration with a note field
404 * @param array $event
405 * @param int $contact_id
409 private function submitWithNote($event, $contact_id) {
410 if (empty($contact_id)) {
411 $contact_id = $this->createLoggedInUser();
413 $mut = new CiviMailUtils($this, TRUE);
414 CRM_Event_Form_Registration_Confirm
::testSubmit([
415 'id' => $event['id'],
416 'contributeMode' => 'direct',
417 'registerByID' => $contact_id,
418 'custom_pre_id' => $event['custom_pre_id'],
421 'qfKey' => 'e6eb2903eae63d4c5c6cc70bfdda8741_2801',
422 'entryURL' => 'http://dmaster.local/civicrm/event/register?reset=1&id=3',
425 'email-Primary' => 'demo@example.com',
426 'hidden_processor' => '1',
427 'credit_card_number' => '4111111111111111',
429 'credit_card_exp_date' => [
433 'credit_card_type' => 'Visa',
434 'billing_first_name' => 'p',
435 'billing_middle_name' => '',
436 'billing_last_name' => 'p',
437 'billing_street_address-5' => 'p',
438 'billing_city-5' => 'p',
439 'billing_state_province_id-5' => '1061',
440 'billing_postal_code-5' => '7',
441 'billing_country_id-5' => '1228',
448 'payment_processor_id' => '1',
449 'bypass_payment' => '',
450 'MAX_FILE_SIZE' => '33554432',
453 'campaign_id' => NULL,
455 'participant_role_id' => '1',
456 'currencyID' => 'USD',
457 'amount_level' => '\ 1Tiny-tots (ages 5-8) - 1\ 1',
458 'amount' => '800.00',
459 'tax_amount' => NULL,
462 'ip_address' => '127.0.0.1',
463 'invoiceID' => '57adc34957a29171948e8643ce906332',
464 'button' => '_qf_Register_upload',
465 'billing_state_province-5' => 'AP',
466 'billing_country-5' => 'US',
467 'note' => $event['note'],
471 $participant = $this->callAPISuccessGetSingle('Participant', []);
472 $mut->checkMailLog(['Comment: ' . $event['note'] . chr(0x0A)]);
474 $mut->clearMessages();
475 $tplVars = CRM_Core_Smarty
::singleton()->get_template_vars();
476 $this->assertEquals($participant['id'], $tplVars['participantID']);
477 //return ['contact_id' => $contact_id, 'participant_id' => $participant['id']];
478 return [$contact_id, $participant['id']];
482 * Create an event with a "pre" profile
484 * @throws \CRM_Core_Exception
487 private function creatEventWithProfile($event) {
489 $event = $this->eventCreate();
490 $this->createJoinedProfile(['entity_table' => 'civicrm_event', 'entity_id' => $event['id']]);
491 $this->uf_field_add($this->ids
["UFGroup"]["our profile"], 'note', 'Contact', 'Comment');
494 /* @var \CRM_Event_Form_Registration_Confirm $form */
495 $form = $this->getFormObject('CRM_Event_Form_Registration_Confirm');
496 $form->set('params', [[]]);
497 $form->set('id', $event['id']);
498 $form->set('values', [
499 'event' => $event['values'][$event['id']],
501 'custom_pre_id' => $this->ids
['UFGroup']['our profile'],
505 CRM_Event_Form_Registration_Confirm
::assignProfiles($form);
507 $smarty = CRM_Core_Smarty
::singleton();
508 $tplVar = $smarty->get_template_vars();
509 $this->assertEquals([
510 'CustomPre' => ['First Name' => NULL, 'Comment' => NULL],
511 'CustomPreGroupTitle' => 'Public title',
512 ], $tplVar['primaryParticipantProfile']);
517 * Add a field to the specified profile
519 * @param int $uf_group_id
520 * @param string $field_name
521 * @param string $field_type
522 * @param string $field_label
526 private function uf_field_add($uf_group_id, $field_name, $field_type, $field_label) {
528 'field_name' => $field_name,
529 'field_type' => $field_type,
530 'visibility' => 'Public Pages and Listings',
532 'label' => $field_label,
533 'is_searchable' => 1,
535 'uf_group_id' => $uf_group_id,
537 $result = civicrm_api3('UFField', 'create', $params);
543 * Test submission with a note in the profile, ensuring the confirmation
544 * email reflects the submitted value
546 public function testNoteSubmission() {
547 //create an event with an attached profile containing a note
548 $event = $this->creatEventWithProfile(NULL);
549 $event['custom_pre_id'] = $this->ids
["UFGroup"]["our profile"];
550 $event['note'] = "This is note 1";
551 list($contact_id, $participant_id) = $this->submitWithNote($event, NULL);
552 civicrm_api3('Participant', 'delete', ['id' => $participant_id]);
554 //now that the contact has one note, register this contact again with a different note
555 //and confirm that the note shown in the email is the current one
556 $event = $this->creatEventWithProfile($event);
557 $event['custom_pre_id'] = $this->ids
["UFGroup"]["our profile"];
558 $event['note'] = "This is note 2";
559 list($contact_id, $participant_id) = $this->submitWithNote($event, $contact_id);
560 civicrm_api3('Participant', 'delete', ['id' => $participant_id]);
562 //finally, submit a blank note and confirm that the note shown in the email is blank
563 $event = $this->creatEventWithProfile($event);
564 $event['custom_pre_id'] = $this->ids
["UFGroup"]["our profile"];
566 list($contact_id, $participant_id) = $this->submitWithNote($event, $contact_id);