function testFindByContactIdNoSyncSettings()
 {
     $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams();
     $contact = CRM_Contact_BAO_Contact::create($params);
     $mailchimp_sync_settings = CRM_CiviMailchimp_BAO_SyncSettings::findByContactId($contact->id);
     $this->assertEmpty($mailchimp_sync_settings);
 }
 function testForceCiviToMailchimpSyncNullEmail()
 {
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test Group testForceCiviToMailchimpSyncNullEmail');
     $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams();
     unset($params['email']);
     $contact = CRM_Contact_BAO_Contact::create($params);
     $contact_ids = array($contact->id);
     CRM_Contact_BAO_GroupContact::addContactsToGroup($contact_ids, $mailchimp_sync_setting->civicrm_group_id);
     $contacts = CRM_CiviMailchimp_Form_Sync::forceCiviToMailchimpSync($mailchimp_sync_setting);
     $mailchimp_api_subscribe = civimailchimp_static('mailchimp_api_subscribe');
     $this->assertCount(0, $contacts);
     $this->assertNull($mailchimp_api_subscribe);
 }
 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());
 }
 function testClearQueueItem()
 {
     $mailchimp_list_id = 'MailchimpListsTestListA';
     $mailchimp_sync_setting = CRM_CiviMailchimp_BAO_SyncSettingsTest::createTestGroupAndSyncSettings('Test Group testClearQueueItem', $mailchimp_list_id);
     $group = CRM_CiviMailchimp_Utils::getGroupById($mailchimp_sync_setting->civicrm_group_id);
     $params = CRM_CiviMailchimp_UtilsTest::sampleContactParams();
     $contact = CRM_Contact_BAO_Contact::create($params);
     $queue = CRM_Queue_Service::singleton()->create(array('type' => 'Sql', 'name' => 'mailchimp-sync', 'reset' => TRUE));
     civimailchimp_civicrm_contact_removed_from_group($group, $contact);
     $item = $queue->claimItem($lease_time = 0);
     $existing_mailchimp_sync_log = self::createTestLogMessage('This is a test error message', $details = NULL, $direction = 'civicrm_to_mailchimp', $type = 'error', $item->id);
     CRM_CiviMailchimp_BAO_SyncLog::clearQueueItem($item->id);
     $query = "\n      SELECT\n        *\n      FROM\n        civicrm_queue_item\n      WHERE\n        queue_name = 'mailchimp-sync'\n      AND\n        id = %1\n      LIMIT 1\n      ";
     $params = array(1 => array($item->id, 'Integer'));
     $new_item = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Queue_DAO_QueueItem');
     $mailchimp_sync_log = CRM_CiviMailchimp_BAO_SyncLog::findById($existing_mailchimp_sync_log->id);
     $this->assertFalse($new_item->fetch());
     $this->assertNull($mailchimp_sync_log->civicrm_queue_item_id);
 }