CRM-17120 replace option label calls with cached function.
authoreileenmcnaugton <eileen@fuzion.co.nz>
Wed, 2 Sep 2015 03:43:41 +0000 (15:43 +1200)
committereileenmcnaugton <eileen@fuzion.co.nz>
Wed, 2 Sep 2015 03:43:41 +0000 (15:43 +1200)
I added a test before swapping out the values. In the process I formed a few doubts about the
code. I couldn't find anyway to access the case_activity where parameters outside a
carefully crafted test and when I did so I got invalid sql & a minor error in the qill
I added comments about these findings & locked in the existing behaviour, keeping this
change very safe

CRM/Case/BAO/Query.php
tests/phpunit/CRM/Case/BAO/QueryTest.php [new file with mode: 0644]

index da243165ea405f75dd9a0a061f80635a9626f840..273e6e55567f10dcd104d23a781e61973dbd6aea 100644 (file)
@@ -237,6 +237,16 @@ class CRM_Case_BAO_Query {
   /**
    * Where clause for a single field.
    *
+   * CRM-17120 adds a test that checks the Qill on some of these parameters.
+   * However, I couldn't find a way, other than via test, to access the
+   * case_activity options in the code below and invalid sql was returned.
+   * Perhaps the options are just legacy?
+   *
+   * Also, CRM-17120 locks in the Qill - but it probably is not quite right as I
+   * see 'Activity Type = Scheduled' (rather than activity status).
+   *
+   * See CRM_Case_BAO_QueryTest for more.
+   *
    * @param array $values
    * @param CRM_Contact_BAO_Query $query
    */
@@ -348,7 +358,7 @@ class CRM_Case_BAO_Query {
 
       case 'case_recent_activity_type':
         $names = $value;
-        if ($activityType = CRM_Core_OptionGroup::getLabel('activity_type', $value, 'value')) {
+        if (($activityType = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'activity_type_id', $value)) != FALSE) {
           $names = $activityType;
         }
 
@@ -361,8 +371,10 @@ class CRM_Case_BAO_Query {
         return;
 
       case 'case_activity_status_id':
+        echo $value;
+
         $names = $value;
-        if ($activityStatus = CRM_Core_OptionGroup::getLabel('activity_status', $value, 'value')) {
+        if (($activityStatus = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'status_id', $value)) != FALSE) {
           $names = $activityStatus;
         }
 
@@ -384,7 +396,7 @@ class CRM_Case_BAO_Query {
 
       case 'case_activity_medium_id':
         $names = $value;
-        if ($activityMedium = CRM_Core_OptionGroup::getLabel('encounter_medium', $value, 'value')) {
+        if (($activityMedium = CRM_Core_PseudoConstant::getLabel('CRM_Activity_BAO_Activity', 'medium_id', $value)) != FALSE) {
           $names = $activityMedium;
         }
 
diff --git a/tests/phpunit/CRM/Case/BAO/QueryTest.php b/tests/phpunit/CRM/Case/BAO/QueryTest.php
new file mode 100644 (file)
index 0000000..828b534
--- /dev/null
@@ -0,0 +1,68 @@
+<?php
+require_once 'CiviTest/CiviUnitTestCase.php';
+/**
+ *  Include dataProvider for tests
+ */
+class CRM_Case_BAO_QueryTest extends CiviUnitTestCase {
+
+  /**
+   * Set up function.
+   *
+   * Ensure CiviCase is enabled.
+   */
+  public function setUp() {
+    parent::setUp();
+    CRM_Core_BAO_ConfigSetting::enableComponent('CiviCase');
+  }
+
+  /**
+   * Check that Qill is calculated correctly.
+   *
+   * CRM-17120 check the qill is still calculated after changing function used
+   * to retrieve function.
+   *
+   * Note that the Qill doesn't actually appear to have the correct labels to
+   * start with. I didn't attempt to fix that. I just prevented regression.
+   *
+   * I could not find anyway to actually do this search with the relevant fields
+   * as parameters & don't know if they exist as legitimate code or code cruft so
+   * this test was the only way I could verify the change.
+   *  - case_recent_activity_type
+   *  - case_activity_status_id
+   *  - case_activity_medium_id
+   */
+  public function testWhereClauseSingle() {
+    $params = array(
+      0 => array(
+        0 => 'case_recent_activity_type',
+        1 => '=',
+        2 => 6,
+        3 => 1,
+        4 => 0,
+      ),
+      1 => array(
+        0 => 'case_activity_status_id',
+        1 => '=',
+        2 => 1,
+        3 => 1,
+        4 => 0,
+      ),
+      2 => array(
+        0 => 'case_activity_medium_id',
+        1 => '=',
+        2 => 1,
+        3 => 1,
+        4 => 0,
+      ),
+    );
+
+    $queryObj = new CRM_Contact_BAO_Query($params, NULL, NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CASE);
+    $this->assertEquals(array(
+        0 => 'Activity Type = Contribution',
+        1 => 'Activity Type = Scheduled',
+        2 => 'Activity Medium = In Person',
+    ),
+    $queryObj->_qill[1]
+    );
+  }
+}