Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable ability to require tags in profiles #23645

Merged
merged 1 commit into from
Jun 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Edit/TagsAndGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 58 additions & 0 deletions tests/phpunit/CRM/Profile/Form/EditTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking maybe adding $errors = $form->validate() here after buildQuickForm might do it but doesn't seem to. Hmm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see it. This form handles its own error handling slightly differently than other forms (because it's Civi!) It expects there to be an errorUrl param and then does a redirect. The return value from validate() is just a true/false here, so it actually does work to call $form->validate(), I was just expecting an array of error messages. So you can do $this->assertFalse($form->validate()); after the buildQuickForm(), and don't need the postProcess since then you're done with the test.

$this->assertFalse($form->validate(), 'Ensure tags can be required on a form.');
}

}