From f1a0eb0af9f762deaf24e56788e11056f1c0c1d7 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 6 Jun 2019 16:32:09 +1200 Subject: [PATCH] Fix Exception to remove result from duplicates This updates, and tests, the Exception.create function (via the api) to remove duplicates from the prevnext_cache. This feels like a better fix than the cartwheels the prevnext cache code does to avoid retrieving cached duplicates. --- CRM/Dedupe/BAO/Exception.php | 66 ++++++++++++++++++++++++ tests/phpunit/api/v3/ExceptionTest.php | 70 ++++++++++++++++++++++++++ 2 files changed, 136 insertions(+) create mode 100644 CRM/Dedupe/BAO/Exception.php create mode 100644 tests/phpunit/api/v3/ExceptionTest.php diff --git a/CRM/Dedupe/BAO/Exception.php b/CRM/Dedupe/BAO/Exception.php new file mode 100644 index 0000000000..7286f80e5f --- /dev/null +++ b/CRM/Dedupe/BAO/Exception.php @@ -0,0 +1,66 @@ +copyValues($params); + $dao->save(); + if ($dao->contact_id1 && $dao->contact_id2) { + CRM_Core_DAO::singleValueQuery(" + DELETE FROM civicrm_prevnext_cache + WHERE (entity_id1 = %1 AND entity_id2 = %2) + OR (entity_id1 = %2 AND entity_id2 = %2)", + [1 => [$dao->contact_id1, 'Integer'], 2 => [$dao->contact_id2, 'Integer']] + ); + } + + CRM_Utils_Hook::post($hook, 'Exception', $dao->id, $dao); + return $dao; + } + +} diff --git a/tests/phpunit/api/v3/ExceptionTest.php b/tests/phpunit/api/v3/ExceptionTest.php new file mode 100644 index 0000000000..1bd986b62c --- /dev/null +++ b/tests/phpunit/api/v3/ExceptionTest.php @@ -0,0 +1,70 @@ +useTransaction(TRUE); + parent::setUp(); + } + + /** + * Test that when a dedupe exception is created the pair are saved from the merge cache. + */ + public function testCreatingAnExceptionRemovesFromCachedMergePairs() { + $contact1 = $this->individualCreate(); + $contact2 = $this->individualCreate(); + $defaultRuleGroupID = $this->callAPISuccess('RuleGroup', 'getvalue', [ + 'contact_type' => 'Individual', + 'used' => 'Unsupervised', + 'return' => 'id', + 'options' => ['limit' => 1], + ]); + $dupes = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]); + $this->assertEquals(1, $dupes['count']); + $this->callAPISuccess('Exception', 'create', ['contact_id1' => $contact1, 'contact_id2' => $contact2]); + $this->assertEquals(0, CRM_Core_DAO::singleValueQuery(' + SELECT count(*) FROM civicrm_prevnext_cache + WHERE (entity_id1 = ' . $contact1 . ' AND entity_id2 = ' . $contact2 . ') + OR (entity_id1 = ' . $contact2 . ' AND entity_id2 = ' . $contact1 . ')' + )); + $dupes = $this->callAPISuccess('Dedupe', 'getduplicates', ['rule_group_id' => $defaultRuleGroupID]); + $this->assertEquals(0, $dupes['count']); + } + +} -- 2.25.1