From e661b0f61e1f3fefe0598f9c8b2dca212485db62 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 31 May 2022 15:46:55 -0400 Subject: [PATCH] Enable ability to require tags in profiles Prior to this change, it was not possible to make tags required in a profile. --- CRM/Contact/Form/Edit/TagsAndGroups.php | 2 +- tests/phpunit/CRM/Profile/Form/EditTest.php | 58 +++++++++++++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/CRM/Contact/Form/Edit/TagsAndGroups.php b/CRM/Contact/Form/Edit/TagsAndGroups.php index e9ffa6f6677b..f06a3e55cae7 100644 --- a/CRM/Contact/Form/Edit/TagsAndGroups.php +++ b/CRM/Contact/Form/Edit/TagsAndGroups.php @@ -136,7 +136,7 @@ public static function buildQuickForm( $tags = CRM_Core_BAO_Tag::getColorTags('civicrm_contact'); if (!empty($tags)) { - $form->add('select2', 'tag', ts('Tag(s)'), $tags, FALSE, ['class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE]); + $form->add('select2', 'tag', ts('Tag(s)'), $tags, $isRequired, ['class' => 'huge', 'placeholder' => ts('- select -'), 'multiple' => TRUE]); } // build tag widget diff --git a/tests/phpunit/CRM/Profile/Form/EditTest.php b/tests/phpunit/CRM/Profile/Form/EditTest.php index 753ce74f27d1..863416730c60 100644 --- a/tests/phpunit/CRM/Profile/Form/EditTest.php +++ b/tests/phpunit/CRM/Profile/Form/EditTest.php @@ -46,4 +46,62 @@ public function testProfileUrl(): void { $this->assertEquals('civicrm/Mr. Anthony Anderson II', CRM_Core_Session::singleton()->popUserContext()); } + /** + * Test that requiring tags on a profile works. + * + * @throws \API_Exception + */ + public function testProfileRequireTag(): void { + $ufGroupParams = [ + 'group_type' => 'Individual,Contact', + 'name' => 'test_individual_contact_tag_profile', + 'title' => 'Gimme a tag', + 'api.uf_field.create' => [ + [ + 'field_name' => 'first_name', + 'is_required' => 1, + 'visibility' => 'Public Pages and Listings', + 'field_type' => 'Individual', + 'label' => 'First Name', + ], + [ + 'field_name' => 'last_name', + 'is_required' => 1, + 'visibility' => 'Public Pages and Listings', + 'field_type' => 'Individual', + 'label' => 'Last Name', + ], + [ + 'field_name' => 'tag', + 'is_required' => 1, + 'visibility' => 'Public Pages and Listings', + 'field_type' => 'Contact', + 'label' => 'Tag', + ], + ], + ]; + + $profile = $this->callAPISuccess('uf_group', 'create', $ufGroupParams); + $profileID = $profile['id']; + + // Configure the profile to be used as a standalone profile for data entry. + UFJoin::create(FALSE)->setValues([ + 'module' => 'Profile', + 'uf_group_id' => $profileID, + ])->execute(); + + // Populate the form. + $formParams = [ + 'first_name' => 'Foo', + 'last_name' => 'McGoo', + 'gid' => $profileID, + 'tag' => [], + ]; + $form = $this->getFormObject('CRM_Profile_Form_Edit', $formParams); + $form->set('gid', $profileID); + $form->preProcess(); + $form->buildQuickForm(); + $this->assertFalse($form->validate(), 'Ensure tags can be required on a form.'); + } + }