[REF] minor code simplification. Test where handling for gender_id etc
authoreileen <emcnaughton@wikimedia.org>
Sat, 30 Nov 2019 00:16:19 +0000 (13:16 +1300)
committereileen <emcnaughton@wikimedia.org>
Sun, 1 Dec 2019 05:34:04 +0000 (18:34 +1300)
Add test & reduce special handling for gender_id, suffix_id, prefix_id, communication_style_id

CRM/Contact/BAO/Query.php
tests/phpunit/CRM/Contact/BAO/QueryTest.php
tests/phpunit/CRM/Mailing/BaseMailingSystemTest.php

index 60dd0f86f6f3b25f75dac06f09c8d7df9cbfccee..566adf24fe7bbfc3f7ddf545dc98a59378e03488 100644 (file)
@@ -2170,15 +2170,17 @@ class CRM_Contact_BAO_Query {
       $this->_qill[$grouping][] = ts("%1 %2 %3", [1 => $field['title'], 2 => $qillop, 3 => $qillVal]);
     }
     elseif (!empty($field['pseudoconstant'])) {
+      // For the hacked fields we want to undo the hack to type to avoid missing the index by adding quotes.
+      $dataType = !empty($this->legacyHackedFields[$name]) ? CRM_Utils_Type::T_INT : $field['type'];
       $this->optionValueQuery(
         $name, $op, $value, $grouping,
         'CRM_Contact_DAO_Contact',
         $field,
         $field['title'],
-        'String',
+        CRM_Utils_Type::typeToString($dataType),
         TRUE
       );
-      if ($name == 'gender_id') {
+      if ($name === 'gender_id') {
         self::$_openedPanes[ts('Demographics')] = TRUE;
       }
     }
@@ -5892,10 +5894,6 @@ AND   displayRelType.is_active = 1
       'email_greeting',
       'postal_greeting',
       'addressee',
-      'gender_id',
-      'prefix_id',
-      'suffix_id',
-      'communication_style_id',
     ];
 
     if ($useIDsOnly) {
@@ -5907,8 +5905,8 @@ AND   displayRelType.is_active = 1
         // Special handling for on_hold, so that we actually use the 'where'
         // property in order to limit the query by the on_hold status of the email,
         // instead of using email.id which would be nonsensical.
-        if ($field['name'] == 'on_hold') {
-          $wc = "{$field['where']}";
+        if ($field['name'] === 'on_hold') {
+          $wc = $field['where'];
         }
         else {
           $wc = "$tableName.id";
@@ -5920,9 +5918,7 @@ AND   displayRelType.is_active = 1
       $wc = "{$field['where']}";
     }
     if (in_array($name, $pseudoFields)) {
-      if (!in_array($name, ['gender_id', 'prefix_id', 'suffix_id', 'communication_style_id'])) {
-        $wc = "contact_a.{$name}_id";
-      }
+      $wc = "contact_a.{$name}_id";
       $dataType = 'Positive';
       $value = (!$value) ? 0 : $value;
     }
index 2e3787e9c0dd3678fc3cd3f25e3b653e7e98befa..f70937de060383ade42d5816644159fcfab697a0 100644 (file)
@@ -1129,4 +1129,50 @@ civicrm_relationship.is_active = 1 AND
     $this->assertEquals(['=', 'Normal'], $qill);
   }
 
+  /**
+   * Test tests that a value on 'any entity' with the right metadata will be handled.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testGenericWhereHandling() {
+    $query = new CRM_Contact_BAO_Query([['suffix_id', '=', 2, 0]]);
+    $this->assertEquals("contact_a.suffix_id = 2", $query->_where[0][0]);
+    $this->assertEquals('Individual Suffix = Sr.', $query->_qill[0][0]);
+    $this->assertNotTrue(isset($query->_tables['civicrm_activity']));
+
+    $query = new CRM_Contact_BAO_Query([['prefix_id', '=', 2, 0]]);
+    $this->assertEquals('contact_a.prefix_id = 2', $query->_where[0][0]);
+    $this->assertEquals('Individual Prefix = Ms.', $query->_qill[0][0]);
+    $this->assertNotTrue(isset($query->_tables['civicrm_activity']));
+
+    $query = new CRM_Contact_BAO_Query([['gender_id', '=', 2, 0]]);
+    $this->assertEquals('contact_a.gender_id = 2', $query->_where[0][0]);
+    $this->assertEquals('Gender = Male', $query->_qill[0][0]);
+    $this->assertNotTrue(isset($query->_tables['civicrm_activity']));
+
+    $query = new CRM_Contact_BAO_Query([['communication_style_id', '=', 2, 0]]);
+    $this->assertEquals('contact_a.communication_style_id = 2', $query->_where[0][0]);
+    $this->assertEquals('Communication Style = Familiar', $query->_qill[0][0]);
+
+    $query = new CRM_Contact_BAO_Query([['communication_style_id', '=', 2, 0]]);
+    $this->assertEquals('contact_a.communication_style_id = 2', $query->_where[0][0]);
+    $this->assertEquals('Communication Style = Familiar', $query->_qill[0][0]);
+
+    $query = new CRM_Contact_BAO_Query([['contact_type', '=', 'Household', 0]]);
+    $this->assertEquals("contact_a.contact_type = 'Household'", $query->_where[0][0]);
+    $this->assertEquals('Contact Type = Household', $query->_qill[0][0]);
+
+    $query = new CRM_Contact_BAO_Query([['on_hold', '=', 0, 0]]);
+    $this->assertEquals('civicrm_email.on_hold = 0', $query->_where[0][0]);
+    $this->assertEquals('On Hold = 0', $query->_qill[0][0]);
+
+    $query = new CRM_Contact_BAO_Query([['on_hold', '=', 1, 0]]);
+    $this->assertEquals('civicrm_email.on_hold = 1', $query->_where[0][0]);
+    $this->assertEquals('On Hold = 1', $query->_qill[0][0]);
+
+    $query = new CRM_Contact_BAO_Query([['world_region', '=', 3, 0]]);
+    $this->assertEquals('civicrm_worldregion.id = 3', $query->_where[0][0]);
+    $this->assertEquals('World Region = Middle East and North Africa', $query->_qill[0][0]);
+  }
+
 }
index c2d4e8af6b861fff8e1e16c9d281cd92cde461b2..fa637eb0cdf32d821b0c77f7658814b92a67a321 100644 (file)
@@ -381,7 +381,9 @@ abstract class CRM_Mailing_BaseMailingSystemTest extends CiviUnitTestCase {
    *
    * @param array $params
    *   List of parameters to send to Mailing.create API.
+   *
    * @return array<ezcMail>
+   * @throws \CRM_Core_Exception
    */
   protected function runMailingSuccess($params) {
     $mailingParams = array_merge($this->defaultParams, $params);