diff --git a/CRM/Core/BAO/UFGroup.php b/CRM/Core/BAO/UFGroup.php index 2aabfc253b8c..c7d985b299bf 100644 --- a/CRM/Core/BAO/UFGroup.php +++ b/CRM/Core/BAO/UFGroup.php @@ -283,7 +283,8 @@ public static function getListingFields( * * @return array * The fields that belong to this ufgroup(s) - * @throws \Exception + * + * @throws \CRM_Core_Exception */ public static function getFields( $id, @@ -366,7 +367,7 @@ public static function getFields( } if (empty($fields) && !$validGroup) { - CRM_Core_Error::fatal(ts('The requested Profile (gid=%1) is disabled OR it is not configured to be used for \'Profile\' listings in its Settings OR there is no Profile with that ID OR you do not have permission to access this profile. Please contact the site administrator if you need assistance.', + throw new CRM_Core_Exception(ts('The requested Profile (gid=%1) is disabled OR it is not configured to be used for \'Profile\' listings in its Settings OR there is no Profile with that ID OR you do not have permission to access this profile. Please contact the site administrator if you need assistance.', [1 => implode(',', $profileIds)] )); } diff --git a/CRM/Event/BAO/Event.php b/CRM/Event/BAO/Event.php index cf52a5f32cc0..cc133d64cbe0 100644 --- a/CRM/Event/BAO/Event.php +++ b/CRM/Event/BAO/Event.php @@ -1431,6 +1431,10 @@ public static function buildCustomDisplay( * Formatted array of key value. * * @param array $profileFields + * + * @throws \CRM_Core_Exception + * @throws \API_Exception + * @throws \CiviCRM_API3_Exception */ public static function displayProfile(&$params, $gid, &$groupTitle, &$values, &$profileFields = []) { if ($gid) { @@ -1446,11 +1450,9 @@ public static function displayProfile(&$params, $gid, &$groupTitle, &$values, &$ $fields = CRM_Core_BAO_UFGroup::getFields($gid, FALSE, CRM_Core_Action::ADD); } - foreach ($fields as $v) { - if (!empty($v['groupTitle'])) { - $groupTitle['groupTitle'] = $v['groupTitle']; - break; - } + if (!empty($fields)) { + $profile = civicrm_api3('UFGroup', 'getsingle', ['id' => $gid, ['return' => ['title', 'frontend_title']]]); + $groupTitle['groupTitle'] = $profile['frontend_title'] ?? $profile['title']; } $imProviders = CRM_Core_PseudoConstant::get('CRM_Core_DAO_IM', 'provider_id'); diff --git a/CRM/Event/Form/Registration/Confirm.php b/CRM/Event/Form/Registration/Confirm.php index 1aa29c43e0ee..086e7e901c9a 100644 --- a/CRM/Event/Form/Registration/Confirm.php +++ b/CRM/Event/Form/Registration/Confirm.php @@ -1217,11 +1217,13 @@ public static function updateContactFields($contactID, $params, $fields, &$form) } /** - * Assign Profiles. + * Assign Profiles to the template. * - * @param CRM_Core_Form $form + * @param CRM_Event_Form_Registration_Confirm $form + * + * @throws \Exception */ - public static function assignProfiles(&$form) { + public static function assignProfiles($form) { $participantParams = $form->_params; $formattedValues = $profileFields = []; $count = 1; @@ -1234,7 +1236,7 @@ public static function assignProfiles(&$form) { $prefix1 = ''; $prefix2 = ''; } - if ($participantValue != 'skip') { + if ($participantValue !== 'skip') { //get the customPre profile info if (!empty($form->_values[$prefix2 . 'custom_pre_id'])) { $values = $groupName = []; diff --git a/api/api.php b/api/api.php index 9b1a00941d1d..5d67281e4581 100644 --- a/api/api.php +++ b/api/api.php @@ -36,6 +36,7 @@ function civicrm_api($entity, $action, $params, $extra = NULL) { * Array to be passed to function. * * @throws CiviCRM_API3_Exception + * * @return array */ function civicrm_api3($entity, $action, $params = []) { diff --git a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php index 4fa5ca7e8f72..782874b2bf0b 100644 --- a/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php +++ b/tests/phpunit/CRM/Event/Form/Registration/ConfirmTest.php @@ -8,6 +8,8 @@ */ class CRM_Event_Form_Registration_ConfirmTest extends CiviUnitTestCase { + use CRMTraits_Profile_ProfileTrait; + public function setUp() { $this->useTransaction(TRUE); parent::setUp(); @@ -351,4 +353,35 @@ public function testOnlineRegNoPrice() { $this->assertEquals($contribution['count'], '0', "Contribution should not be created for zero fee event registration when no price field selected."); } + /** + * Test form profile assignment. + * + * @throws \CRM_Core_Exception + * @throws \Exception + */ + public function testAssignProfiles() { + $event = $this->eventCreate(); + $this->createJoinedProfile(['entity_table' => 'civicrm_event', 'entity_id' => $event['id']]); + + /* @var \CRM_Event_Form_Registration_Confirm $form */ + $form = $this->getFormObject('CRM_Event_Form_Registration_Confirm'); + $form->set('params', [[]]); + $form->set('id', $event['id']); + $form->set('values', [ + 'event' => $event['values'][$event['id']], + 'location' => [], + 'custom_pre_id' => $this->ids['UFGroup']['our profile'], + ]); + $form->preProcess(); + + CRM_Event_Form_Registration_Confirm::assignProfiles($form); + + $smarty = CRM_Core_Smarty::singleton(); + $tplVar = $smarty->get_template_vars(); + $this->assertEquals([ + 'CustomPre' => ['First Name' => NULL], + 'CustomPreGroupTitle' => 'Public title', + ], $tplVar['primaryParticipantProfile']); + } + } diff --git a/tests/phpunit/CRMTraits/Profile/ProfileTrait.php b/tests/phpunit/CRMTraits/Profile/ProfileTrait.php new file mode 100644 index 000000000000..b41dbf4a7eb0 --- /dev/null +++ b/tests/phpunit/CRMTraits/Profile/ProfileTrait.php @@ -0,0 +1,79 @@ +createProfile($ufGroupParams); + $joinParams = array_merge([ + 'uf_group_id' => 'our profile', + 'entity_table' => 'civicrm_contribution_page', + 'weight' => 1, + ], $joinParams); + if (empty($joinParams['module'])) { + $joinParams['module'] = $joinParams['entity_table'] === 'civicrm_event' ? 'CiviEvent' : 'CiviContribute'; + } + if ($joinParams['module'] !== 'CiviContribute' && empty($joinParams['module_data'])) { + $params['module_data'] = [$joinParams['module'] => []]; + } + $this->callAPISuccess('UFJoin', 'create', $joinParams); + } + + /** + * Create a profile. + * + * @param $ufGroupParams + */ + protected function createProfile($ufGroupParams) { + $profile = $this->callAPISuccess('UFGroup', 'create', array_merge([ + 'group_type' => 'Contact', + 'title' => 'Back end title', + 'frontend_title' => 'Public title', + 'name' => 'our profile', + + ], $ufGroupParams)); + $this->ids['UFGroup'][$profile['values'][$profile['id']]['name']] = $profile['id']; + + $this->callAPISuccess('UFField', 'create', [ + 'uf_group_id' => $profile['id'], + 'field_name' => 'first_name', + ]); + } + +}