From 6c866f0c80eaa57d8b26fbf98834cc293255ce6c Mon Sep 17 00:00:00 2001 From: eileen Date: Wed, 15 Jun 2016 21:10:30 +1200 Subject: [PATCH] CRM-18522 tests to attempt to reproduce the reported sql error. I could not reproduce the error through the UI alone. I had to basically hack the rules out in the db. I guess the config arguably makes sense when configured in conjunction with the extension but that is not 100% clear. However the code is a little less hacky & a lot better tested as a result of committing this --- CRM/Dedupe/BAO/RuleGroup.php | 2 +- CRM/Dedupe/Finder.php | 8 ++++++- api/v3/Job.php | 6 +++-- api/v3/RuleGroup.php | 4 ++++ tests/phpunit/api/v3/JobTest.php | 41 ++++++++++++++++++++++++++++++++ 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/CRM/Dedupe/BAO/RuleGroup.php b/CRM/Dedupe/BAO/RuleGroup.php index 5ac720a49e..a6ea3542fc 100644 --- a/CRM/Dedupe/BAO/RuleGroup.php +++ b/CRM/Dedupe/BAO/RuleGroup.php @@ -182,8 +182,8 @@ class CRM_Dedupe_BAO_RuleGroup extends CRM_Dedupe_DAO_RuleGroup { // if there are no rules in this rule group // add an empty query fulfilling the pattern if (!$queries) { - $queries = array('SELECT 0 id1, 0 id2, 0 weight LIMIT 0'); $this->noRules = TRUE; + return array(); } return $queries; diff --git a/CRM/Dedupe/Finder.php b/CRM/Dedupe/Finder.php index 02e937c48b..2d8be85400 100644 --- a/CRM/Dedupe/Finder.php +++ b/CRM/Dedupe/Finder.php @@ -51,9 +51,15 @@ class CRM_Dedupe_Finder { * @param bool $checkPermissions * Respect logged in user permissions. * + * @param int $limit + * Optional limit. This limits the number of contacts for which the code will + * attempt to find matches. + * * @return array * Array of (cid1, cid2, weight) dupe triples - * @throws \Exception + * + * @throws CiviCRM_API3_Exception + * @throws Exception */ public static function dupes($rgid, $cids = array(), $checkPermissions = TRUE, $limit = NULL) { $rgBao = new CRM_Dedupe_BAO_RuleGroup(); diff --git a/api/v3/Job.php b/api/v3/Job.php index 5ae0363f8d..e859cf55f5 100644 --- a/api/v3/Job.php +++ b/api/v3/Job.php @@ -477,12 +477,13 @@ function civicrm_api3_job_process_respondent($params) { * @throws \CiviCRM_API3_Exception */ function civicrm_api3_job_process_batch_merge($params) { - $rule_group_id = CRM_Utils_Array::value('rgid', $params); + $rule_group_id = CRM_Utils_Array::value('rule_group_id', $params); if (!$rule_group_id) { $rule_group_id = civicrm_api3('RuleGroup', 'getvalue', array( 'contact_type' => 'Individual', 'used' => 'Unsupervised', 'return' => 'id', + 'options' => array('limit' => 1), )); } $gid = CRM_Utils_Array::value('gid', $params); @@ -501,9 +502,10 @@ function civicrm_api3_job_process_batch_merge($params) { * @param $params */ function _civicrm_api3_job_process_batch_merge_spec(&$params) { - $params['rgid'] = array( + $params['rule_group_id'] = array( 'title' => 'Dedupe rule group id, defaults to Contact Unsupervised rule', 'type' => CRM_Utils_Type::T_INT, + 'api.aliases' => array('rgid'), ); $params['gid'] = array( 'title' => 'group id', diff --git a/api/v3/RuleGroup.php b/api/v3/RuleGroup.php index bbd4ff05bb..89112c3d9a 100644 --- a/api/v3/RuleGroup.php +++ b/api/v3/RuleGroup.php @@ -55,6 +55,10 @@ function civicrm_api3_rule_group_create($params) { * @param array $params */ function _civicrm_api3_rule_group_create_spec(&$params) { + $params['contact_type']['api.required'] = TRUE; + $params['threshold']['api.required'] = TRUE; + $params['used']['api.required'] = TRUE; + $params['name']['api.required'] = TRUE; } /** diff --git a/tests/phpunit/api/v3/JobTest.php b/tests/phpunit/api/v3/JobTest.php index 19c46026fb..5d450396b7 100644 --- a/tests/phpunit/api/v3/JobTest.php +++ b/tests/phpunit/api/v3/JobTest.php @@ -353,6 +353,47 @@ class api_v3_JobTest extends CiviUnitTestCase { ), 4); } + /** + * Test the batch merge does not fatal on an empty rule. + * + * @dataProvider getRuleSets + * + * @param string $contactType + * @param string $used + * @param bool $isReserved + * @param int $threshold + */ + public function testBatchMergeEmptyRule($contactType, $used, $name, $isReserved, $threshold) { + $ruleGroup = $this->callAPISuccess('RuleGroup', 'create', array( + 'contact_type' => $contactType, + 'threshold' => $threshold, + 'used' => $used, + 'name' => $name, + 'is_reserved' => $isReserved, + )); + $this->callAPISuccess('Job', 'process_batch_merge', array('rule_group_id' => $ruleGroup['id'])); + $this->callAPISuccess('RuleGroup', 'delete', array('id' => $ruleGroup['id'])); + } + + /** + * Get the various rule combinations. + */ + public function getRuleSets() { + $contactTypes = array('Individual', 'Organization', 'Household'); + $useds = array('Unsupervised', 'General', 'Supervised'); + $ruleGroups = array(); + foreach ($contactTypes as $contactType) { + foreach ($useds as $used) { + $ruleGroups[] = array($contactType, $used, 'Bob', FALSE, 0); + $ruleGroups[] = array($contactType, $used, 'Bob', FALSE, 10); + $ruleGroups[] = array($contactType, $used, 'Bob', TRUE, 10); + $ruleGroups[] = array($contactType, $used, $contactType . $used, FALSE, 10); + $ruleGroups[] = array($contactType, $used, $contactType . $used, TRUE, 10); + } + } + return $ruleGroups; + } + /** * Test the batch merge does not create duplicate emails. * -- 2.25.1