function civimailchimp_civicrm_contact_added_to_group($group, $contact)
{
    $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettings::findByGroupId($group->id);
    if ($mailchimp_sync_setting) {
        $mailchimp_email = CRM_CiviMailchimp_Utils::determineMailchimpEmailForContact($contact);
        if ($mailchimp_email !== NULL) {
            $merge_fields = CRM_CiviMailchimp_Utils::getMailchimpMergeFields($mailchimp_sync_setting->mailchimp_list_id);
            $merge_vars = CRM_CiviMailchimp_Utils::formatMailchimpMergeVars($merge_fields, $contact);
            CRM_CiviMailchimp_Utils::addMailchimpSyncQueueItem('subscribeContactToMailchimpList', $mailchimp_sync_setting->mailchimp_list_id, $mailchimp_email, $merge_vars);
        }
    }
}
 static function forceCiviToMailchimpSync($mailchimp_sync_setting)
 {
     $contacts = CRM_CiviMailchimp_Utils::getActiveGroupMembers($mailchimp_sync_setting->civicrm_group_id);
     $skipped_contacts = 0;
     foreach ($contacts as $key => $contact_id) {
         $contact = CRM_CiviMailchimp_Utils::getContactById($contact_id);
         if ($contact->is_deleted != 1) {
             $email = CRM_CiviMailchimp_Utils::determineMailchimpEmailForContact($contact);
             if ($email === NULL) {
                 ++$skipped_contacts;
                 unset($contacts[$key]);
             } else {
                 $merge_fields = CRM_CiviMailchimp_Utils::getMailchimpMergeFields($mailchimp_sync_setting->mailchimp_list_id);
                 $merge_vars = CRM_CiviMailchimp_Utils::formatMailchimpMergeVars($merge_fields, $contact);
                 CRM_CiviMailchimp_Utils::subscribeContactToMailchimpList($mailchimp_sync_setting->mailchimp_list_id, $email, $merge_vars);
             }
         }
     }
     if ($skipped_contacts > 0) {
         $message = ts('%1 records were not synced to Mailchimp because they did not have a valid email address.', array(1 => $skipped_contacts));
         CRM_CiviMailchimp_BAO_SyncLog::saveMessage('error', 'civicrm_to_mailchimp', $message);
     }
     return $contacts;
 }
 function test_civicrm_api3_civi_mailchimp_sync()
 {
     $action = 'subscribeContactToMailchimpList';
     $mailchimp_list_id = 'MailchimpListsTestListA';
     $mailchimp_interest_groups = array('MailchimpTestInterestGroupingA_MailchimpTestInterestGroupA', 'MailchimpTestInterestGroupingA_MailchimpTestInterestGroupC');
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test Group test_civicrm_api3_civi_mailchimp_sync', $mailchimp_list_id, $mailchimp_interest_groups);
     $merge_fields = CRM_CiviMailchimp_Utils::getMailchimpMergeFields();
     $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams();
     $contact = CRM_Contact_BAO_Contact::create($params);
     $merge_vars = CRM_CiviMailchimp_Utils::formatMailchimpMergeVars($merge_fields, $contact);
     CRM_CiviMailchimp_Utils::addMailchimpSyncQueueItem($action, $mailchimp_list_id, $params['email'][0]['email'], $merge_vars);
     $action = 'unsubscribeContactFromMailchimpList';
     CRM_CiviMailchimp_Utils::addMailchimpSyncQueueItem($action, $mailchimp_list_id, $params['email'][0]['email'], $merge_vars);
     $job_params['records_to_process_per_run'] = 100;
     civicrm_api3_civi_mailchimp_sync($job_params);
     $queue = CRM_Queue_Service::singleton()->create(array('type' => 'Sql', 'name' => 'mailchimp-sync', 'reset' => FALSE));
     $this->assertEquals(0, $queue->numberOfItems());
 }