From 5fda4b160e7757144155dcba819f9cf8737b6704 Mon Sep 17 00:00:00 2001 From: eileen Date: Sun, 23 Jul 2017 19:25:20 +1200 Subject: [PATCH] Test for CRM-20915 - Initial testing on Back office participant form class. Although the failing assertion is not present, this test demonstrates the bug described in CRM-20915. It also adds initial test to the form --- CRM/Event/Form/Participant.php | 100 ++++++++++++------ .../CRM/Event/Form/ParticipantTest.php | 95 +++++++++++++++++ 2 files changed, 161 insertions(+), 34 deletions(-) create mode 100644 tests/phpunit/CRM/Event/Form/ParticipantTest.php diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index d4f35c4b6b..c3555bad67 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -263,15 +263,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment } } } - - // get the option value for custom data type - $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name'); - $this->_roleCustomDataTypeID = array_search('ParticipantRole', $customDataType); - $this->_eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType); - $this->_eventTypeCustomDataTypeID = array_search('ParticipantEventType', $customDataType); - $this->assign('roleCustomDataTypeID', $this->_roleCustomDataTypeID); - $this->assign('eventNameCustomDataTypeID', $this->_eventNameCustomDataTypeID); - $this->assign('eventTypeCustomDataTypeID', $this->_eventTypeCustomDataTypeID); + $this->setCustomDataTypes(); if ($this->_mode) { $this->assign('participantMode', $this->_mode); @@ -937,6 +929,42 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment } } + $statusMsg = $this->submit($params); + CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); + $session = CRM_Core_Session::singleton(); + $buttonName = $this->controller->getButtonName(); + if ($this->_context == 'standalone') { + if ($buttonName == $this->getButtonName('upload', 'new')) { + $urlParams = 'reset=1&action=add&context=standalone'; + if ($this->_mode) { + $urlParams .= '&mode=' . $this->_mode; + } + if ($this->_eID) { + $urlParams .= '&eid=' . $this->_eID; + } + $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams)); + } + else { + $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', + "reset=1&cid={$this->_contactId}&selectedChild=participant" + )); + } + } + elseif ($buttonName == $this->getButtonName('upload', 'new')) { + $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', + "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}" + )); + } + } + + /** + * Submit form. + * + * @param array $params + * + * @return array + */ + public function submit($params) { $participantStatus = CRM_Event_PseudoConstant::participantStatus(); // set the contact, when contact is selected if (!empty($params['contact_id'])) { @@ -1720,6 +1748,34 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $this->_id = $participants[0]->id; } + return $this->getStatusMsg($params, $sent, $updateStatusMsg, $notSent); + } + + /** + * Set the various IDs relating to custom data types. + */ + public function setCustomDataTypes() { + $customDataType = CRM_Core_OptionGroup::values('custom_data_type', FALSE, FALSE, FALSE, NULL, 'name'); + $this->_roleCustomDataTypeID = array_search('ParticipantRole', $customDataType); + $this->_eventNameCustomDataTypeID = array_search('ParticipantEventName', $customDataType); + $this->_eventTypeCustomDataTypeID = array_search('ParticipantEventType', $customDataType); + $this->assign('roleCustomDataTypeID', $this->_roleCustomDataTypeID); + $this->assign('eventNameCustomDataTypeID', $this->_eventNameCustomDataTypeID); + $this->assign('eventTypeCustomDataTypeID', $this->_eventTypeCustomDataTypeID); + } + + /** + * Get status message + * + * @param array $params + * @param int $sent + * @param string $updateStatusMsg + * @param int $notSent + * + * @return string + */ + protected function getStatusMsg($params, $sent, $updateStatusMsg, $notSent) { + $statusMsg = ''; if (($this->_action & CRM_Core_Action::UPDATE)) { $statusMsg = ts('Event registration information for %1 has been updated.', array(1 => $this->_contributorDisplayName)); if (!empty($params['send_receipt']) && count($sent)) { @@ -1747,31 +1803,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment } } } - CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); - $session = CRM_Core_Session::singleton(); - $buttonName = $this->controller->getButtonName(); - if ($this->_context == 'standalone') { - if ($buttonName == $this->getButtonName('upload', 'new')) { - $urlParams = 'reset=1&action=add&context=standalone'; - if ($this->_mode) { - $urlParams .= '&mode=' . $this->_mode; - } - if ($this->_eID) { - $urlParams .= '&eid=' . $this->_eID; - } - $session->replaceUserContext(CRM_Utils_System::url('civicrm/participant/add', $urlParams)); - } - else { - $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view', - "reset=1&cid={$this->_contactId}&selectedChild=participant" - )); - } - } - elseif ($buttonName == $this->getButtonName('upload', 'new')) { - $session->replaceUserContext(CRM_Utils_System::url('civicrm/contact/view/participant', - "reset=1&action=add&context={$this->_context}&cid={$this->_contactId}" - )); - } + return $statusMsg; } } diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php new file mode 100644 index 0000000000..10dd2269ac --- /dev/null +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -0,0 +1,95 @@ +useTransaction(TRUE); + parent::setUp(); + } + + /** + * Initial test of submit function. + * + * @throws \Exception + */ + public function testSubmit() { + $event = $this->eventCreate(); + $contactID = $this->individualCreate(); + $form = $this->getFormObject('CRM_Event_Form_Participant'); + $form->_single = TRUE; + $form->_contactId = $contactID; + $form->setCustomDataTypes(); + $form->submit(array( + 'register_date' => 'now', + 'register_date_time' => '00:00:00', + 'status_id' => 1, + 'role_id' => 1, + 'event_id' => $event['id'], + )); + $participants = $this->callAPISuccess('Participant', 'get', array()); + $this->assertEquals(1, $participants['count']); + } + + /** + * Initial test of submit function. + * + * @throws \Exception + */ + public function testSubmitWithPayment() { + $event = $this->eventCreate(array('is_monetary' => 1, 'financial_type_id' => 1)); + $contactID = $this->individualCreate(); + $form = $this->getFormObject('CRM_Event_Form_Participant'); + $form->_single = TRUE; + $form->_contactId = $contactID; + $form->setCustomDataTypes(); + $form->_bltID = 5; + $form->_eventId = $event['id']; + $paymentProcessorID = $this->processorCreate(array('is_test' => 0)); + $form->_mode = 'Live'; + $form->_values['fee'] = array(); + $form->_isPaidEvent = TRUE; + $form->_quickConfig = TRUE; + $form->submit(array( + 'register_date' => 'now', + 'register_date_time' => '00:00:00', + 'status_id' => 1, + 'role_id' => 1, + 'event_id' => $event['id'], + 'credit_card_number' => 4444333322221111, + 'cvv2' => 123, + 'credit_card_exp_date' => array( + 'M' => 9, + 'Y' => 2025, + ), + 'credit_card_type' => 'Visa', + 'billing_first_name' => 'Junko', + 'billing_middle_name' => '', + 'billing_last_name' => 'Adams', + 'billing_street_address-5' => '790L Lincoln St S', + 'billing_city-5' => 'Maryknoll', + 'billing_state_province_id-5' => 1031, + 'billing_postal_code-5' => 10545, + 'billing_country_id-5' => 1228, + 'payment_processor_id' => $paymentProcessorID, + 'priceSetId' => '6', + 'price_7' => array( + 13 => 1, + ), + 'amount_level' => 'Too much', + 'fee_amount' => 55, + 'total_amount' => 55, + )); + $participants = $this->callAPISuccess('Participant', 'get', array()); + $this->assertEquals(1, $participants['count']); + $contribution = $this->callAPISuccessGetSingle('Contribution', array()); + $this->assertEquals(55, $contribution['total_amount']); + $this->assertEquals('Debit Card', $contribution['payment_instrument']); + } + +} -- 2.25.1