Skip to content

Commit

Permalink
Merge pull request #13179 from eileenmcnaughton/dedupe_rule
Browse files Browse the repository at this point in the history
Test support for fixing dev/core#397 including adding Rule api
  • Loading branch information
seamuslee001 authored Nov 30, 2018
2 parents 2b20bc0 + 63926b5 commit 6ea5175
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 16 deletions.
1 change: 0 additions & 1 deletion CRM/Dedupe/BAO/RuleGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ public function fillTable() {
if ($dao->affectedRows() >= 1) {
$exclWeightSum[] = substr($fieldWeight, strrpos($fieldWeight, '.') + 1);
}
$dao->free();
}
else {
// its a die situation
Expand Down
87 changes: 87 additions & 0 deletions api/v3/Rule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/*
+--------------------------------------------------------------------+
| CiviCRM version 5 |
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC (c) 2004-2017 |
+--------------------------------------------------------------------+
| This file is a part of CiviCRM. |
| |
| CiviCRM is free software; you can copy, modify, and distribute it |
| under the terms of the GNU Affero General Public License |
| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
| |
| CiviCRM is distributed in the hope that it will be useful, but |
| WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| See the GNU Affero General Public License for more details. |
| |
| You should have received a copy of the GNU Affero General Public |
| License and the CiviCRM Licensing Exception along |
| with this program; if not, contact CiviCRM LLC |
| at info[AT]civicrm[DOT]org. If you have questions about the |
| GNU Affero General Public License or the licensing of CiviCRM, |
| see the CiviCRM license FAQ at http://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

/**
* This api exposes CiviCRM (dedupe) rules.
*
* Rules dedupe critieria assigned to RuleGroups.
*
* @package CiviCRM_APIv3
*/

/**
* Create or update a rule.
*
* @param array $params
* Array per getfields metadata.
*
* @return array
* API result array
*/
function civicrm_api3_rule_create($params) {
return _civicrm_api3_basic_create(_civicrm_api3_get_BAO(__FUNCTION__), $params, 'Rule');
}

/**
* Specify Meta data for create.
*
* Note that this data is retrievable via the getfields function
* and is used for pre-filling defaults and ensuring mandatory requirements are met.
*
* @param array $params
*/
function _civicrm_api3_rule_create_spec(&$params) {
$params['dedupe_rule_group_id']['api.required'] = TRUE;
$params['rule_table']['api.default'] = 'civicrm_contact';
$params['rule_field']['api.required'] = TRUE;
$params['rule_weight']['api.required'] = TRUE;
}

/**
* Delete an existing Rule.
*
* @param array $params
*
* @return array
* API result array
*/
function civicrm_api3_rule_delete($params) {
return _civicrm_api3_basic_delete(_civicrm_api3_get_BAO(__FUNCTION__), $params);
}

/**
* Get a Rule.
*
* @param array $params
* Array per getfields metadata.
*
* @return array
* API result array
*/
function civicrm_api3_rule_get($params) {
return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params, TRUE, 'Rule');
}
33 changes: 18 additions & 15 deletions tests/phpunit/CRM/Dedupe/DedupeFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ public function testUnsupervisedDupes() {
$this->assertEquals(count($foundDupes), 3, 'Check Individual-Fuzzy dupe rule for dupesInGroup().');
}

/**
* Test that a rule set to is_reserved = 0 works.
*
* There is a different search used dependent on this variable.
*/
public function testCustomRule() {
$this->setupForGroupDedupe();

Expand All @@ -69,18 +74,20 @@ public function testCustomRule() {
'title' => 'TestRule',
'is_reserved' => 0,
));
foreach (array('first_name', 'last_name') as $field) {
$ruleDao = new CRM_Dedupe_DAO_Rule();
$ruleDao->dedupe_rule_group_id = $ruleGroup['id'];
$ruleDao->rule_table = 'civicrm_contact';
$ruleDao->rule_field = $field;
$ruleDao->rule_length = NULL;
$ruleDao->rule_weight = 4;
$ruleDao->save();
$ruleDao->free();
$rules = [];
foreach (array('birth_date', 'first_name', 'last_name') as $field) {
$rules[$field] = $this->callAPISuccess('Rule', 'create', [
'dedupe_rule_group_id' => $ruleGroup['id'],
'rule_table' => 'civicrm_contact',
'rule_weight' => 4,
'rule_field' => $field,
]);
}
$foundDupes = CRM_Dedupe_Finder::dupesInGroup($ruleGroup['id'], $this->groupID);
$this->assertEquals(count($foundDupes), 4);
$this->markTestIncomplete('This currenctly fails - see https://lab.civicrm.org/dev/core/issues/397');
CRM_Dedupe_Finder::dupes($ruleGroup['id']);

}

/**
Expand Down Expand Up @@ -179,12 +186,6 @@ public function testDupesByParams() {
// 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',
Expand Down Expand Up @@ -224,12 +225,14 @@ protected function setupForGroupDedupe() {
'last_name' => 'hood',
'email' => 'robin@example.com',
'contact_type' => 'Individual',
'birth_date' => '2016-01-01',
),
array(
'first_name' => 'robin',
'last_name' => 'hood',
'email' => 'hood@example.com',
'contact_type' => 'Individual',
'birth_date' => '2016-01-01',
),
array(
'first_name' => 'robin',
Expand Down

0 comments on commit 6ea5175

Please sign in to comment.