function testContactAddedToGroup()
 {
     $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams();
     $contact = CRM_Contact_BAO_Contact::create($params);
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test group testContactAddedToGroup');
     // Test that the contact is not in the group (contactAddedToGroup returns TRUE)
     $contact_added_to_group = CRM_CiviMailchimp_Utils::contactAddedToGroup($mailchimp_sync_setting->civicrm_group_id, $contact->id);
     $this->assertTrue($contact_added_to_group);
     // Test that the contact is in the group (contactAddedToGroup returns FALSE)
     $contact_ids = array($contact->id);
     CRM_Contact_BAO_GroupContact::addContactsToGroup($contact_ids, $mailchimp_sync_setting->civicrm_group_id);
     $contact_added_to_group = CRM_CiviMailchimp_Utils::contactAddedToGroup($mailchimp_sync_setting->civicrm_group_id, $contact->id);
     $this->assertFalse($contact_added_to_group);
     // Test that a status other than 'Added' returns TRUE
     CRM_Contact_BAO_GroupContact::removeContactsFromGroup($contact_ids, $mailchimp_sync_setting->civicrm_group_id);
     $contact_added_to_group = CRM_CiviMailchimp_Utils::contactAddedToGroup($mailchimp_sync_setting->civicrm_group_id, $contact->id);
     $this->assertTrue($contact_added_to_group);
 }
/**
 * Implements hook_civicrm_pre for GroupContact create.
 */
function civimailchimp_civicrm_pre_GroupContact_create($group_id, &$contact_ids)
{
    // The create operation for GroupContact is thrown for every existing and
    // new group when a contact is saved, so we have to do some extra work
    // to determine whether the contact has just been added to the group or
    // not. We're storing the actually added Groups in a static variable for
    // use in the civicrm_post hook.
    foreach ($contact_ids as $contact_id) {
        $contacts_added_to_group = array();
        $contact_added_to_group = CRM_CiviMailchimp_Utils::contactAddedToGroup($group_id, $contact_id);
        if ($contact_added_to_group) {
            $contacts_added_to_group[] = $contact_id;
        }
        civimailchimp_static('contacts_added_to_group', $contacts_added_to_group);
    }
}