/**
  * Tests the guessContactIdsBySubscribers method.
  *
  */
 public function testGuessContactIdsBySubscribers()
 {
     // Create empty tables.
     CRM_Mailchimp_Sync::createTemporaryTableForMailchimp();
     CRM_Mailchimp_Sync::createTemporaryTableForCiviCRM();
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_c (email, contact_id) VALUES\n      ('*****@*****.**', 1),\n      ('*****@*****.**', 2);");
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email) VALUES\n      ('*****@*****.**'),\n      ('*****@*****.**');");
     // Check for a match.
     $matched = CRM_Mailchimp_Sync::guessContactIdsBySubscribers();
     $this->assertEquals(1, $matched);
     // Check the matched record did indeed match.
     $result = CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) c FROM tmp_mailchimp_push_m WHERE email = "*****@*****.**" AND cid_guess = 1');
     $this->assertEquals(1, $result);
     // Check the other one did not.
     $result = CRM_Core_DAO::singleValueQuery('SELECT COUNT(*) c FROM tmp_mailchimp_push_m WHERE email = "*****@*****.**" AND cid_guess IS NULL');
     $this->assertEquals(1, $result);
     CRM_Mailchimp_Sync::dropTemporaryTables();
 }
 /**
  * Tests the removeInSync method.
  *
  */
 public function testRemoveInSync()
 {
     // Create empty tables.
     CRM_Mailchimp_Sync::createTemporaryTableForMailchimp();
     CRM_Mailchimp_Sync::createTemporaryTableForCiviCRM();
     // 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/interest-categories', Argument::any())->willReturn(json_decode('{"http_code":200,"data":{"categories":[{"id":"categoryid","title":"' . static::MC_INTEREST_CATEGORY_TITLE . '"}]}}'));
     $api_prophecy->get("/lists/dummylistid/interest-categories/categoryid/interests", Argument::any())->willReturn(json_decode('{"http_code":200,"data":{"interests":[{"id":"interestId1","name":"' . static::MC_INTEREST_NAME_1 . '"},{"id":"interestId2","name":"' . static::MC_INTEREST_NAME_2 . '"}]}}'));
     $api_prophecy->get('/lists', ['fields' => 'lists.id,lists.name', 'count' => 10000])->willReturn(json_decode(json_encode(['http_code' => 200, 'data' => ['lists' => []]])));
     $sync = new CRM_Mailchimp_Sync(static::$test_list_id);
     // Test 1.
     //
     // Delete records from both tables when there's a cid_guess--contact link
     // and the hash is the same.
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_c (email, hash, contact_id) VALUES\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 1),\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 2);");
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email, hash, cid_guess) VALUES\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 1),\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 2);");
     $result = $sync->removeInSync('pull');
     $this->assertEquals(2, $result);
     $this->assertEquals(0, $sync->countMailchimpMembers());
     $this->assertEquals(0, $sync->countCiviCrmMembers());
     // Test 2.
     //
     // Check different hashes stops removals.
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_c (email, hash, contact_id) VALUES\n      ('*****@*****.**', 'different', 1),\n      ('*****@*****.**', 'different', 2);");
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email, hash, cid_guess) VALUES\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 1),\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 2);");
     $result = $sync->removeInSync('pull');
     $this->assertEquals(0, $result);
     $this->assertEquals(2, $sync->countMailchimpMembers());
     $this->assertEquals(2, $sync->countCiviCrmMembers());
     // Test 3.
     //
     // Check nothing removed if no cid-contact match.
     CRM_Mailchimp_Sync::createTemporaryTableForMailchimp();
     CRM_Mailchimp_Sync::createTemporaryTableForCiviCRM();
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_c (email, hash, contact_id) VALUES\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 1),\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 2);");
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email, hash, cid_guess) VALUES\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 0),\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', NULL);");
     $result = $sync->removeInSync('pull');
     $this->assertEquals(0, $result);
     $this->assertEquals(2, $sync->countMailchimpMembers());
     $this->assertEquals(2, $sync->countCiviCrmMembers());
     // Test 4.
     //
     // Check duplicate civi contact deleted.
     CRM_Mailchimp_Sync::createTemporaryTableForMailchimp();
     CRM_Mailchimp_Sync::createTemporaryTableForCiviCRM();
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_c (email, hash, contact_id) VALUES\n      ('*****@*****.**', 'Xaaaaaaaaaaaaaaa', 1),\n      ('*****@*****.**', 'Yaaaaaaaaaaaaaaa', 2);");
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email, hash, cid_guess) VALUES\n      ('*****@*****.**', 'bbbbbbbbbbbbbbbb', 1);");
     $result = $sync->removeInSync('push');
     $this->assertEquals(1, $result);
     $this->assertEquals(1, $sync->countMailchimpMembers());
     $this->assertEquals(1, $sync->countCiviCrmMembers());
     // Test 5.
     //
     // Check duplicate civi contact NOT deleted when in pull mode.
     CRM_Mailchimp_Sync::createTemporaryTableForMailchimp();
     CRM_Mailchimp_Sync::createTemporaryTableForCiviCRM();
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_c (email, hash, contact_id) VALUES\n      ('*****@*****.**', 'Xaaaaaaaaaaaaaaa', 1),\n      ('*****@*****.**', 'Yaaaaaaaaaaaaaaa', 2);");
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email, hash, cid_guess) VALUES\n      ('*****@*****.**', 'bbbbbbbbbbbbbbbb', 1);");
     $result = $sync->removeInSync('pull');
     $this->assertEquals(0, $result);
     $this->assertEquals(1, $sync->countMailchimpMembers());
     $this->assertEquals(2, $sync->countCiviCrmMembers());
     // Test 5: one contact should be removed because it's in sync, the other
     // because it's a duplicate.
     //
     // Check duplicate civi contact deleted.
     CRM_Mailchimp_Sync::createTemporaryTableForMailchimp();
     CRM_Mailchimp_Sync::createTemporaryTableForCiviCRM();
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_c (email, hash, contact_id) VALUES\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 1),\n      ('*****@*****.**', 'Yaaaaaaaaaaaaaaa', 2);");
     CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email, hash, cid_guess) VALUES\n      ('*****@*****.**', 'aaaaaaaaaaaaaaaa', 1);");
     $result = $sync->removeInSync('push');
     $this->assertEquals(2, $result);
     $this->assertEquals(0, $sync->countMailchimpMembers());
     $this->assertEquals(0, $sync->countCiviCrmMembers());
     CRM_Mailchimp_Sync::dropTemporaryTables();
 }