CRM-18522 tests to attempt to reproduce the reported sql error.
authoreileen <emcnaughton@wikimedia.org>
Wed, 15 Jun 2016 09:10:30 +0000 (21:10 +1200)
committereileen <emcnaughton@wikimedia.org>
Sat, 25 Jun 2016 01:07:04 +0000 (13:07 +1200)
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
CRM/Dedupe/Finder.php
api/v3/Job.php
api/v3/RuleGroup.php
tests/phpunit/api/v3/JobTest.php

index 5ac720a49e85279a475dcb8610320a953fa126ee..a6ea3542fcd86b6ddca962c7d46d61926ddadac3 100644 (file)
@@ -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;
index 02e937c48b48ee01afd0526039dd7f4edb6fdffe..2d8be854005cdb05020c3e7b8e9fb28959e618e4 100644 (file)
@@ -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();
index 5ae0363f8d5b836a496512fb429e97724b102a9c..e859cf55f515c8b092d581b4b504b7bc84033110 100644 (file)
@@ -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',
index bbd4ff05bbd6d9f895534ebd08ca02b2ff1b89f0..89112c3d9ae8e83f90e46170ef8bceeef7b3bb2a 100644 (file)
@@ -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;
 }
 
 /**
index 19c46026fb15d2b11d74d7a8aea5e18112744fdb..5d450396b7216905bd5e13910a7355241619dd85 100644 (file)
@@ -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.
    *