/** * Starting with an empty MC list and one person on the CiviCRM mailchimp * group, a push should subscribe the person. * * @group push */ public function testPushAddsNewPerson() { $api = CRM_Mailchimp_Utils::getMailchimpApi(); try { // Add contact to membership group without telling MC. $this->joinMembershipGroup(static::$civicrm_contact_1, TRUE); // Check they are definitely in the group. $this->assertContactIsInGroup(static::$civicrm_contact_1['contact_id'], static::$civicrm_group_id_membership); // Double-check this member is not known at Mailchimp. $this->assertContactNotListMember(static::$civicrm_contact_1); $sync = new CRM_Mailchimp_Sync(static::$test_list_id); // Now trigger a push for this test list. // Collect data from CiviCRM. // There should be one member. $sync->collectCiviCrm('push'); $this->assertEquals(1, $sync->countCiviCrmMembers()); // Collect data from Mailchimp. // There shouldn't be any members in this list yet. $sync->collectMailchimp('push'); $this->assertEquals(0, $sync->countMailchimpMembers()); $matches = $sync->matchMailchimpMembersToContacts(); $this->assertEquals(['bySubscribers' => 0, 'byUniqueEmail' => 0, 'byNameEmail' => 0, 'bySingle' => 0, 'totalMatched' => 0, 'newContacts' => 0, 'failures' => 0], $matches); // There should not be any in sync records. $in_sync = $sync->removeInSync('push'); $this->assertEquals(0, $in_sync); // Check that removals (i.e. someone in Mailchimp but not/no longer in // Civi's group) are zero. $to_delete = $sync->getEmailsNotInCiviButInMailchimp(); $this->assertEquals(0, count($to_delete)); // Run bulk subscribe... $stats = $sync->updateMailchimpFromCivi(); $this->assertEquals(0, $stats['updates']); $this->assertEquals(0, $stats['unsubscribes']); $this->assertEquals(1, $stats['additions']); // Now check they are subscribed. $not_found = TRUE; $i = 0; $start = time(); //print date('Y-m-d H:i:s') . " Mailchimp batch returned 'finished'\n"; while ($not_found && $i++ < 2 * 10) { try { $result = $api->get("/lists/" . static::$test_list_id . "/members/" . static::$civicrm_contact_1['subscriber_hash'], ['fields' => 'status']); // print date('Y-m-d H:i:s') . " found now " . round(time() - $start, 2) . "s after Mailchimp reported the batch had finished.\n"; $not_found = FALSE; } catch (CRM_Mailchimp_RequestErrorException $e) { if ($e->response->http_code == 404) { // print date('Y-m-d H:i:s') . " not found yet\n"; sleep(10); } else { throw $e; } } } } 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; } }