From a2dd33d31f0e5238cdfd4126d62f3bc1967eccbc Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 23 Mar 2017 17:49:58 +1300 Subject: [PATCH] Towards CRM-19612 improve tests to allow replication of the issue --- tests/phpunit/CRM/Dedupe/DedupeFinderTest.php | 182 +++++++++++------- tests/phpunit/api/v3/ContactTest.php | 24 +++ 2 files changed, 135 insertions(+), 71 deletions(-) diff --git a/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php b/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php index 417fb63f79..aeab019498 100644 --- a/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php +++ b/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php @@ -5,7 +5,41 @@ * @group headless */ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase { - public function testFuzzyDupes() { + + /** + * IDs of created contacts. + * + * @var array + */ + protected $contactIDs = array(); + + /** + * ID of the group holding the contacts. + * + * @var int + */ + protected $groupID; + + /** + * Clean up after the test. + */ + public function tearDown() { + + foreach ($this->contactIDs as $contactId) { + $this->contactDelete($contactId); + } + if ($this->groupID) { + $this->callAPISuccess('group', 'delete', array('id' => $this->groupID)); + } + parent::tearDown(); + } + + /** + * Test the unsupervised dedupe rule against a group. + * + * @throws \Exception + */ + public function testUnsupervisedDupes() { // make dupe checks based on based on following contact sets: // FIRST - LAST - EMAIL // --------------------------------- @@ -16,18 +50,46 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase { // will - dale - dale@example.com // will - dale - will@example.com // will - dale - will@example.com + $this->setupForGroupDedupe(); - // create a group to hold contacts, so that dupe checks don't consider any other contacts in the DB - $params = array( - 'name' => 'Dupe Group', - 'title' => 'New Test Dupe Group', - 'domain_id' => 1, - 'is_active' => 1, - 'visibility' => 'Public Pages', - ); + $ruleGroup = $this->callAPISuccessGetSingle('RuleGroup', array('is_reserved' => 1, 'contact_type' => 'Individual', 'used' => 'Unsupervised')); - $result = $this->callAPISuccess('group', 'create', $params); - $groupId = $result['id']; + $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); + $this->assertEquals(count($foundDupes), 3, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); + } + + /** + * Test the supervised dedupe rule against a group. + * + * @throws \Exception + */ + public function testSupervisedDupes() { + $this->setupForGroupDedupe(); + $ruleGroup = $this->callAPISuccessGetSingle('RuleGroup', array('s_reserved' => 1, 'contact_type' => 'Individual', 'used' => 'Supervised')); + $foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID); + // ------------------------------------------------------------------------- + // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair ) + // -------------------------------------------------------------------------- + // will - dale - will@example.com + // will - dale - will@example.com + // so 1 pair for - first + last + mail + $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); + } + + /** + * Test dupesByParams function. + */ + public function testDupesByParams() { + // make dupe checks based on based on following contact sets: + // FIRST - LAST - EMAIL + // --------------------------------- + // robin - hood - robin@example.com + // robin - hood - hood@example.com + // robin - dale - robin@example.com + // little - dale - dale@example.com + // will - dale - dale@example.com + // will - dale - will@example.com + // will - dale - will@example.com // contact data set // FIXME: move create params to separate function @@ -77,15 +139,16 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase { ); $count = 1; + foreach ($params as $param) { $contact = $this->callAPISuccess('contact', 'create', $param); - $contactIds[$count++] = $contact['id']; - - $grpParams = array( + $params = array( 'contact_id' => $contact['id'], - 'group_id' => $groupId, + 'street_address' => 'Ambachtstraat 23', + 'location_type_id' => 1, ); - $this->callAPISuccess('group_contact', 'create', $grpParams); + $this->callAPISuccess('address', 'create', $params); + $contactIds[$count++] = $contact['id']; } // verify that all contacts have been created separately @@ -93,42 +156,43 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase { $dao = new CRM_Dedupe_DAO_RuleGroup(); $dao->contact_type = 'Individual'; - $dao->level = 'Fuzzy'; + $dao->used = 'General'; $dao->is_default = 1; $dao->find(TRUE); - $foundDupes = CRM_Dedupe_Finder::dupesInGroup($dao->id, $groupId); + $fields = array( + 'first_name' => 'robin', + 'last_name' => 'hood', + 'email' => 'hood@example.com', + 'street_address' => 'Ambachtstraat 23', + ); + CRM_Core_TemporaryErrorScope::useException(); + $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($fields, 'Individual', 'General'); - // ------------------------------------------------------------------------- - // default dedupe rule: threshold = 20 => (First + Last + Email) Matches ( 1 pair ) - // -------------------------------------------------------------------------- - // will - dale - will@example.com - // will - dale - will@example.com - // so 1 pair for - first + last + mail - $this->assertEquals(count($foundDupes), 1, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); + // Check with default Individual-General rule + $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().'); + // delete all created contacts foreach ($contactIds as $contactId) { $this->contactDelete($contactId); } - // delete dupe group - $params = array('id' => $groupId, 'version' => 3); - civicrm_api('group', 'delete', $params); } - public function testDupesByParams() { - // make dupe checks based on based on following contact sets: - // FIRST - LAST - EMAIL - // --------------------------------- - // robin - hood - robin@example.com - // robin - hood - hood@example.com - // robin - dale - robin@example.com - // little - dale - dale@example.com - // will - dale - dale@example.com - // will - dale - will@example.com - // will - dale - will@example.com + /** + * Set up a group of dedupable contacts. + */ + protected function setupForGroupDedupe() { + $params = array( + 'name' => 'Dupe Group', + 'title' => 'New Test Dupe Group', + 'domain_id' => 1, + 'is_active' => 1, + 'visibility' => 'Public Pages', + ); + + $result = $this->callAPISuccess('group', 'create', $params); + $this->groupID = $result['id']; - // contact data set - // FIXME: move create params to separate function $params = array( array( 'first_name' => 'robin', @@ -175,43 +239,19 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase { ); $count = 1; - foreach ($params as $param) { $contact = $this->callAPISuccess('contact', 'create', $param); - $params = array( + $this->contactIDs[$count++] = $contact['id']; + + $grpParams = array( 'contact_id' => $contact['id'], - 'street_address' => 'Ambachtstraat 23', - 'location_type_id' => 1, + 'group_id' => $this->groupID, ); - $this->callAPISuccess('address', 'create', $params); - $contactIds[$count++] = $contact['id']; + $this->callAPISuccess('group_contact', 'create', $grpParams); } // verify that all contacts have been created separately - $this->assertEquals(count($contactIds), 7, 'Check for number of contacts.'); - - $dao = new CRM_Dedupe_DAO_RuleGroup(); - $dao->contact_type = 'Individual'; - $dao->used = 'General'; - $dao->is_default = 1; - $dao->find(TRUE); - - $fields = array( - 'first_name' => 'robin', - 'last_name' => 'hood', - 'email' => 'hood@example.com', - 'street_address' => 'Ambachtstraat 23', - ); - CRM_Core_TemporaryErrorScope::useException(); - $ids = CRM_Contact_BAO_Contact::getDuplicateContacts($fields, 'Individual', 'General'); - - // Check with default Individual-General rule - $this->assertEquals(count($ids), 2, 'Check Individual-General rule for dupesByParams().'); - - // delete all created contacts - foreach ($contactIds as $contactId) { - $this->contactDelete($contactId); - } + $this->assertEquals(count($this->contactIDs), 7, 'Check for number of contacts.'); } } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 406d328fbb..3733886aac 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3015,6 +3015,30 @@ class api_v3_ContactTest extends CiviUnitTestCase { $this->callAPIFailure('Contact', 'create', array_merge($harry, array('dupe_check' => 1))); } + /** + * Test the duplicate check function. + */ + public function testDuplicateCheckRuleNotReserved() { + $harry = array( + 'first_name' => 'Harry', + 'last_name' => 'Potter', + 'email' => 'harry@hogwarts.edu', + 'contact_type' => 'Individual', + ); + $defaultRule = $this->callAPISuccess('RuleGroup', 'getsingle', array('used' => 'Unsupervised', 'is_reserved' => 1)); + $this->callAPISuccess('RuleGroup', 'create', array('id' => $defaultRule['id'], 'is_reserved' => 0)); + $this->callAPISuccess('Contact', 'create', $harry); + $result = $this->callAPISuccess('Contact', 'duplicatecheck', array( + 'match' => $harry, + )); + + $this->assertEquals(1, $result['count']); + $this->callAPISuccess('RuleGroup', 'create', array('id' => $defaultRule['id'], 'is_reserved' => 1)); + } + + /** + * Test variants on retrieving contact by type. + */ public function testGetByContactType() { $individual = $this->callAPISuccess('Contact', 'create', array( 'email' => 'individual@test.com', -- 2.25.1