Skip to content

Commit

Permalink
Ensure groupElementType is always set
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Nov 22, 2021
1 parent 96e52cb commit 8c00494
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 43 deletions.
13 changes: 13 additions & 0 deletions CRM/Contact/Form/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ public function getDefaultContext() {
return 'create';
}

/**
* Get any smarty elements that may not be present in the form.
*
* To make life simpler for smarty we ensure they are set to null
* rather than unset. This is done at the last minute when $this
* is converted to an array to be assigned to the form.
*
* @return array
*/
public function getOptionalSmartyElements(): array {
return ['group'];
}

/**
* Build all the data structures needed to build the form.
*/
Expand Down
13 changes: 7 additions & 6 deletions CRM/Contact/Form/Edit/TagsAndGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ public static function buildQuickForm(
if (!isset($form->_tagGroup)) {
$form->_tagGroup = [];
}

$form->addExpectedSmartyVariables('type');
$form->addOptionalQuickFormElement('group');
// NYSS 5670
if (!$contactId && !empty($form->_contactId)) {
$contactId = $form->_contactId;
Expand Down Expand Up @@ -99,12 +100,12 @@ public static function buildQuickForm(
$id = $group['id'];
// make sure that this group has public visibility
if ($visibility &&
$group['visibility'] == 'User and User Admin Only'
$group['visibility'] === 'User and User Admin Only'
) {
continue;
}

if ($groupElementType == 'select') {
if ($groupElementType === 'select') {
$groupsOptions[$key] = $group;
}
else {
Expand All @@ -113,23 +114,23 @@ public static function buildQuickForm(
}
}

if ($groupElementType == 'select' && !empty($groupsOptions)) {
if ($groupElementType === 'select' && !empty($groupsOptions)) {
$form->add('select2', $fName, $groupName, $groupsOptions, FALSE,
['placeholder' => ts('- select -'), 'multiple' => TRUE, 'class' => 'twenty']
);
$form->assign('groupCount', count($groupsOptions));
}

if ($groupElementType == 'checkbox' && !empty($elements)) {
if ($groupElementType === 'checkbox' && !empty($elements)) {
$form->addGroup($elements, $fName, $groupName, '&nbsp;<br />');
$form->assign('groupCount', count($elements));
if ($isRequired) {
$form->addRule($fName, ts('%1 is a required field.', [1 => $groupName]), 'required');
}
}
$form->assign('groupElementType', $groupElementType);
}
}
$form->assign('groupElementType', $groupElementType ?? NULL);

if ($type & self::TAG) {
$tags = CRM_Core_BAO_Tag::getColorTags('civicrm_contact');
Expand Down
44 changes: 38 additions & 6 deletions CRM/Core/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ public function setContext() {
'infoTitle',
'infoType',
'infoOptions',
// required for attachmentjs.tpl
'context',
];

/**
Expand Down Expand Up @@ -322,11 +324,6 @@ public function __construct(
if (!isset(self::$_template)) {
self::$_template = CRM_Core_Smarty::singleton();
}
// Smarty $_template is a static var which persists between tests, so
// if something calls clearTemplateVars(), the static still exists but
// our ensured variables get blown away, so we need to set them even if
// it's already been initialized.
self::$_template->ensureVariablesAreAssigned($this->expectedSmartyVariables);

// Workaround for CRM-15153 - give each form a reasonably unique css class
$this->addClass(CRM_Utils_System::getClassName($this));
Expand Down Expand Up @@ -705,6 +702,12 @@ public function buildForm() {
if ($this->submitOnce) {
$this->setAttribute('data-submit-once', 'true');
}
// Smarty $_template is a static var which persists between tests, so
// if something calls clearTemplateVars(), the static still exists but
// our ensured variables get blown away, so we need to set them even if
// it's already been initialized.
self::$_template->ensureVariablesAreAssigned($this->expectedSmartyVariables);

}

/**
Expand Down Expand Up @@ -1052,6 +1055,26 @@ public function setOptions($options) {
$this->_options = $options;
}

/**
* Quick form elements which are conditionally added to the form.
*
* Elements in this array will be added to the form at the end if not present
* so that smarty does not e-notice on things like '{if $form.group}' when
* 'group' is not added to the form (e.g when no groups exist).
*
* @var array
*/
protected $optionalQuickFormElements = [];

/**
* Add an optional element to the optional elements array.
*
* @param string $elementName
*/
public function addOptionalQuickFormElement(string $elementName): void {
$this->optionalQuickFormElements[] = $elementName;
}

/**
* Get any smarty elements that may not be present in the form.
*
Expand All @@ -1062,7 +1085,16 @@ public function setOptions($options) {
* @return array
*/
public function getOptionalQuickFormElements(): array {
return [];
return $this->optionalQuickFormElements;
}

/**
* Add an optional element to the expected smarty variables array.
*
* @param string $elementName
*/
public function addExpectedSmartyVariables(string $elementName): void {
$this->expectedSmartyVariables[] = $elementName;
}

/**
Expand Down
26 changes: 12 additions & 14 deletions templates/CRM/Contact/Form/Edit/TagsAndGroups.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
{/if}
<table class="form-layout-compressed{if $context EQ 'profile'} crm-profile-tagsandgroups{/if}">
<tr>
{if empty($type) || $type eq 'tag'}
{if $form.tag}
<td>
<div class="crm-section tag-section">
{if !empty($title)}{$form.tag.label}<br>{/if}
Expand All @@ -25,24 +25,22 @@
{/if}
</td>
{/if}
{if empty($type) || $type eq 'group'}
{if $form.group}
<td>
{if isset($groupElementType) && $groupElementType eq 'select'}
{if $groupElementType eq 'select'}
<div class="crm-section group-section">
{if !empty($title)}{$form.group.label}<br>{/if}
{if $title}{$form.group.label}<br>{/if}
{$form.group.html}
</div>
{else}
{if isset($form.group)}
{foreach key=key item=item from=$tagGroup.group}
<div class="group-wrapper">
{$form.group.$key.html}
{if $item.description}
<div class="description">{$item.description}</div>
{/if}
</div>
{/foreach}
{/if}
{foreach key=key item=item from=$tagGroup.group}
<div class="group-wrapper">
{$form.group.$key.html}
{if $item.description}
<div class="description">{$item.description}</div>
{/if}
</div>
{/foreach}
{/if}
</td>
{/if}
Expand Down
2 changes: 1 addition & 1 deletion templates/CRM/common/CMSPrint.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
</div>

{crmRegion name='page-footer'}
{if !empty($urlIsPublic)}
{if $urlIsPublic}
{include file="CRM/common/publicFooter.tpl"}
{else}
{include file="CRM/common/footer.tpl"}
Expand Down
32 changes: 16 additions & 16 deletions tests/phpunit/CRM/Contact/Form/IndividualTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@ class CRM_Contact_Form_IndividualTest extends CiviUnitTestCase {
* variable in the form so it has a typo, and then this test will throw an
* exception.
*/
public function testOpeningNewIndividualForm() {
$form = new CRM_Contact_Form_Contact();
$form->controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Contact', 'New Individual');

$form->set('reset', '1');
$form->set('ct', 'Individual');
public function testOpeningNewIndividualForm(): void {
$_REQUEST['reset'] = 1;
$_REQUEST['ct'] = 'Individual';
$form = $this->getFormObject('CRM_Contact_Form_Contact');
$form->assign('urlIsPublic');
$form->assign('linkButtons', []);

ob_start();
$form->controller->_actions['display']->perform($form, 'display');
$contents = ob_get_contents();
ob_end_clean();
$contents = ob_get_clean();

// There's a bunch of other stuff we could check for in $contents, but then
// it's tied to the html output and has the same maintenance problems
// as webtests. Mostly what we're doing in this test is checking that it
// as web tests. Mostly what we're doing in this test is checking that it
// doesn't generate any E_NOTICES/WARNINGS or other errors. But let's do
// one check.
$this->assertStringContainsString('<label for="first_name">', $contents);
Expand All @@ -40,32 +39,33 @@ public function testOpeningNewIndividualForm() {
* defined. It maybe doesn't need to be a separate test but might make it
* easier to track down problems if one fails but not the other.
*/
public function testOpeningNewIndividualFormWithCustomField() {
public function testOpeningNewIndividualFormWithCustomField(): void {
$custom_group = $this->customGroupCreate([]);
$custom_field1 = $this->customFieldCreate(['custom_group_id' => $custom_group['id']]);
$custom_field2 = $this->customFieldCreate([
$this->customFieldCreate(['custom_group_id' => $custom_group['id']]);
$this->customFieldCreate([
'custom_group_id' => $custom_group['id'],
'label' => 'f2',
'html_type' => 'Select',
// being lazy, just re-use activity type choices
'option_group_id' => 'activity_type',
]);
$custom_field3 = $this->customFieldCreate([
$this->customFieldCreate([
'custom_group_id' => $custom_group['id'],
'label' => 'f3',
'html_type' => 'Radio',
'option_group_id' => 'gender',
]);
$form = new CRM_Contact_Form_Contact();
$form = $this->getFormObject('CRM_Contact_Form_Contact');
$form->controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Contact', 'New Individual');
$form->assign('urlIsPublic');
$form->assign('linkButtons', []);

$form->set('reset', '1');
$form->set('ct', 'Individual');

ob_start();
$form->controller->_actions['display']->perform($form, 'display');
$contents = ob_get_contents();
ob_end_clean();
$contents = ob_get_clean();

$this->assertStringContainsString('<label for="first_name">', $contents);
}
Expand Down
4 changes: 4 additions & 0 deletions tests/phpunit/CiviTest/CiviUnitTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -3200,6 +3200,10 @@ public function getFormObject($class, $formValues = [], $pageName = '', $searchF
$form->controller = new CRM_Event_Cart_Controller_Checkout();
break;

case 'CRM_Contact_Form_Contact':
$form->controller = new CRM_Core_Controller_Simple('CRM_Contact_Form_Contact', $pageName);
break;

default:
$form->controller = new CRM_Core_Controller();
}
Expand Down

0 comments on commit 8c00494

Please sign in to comment.