From f84f92f2faa6ac94483dc9a5e068f69f8127e794 Mon Sep 17 00:00:00 2001 From: eileen Date: Sat, 18 Jan 2020 11:15:21 +1300 Subject: [PATCH] Start to use function rather than multiple queries for event details, add test --- CRM/Event/Form/Participant.php | 19 ++++++++++- .../CRM/Event/Form/ParticipantTest.php | 32 +++++++++++++++++-- 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/CRM/Event/Form/Participant.php b/CRM/Event/Form/Participant.php index c48703d543..26f3127571 100644 --- a/CRM/Event/Form/Participant.php +++ b/CRM/Event/Form/Participant.php @@ -1838,7 +1838,9 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment * @param $params * * @return array + * * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception */ protected function preparePaidEventProcessing($params): array { $participantStatus = CRM_Event_PseudoConstant::participantStatus(); @@ -1846,7 +1848,7 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment $lineItem = []; $additionalParticipantDetails = []; if (Civi::settings()->get('deferred_revenue_enabled')) { - $eventStartDate = CRM_Core_DAO::getFieldValue('CRM_Event_DAO_Event', $this->_eventId, 'start_date'); + $eventStartDate = $this->getEventValue('start_date'); if (strtotime($eventStartDate) > strtotime(date('Ymt'))) { $contributionParams['revenue_recognition_date'] = date('Ymd', strtotime($eventStartDate)); } @@ -2000,4 +2002,19 @@ class CRM_Event_Form_Participant extends CRM_Contribute_Form_AbstractEditPayment return ($this->_id && $this->_action & CRM_Core_Action::UPDATE) && $this->_paymentId; } + /** + * Get the value for a field relating to the event. + * + * @param string $fieldName + * + * @return mixed + * @throws \CiviCRM_API3_Exception + */ + protected function getEventValue(string $fieldName) { + if (!isset($this->_event)) { + $this->_event = civicrm_api3('Event', 'getsingle', ['id' => $this->_eventId]); + } + return $this->_event[$fieldName]; + } + } diff --git a/tests/phpunit/CRM/Event/Form/ParticipantTest.php b/tests/phpunit/CRM/Event/Form/ParticipantTest.php index 9ad163a126..69c720d52b 100644 --- a/tests/phpunit/CRM/Event/Form/ParticipantTest.php +++ b/tests/phpunit/CRM/Event/Form/ParticipantTest.php @@ -186,9 +186,6 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1]); $form->_mode = 'Live'; $form->_quickConfig = TRUE; - $form->_fromEmails = [ - 'from_email_id' => ['abc@gmail.com' => 1], - ]; $paymentProcessorID = $this->processorCreate(['is_test' => 0]); $form->submit($this->getSubmitParams($form->_eventId, $paymentProcessorID)); $participants = $this->callAPISuccess('Participant', 'get', []); @@ -315,6 +312,9 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { $form->_single = TRUE; $form->_contactID = $form->_contactId = $contactID; $form->setCustomDataTypes(); + $form->_fromEmails = [ + 'from_email_id' => ['abc@gmail.com' => 1], + ]; $form->_eventId = $event['id']; if (!empty($eventParams['is_monetary'])) { $form->_bltID = 5; @@ -466,4 +466,30 @@ class CRM_Event_Form_ParticipantTest extends CiviUnitTestCase { return $submitParams; } + /** + * + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + */ + public function testSubmitWithDeferredRecognition() { + Civi::settings()->set('deferred_revenue_enabled', TRUE); + $futureDate = date('Y') + 1 . '-09-20'; + $form = $this->getForm(['is_monetary' => 1, 'financial_type_id' => 1, 'start_date' => $futureDate]); + $form->_quickConfig = TRUE; + + $form->submit([ + 'register_date' => date('Ymd'), + 'status_id' => 1, + 'role_id' => 1, + 'event_id' => $form->_eventId, + 'record_contribution' => TRUE, + 'amount' => 100, + 'amount_level' => 'blah', + 'financial_type_id' => 1, + ]); + $contribution = $this->callAPISuccessGetSingle('Contribution', []); + // Api doesn't retrieve it & we don't much want to change that as we want to feature freeze BAO_Query. + $this->assertEquals($futureDate . ' 00:00:00', CRM_Core_DAO::singleValueQuery("SELECT revenue_recognition_date FROM civicrm_contribution WHERE id = {$contribution['id']}")); + } + } -- 2.25.1