Skip to content
Snippets Groups Projects
Unverified Commit ccaff001 authored by Seamus Lee's avatar Seamus Lee Committed by GitHub
Browse files

Merge pull request #16576 from eileenmcnaughton/522

#1597 & #1595 fix regression when deduping on custom …
parents 9fe495e6 b36dc5b8
No related branches found
No related tags found
No related merge requests found
......@@ -58,15 +58,13 @@ class CRM_Dedupe_BAO_Rule extends CRM_Dedupe_DAO_Rule {
// full matches, respectively)
$where = [];
$on = ["SUBSTR(t1.{$this->rule_field}, 1, {$this->rule_length}) = SUBSTR(t2.{$this->rule_field}, 1, {$this->rule_length})"];
$entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table));
$fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values'];
$innerJoinClauses = [
"t1.{$this->rule_field} IS NOT NULL",
"t2.{$this->rule_field} IS NOT NULL",
"t1.{$this->rule_field} = t2.{$this->rule_field}",
];
if ($fields[$this->rule_field]['type'] === CRM_Utils_Type::T_DATE) {
if ($this->getFieldType($this->rule_field) === CRM_Utils_Type::T_DATE) {
$innerJoinClauses[] = "t1.{$this->rule_field} > '1000-01-01'";
$innerJoinClauses[] = "t2.{$this->rule_field} > '1000-01-01'";
}
......@@ -238,4 +236,26 @@ class CRM_Dedupe_BAO_Rule extends CRM_Dedupe_DAO_Rule {
return $exception->find(TRUE) ? FALSE : TRUE;
}
/**
* Get the specification for the given field.
*
* @param string $fieldName
*
* @return array
* @throws \CiviCRM_API3_Exception
*/
public function getFieldType($fieldName) {
$entity = CRM_Core_DAO_AllCoreTables::getBriefName(CRM_Core_DAO_AllCoreTables::getClassForTable($this->rule_table));
if (!$entity) {
// This means we have stored a custom field rather than an entity name in rule_table, figure out the entity.
$entity = civicrm_api3('CustomGroup', 'getvalue', ['table_name' => $this->rule_table, 'return' => 'extends']);
if (in_array($entity, ['Individual', 'Household', 'Organization'])) {
$entity = 'Contact';
}
$fieldName = 'custom_' . civicrm_api3('CustomField', 'getvalue', ['column_name' => $fieldName, 'return' => 'id']);
}
$fields = civicrm_api3($entity, 'getfields', ['action' => 'create'])['values'];
return $fields[$fieldName]['type'];
}
}
......@@ -6,6 +6,7 @@
*/
class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
use CRMTraits_Custom_CustomDataTrait;
/**
* IDs of created contacts.
*
......@@ -31,6 +32,10 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
if ($this->groupID) {
$this->callAPISuccess('group', 'delete', ['id' => $this->groupID]);
}
$this->quickCleanup(['civicrm_contact'], TRUE);
CRM_Core_DAO::executeQuery("DELETE r FROM civicrm_dedupe_rule_group rg INNER JOIN civicrm_dedupe_rule r ON rg.id = r.dedupe_rule_group_id WHERE rg.is_reserved = 0 AND used = 'General'");
CRM_Core_DAO::executeQuery("DELETE FROM civicrm_dedupe_rule_group WHERE is_reserved = 0 AND used = 'General'");
parent::tearDown();
}
......@@ -88,15 +93,7 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
public function testCustomRule() {
$this->setupForGroupDedupe();
$ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
'contact_type' => 'Individual',
'threshold' => 8,
'used' => 'General',
'name' => 'TestRule',
'title' => 'TestRule',
'is_reserved' => 0,
]);
$rules = [];
$ruleGroup = $this->createRuleGroup();
foreach (['birth_date', 'first_name', 'last_name'] as $field) {
$rules[$field] = $this->callAPISuccess('Rule', 'create', [
'dedupe_rule_group_id' => $ruleGroup['id'],
......@@ -111,6 +108,25 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
}
/**
* Test that we do not get a fatal error when our rule group is a custom date field.
*
* @throws \CRM_Core_Exception
*/
public function testCustomRuleCustomDateField() {
$ruleGroup = $this->createRuleGroup();
$this->createCustomGroupWithFieldOfType([], 'date');
$this->callAPISuccess('Rule', 'create', [
'dedupe_rule_group_id' => $ruleGroup['id'],
'rule_table' => $this->getCustomGroupTable(),
'rule_weight' => 4,
'rule_field' => $this->getCustomFieldColumnName('date'),
]);
CRM_Dedupe_Finder::dupes($ruleGroup['id']);
}
/**
* Test a custom rule with a non-default field.
*/
......@@ -173,15 +189,7 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
public function testInclusiveRule() {
$this->setupForGroupDedupe();
$ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
'contact_type' => 'Individual',
'threshold' => 8,
'used' => 'General',
'name' => 'TestRule',
'title' => 'TestRule',
'is_reserved' => 0,
]);
$rules = [];
$ruleGroup = $this->createRuleGroup();
foreach (['first_name', 'last_name'] as $field) {
$rules[$field] = $this->callAPISuccess('Rule', 'create', [
'dedupe_rule_group_id' => $ruleGroup['id'],
......@@ -424,4 +432,20 @@ class CRM_Dedupe_DedupeFinderTest extends CiviUnitTestCase {
$this->assertEquals(count($this->contactIDs), 7, 'Check for number of contacts.');
}
/**
* @return array|int
* @throws \CRM_Core_Exception
*/
protected function createRuleGroup() {
$ruleGroup = $this->callAPISuccess('RuleGroup', 'create', [
'contact_type' => 'Individual',
'threshold' => 8,
'used' => 'General',
'name' => 'TestRule',
'title' => 'TestRule',
'is_reserved' => 0,
]);
return $ruleGroup;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment