/** * Tests the guessContactIdsByUniqueEmail method ignores deleted contacts. * */ public function testGuessContactIdsByUniqueEmailIgnoresDeletedContacts() { // // Test 1: Primary case: match a unique email. // // Create empty tables. CRM_Mailchimp_Sync::createTemporaryTableForMailchimp(); CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email) VALUES (%1), (%2);", [1 => [static::$civicrm_contact_1['email'], 'String'], 2 => ['*****@*****.**', 'String']]); // Delete (trash) the contact. civicrm_api3('Contact', 'delete', ['contact_id' => static::$civicrm_contact_1['contact_id']]); $result = CRM_Mailchimp_Sync::guessContactIdsByUniqueEmail(); $this->assertEquals(0, $result); // Check that the email that belongs to the deleted contact did not match. $dao = CRM_Core_DAO::executeQuery('SELECT COUNT(*) c FROM tmp_mailchimp_push_m WHERE email = %1 AND cid_guess = ' . static::$civicrm_contact_1['contact_id'], [1 => [static::$civicrm_contact_1['email'], 'String']]); $dao->fetch(); $this->assertEquals(0, $dao->c); $dao->free(); // Check the other one did not match either. $dao = CRM_Core_DAO::executeQuery('SELECT COUNT(*) c FROM tmp_mailchimp_push_m WHERE email = "*****@*****.**" AND cid_guess IS NULL'); $dao->fetch(); $this->assertEquals(1, $dao->c); $dao->free(); // Test 2: the email belongs to two separate contacts, but one is deleted. // so there's only one non-deleted unique contact. // civicrm_api3('Email', 'create', ['contact_id' => static::$civicrm_contact_2['contact_id'], 'email' => static::$civicrm_contact_1['email'], 'is_billing' => 1]); CRM_Mailchimp_Sync::dropTemporaryTables(); CRM_Mailchimp_Sync::createTemporaryTableForMailchimp(); CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email) VALUES (%1);", [1 => [static::$civicrm_contact_1['email'], 'String']]); $result = CRM_Mailchimp_Sync::guessContactIdsByUniqueEmail(); $this->assertEquals(1, $result); // Test 4: the email belongs to two non-deleted contacts and one deleted // contact, therefore is not unique. // Need a third contact. $contact3 = civicrm_api3('Contact', 'create', ['contact_type' => 'Individual', 'first_name' => 'Other ' . static::C_CONTACT_1_FIRST_NAME, 'last_name' => static::C_CONTACT_1_LAST_NAME, 'email' => static::$civicrm_contact_1['email']]); CRM_Mailchimp_Sync::dropTemporaryTables(); CRM_Mailchimp_Sync::createTemporaryTableForMailchimp(); CRM_Core_DAO::executeQuery("INSERT INTO tmp_mailchimp_push_m (email) VALUES (%1);", [1 => [static::$civicrm_contact_1['email'], 'String']]); $result = CRM_Mailchimp_Sync::guessContactIdsByUniqueEmail(); // remove contact3. civicrm_api3('Contact', 'delete', ['contact_id' => $contact3['id'], 'skip_undelete' => 1]); $this->assertEquals(0, $result); }