function testFormatInterestGroupsLookup()
 {
     $mailchimp_lists = CRM_CiviMailchimp_Utils::getLists();
     $interest_groups_lookup = CRM_CiviMailchimp_Utils::formatInterestGroupsLookup($mailchimp_lists);
     $this->assertCount(3, $interest_groups_lookup['MailchimpListsTestListA']);
     $this->assertEquals('Test Interest Group A', $interest_groups_lookup['MailchimpListsTestListA']['MailchimpTestInterestGroupingA_MailchimpTestInterestGroupA']);
     $this->assertEquals('Test Interest Group B', $interest_groups_lookup['MailchimpListsTestListA']['MailchimpTestInterestGroupingA_MailchimpTestInterestGroupB']);
     $this->assertEquals('Test Interest Group C', $interest_groups_lookup['MailchimpListsTestListA']['MailchimpTestInterestGroupingA_MailchimpTestInterestGroupC']);
     $this->assertArrayNotHasKey('MailchimpListsTestListB', $interest_groups_lookup);
     $this->assertArrayNotHasKey('MailchimpListsTestListC', $interest_groups_lookup);
 }
/**
 * Implementation of hook_civicrm_buildForm
 */
function civimailchimp_civicrm_buildForm($formName, &$form)
{
    // Render any Sync Log Messages.
    if (CRM_Core_Permission::check('administer CiviCRM')) {
        CRM_CiviMailchimp_BAO_SyncLog::renderMessages();
    }
    // Don't display the Mailchimp fields if this is a Smart Group.
    if ($formName === "CRM_Group_Form_Edit" && empty($form->_defaultValues['saved_search_id'])) {
        $interest_groups_lookup = array();
        try {
            $mailchimp_lists = CRM_CiviMailchimp_Utils::getLists();
        } catch (Exception $e) {
            $mailchimp_lists = NULL;
            civimailchimp_catch_mailchimp_api_error($e);
        }
        if ($mailchimp_lists) {
            $group_id = $form->getVar('_id');
            $list_options = CRM_CiviMailchimp_Utils::formatListsAsSelectOptions($mailchimp_lists);
            $interest_groups_lookup = CRM_CiviMailchimp_Utils::formatInterestGroupsLookup($mailchimp_lists);
            $interest_groups_options = '';
            if ($group_id) {
                $mailchimp_sync_settings = CRM_CiviMailchimp_BAO_SyncSettings::findByGroupId($group_id);
                if ($mailchimp_sync_settings) {
                    if (isset($interest_groups_lookup[$mailchimp_sync_settings->mailchimp_list_id])) {
                        $interest_groups_options = $interest_groups_lookup[$mailchimp_sync_settings->mailchimp_list_id];
                    }
                    civimailchimp_civicrm_setDefaults($form, $mailchimp_sync_settings);
                }
            }
            $form->add('select', 'mailchimp_list', ts('Mailchimp List'), $list_options, FALSE, array('class' => 'crm-select2'));
            $form->add('select', 'mailchimp_interest_groups', ts('Mailchimp Interest Groups'), $interest_groups_options, FALSE, array('multiple' => 'multiple', 'class' => 'crm-select2'));
            $form->assign('mailchimp_lists', TRUE);
        }
        CRM_Core_Resources::singleton()->addScriptFile('com.giantrabbit.civimailchimp', 'js/group_add_edit_form.js')->addSetting(array('civiMailchimp' => array('interest_groups_lookup' => $interest_groups_lookup)));
    }
}