function civimailchimp_civicrm_contact_updated($old_contact, $new_contact) { $mailchimp_sync_settings = CRM_CiviMailchimp_BAO_SyncSettings::findByContactId($new_contact->id); if ($mailchimp_sync_settings) { $old_contact_email = CRM_CiviMailchimp_Utils::determineMailchimpEmailForContact($old_contact); $new_contact_email = CRM_CiviMailchimp_Utils::determineMailchimpEmailForContact($new_contact); if ($old_contact_email === NULL && $new_contact_email !== NULL) { // Add contact to mailchimp list. CRM_CiviMailchimp_Utils::addUpdatedContactToMailchimpList($new_contact, $new_contact_email, $mailchimp_sync_settings); } elseif ($old_contact_email !== NULL && $new_contact_email === NULL) { // Remove contact from mailchimp list. CRM_CiviMailchimp_Utils::removeUpdatedContactFromMailchimpList($old_contact_email, $mailchimp_sync_settings); } elseif ($old_contact_email !== NULL && $new_contact_email !== NULL) { // Update contact record in mailcimp. CRM_CiviMailchimp_Utils::updateContactInMailchimpList($old_contact_email, $new_contact_email, $old_contact, $new_contact, $mailchimp_sync_settings); } } }
function testAddUpdatedContactToMailchimpList() { $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams(); $contact = CRM_Contact_BAO_Contact::create($params); $mailchimp_email = CRM_CiviMailchimp_Utils::determineMailchimpEmailForContact($contact); $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test group testAddUpdatedContactToMailchimpList'); $mailchimp_sync_settings = array($mailchimp_sync_setting); CRM_CiviMailchimp_Utils::addUpdatedContactToMailchimpList($contact, $mailchimp_email, $mailchimp_sync_settings); $queue = CRM_Queue_Service::singleton()->create(array('type' => 'Sql', 'name' => 'mailchimp-sync', 'reset' => FALSE)); $item = $queue->claimItem(); $this->assertEquals('CRM_CiviMailchimp_Utils', $item->data->callback[0]); $this->assertEquals('processCiviMailchimpQueueItem', $item->data->callback[1]); $this->assertEquals('subscribeContactToMailchimpList', $item->data->arguments[0]); $this->assertEquals($mailchimp_sync_setting->mailchimp_list_id, $item->data->arguments[1]); $this->assertEquals($mailchimp_email, $item->data->arguments[2]); }