/** * Tests the copying of names from Mailchimp to temp table. * * 1. Check FNAME and LNAME are copied to first_name and last_name. * 2. Repeat check 1 but with presence of populated NAME field also. * 3. Check first and last _name fields are populated from NAME field * if NAME not empty and FNAME and LNAME are empty. * * @depends testGetMCInterestGroupings */ public function testCollectMailchimpParsesNames() { // Prepare the mock for the subscription the post hook will do. $api_prophecy = $this->prophesize('CRM_Mailchimp_Api3'); CRM_Mailchimp_Utils::setMailchimpApi($api_prophecy->reveal()); $api_prophecy->get("/lists/dummylistid/members", Argument::any())->shouldBeCalled()->willReturn(json_decode(json_encode(['http_code' => 200, 'data' => ['total_items' => 5, 'members' => [['email_address' => '*****@*****.**', 'interests' => [], 'merge_fields' => ['FNAME' => 'Foo', 'LNAME' => 'Bar']], ['email_address' => '*****@*****.**', 'interests' => [], 'merge_fields' => ['FNAME' => 'Foo', 'LNAME' => 'Bar', 'NAME' => 'Some other name']], ['email_address' => '*****@*****.**', 'interests' => [], 'merge_fields' => ['FNAME' => '', 'LNAME' => '', 'NAME' => 'Foo Bar']], ['email_address' => '*****@*****.**', 'interests' => [], 'merge_fields' => ['NAME' => 'Foo Bar']]]]]))); $sync = new CRM_Mailchimp_Sync(static::$test_list_id); $sync->collectMailchimp('pull'); // Test expected results. $dao = CRM_Core_DAO::executeQuery("SELECT * FROM tmp_mailchimp_push_m;"); while ($dao->fetch()) { $this->assertEquals(['Foo', 'Bar'], [$dao->first_name, $dao->last_name], "Error on {$dao->email}"); } $dao->free(); }
/** * Collect Mailchimp data into temporary working table. */ public static function syncPullCollectMailchimp(CRM_Queue_TaskContext $ctx, $listID) { // Nb. collectCiviCrm must have run before we call this. $sync = new CRM_Mailchimp_Sync($listID); $stats[$listID]['mc_count'] = $sync->collectMailchimp('pull'); CRM_Mailchimp_Utils::checkDebug('CRM_Mailchimp_Form_Pull syncPullCollectMailchimp count=', $stats[$listID]['mc_count']); static::updatePullStats($stats); return CRM_Queue_Task::TASK_SUCCESS; }
/** * Collect Mailchimp data into temporary working table. */ public static function syncPushCollectMailchimp(CRM_Queue_TaskContext $ctx, $listID) { CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Form_Sync syncPushCollectMailchimp $listID= ', $listID); // Nb. collectCiviCrm must have run before we call this. $sync = new CRM_Mailchimp_Sync($listID); $stats[$listID]['mc_count'] = $sync->collectMailchimp('push', $civi_collect_has_already_run = TRUE); CRM_Mailchimp_Utils::checkDebug('Start-CRM_Mailchimp_Form_Sync syncPushCollectMailchimp $stats[$listID][mc_count]', $stats[$listID]['mc_count']); static::updatePushStats($stats); return CRM_Queue_Task::TASK_SUCCESS; }
/** * Check interests are properly mapped as groups are changed and that * collectMailchimp and collectCiviCrm work as expected. * * * This uses the posthook, which in turn uses * updateMailchimpFromCiviSingleContact. * * If all is working then at that point both collections should match. * */ public function testSyncInterestGroupings() { $api = CRM_Mailchimp_Utils::getMailchimpApi(); try { // Add them to the interest group (this should not trigger a Mailchimp // update as they are not in thet membership list yet). $this->joinGroup(static::$civicrm_contact_1, static::$civicrm_group_id_interest_1); // The post hook should subscribe this person and set their interests. $this->joinMembershipGroup(static::$civicrm_contact_1); // Check their interest group was set. $result = $api->get("/lists/" . static::$test_list_id . "/members/" . static::$civicrm_contact_1['subscriber_hash'], ['fields' => 'status,interests'])->data; $this->assertEquals((object) [static::$test_interest_id_1 => TRUE, static::$test_interest_id_2 => FALSE], $result->interests); // Remove them to the interest group. $this->removeGroup(static::$civicrm_contact_1, static::$civicrm_group_id_interest_1); // Check their interest group was unset. $result = $api->get("/lists/" . static::$test_list_id . "/members/" . static::$civicrm_contact_1['subscriber_hash'], ['fields' => 'status,interests'])->data; $this->assertEquals((object) [static::$test_interest_id_1 => FALSE, static::$test_interest_id_2 => FALSE], $result->interests); // Add them to the 2nd interest group. // While this is a dull test, we assume it works if the other interest // group one did, it leaves the fixture with one on and one off which is a // good mix for the next test. $this->joinGroup(static::$civicrm_contact_1, static::$civicrm_group_id_interest_2); // Check their interest group was set. $result = $api->get("/lists/" . static::$test_list_id . "/members/" . static::$civicrm_contact_1['subscriber_hash'], ['fields' => 'status,interests'])->data; $this->assertEquals((object) [static::$test_interest_id_1 => FALSE, static::$test_interest_id_2 => TRUE], $result->interests); // Now check collections work. $sync = new CRM_Mailchimp_Sync(static::$test_list_id); $sync->collectCiviCrm('push'); $this->assertEquals(1, $sync->countCiviCrmMembers()); $sync->collectMailchimp('push'); $this->assertEquals(1, $sync->countMailchimpMembers()); $matches = $sync->matchMailchimpMembersToContacts(); $this->assertEquals(['bySubscribers' => 1, 'byUniqueEmail' => 0, 'byNameEmail' => 0, 'bySingle' => 0, 'totalMatched' => 1, 'newContacts' => 0, 'failures' => 0], $matches); // This should return 1 $dao = CRM_Core_DAO::executeQuery("SELECT * FROM tmp_mailchimp_push_m"); $dao->fetch(); $mc = ['email' => $dao->email, 'first_name' => $dao->first_name, 'last_name' => $dao->last_name, 'interests' => $dao->interests, 'hash' => $dao->hash, 'cid_guess' => $dao->cid_guess]; $dao = CRM_Core_DAO::executeQuery("SELECT * FROM tmp_mailchimp_push_c"); $dao->fetch(); $civi = ['email' => $dao->email, 'email_id' => $dao->email_id, 'contact_id' => $dao->contact_id, 'first_name' => $dao->first_name, 'last_name' => $dao->last_name, 'interests' => $dao->interests, 'hash' => $dao->hash]; $this->assertEquals($civi['first_name'], $mc['first_name']); $this->assertEquals($civi['last_name'], $mc['last_name']); $this->assertEquals($civi['email'], $mc['email']); $this->assertEquals($civi['interests'], $mc['interests']); $this->assertEquals($civi['hash'], $mc['hash']); // As the records are in sync, they should be and deleted. $in_sync = $sync->removeInSync('push'); $this->assertEquals(1, $in_sync); // Now check the tables are both empty. $this->assertEquals(0, $sync->countMailchimpMembers()); $this->assertEquals(0, $sync->countCiviCrmMembers()); } catch (CRM_Mailchimp_Exception $e) { // Spit out request and response for debugging. print "Request:\n"; print_r($e->request); print "Response:\n"; print_r($e->response); // re-throw exception. throw $e; } }