diff --git a/CRM/Dedupe/Finder.php b/CRM/Dedupe/Finder.php index 7b6c936e72df..9c3a804b6ab1 100644 --- a/CRM/Dedupe/Finder.php +++ b/CRM/Dedupe/Finder.php @@ -281,6 +281,8 @@ public static function formatParams($fields, $ctype) { if (preg_match('/(.*)-(Primary-[\d+])$|(.*)-(\d+|Primary)$/', $key, $matches)) { $return = array_values(array_filter($matches)); $flat[$return[1]] = $value; + // make sure the first occurrence is kept, not the last + $flat[$return[1]] = !isset($flat[$return[1]]) ? $value : $flat[$return[1]]; unset($flat[$key]); } } diff --git a/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php b/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php index 65a728c05c6b..250e05344a7d 100644 --- a/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php +++ b/tests/phpunit/CRM/Dedupe/DedupeFinderTest.php @@ -40,7 +40,7 @@ public function tearDown() { * @throws \Exception */ public function testUnsupervisedDupes() { - // make dupe checks based on based on following contact sets: + // make dupe checks based on following contact sets: // FIRST - LAST - EMAIL // --------------------------------- // robin - hood - robin@example.com @@ -58,6 +58,22 @@ public function testUnsupervisedDupes() { $this->assertEquals(count($foundDupes), 3, 'Check Individual-Fuzzy dupe rule for dupesInGroup().'); } + /** + * Test duplicate contact retrieval with 2 email fields. + */ + public function testUnsupervisedWithTwoEmailFields() { + $this->setupForGroupDedupe(); + $fields = [ + 'first_name' => 'robin', + 'last_name' => 'hood', + 'email-1' => 'hood@example.com', + 'email-2' => '', + ]; + $dedupeParams = CRM_Dedupe_Finder::formatParams($fields, 'Individual'); + $dedupeResults = CRM_Dedupe_Finder::dupesByParams($dedupeParams, 'Individual'); + $this->assertEquals(count($dedupeResults), 1); + } + /** * Test that a rule set to is_reserved = 0 works. *